使用 Anthropic Claude 模型进行网页搜索

网络搜索是一项功能,可让 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 模型的一项可选功能。您可以在请求中提供 Web 搜索工具来启用它。启用此功能后,您即指示 Vertex AI 允许 Claude 模型将从您的请求中派生的搜索查询发送给 Anthropic 选择的第三方搜索服务提供商。Google 对此第三方提供商的数据处理实践不承担任何责任。搜索查询传输完成后,该数据的处理将受第三方的服务条款和隐私权政策(而非 Google 的条款)约束。

启用网页搜索功能会直接影响您的数据治理和安全状况。Vertex AI 的其他承诺(例如客户管理的加密密钥 [CMEK] 和数据驻留)不适用于第三方搜索提供商处理的数据。如果您的消费项目位于 VPC-SC 边界内,VertexAI 将拒绝请求,并显示以下错误消息:

{
  "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 用量的加购项,基于网络请求量。由于额外的抽样调用,用户还需要为额外的输入令牌付费。

如需了解详情,请参阅价格