跳转到主要内容

Workers binding (legacy)

import { Render, MetaInfo, Type } from "~/components";

The env.AI.autorag() binding is the legacy API for AI Search. It will continue to work, but new projects should use the new AI Search bindings instead. For a step-by-step upgrade guide, refer to Workers binding migration.

aiSearch()

This method searches for relevant results from your data source and generates a response using your default model and the retrieved context:

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,
});

Parameters

Response

This is the response structure without stream enabled.

{
	"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
}

search()

This method searches for results from your corpus and returns the relevant results:

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",
	},
});

Parameters

Response

{
	"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
}