[ PROMPT_NODE_23942 ]
AI Search 设计模式
[ SKILL_DOCUMENTATION ]
# AI Search 开发模式
## search() vs aiSearch()
| 用途 | 方法 | 返回值 |
|-----|--------|---------|
| 自定义 UI、分析 | `search()` | 仅原始分块 (~100-300ms) |
| 聊天机器人、问答 | `aiSearch()` | AI 响应 + 分块 (~500-2000ms) |
## rewrite_query (查询重写)
| 设置 | 使用场景 |
|---------|----------|
| `true` | 用户输入(存在拼写错误、模糊查询) |
| `false` | LLM 生成的查询(已优化) |
## 多租户(基于文件夹)
typescript
const answer = await env.AI.autorag("saas-docs").aiSearch({
query: "refund policy",
model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
filters: {
column: "folder",
operator: "gte", // "以...开头" 模式
value: `tenants/${tenantId}/`
}
});
## 流式传输
typescript
const stream = await env.AI.autorag("docs").aiSearch({
query, model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast", stream: true
});
return new Response(stream, { headers: { "Content-Type": "text/event-stream" } });
## 分数阈值 (Score Threshold)
| 阈值 | 用途 |
|-----------|-----|
| 0.3 (默认) | 广泛召回,探索性搜索 |
| 0.5 | 平衡,生产环境默认 |
| 0.7 | 高精度,关键准确性要求 |
## 提示词模板 (System Prompt)
typescript
const systemPrompt = `You are a documentation assistant.
- Answer ONLY based on provided context
- If context doesn't contain answer, say "I don't have information"
- Include code examples from context`;
## 复合过滤器
typescript
// OR: 多个文件夹
filters: {
operator: "or",
filters: [
{ column: "folder", operator: "gte", value: "docs/api/" },
{ column: "folder", operator: "gte", value: "docs/auth/" }
]
}
// AND: 文件夹 + 日期
filters: {
operator: "and",
filters: [
{ column: "folder", operator: "gte", value: "docs/" },
{ column: "timestamp", operator: "gte", value: oneWeekAgoSeconds }
]
}
## 重排序 (Reranking)
在高风险场景下启用(增加约 300ms 延迟):
typescript
reranking: { enabled: true, model: "@cf/baai/bge-reranker-base" }