env.AI.autorag() 绑定是 AI Search 的旧版 API。它将继续工作,但新项目应改用新的 AI Search 绑定。有关逐步升级指南,请参阅 Workers 绑定迁移。
此方法从您的数据源中搜索相关结果,并使用您的默认模型和检索到的上下文生成响应:
const answer = await env.AI.autorag("my-autorag").aiSearch({
query: "How do I train a llama to deliver coffee?",
model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
rewrite_query: true,
max_num_results: 2,
ranking_options: {
score_threshold: 0.3,
},
reranking: {
enabled: true,
model: "@cf/baai/bge-reranker-base",
},
stream: true,
});query stringrequired
输入查询。
model stringoptional
用于为查询生成响应的文本生成模型。有关有效选项列表,请查看 AI Search 生成模型设置。默认为 AI Search 设置中选定的生成模型。
system_prompt stringoptional
用于生成答案的系统提示。
rewrite_query booleanoptional
将原始查询改写为经过搜索优化的查询,以提高检索准确性。默认为 false。
max_num_results numberoptional
可从 Vectorize 数据库返回的最大结果数。默认为 10。必须介于 1 和 50 之间。
ranking_options objectoptional
用于自定义结果排名的配置。默认为 {}。
score_thresholdnumberoptional- 结果被视为匹配所需的最低匹配分数。默认为
0。必须介于0和1之间。
- 结果被视为匹配所需的最低匹配分数。默认为
reranking objectoptional
用于自定义重排序的配置。默认为 {}。
enabledbooleanoptional- 启用或禁用重排序,重排序会使用重排序模型根据语义相关性对检索结果重新排序。默认为
false。
- 启用或禁用重排序,重排序会使用重排序模型根据语义相关性对检索结果重新排序。默认为
modelstringoptional- 启用重排序时要使用的重排序模型。
stream booleanoptional
在结果可用时以流式返回。默认为 false。
filters objectoptional
根据文件夹和日期等元数据缩小搜索结果范围,从而仅检索相关内容。更多详情请参阅 元数据筛选。
这是未启用 stream(流式传输)时的响应结构。
{
"object": "vector_store.search_results.page",
"search_query": "How do I train a llama to deliver coffee?",
"response": "To train a llama to deliver coffee:\n\n1. **Build trust** — Llamas appreciate patience (and decaf).\n2. **Know limits** — Max 3 cups per llama, per `llama-logistics.md`.\n3. **Use voice commands** — Start with \"Espresso Express!\"\n4.",
"data": [
{
"file_id": "llama001",
"filename": "llama/logistics/llama-logistics.md",
"score": 0.45,
"attributes": {
"modified_date": 1735689600000,
"folder": "llama/logistics/"
},
"content": [
{
"id": "llama001",
"type": "text",
"text": "Llamas can carry 3 drinks max."
}
]
},
{
"file_id": "llama042",
"filename": "llama/llama-commands.md",
"score": 0.4,
"attributes": {
"modified_date": 1735689600000,
"folder": "llama/"
},
"content": [
{
"id": "llama042",
"type": "text",
"text": "Start with basic commands like 'Espresso Express!' Llamas love alliteration."
}
]
}
],
"has_more": false,
"next_page": null
}此方法从您的语料库中搜索结果并返回相关结果:
const answer = await env.AI.autorag("my-autorag").search({
query: "How do I train a llama to deliver coffee?",
rewrite_query: true,
max_num_results: 2,
ranking_options: {
score_threshold: 0.3,
},
reranking: {
enabled: true,
model: "@cf/baai/bge-reranker-base",
},
});messages arrayrequired
消息对象数组。每条消息包含:
contentstring- 搜索查询内容。rolestring- 角色:user、system或assistant。
ai_search_options objectoptional
针对检索和模型行为的单次请求覆盖。支持以下嵌套选项:
retrieval.filtersobject- 基于元数据缩小搜索结果范围。语法和示例请参阅 元数据筛选。retrieval.max_num_resultsnumber- 返回的最大 chunk 数。默认为10,最大为50。retrieval.retrieval_typestring- 取值为vector、keyword或hybrid之一。retrieval.match_thresholdnumber- 最低相似度分数(0-1)。默认为0.4。cache.enabledboolean- 覆盖此请求的实例级缓存设置。reranking.enabledboolean- 覆盖此请求的实例级重排序设置。
有关可选参数的完整列表,请参阅 Search API 参考。
{
"object": "vector_store.search_results.page",
"search_query": "How do I train a llama to deliver coffee?",
"data": [
{
"file_id": "llama001",
"filename": "llama/logistics/llama-logistics.md",
"score": 0.45,
"attributes": {
"modified_date": 1735689600000,
"folder": "llama/logistics/"
},
"content": [
{
"id": "llama001",
"type": "text",
"text": "Llamas can carry 3 drinks max."
}
]
},
{
"file_id": "llama042",
"filename": "llama/llama-commands.md",
"score": 0.4,
"attributes": {
"modified_date": 1735689600000,
"folder": "llama/"
},
"content": [
{
"id": "llama042",
"type": "text",
"text": "Start with basic commands like 'Espresso Express!' Llamas love alliteration."
}
]
}
],
"has_more": false,
"next_page": null
}