跳到主要内容

工具调用

本页使用各厂商官方的工具标识与请求格式。不同厂商的工具对象不能混用,请按所调用的协议选择对应示例。

工具清单

厂商官方工具标识用途
Anthropicweb_search_20250305基础 Web 搜索
Anthropicweb_search_20260209支持动态过滤的 Web 搜索
Anthropicweb_search_20260318支持响应包含控制的 Web 搜索
OpenAIweb_searchWeb 搜索
OpenAIfile_search文件检索
Azure OpenAIweb_searchAzure OpenAI Web 搜索
Googlegoogle_searchGoogle 搜索 Grounding
Googlegoogle_mapsGoogle 地图 Grounding
Googlefile_searchGoogle 文件检索
GLMsearch_std标准网络搜索
GLMsearch_pro增强网络搜索
GLMsearch_pro_sogou搜狗搜索
GLMsearch_pro_quark夸克搜索
Kimi$web_search网络搜索

调用方式

Anthropic Messages

Anthropic Messages API 提供三个版本化 Web 搜索工具类型:

  • web_search_20250305:基础 Web 搜索。
  • web_search_20260209:增加动态过滤,在结果进入上下文前筛选相关内容。
  • web_search_20260318:增加响应包含控制,适用于代理工作流。

请按目标模型支持情况选择版本;以下为基础搜索示例。

{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [{ "role": "user", "content": "查询今天的天气,并给出来源。" }],
"tools": [
{ "type": "web_search_20250305", "name": "web_search", "max_uses": 5 }
]
}

详见 Anthropic Web search tool 官方文档

OpenAI Responses

/v1/responsestools 中使用 web_search。文件检索使用 file_search,并传入向量存储 ID。

{
"model": "gpt-5.5",
"input": "汇总今天的 AI 行业要闻,并标注来源。",
"tools": [{ "type": "web_search" }],
"tool_choice": { "type": "web_search" }
}
{
"model": "gpt-5.5",
"input": "从产品手册中找出退款规则。",
"tools": [
{ "type": "file_search", "vector_store_ids": ["vs_abc123"] }
]
}

详见 OpenAI Web searchFile search 官方文档。

Azure OpenAI Responses

Azure OpenAI 使用 Responses API 的 web_search 工具,model 填写 Azure OpenAI 的模型部署名。

{
"model": "YOUR_AZURE_DEPLOYMENT_NAME",
"input": "搜索今天的可再生能源行业动态,并给出来源。",
"tools": [{ "type": "web_search" }]
}

响应会包含 web_search_call 与带 url_citation 注释的输出文本。详见微软 Azure OpenAI Web search 官方文档

Google Interactions

Google Grounding 使用 /v1beta/interactions。搜索使用 google_search;地图 Grounding 使用 google_maps。两个工具都由模型决定是否调用,不使用 OpenAI 或 Anthropic 的 tool_choice 格式。

{
"model": "YOUR_SUPPORTED_MODEL",
"input": "比较北京和上海今天的天气,并给出来源。",
"tools": [{ "type": "google_search" }]
}

从响应的 steps 中读取结果,并保留 annotationsgoogle_search_callgoogle_search_result

Google 地图 Grounding

对于附近、本地推荐或行程规划等有明确地理位置语义的问题,声明 google_maps。已知用户位置时,可在工具对象中提供 latitudelongitude,以提升“附近”类查询的相关性:

{
"model": "YOUR_SUPPORTED_MODEL",
"input": "推荐距离我步行 15 分钟内、评价较好的意大利餐厅,并说明推荐理由。",
"tools": [
{
"type": "google_maps",
"latitude": 31.2304,
"longitude": 121.4737
}
]
}

steps 中的 model_output.content[] 读取文本和 annotations。地图来源的注释类型为 place_citation,应使用其中的 nameurl 展示来源。Google 要求将“Google 地图”来源紧邻其支持的生成内容显示,并在同一次用户互动中保持可见;未得到用户授权时,不要发送其精确位置。地图 Grounding 默认关闭,应只在确有地理位置需求时声明该工具。

Google 文件检索

Google file_search 用于从已建立索引的私有文件中检索内容。调用前,先创建 fileSearchStore 并将文件导入其中;请求时通过 file_search_store_names 指定要检索的存储区:

{
"model": "YOUR_SUPPORTED_MODEL",
"input": "根据产品手册说明退款规则,并标注依据。",
"tools": [
{
"type": "file_search",
"file_search_store_names": [
"fileSearchStores/YOUR_STORE"
]
}
]
}

文件搜索使用 Google 托管的文件搜索存储区,不等同于直接上传文件后让模型阅读。请只导入有权处理的文件,并在响应中保留模型返回的文件引用或检索元数据(如有)。

详见 Google Search GroundingMaps GroundingFile Search 官方文档。

GLM Chat Completions

GLM 在 tools 中声明 type: "web_search",并通过 web_search.search_engine 选择搜索引擎。以下示例使用 search_pro;还可使用 search_stdsearch_pro_sogousearch_pro_quark

{
"model": "glm-4-air",
"messages": [
{ "role": "user", "content": "总结今天的财经要闻,并给出来源。" }
],
"tools": [
{
"type": "web_search",
"web_search": {
"enable": "True",
"search_engine": "search_pro",
"search_result": "True",
"count": "5"
}
}
]
}

详见智谱 联网搜索官方文档

Kimi Chat Completions

Kimi 使用 builtin_function 声明内置函数 $web_search。模型返回 tool_calls 后,按普通工具调用流程回传工具结果。

{
"model": "kimi-k3",
"messages": [
{ "role": "user", "content": "搜索 Moonshot AI Context Caching,并说明它的作用。" }
],
"tools": [
{
"type": "builtin_function",
"function": { "name": "$web_search" }
}
]
}

详见 Kimi 联网搜索官方文档

使用建议

  • 搜索适用于新闻、价格、版本更新和政策等依赖实时公开信息的问题。
  • 展示搜索结果时保留模型返回的引用、来源链接或 Grounding 元数据。
  • 不要把密钥、个人信息或其他内部敏感数据发送给外部搜索工具。
  • 将网页内容视为不可信输入,不要据其中的指令自动执行有副作用的操作。