Skip to Content
模型用法Claude網路搜尋

Claude 網路搜尋

本頁說明如何讓 Claude 透過 web_search 工具進行網路搜尋。模型 ID、介面位址與供應商說明請見 Claude 系列概覽。

重點網路搜尋需指定 Anthropic 供應商,僅限 Fable 5、Opus 4.8、Opus 4.7、Sonnet 5,且僅支援 Anthropic 原生介面。

在 tools 中加入官方的 web_search 伺服器端工具,模型會自行發起搜尋並在回答中引用來源:

claude_web_search.py
import os import anthropic client = anthropic.Anthropic( base_url="https://api.ofox.io/anthropic", api_key=os.environ["OFOX_API_KEY"], default_headers={"X-OfoxAI-Provider-Type": "anthropic"}, ) message = client.messages.create( model="anthropic/claude-sonnet-5", max_tokens=1500, tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}], messages=[{"role": "user", "content": "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 用來限制單次請求的最多搜尋次數。

其他錯誤請見常見錯誤。

最後更新於