跳转到内容
搜索文档

Workers 绑定(旧版)

最后更新 查看 MarkdownAgent 设置

env.AI.autorag() 绑定是 AI Search 的旧版 API。它将继续工作,但新项目应改用新的 AI Search 绑定。有关逐步升级指南,请参阅 Workers 绑定迁移

aiSearch()

此方法从您的数据源中搜索相关结果,并使用您的默认模型和检索到的上下文生成响应:

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。必须介于 150 之间。


ranking_options objectoptional

用于自定义结果排名的配置。默认为 {}

  • score_threshold numberoptional
    • 结果被视为匹配所需的最低匹配分数。默认为 0。必须介于 01 之间。

reranking objectoptional

用于自定义重排序的配置。默认为 {}

  • enabled booleanoptional

    • 启用或禁用重排序,重排序会使用重排序模型根据语义相关性对检索结果重新排序。默认为 false
  • model stringoptional

    • 启用重排序时要使用的重排序模型。

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

消息对象数组。每条消息包含:

  • content string - 搜索查询内容。
  • role string - 角色:usersystemassistant

ai_search_options objectoptional

针对检索和模型行为的单次请求覆盖。支持以下嵌套选项:

  • retrieval.filters object - 基于元数据缩小搜索结果范围。语法和示例请参阅 元数据筛选
  • retrieval.max_num_results number - 返回的最大 chunk 数。默认为 10,最大为 50
  • retrieval.retrieval_type string - 取值为 vectorkeywordhybrid 之一。
  • retrieval.match_threshold number - 最低相似度分数(0-1)。默认为 0.4
  • cache.enabled boolean - 覆盖此请求的实例级缓存设置。
  • reranking.enabled boolean - 覆盖此请求的实例级重排序设置。

有关可选参数的完整列表,请参阅 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
}

这篇文档对您有帮助吗?