Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/workers-ai/api.md
1# Workers AI API Reference23## Core Method45```typescript6const response = await env.AI.run(model, input);7```89## Text Generation1011```typescript12const result = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {13messages: [14{ role: 'system', content: 'You are helpful' },15{ role: 'user', content: 'Hello' }16],17temperature: 0.7, // 0-118max_tokens: 10019});20console.log(result.response);21```2223**Streaming:**24```typescript25const stream = await env.AI.run(model, { messages, stream: true });26return new Response(stream, { headers: { 'Content-Type': 'text/event-stream' } });27```2829## Embeddings3031```typescript32const result = await env.AI.run('@cf/baai/bge-base-en-v1.5', {33text: ['Query', 'Doc 1', 'Doc 2'] // Batch for efficiency34});35const [queryEmbed, doc1Embed, doc2Embed] = result.data; // 768-dim vectors36```3738## Function Calling3940```typescript41const tools = [{42type: 'function',43function: {44name: 'getWeather',45description: 'Get weather for location',46parameters: {47type: 'object',48properties: { location: { type: 'string' } },49required: ['location']50}51}52}];5354const response = await env.AI.run(model, { messages, tools });55if (response.tool_calls) {56const args = JSON.parse(response.tool_calls[0].function.arguments);57// Execute function, send result back58}59```6061## Image Generation6263```typescript64const image = await env.AI.run('@cf/stabilityai/stable-diffusion-xl-base-1.0', {65prompt: 'Mountain sunset',66num_steps: 20, // 1-2067guidance: 7.5 // 1-2068});69return new Response(image, { headers: { 'Content-Type': 'image/png' } });70```7172## Speech Recognition7374```typescript75const audioArray = Array.from(new Uint8Array(await request.arrayBuffer()));76const result = await env.AI.run('@cf/openai/whisper', { audio: audioArray });77console.log(result.text);78```7980## Translation8182```typescript83const result = await env.AI.run('@cf/meta/m2m100-1.2b', {84text: 'Hello',85source_lang: 'en',86target_lang: 'es'87});88console.log(result.translated_text);89```9091## REST API9293```bash94curl https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/@cf/meta/llama-3.1-8b-instruct \95-H "Authorization: Bearer $TOKEN" \96-d '{"messages":[{"role":"user","content":"Hello"}]}'97```9899## Error Codes100101| Code | Meaning | Fix |102|------|---------|-----|103| 7502 | Model not found | Check spelling |104| 7504 | Validation failed | Verify input schema |105| 7505 | Rate limited | Reduce rate or upgrade |106| 7506 | Context exceeded | Reduce input size |107108## Performance Tips1091101. **Batch embeddings** - single request for multiple texts1112. **Stream long responses** - reduce perceived latency1123. **Accept cold starts** - first request ~1-3s, subsequent ~100-500ms113