Skip to main content

Workers AI

定价

  • 免费额度: 每天 10,000 神经元 (00:00 UTC 重置)
  • 付费: $0.011 / 1,000 神经元
  • 神经元 = GPU 计算单位,不同模型消耗不同

快速开始

Workers 方式

# 创建项目
npm create cloudflare@latest -- hello-ai
# 选择 "Hello World example" + TypeScript

wrangler.jsonc

{
"ai": {
"binding": "AI",
},
}

src/index.ts

export default {
async fetch(request, env): Promise<Response> {
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
prompt: 'What is the origin of the phrase Hello, World',
});
return Response.json(response);
},
} satisfies ExportedHandler<Env>;
npx wrangler dev    # 本地开发
npx wrangler deploy # 部署

REST API 方式

# 获取 API Token 和 Account ID: https://dash.cloudflare.com/

curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct \
-H 'Authorization: Bearer {API_TOKEN}' \
-d '{ "prompt": "Hello" }'

模型分类

类型模型示例
Text LLMLlama 3.1/3.2/4, Qwen3, Mistral, DeepSeek R1
Text-to-ImageFLUX.1, Stable Diffusion XL
Speech-to-TextWhisper, Deepgram Nova-3
Text-to-SpeechDeepgram Aura, MeloTTS
EmbeddingsBGE, EmbeddingGemma, Qwen3-Embedding
MultimodalLlama 3.2-Vision, Gemma-3
TranslationM2M100, IndicTrans2
ClassificationDistilbert, BGE-Reranker
Object DetectDETR-ResNet-50
  • 部分模型支持 LoRA 微调、批量处理、函数调用