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/patterns.md
1# Workers AI Patterns23## RAG (Retrieval-Augmented Generation)45```typescript6// 1. Embed query7const embedding = await env.AI.run('@cf/baai/bge-base-en-v1.5', { text: query });89// 2. Search vectors10const results = await env.VECTORIZE.query(embedding.data[0], {11topK: 5, returnMetadata: true12});1314// 3. Build context15const context = results.matches.map(m => m.metadata?.text).join('\n\n');1617// 4. Generate with context18const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {19messages: [20{ role: 'system', content: `Answer based on:\n\n${context}` },21{ role: 'user', content: query }22]23});24```2526## Streaming (SSE)2728```typescript29const stream = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {30messages, stream: true31});3233const { readable, writable } = new TransformStream();34const writer = writable.getWriter();3536(async () => {37for await (const chunk of stream) {38await writer.write(new TextEncoder().encode(`data: ${JSON.stringify(chunk)}\n\n`));39}40await writer.write(new TextEncoder().encode('data: [DONE]\n\n'));41await writer.close();42})();4344return new Response(readable, {45headers: { 'Content-Type': 'text/event-stream' }46});47```4849## Error Handling & Retry5051```typescript52async function runWithRetry(env, model, input, maxRetries = 3) {53for (let attempt = 0; attempt < maxRetries; attempt++) {54try {55return await env.AI.run(model, input);56} catch (error) {57if (error.message?.includes('7505') && attempt < maxRetries - 1) {58await new Promise(r => setTimeout(r, Math.pow(2, attempt) * 1000));59continue;60}61throw error;62}63}64}65```6667## Model Fallback6869```typescript70try {71return await env.AI.run('@cf/meta/llama-3.1-70b-instruct', { messages });72} catch {73return await env.AI.run('@cf/meta/llama-3.1-8b-instruct', { messages });74}75```7677## Prompt Patterns7879```typescript80// System prompts81const PROMPTS = {82json: 'Respond with valid JSON only.',83concise: 'Keep responses brief.',84cot: 'Think step by step before answering.'85};8687// Few-shot88messages: [89{ role: 'system', content: 'Extract as JSON' },90{ role: 'user', content: 'John bought 3 apples for $5' },91{ role: 'assistant', content: '{"name":"John","item":"apples","qty":3}' },92{ role: 'user', content: actualInput }93]94```9596## Parallel Execution9798```typescript99const [sentiment, summary, embedding] = await Promise.all([100env.AI.run('@cf/mistral/mistral-7b-instruct-v0.1', { messages: sentimentPrompt }),101env.AI.run('@cf/meta/llama-3.1-8b-instruct', { messages: summaryPrompt }),102env.AI.run('@cf/baai/bge-base-en-v1.5', { text })103]);104```105106## Cost Optimization107108| Task | Model | Neurons |109|------|-------|---------|110| Classify | `@cf/mistral/mistral-7b-instruct-v0.1` | ~50 |111| Chat | `@cf/meta/llama-3.1-8b-instruct` | ~200 |112| Complex | `@cf/meta/llama-3.1-70b-instruct` | ~2000 |113| Embed | `@cf/baai/bge-base-en-v1.5` | ~10 |114115```typescript116// Batch embeddings117const response = await env.AI.run('@cf/baai/bge-base-en-v1.5', {118text: textsArray // Process multiple at once119});120```121