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/api.md
1# AI Search API Reference23## Workers Binding45```typescript6const answer = await env.AI.autorag("instance-name").aiSearch(options);7const results = await env.AI.autorag("instance-name").search(options);8const instances = await env.AI.autorag("_").listInstances();9```1011## aiSearch() Options1213```typescript14interface AiSearchOptions {15query: string; // User query16model: string; // Workers AI model ID17system_prompt?: string; // LLM instructions18rewrite_query?: boolean; // Fix typos (default: false)19max_num_results?: number; // Max chunks (default: 10)20ranking_options?: { score_threshold?: number }; // 0.0-1.0 (default: 0.3)21reranking?: { enabled: boolean; model: string };22stream?: boolean; // Stream response (default: false)23filters?: Filter; // Metadata filters24page?: string; // Pagination token25}26```2728## Response2930```typescript31interface AiSearchResponse {32search_query: string; // Query used (rewritten if enabled)33response: string; // AI-generated answer34data: SearchResult[]; // Retrieved chunks35has_more: boolean;36next_page?: string;37}3839interface SearchResult {40id: string;41score: number;42content: string;43metadata: { filename: string; folder: string; timestamp: number };44}45```4647## Filters4849```typescript50// Comparison51{ column: "folder", operator: "gte", value: "docs/" }5253// Compound54{ operator: "and", filters: [55{ column: "folder", operator: "gte", value: "docs/" },56{ column: "timestamp", operator: "gte", value: 1704067200 }57]}58```5960**Operators:** `eq`, `ne`, `gt`, `gte`, `lt`, `lte`6162**Built-in metadata:** `filename`, `folder`, `timestamp` (Unix seconds)6364## Streaming6566```typescript67const stream = await env.AI.autorag("docs").aiSearch({ query, model, stream: true });68return new Response(stream, { headers: { "Content-Type": "text/event-stream" } });69```7071## Error Types7273| Error | Cause |74|-------|-------|75| `AutoRAGNotFoundError` | Instance doesn't exist |76| `AutoRAGUnauthorizedError` | Invalid/missing token |77| `AutoRAGValidationError` | Invalid parameters |7879## REST API8081```bash82curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/autorag/rags/{NAME}/ai-search \83-H "Authorization: Bearer {TOKEN}" \84-d '{"query": "...", "model": "@cf/meta/llama-3.3-70b-instruct-fp8-fast"}'85```8687Requires Service API token with "AI Search - Read" permission.88