Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

Workers バインディング(レガシー)

最終更新 Markdown で表示Agent セットアップ

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 string 必須

入力クエリです。


model string 任意

クエリへの応答を生成するテキスト生成モデルです。有効な選択肢は、AI Search の生成モデル設定を確認してください。デフォルトは、AI Search 設定で選んだ生成モデルです。


system_prompt string 任意

回答生成に使うシステムプロンプトです。


rewrite_query boolean 任意

元のクエリを検索向けに書き換え、取得精度を上げます。デフォルトは false です。


max_num_results number 任意

Vectorize データベースから返せる結果の最大件数です。デフォルトは 10 です。1 から 50 の間である必要があります。


ranking_options object 任意

結果のランキングをカスタマイズする設定です。デフォルトは {} です。

  • score_threshold number 任意
    • 結果を一致とみなすために必要な最小マッチスコアです。デフォルトは 0 です。0 から 1 の間である必要があります。

reranking object 任意

リランキングをカスタマイズする設定です。デフォルトは {} です。

  • enabled boolean 任意

    • リランキングの有効/無効です。リランキングモデルを使い、取得結果を意味的な関連度で並べ替えます。デフォルトは false です。
  • model string 任意

    • リランキングが有効なときに使うリランキングモデルです。

stream boolean 任意

結果が利用可能になり次第、ストリームで返します。デフォルトは false です。

filters object 任意

フォルダーや日付などのメタデータで検索結果を絞り込み、関連するコンテンツだけを取得します。詳細は メタデータフィルタリング を参照してください。

レスポンス

これは 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 array required

メッセージオブジェクトの配列です。各メッセージには次があります。

  • content string - 検索クエリの内容です。
  • role string - ロールです。usersystem、または assistant です。

ai_search_options object optional

検索とモデルの動作をリクエスト単位で上書きします。次の入れ子オプションに対応しています。

  • retrieval.filters object - メタデータで検索結果を絞り込みます。構文と例は Metadata filtering を参照してください。
  • retrieval.max_num_results number - 返すチャンクの最大数です。デフォルトは 10、最大は 50 です。
  • retrieval.retrieval_type string - vectorkeyword、または hybrid のいずれかです。
  • retrieval.match_threshold number - 類似度スコアの下限(0〜1)です。デフォルトは 0.4 です。
  • cache.enabled boolean - このリクエストについて、インスタンスのキャッシュ設定を上書きします。
  • reranking.enabled boolean - このリクエストについて、インスタンスの reranking 設定を上書きします。

省略可能なパラメーターの一覧は、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
}

役に立ちましたか?