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/ai-search/patterns.md
1# AI Search Patterns23## search() vs aiSearch()45| Use | Method | Returns |6|-----|--------|---------|7| Custom UI, analytics | `search()` | Raw chunks only (~100-300ms) |8| Chatbots, Q&A | `aiSearch()` | AI response + chunks (~500-2000ms) |910## rewrite_query1112| Setting | Use When |13|---------|----------|14| `true` | User input (typos, vague queries) |15| `false` | LLM-generated queries (already optimized) |1617## Multitenancy (Folder-Based)1819```typescript20const answer = await env.AI.autorag("saas-docs").aiSearch({21query: "refund policy",22model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",23filters: {24column: "folder",25operator: "gte", // "starts with" pattern26value: `tenants/${tenantId}/`27}28});29```3031## Streaming3233```typescript34const stream = await env.AI.autorag("docs").aiSearch({35query, model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast", stream: true36});37return new Response(stream, { headers: { "Content-Type": "text/event-stream" } });38```3940## Score Threshold4142| Threshold | Use |43|-----------|-----|44| 0.3 (default) | Broad recall, exploratory |45| 0.5 | Balanced, production default |46| 0.7 | High precision, critical accuracy |4748## System Prompt Template4950```typescript51const systemPrompt = `You are a documentation assistant.52- Answer ONLY based on provided context53- If context doesn't contain answer, say "I don't have information"54- Include code examples from context`;55```5657## Compound Filters5859```typescript60// OR: Multiple folders61filters: {62operator: "or",63filters: [64{ column: "folder", operator: "gte", value: "docs/api/" },65{ column: "folder", operator: "gte", value: "docs/auth/" }66]67}6869// AND: Folder + date70filters: {71operator: "and",72filters: [73{ column: "folder", operator: "gte", value: "docs/" },74{ column: "timestamp", operator: "gte", value: oneWeekAgoSeconds }75]76}77```7879## Reranking8081Enable for high-stakes use cases (adds ~300ms latency):8283```typescript84reranking: { enabled: true, model: "@cf/baai/bge-reranker-base" }85```86