Chuyển đến nội dung
Skillpedia
API và máy chủ (backend)5.4k · #14845

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.md

Sao 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

Hãy dùng skill này để giúp tôi: https://github.com/cloudflare/vibesdk/blob/main/worker/agents/think/skills/app-ai-binding/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

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 200 with fields such as fallbackUsed after 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

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự