Skip to main content

AiSearch

Cloudflare AI Search provides RAG (Retrieval-Augmented Generation) capabilities for building AI-powered search and chat applications.

Props

name
string
Name of the AI Search instance.
source
R2Bucket | AiSearchR2Source | AiSearchWebCrawlerSource
required
Data source for indexing.
aiSearchModel
AiSearch.Model
default:"@cf/meta/llama-3.3-70b-instruct-fp8-fast"
Text generation model for AI responses.
embeddingModel
AiSearch.EmbeddingModel
default:"@cf/baai/bge-m3"
Embedding model for vectorization.
chunk
boolean
default:"true"
Enable chunking of source documents.
chunkSize
number
default:"256"
Size of each chunk (minimum 64).
chunkOverlap
number
default:"10"
Overlap between chunks (0-30).
maxNumResults
number
default:"10"
Maximum search results (1-50).
scoreThreshold
number
default:"0.4"
Minimum match score (0-1).
reranking
boolean
default:"false"
Enable result reranking.
rerankingModel
AiSearch.RerankingModel
default:"@cf/baai/bge-reranker-base"
Reranking model.
rewriteQuery
boolean
default:"false"
Enable query rewriting for better retrieval.
cache
boolean
default:"false"
Enable similarity caching.
indexOnCreate
boolean
default:"true"
Whether to index the source documents when the AI Search instance is created.
delete
boolean
default:"true"
Whether to delete the AI Search instance when removed from Alchemy.
adopt
boolean
default:"false"
Whether to adopt the AI Search instance if it already exists.

Output

id
string
The unique ID of the AI Search instance.
name
string
The name of the AI Search instance.
source
string
Source bucket or domain name.
type
r2 | web-crawler
The source type.
vectorizeName
string
The name of the underlying Vectorize index.

Examples

Basic AI Search with R2

const bucket = await R2Bucket("docs", { dev: { remote: true } });

const aiSearch = await AiSearch("rag", {
  source: bucket
});

const worker = await Worker("search", {
  entrypoint: "./src/search.ts",
  bindings: {
    AI_SEARCH: aiSearch
  }
});

AI Search with R2 Source Config

const aiSearch = await AiSearch("docs-search", {
  source: {
    type: "r2",
    bucket: "my-docs-bucket",
    includePaths: ["docs/**"],
    excludePaths: ["**/*.png", "**/*.jpg"]
  }
});

AI Search with Web Crawler

const aiSearch = await AiSearch("site-search", {
  source: {
    type: "web-crawler",
    domain: "docs.example.com",
    includePaths: ["/guides/**", "/api/**"],
    excludePaths: ["/blog/**"]
  }
});

Advanced AI Search Configuration

const aiSearch = await AiSearch("advanced-search", {
  source: bucket,
  aiSearchModel: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
  embeddingModel: "@cf/baai/bge-m3",
  chunk: true,
  chunkSize: 512,
  chunkOverlap: 20,
  maxNumResults: 20,
  scoreThreshold: 0.5,
  reranking: true,
  rewriteQuery: true,
  cache: true
});

Build docs developers (and LLMs) love