Claude 網路搜尋
本頁說明如何讓 Claude 透過 web_search 工具進行網路搜尋。模型 ID、介面位址與供應商說明請見 Claude 系列概覽。
重點Sonnet 5.5 網路搜尋使用 Anthropic 原生 Messages 介面,並指定 Anthropic 供應商。其他型號請在模型頁查看是否標示 Web Search。
在 tools 中加入官方的 web_search 伺服器端工具,模型會自行發起搜尋並在回答中引用來源:
Python
claude_web_search.py
import os
import anthropic
client = anthropic.Anthropic(
base_url="https://api.ofox.ai/anthropic",
api_key=os.environ["OFOX_API_KEY"],
default_headers={"X-OfoxAI-Provider-Type": "anthropic"},
)
message = client.messages.create(
model="anthropic/claude-sonnet-5.5",
max_tokens=192,
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 1, "allowed_domains": ["python.org"]}],
messages=[{"role": "user", "content": "使用網路搜尋在 python.org 上查找 Python 官方下載頁,只搜尋一次,只回傳網址。"}],
)
print("".join(b.text for b in message.content if b.type == "text"))
print("搜尋次數:", message.usage.server_tool_use.web_search_requests)- 回應的
content中會依序出現server_tool_use(搜尋關鍵字)、web_search_tool_result(搜尋結果)與text(回答)區塊。 usage.server_tool_use.web_search_requests為實際搜尋次數,搜尋按次計費,單價請見模型頁 。max_uses限制單次請求的搜尋次數。搜尋結果也可能計入輸入 token 費用。
其他錯誤請見常見錯誤。
最後更新於