app-ai-binding
Skill này làm gì
Skill này dạy agent cách thêm tính năng dựa trên mô hình vào ứng dụng chạy trên Cloudflare. Skill giải thích cách binding AI có sẵn được dùng ở phía máy chủ và những lệnh gọi nào có thể tin cậy.
Khi nào nên dùng
- ✓Khi người dùng muốn thêm chatbot, tóm tắt hoặc bộ phân loại vào ứng dụng trên Cloudflare.
- ✓Khi người dùng muốn embedding hoặc tìm kiếm ngữ nghĩa được tích hợp vào ứng dụng Cloudflare.
- ✓Khi người dùng muốn nhận diện hình ảnh hoặc chuyển giọng nói thành văn bản bằng mô hình Workers AI.
Liên kết skill
https://github.com/cloudflare/vibesdk/blob/main/worker/agents/think/skills/app-ai-binding/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Thêm một chatbot vào ứng dụng Cloudflare của tôi, để nó trả lời câu hỏi bằng binding AI có sẵn.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Agent gọi binding AI từ lớp ứng dụng phía máy chủ, với mô hình đã chọn và các tin nhắn đầu vào. Trình duyệt chỉ gọi một route của ứng dụng, nên quyền truy cập mô hình được giữ ở phía máy chủ.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Generated apps can call Workers AI from the App Durable Object through this.env.AI. The platform provides this binding automatically in Dynamic Worker previews and native Cloudflare deployments. Do not add an ai section to the generated app's wrangler.json.
Use only AI.run(model, inputs, options?). Preview uses a restricted proxy with this method, while deployed Workers receive the native Workers AI binding. Methods such as AI.gateway(), AI.models(), AI.autorag(), and AI.toMarkdown() are not portable between preview and deployment.
Type and usage
import { DurableObject } from "cloudflare:workers";
interface Env {
AI: Ai;
}
export class App extends DurableObject<Env> {
async fetch(request: Request): Promise<Response> {
const { prompt } = await request.json<{ prompt: string }>();
const result = await this.env.AI.run("@cf/meta/llama-4-scout-17b-16e-instruct", {
messages: [
{ role: "system", content: "Answer clearly and concisely." },
{ role: "user", content: prompt },
],
});
return Response.json(result);
}
}
Keep AI calls in the server-side App class. Browser code should call an app API route; it must never receive model credentials or call the binding directly.
Common tasks
Text generation
Use @cf/meta/llama-4-scout-17b-16e-instruct with either prompt or messages:
const result = await this.env.AI.run("@cf/meta/llama-4-scout-17b-16e-instruct", {
messages: [{ role: "user", content: userPrompt }],
max_tokens: 512,
});
Embeddings
Use @cf/baai/bge-base-en-v1.5. The response contains embedding vectors in data:
const result = await this.env.AI.run("@cf/baai/bge-base-en-v1.5", {
text: ["first document", "second document"],
});
Image classification
Use @cf/microsoft/resnet-50 with image bytes converted to a number array:
const image = [...new Uint8Array(await request.arrayBuffer())];
const result = await this.env.AI.run("@cf/microsoft/resnet-50", { image });
Speech transcription
Use @cf/openai/whisper with audio bytes:
const audio = [...new Uint8Array(await request.arrayBuffer())];
const result = await this.env.AI.run("@cf/openai/whisper", { audio });
Model input and output shapes differ. Keep model-specific parsing close to the call and validate user input before inference. Use only active model IDs from the current Workers AI model catalog; never generate code with a deprecated model.
Limits and errors
Dynamic Worker previews enforce a per-app AI call limit. AI features must fail transparently when inference is unavailable.
- Never return canned, random, mock, placeholder, or hard-coded content as a fallback for an AI response.
- Never catch an
AI.run()error and continue with an apparently successful response. - Never return
200with fields such asfallbackUsedafter inference fails. - Return a non-2xx JSON response containing a safe, useful error message. The frontend must display that error to the user.
- Do not retry rate-limit failures in a loop.
try {
const result = await this.env.AI.run("@cf/meta/llama-4-scout-17b-16e-instruct", {
messages: [{ role: "user", content: userPrompt }],
});
return Response.json(result);
} catch (error) {
const message = error instanceof Error ? error.message : "AI inference failed";
return Response.json({ error: "AI inference failed", details: message }, { status: 503 });
}
Frontend callers must inspect both the HTTP status and error body:
const response = await fetch("./api/chat", requestOptions);
const body = await response.json<{ content?: string; error?: string; details?: string }>();
if (!response.ok) throw new Error(body.details || body.error || "AI request failed");
Use non-streaming calls for preview-compatible code. Native Workers AI deployments support { stream: true }, but streaming across the preview proxy is not guaranteed.
Câu hỏi thường gặp
Tôi có cần thêm phần AI vào file cấu hình không?
Không. Skill nói nền tảng tự cung cấp binding AI.
Trình duyệt có gọi mô hình trực tiếp được không?
Không. Skill nói mã trình duyệt phải gọi một route của ứng dụng, không bao giờ gọi binding.
Phương thức AI nào an toàn để dùng?
Skill khuyên dùng phương thức run, vì các phương thức AI khác không chuyển sang được giữa môi trường xem trước và triển khai.
Chi tiết
Mô tả gốc của tác giả
Build AI features in generated apps using the platform-provided Workers AI binding. Load this skill for chatbots, summarization, classification, embeddings, semantic search, image understanding, transcription, translation, or other model inference.
repo: cloudflare/vibesdk
Độ phổ biến
- Sao GitHub
- 5.4k
- Vị trí xếp hạng
- #14845
- Giấy phép
- MIT
- Cập nhật
- 21/10/2026
Bình luận
- …
Thêm skill từ kho mã này
cloudflare-bundler-apps
5.4kGiúp agent của bạn thiết lập một dự án Cloudflare Worker để nó build và xem trước đúng với bundler, gồm cấu hình, bố cục, tài nguyên tĩnh và giới hạn phụ thuộc.
frontend-design-landing-page
5.4kGiúp agent của bạn thiết kế trang đích tiếp thị ấm áp, tập trung vào chuyển đổi, với phần hero rõ ràng, lưới tính năng, bảng giá và các lời kêu gọi hành động.
frontend-design-saas
5.4kGiúp agent của bạn thiết kế giao diện sản phẩm dày đặc, gọn gàng như bảng điều khiển, trang cài đặt, màn hình thanh toán và bảng dữ liệu, theo phong cách SaaS trung tính và yên tĩnh.
Skill tương tự
api-connector-builder
280kGiúp agent của bạn xây dựng kết nối API mới theo đúng mẫu đã có, dùng khi bạn cần thêm một tích hợp mà không tạo cách làm mới.
api-design
280kGiúp agent của bạn thiết kế REST API theo các thực hành tốt nhất, hữu ích để định nghĩa cách API giao tiếp, hiển thị lỗi và tổ chức dữ liệu.
backend-patterns
280kGiúp agent của bạn với các mẫu kiến trúc phía máy chủ và tối ưu cơ sở dữ liệu, hữu ích khi xây dựng hoặc rà soát code chạy trên máy chủ.