搜尋結果內容區塊讓 Claude 能以引用網路搜尋結果的相同方式引用您自己的內容:每個引用都帶有您提供的來源和標題。在 Claude 需要將答案歸屬到您的文件的「RAG」(Retrieval-Augmented Generation,檢索增強生成)應用程式中使用它們。
所有現行模型都支援帶有引用的搜尋結果,但 Claude Haiku 3 除外。不需要 beta 標頭:搜尋結果是標準 Messages API 的一部分。
搜尋結果可以透過兩種方式提供:
在這兩種情況下,當啟用引用時,Claude 會自動引用搜尋結果。不需要特殊的提示:提出您的問題,引用就會出現在使用您內容的文字區塊上。
搜尋結果使用以下結構:
{
"type": "search_result",
"source": "https://example.com/article", // Required: Source URL or identifier
"title": "Article Title", // Required: Title of the result
"content": [
// Required: Array of text blocks
{
"type": "text",
"text": "The actual content of the search result..."
}
],
"citations": {
// Optional: Citation configuration
"enabled": true // Enable/disable citations for this result
}
}| 欄位 | 類型 | 說明 |
|---|---|---|
type | string | 必須是 "search_result" |
source | string | 內容的來源。任何穩定的字串都可以:URL,或內部識別碼,例如 kb://article-1234 |
title | string | 搜尋結果的描述性標題 |
content | array | 包含實際內容的文字區塊陣列 |
| 欄位 | 類型 | 說明 |
|---|---|---|
citations | object | 帶有 enabled 布林欄位的引用設定。引用預設為停用;本頁的每個範例都明確設定 "enabled": true。請求中的所有搜尋結果必須使用相同的設定(請參閱引用控制) |
cache_control | object | 快取控制設定(例如 {"type": "ephemeral"}) |
content 陣列中的每個項目必須是具有以下內容的文字區塊:
type:必須是 "text"text:實際的文字內容(非空字串)搜尋結果僅包含文字。content 陣列內不支援圖片和其他媒體。
從您的自訂工具回傳搜尋結果可實現動態 RAG 應用程式:工具在執行時擷取內容,Claude 在回應中引用它。以下範例使用 tool_choice 強制工具呼叫,因此每次都會執行檢索步驟。
from anthropic.types import (
MessageParam,
TextBlockParam,
SearchResultBlockParam,
ToolResultBlockParam,
)
client = Anthropic()
# 定義知識庫搜尋工具
knowledge_base_tool = {
"name": "search_knowledge_base",
"description": "Search the company knowledge base for information",
"input_schema": {
"type": "object",
"properties": {"query": {"type": "string", "description": "The search query"}},
"required": ["query"],
},
}
# 處理工具呼叫的函式
def search_knowledge_base(query):
# 在此撰寫您的搜尋邏輯
# 以正確格式回傳搜尋結果
return [
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/product-guide",
title="Product Configuration Guide",
content=[
TextBlockParam(
type="text",
text="To configure the product, navigate to Settings > Configuration. The default timeout is 30 seconds, but can be adjusted between 10-120 seconds based on your needs.",
)
],
citations={"enabled": True},
),
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/troubleshooting",
title="Troubleshooting Guide",
content=[
TextBlockParam(
type="text",
text="If you encounter timeout errors, first check the configuration settings. Common causes include network latency and incorrect timeout values.",
)
],
citations={"enabled": True},
),
]
# 以清單建立對話內容,從使用者的問題開始
messages = [
MessageParam(role="user", content="How do I configure the timeout settings?")
]
# 建立包含該工具的訊息
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
tools=[knowledge_base_tool],
tool_choice={"type": "tool", "name": "search_knowledge_base"},
messages=messages,
)
# 當 Claude 呼叫工具時,提供搜尋結果。
# tool_use 區塊不一定在最前面:請迭代尋找。
tool_use = next((block for block in response.content if block.type == "tool_use"), None)
if tool_use is not None:
tool_result = search_knowledge_base(tool_use.input["query"])
# 將 Claude 的回合與工具結果依序附加到進行中的對話
messages.append(MessageParam(role="assistant", content=response.content))
messages.append(
MessageParam(
role="user",
content=[
ToolResultBlockParam(
type="tool_result",
tool_use_id=tool_use.id,
content=tool_result, # Search results go here
)
],
)
)
# 將工具結果傳回
final_response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=messages,
)
print(final_response)您也可以直接在使用者訊息中提供搜尋結果。這適用於:
from anthropic.types import MessageParam, TextBlockParam, SearchResultBlockParam
client = Anthropic()
# 直接在使用者訊息中提供搜尋結果
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
MessageParam(
role="user",
content=[
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/api-reference",
title="API Reference - Authentication",
content=[
TextBlockParam(
type="text",
text="All API requests must include an API key in the Authorization header. Keys can be generated from the dashboard. Rate limits: 1000 requests per hour for standard tier, 10000 for premium.",
)
],
citations={"enabled": True},
),
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/quickstart",
title="Getting Started Guide",
content=[
TextBlockParam(
type="text",
text="To get started: 1) Sign up for an account, 2) Generate an API key from the dashboard, 3) Install our SDK using pip install company-sdk, 4) Initialize the client with your API key.",
)
],
citations={"enabled": True},
),
TextBlockParam(
type="text",
text="Based on these search results, how do I authenticate API requests and what are the rate limits?",
),
],
)
],
)
print(response)無論搜尋結果以何種方式提供,Claude 在使用其中的資訊時都會自動包含引用:
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "All API requests must include an API key in the Authorization header. Keys can be generated from the dashboard.",
"citations": [
{
"type": "search_result_location",
"cited_text": "All API requests must include an API key in the Authorization header. Keys can be generated from the dashboard. Rate limits: 1000 requests per hour for standard tier, 10000 for premium.",
"source": "https://docs.company.com/api-reference",
"title": "API Reference - Authentication",
"search_result_index": 0,
"start_block_index": 0,
"end_block_index": 1
}
]
},
{
"type": "text",
"text": "\n\nTo set this up from scratch, you'll need to "
},
{
"type": "text",
"text": "sign up for an account, generate an API key from the dashboard, install the SDK using `pip install company-sdk`, and initialize the client with your API key.",
"citations": [
{
"type": "search_result_location",
"cited_text": "To get started: 1) Sign up for an account, 2) Generate an API key from the dashboard, 3) Install our SDK using pip install company-sdk, 4) Initialize the client with your API key.",
"source": "https://docs.company.com/quickstart",
"title": "Getting Started Guide",
"search_result_index": 1,
"start_block_index": 0,
"end_block_index": 1
}
]
}
]
}每個引用包含:
| 欄位 | 類型 | 說明 |
|---|---|---|
type | string | 搜尋結果引用一律為 "search_result_location" |
source | string | 來自原始搜尋結果的來源 |
title | string 或 null | 來自原始搜尋結果的標題 |
cited_text | string | 被引用區塊的完整文字,串接而成。等於 content[start_block_index:end_block_index] 的內容合併在一起。不計入 output tokens。 |
search_result_index | integer | 被引用的搜尋結果在請求中所有 search_result 區塊中的索引(從 0 開始),依其出現順序排列(跨所有訊息和工具結果)。 |
start_block_index | integer | 搜尋結果的 content 陣列中第一個被引用區塊的索引(從 0 開始)。 |
end_block_index | integer | 搜尋結果的 content 陣列中被引用區塊範圍的排除式結束索引。一律大於 start_block_index。 |
區塊索引標識搜尋結果 content 陣列的一個切片,而 cited_text 是該切片的完整文字。文字區塊是最小的可引用單位:Claude 引用整個區塊,而不是區塊內的子字串。若要取得更細緻的引用,請將您的搜尋結果內容拆分為較小的區塊(請參閱多個內容區塊)。
搜尋結果可以在 content 陣列中包含多個文字區塊:
{
"type": "search_result",
"source": "https://docs.company.com/api-guide",
"title": "API Documentation",
"content": [
{
"type": "text",
"text": "Authentication: All API requests require an API key."
},
{
"type": "text",
"text": "Rate Limits: The API allows 1000 requests per hour per key."
},
{
"type": "text",
"text": "Error Handling: The API returns standard HTTP status codes."
}
],
"citations": { "enabled": true }
}引用速率限制區塊的引用看起來像這樣:
{
"type": "search_result_location",
"cited_text": "Rate Limits: The API allows 1000 requests per hour per key.",
"source": "https://docs.company.com/api-guide",
"title": "API Documentation",
"search_result_index": 0,
"start_block_index": 1,
"end_block_index": 2
}當此搜尋結果被引用時,start_block_index 和 end_block_index 標識引用涵蓋了哪些區塊,而 cited_text 恰好包含那些區塊的文字。將內容拆分為較小、更聚焦的區塊可為 Claude 提供更細緻的引用邊界;將內容合併為一個區塊則意味著每個引用都會回傳完整文字。這與 Citations 功能中自訂內容文件使用的模型相同。
您可以在同一個對話中混合使用兩種方法。Claude 會從任一來源引用,而 search_result_index 會依請求順序計算所有 search_result 區塊,無論其來源為何。
以下範例重現了一個完整的對話。第一個使用者訊息帶有預先擷取的搜尋結果,助理回合呼叫知識庫工具,而工具結果回傳第二個搜尋結果。Claude 的答案引用了兩個來源:
from anthropic.types import (
MessageParam,
SearchResultBlockParam,
TextBlockParam,
ToolResultBlockParam,
ToolUseBlockParam,
)
client = Anthropic()
knowledge_base_tool = {
"name": "search_knowledge_base",
"description": "Search the company knowledge base for information",
"input_schema": {
"type": "object",
"properties": {"query": {"type": "string", "description": "The search query"}},
"required": ["query"],
},
}
# 重播一段以兩種方式提供搜尋結果的對話:第一則
# 使用者訊息帶有預先擷取的結果,工具結果則回傳另一個
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
tools=[knowledge_base_tool],
messages=[
MessageParam(
role="user",
content=[
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/overview",
title="Product Overview",
content=[
TextBlockParam(
type="text",
text="Acme Dashboard is a monitoring tool for distributed systems. It supports real-time alerting and custom metric dashboards.",
)
],
citations={"enabled": True},
),
TextBlockParam(
type="text",
text="What does Acme Dashboard do, and what plans is it available on?",
),
],
),
MessageParam(
role="assistant",
content=[
TextBlockParam(
type="text", text="Let me check the pricing information."
),
ToolUseBlockParam(
type="tool_use",
id="toolu_01A09q90qw90lq917835lq9",
name="search_knowledge_base",
input={"query": "Acme Dashboard pricing plans"},
),
],
),
MessageParam(
role="user",
content=[
ToolResultBlockParam(
type="tool_result",
tool_use_id="toolu_01A09q90qw90lq917835lq9",
content=[
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/pricing",
title="Pricing Plans",
content=[
TextBlockParam(
type="text",
text="Acme Dashboard is available on the Starter plan at $10 per user per month and the Enterprise plan with custom pricing.",
)
],
citations={"enabled": True},
)
],
)
],
),
],
)
print(response)回應引用了兩個來源。預先擷取的結果是 search_result_index: 0,工具回傳的結果是 search_result_index: 1,與 search_result 區塊在對話中出現的順序一致:
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "Here's what I found about Acme Dashboard:\n\n**What it does:** "
},
{
"type": "text",
"text": "Acme Dashboard is a monitoring tool for distributed systems. It supports real-time alerting and custom metric dashboards.",
"citations": [
{
"type": "search_result_location",
"cited_text": "Acme Dashboard is a monitoring tool for distributed systems. It supports real-time alerting and custom metric dashboards.",
"source": "https://docs.company.com/overview",
"title": "Product Overview",
"search_result_index": 0,
"start_block_index": 0,
"end_block_index": 1
}
]
},
{
"type": "text",
"text": "\n\n**Available plans:** "
},
{
"type": "text",
"text": "Acme Dashboard is available on the Starter plan at $10 per user per month and the Enterprise plan with custom pricing.",
"citations": [
{
"type": "search_result_location",
"cited_text": "Acme Dashboard is available on the Starter plan at $10 per user per month and the Enterprise plan with custom pricing.",
"source": "https://docs.company.com/pricing",
"title": "Pricing Plans",
"search_result_index": 1,
"start_block_index": 0,
"end_block_index": 1
}
]
}
]
}在使用者訊息中,search_result 區塊可以與任何其他內容區塊並列。方法 2 的範例將搜尋結果與 text 問題配對,圖片或文件區塊也可以用相同的方式加入。
工具結果則較為嚴格:如果 tool_result 內容陣列中的任何區塊是 search_result,則其所有區塊都必須是 search_result。在同一個工具結果中將搜尋結果與其他區塊類型混合會回傳驗證錯誤。若要在工具來源的搜尋結果旁回傳輔助文字,請將其作為文字區塊包含在其中一個搜尋結果的 content 陣列內,這樣它也會成為可引用的內容。
在搜尋結果區塊上加入 cache_control 以快取它,供跨請求重複使用。它與 citations 位於同一個區塊上:
{
"type": "search_result",
"source": "https://docs.company.com/guide",
"title": "User Guide",
"content": [{ "type": "text", "text": "..." }],
"citations": { "enabled": true },
"cache_control": { "type": "ephemeral" }
}請參閱提示快取以了解最小可快取長度和其他要求。
預設情況下,搜尋結果的引用是停用的。您可以透過明確設定 citations 設定來啟用引用:
{
"type": "search_result",
"source": "https://docs.company.com/guide",
"title": "User Guide",
"content": [{ "type": "text", "text": "Important documentation..." }],
"citations": {
"enabled": true // Enable citations for this result
}
}當 citations.enabled 設為 true 時,Claude 會將引用參照附加到使用該搜尋結果的文字區塊上。
有效地建構結果:
保持一致性:
優雅地處理錯誤: 當搜尋失敗或沒有回傳任何結果時,回傳描述結果的純文字區塊(例如 {"type": "text", "text": "No results found."}),而不是引發錯誤:Claude 會向使用者解釋空結果,對話得以繼續。
search_result 區塊只能出現在使用者訊息中(包括工具結果內)。帶有搜尋結果的助理訊息會被拒絕。search_result 區塊都必須啟用引用。在串流回應中偵測和處理拒絕停止原因,並在備援模型上重試被拒絕的請求。
讓 Claude 的回應以您的來源文件為依據。引用會回傳支持每個主張的確切段落,讓您可以驗證答案並向使用者呈現來源。
讓 Claude 存取帶有引用來源的最新網路內容,並提供選用的動態篩選和網域控制。
查看完整的 Messages API 文件,包括內容區塊類型。
使用 cache_control 快取搜尋結果,以降低重複請求的成本和延遲。
Was this page helpful?