Cloudflare Agents 从入门到精通
附录

§18 AI 搜索与知识库:RAG 交给托管

18.1 你不用自己搭检索

想让 Agent 查产品文档、用户文件、内部知识库?AI Search 把索引、检索、可选的聊天补全全托管了。

18.2 两种用法

// 1. 只检索
const instance = this.env.AI_SEARCH.get("my-instance");
const results = await instance.search({ messages: [{ role: "user", content: query }] });

// 2. 检索 + 生成答案
const resp = await instance.chatCompletions({
  messages: [{ role: "user", content: "How do I deploy an Agent?" }],
  model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
  ai_search_options: { retrieval: { max_num_results: 5 } },
});

wrangler 用 namespace binding:

"ai_search_namespaces": [{ "binding": "AI_SEARCH", "namespace": "default", "remote": true }]
维度 自己搭 RAG Cloudflare AI Search 标叔的结论
向量库 自己装 托管 省一个组件
索引 自己写 托管 省心力
适用 极致定制 标准检索 先托管后自研
核心建议
先做托管 AI Search,跑通再考虑自己接 Vectorize 做精细控制。别一上来就自建向量库。

RAG 讲完。下一 Part 进模型与智能体深潜:密钥、鉴权、工作流、支付。