網路搜尋功能可讓 Claude 模型從網路上擷取即時資料,擴充知識。Claude 模型會使用第三方搜尋供應商的服務,提供網頁搜尋功能。對於需要準確引用來源的 RAG (檢索增強生成) 應用程式而言,網頁搜尋功能特別強大。
網頁搜尋功能非常適合下列用途:
- 取得最新技術文件:軟體開發人員可以向 Claude 詢問最新的 API 說明文件或技術規格,Claude 能夠直接從來源說明文件擷取並整合資訊。
- 收集業務和市場資訊:業務分析師可以收集目前的市場資料、公司資訊或產業新聞,做為決策過程的參考依據
- 協助研究:知識工作者可將 Claude 做為研究助理,從多個網路來源尋找、整合及說明資訊,同時維持準確的引用出處
- 支援客戶:客戶服務應用程式可從公司網站存取最新的產品資訊、價格或支援文件
支援的模型
Vertex AI 支援下列 Anthropic Claude 模型的網頁搜尋功能:
使用網路搜尋
在 API 要求中提供新的 Anthropic 工具定義 (web_search
),並使用 Beta 版標頭傳送要求,即可啟用網頁搜尋功能:"anthropic-beta": "web-search-2025-03-05"
舉例來說,以下程式碼片段會使用網路搜尋功能,擷取即時天氣資訊:
response = await client.messages.create(
model="claude-4-sonnet-latest",
max_tokens=1024,
messages=[{
"role": "user",
"content": "What is the weather in San Francisco?"
}],
tools=[{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 5
}]
)
您也可以使用選填欄位指定允許和封鎖的網域。例如:
tools=[{
...
# Only one of allowed_domains and blocked_domains can be provided.
# allowed_domains excludes everything except the specified domains
"allowed_domains": ["example.com", "example.org"],
# blocked_domains includes everything except the specified domains
# Use the `NOT site:` search operator
"blocked_domains": ["example.net"],
}]
如要進一步瞭解選填欄位,請參閱 Claude 網頁搜尋工具說明文件
模型判斷需要網路搜尋時,API 會叫用網路搜尋工具,直到不需要再呼叫工具為止。回應會包含中繼搜尋的詳細資料,結果則會經過加密。
以下程式碼片段是包含網頁搜尋的範例回應:
{
...
"role": "assistant",
"content": [
// Initial output from the model
{
"type": "text",
"text": "I need to search the web to answer this question about the weather in San Francisco."
},
{
"type": "server_tool_use",
"id": "srvtoolu_9a90a390a",
"name": "web_search",
"input": {
"query": "What is the weather in San Francisco?",
}
},
// API sees an `</antml:function_calls>` stop sequence here, sees that
// the tool use is for a server-side tool, and then runs that
// tool. If there is a mix of client and server-side, the API would
// need to return to the client and let it pass back client tool results
// before continuing with server tools.
{
"type": "web_search_tool_result",
// "name": "web_search", use this to determine what types of blocks are inside `content`
"tool_use_id": "srvtoolu_9a90a390a",
"content": [
{
"type": "web_search_result",
"uri": "https://weather.com",
"title": "National and Local Weather",
"encrypted_content": "eyJlbmNyeXB0ZWRfa2V5Ijoic2VjcmV0LW...", // encrypted list of snippets from the web search provider
},
{
"type": "web_search_result",
"uri": "https://accuweather.com",
"title": "San Francisco, CA Hourly Weather",
"encrypted_content": "yaJlbmNyeXB0ZWRfa2V5Ijoic2VjcmV0LW...", // encrypted list of snippets from the web search provider
},
],
},
// Server starts a new sampling run with the above content appended (unencrypted)
// to the original request. Everything below is completion content from the
// second sampling run.
{
"type": "text",
"text": "The current weather in San Francisco, CA is 65 degrees Fahrenheit and partly cloudy."
}
],
// Server output is no longer 1-to-1 with a single sampling run. From the
// user's perspective, there is just one "Message" with a single id
"id": "msg_a930390d3",
"usage": {
"input_tokens": 3029,
"output_tokens": 6039,
"server_tool_use": {
"web_search_requests": 2
}
},
"stop_reason": "end_turn"
}
透過網頁搜尋串流播放
啟用串流後,模型會將搜尋事件做為串流的一部分傳回。 例如:
// Claude's decision to search
event: content_block_start
data: {"type": "content_block_start", "index": 1, "content_block": {"type": "server_tool_use", "id": "srvtoolu_xyz789", "name": "web_search"}}
// Search query streamed
event: content_block_delta
data: {"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": "{\"query\":\"latest quantum computing breakthroughs 2025\"}"}}
// Pause while search executes
// Search results streamed
event: content_block_start
data: {"type": "content_block_start", "index": 2, "content_block": {"type": "web_search_tool_result", "tool_use_id": "srvtoolu_xyz789", "content": [{"type": "web_search_result", "title": "Quantum Computing Breakthroughs in 2025", "url": "https://example.com"}]}}
用量會經過計量,並納入 message_delta
訊息,包括 web_search
項目累計數量。例如:
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":10682,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":510,"server_tool_use":{"web_search_requests":1}}}
啟用網頁搜尋功能的要求資料管理
Claude 4.0 和更新版本的模型提供網頁搜尋功能,但這項功能是 Anthropic Claude 模型的選用功能。如要啟用這項功能,請在要求中提供網頁搜尋工具。啟用這項功能後,您會指示 Vertex AI 啟用 Claude 模型,將從要求衍生的搜尋查詢傳送至 Anthropic 選取的第三方搜尋服務供應商。Google 對於第三方供應商的資料處理做法概不負責。搜尋查詢傳輸完成後,該資料的處理方式會受到第三方的《服務條款》和《隱私權政策》規範,而非 Google 的條款。
啟用網頁搜尋功能會直接影響資料控管和安全措施。在第三方搜尋服務供應商處理資料時,Vertex AI 的額外承諾 (例如客戶自行管理的加密金鑰 (CMEK) 和資料落地) 不適用於這些資料。如果您的消費者專案位於 VPC-SC 範圍內,Vertex AI 會拒絕要求,並顯示下列錯誤訊息:
{
"error": {
"code": 400,
"message": "The requested model does not serve requests restricted by VPCSC",
"status": "FAILED_PRECONDITION"
}
}
這項功能僅適用於 Claude 4.0 和後續模型,不適用於 Vertex AI 上的舊版 Claude 模型。如果使用網頁搜尋功能,Anthropic 網頁搜尋功能可呼叫的第三方搜尋服務清單如下:
定價
網路搜尋是標準 Messages API 用量的加購項目,費用取決於網路要求量。由於額外取樣呼叫,使用者也需支付額外的輸入權杖費用。
詳情請參閱「定價」一文。