Gemini API 工具检索功能入门指南
Gemini API 推出工具检索预览功能,通过按需加载工具参数模式,减少输入令牌和延迟,并提升工具选择准确性。本指南介绍如何启用该功能、查看交互步骤以及客户端检索的用法。
查看官方参考| 项目 | 详情 |
|---|---|
| 提供方 | |
| 难度 | 入门 |
| 发布日期 | 2026-11-19 |
| 官方来源 | |
| 主题 | #llm · #engineering |
什么是工具检索
Gemini API 中的工具检索(Tool Retrieval)现已推出预览版,可在 Interactions API 中使用。当智能体连接数十甚至数百个工具时,每次请求都发送完整的参数模式会快速增加输入令牌数量、增加延迟,并随着候选列表增长而降低工具选择准确性。工具检索让智能体将完整工具库保持在待命状态,仅在当前任务需要时加载参数模式。
如何启用工具检索
要开启工具检索,请在请求中添加 tool_search 工具,并对希望暂缓加载的工具设置 defer_loading: true。模型仍然可以看到每个延迟工具的名称和描述,因此知道有哪些工具可用,但参数模式不会进入上下文。当模型需要某个工具时,它会按名称请求该工具,API 加载完整模式,然后模型调用该函数,所有这些都在同一个请求内完成。
以下代码示例展示了如何配置:
from google import genai
client = genai.Client()
tools = [
{"type": "tool_search"},
{
"type": "function",
"name": "issue_refund",
"description": "Refund an order to the original payment method.",
"defer_loading": True,
"parameters": {
"type": "object",
"properties": {
"order_id": {"type": "string"},
"amount": {"type": "number"},
},
"required": ["order_id", "amount"],
},
},
# ... 50+ other deferred functions (leave frequent tools undeferred)
]
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="Order A-1042 arrived damaged. Please refund 40 dollars.",
tools=tools,
)检查交互步骤
在单个 API 调用内部,模型搜索 issue_refund,API 加载其模式,模型发出 function_call。所有三个步骤都出现在 interaction.steps 中,便于在调试工具名称或描述时检查模型考虑了哪些工具。步骤示例如下:
"steps": [
{
"type": "tool_search_call",
"id": "tsc_7f2a",
"arguments": {"function_names": ["issue_refund"]},
"signature": "..."
},
{
"type": "tool_search_result",
"call_id": "tsc_7f2a",
"result": [{"type": "function", "name": "issue_refund", "parameters": {...}}],
"signature": "..."
},
{
"type": "function_call",
"id": "fc_91c3",
"name": "issue_refund",
"arguments": {"order_id": "A-1042", "amount": 40}
}
]之后就是常规的函数调用:代码执行退款并返回 function_result。无需构建或托管索引。Gemini 在服务器端运行搜索,因此代码的唯一更改是添加新工具并在要延迟的工具上设置标志。可以将最常用的工具保持在前端加载,并延迟长尾工具。
与前缀缓存和远程 MCP 服务器配合
工具检索与前缀缓存良好配合。延迟的模式不会出现在提示开头,模型加载的模式会在对话后期添加,因此缓存前缀在各轮之间保持不变。
工具检索也适用于 Gemini API 中的远程 MCP 服务器。在 mcp_server 工具上设置 defer_loading: true,该服务器暴露的每个工具都会按需加载,因此可以连接大型服务器而无需逐个列出其工具。
客户端检索的工作方式
如果维护自己的工具注册表、嵌入索引或按用户权限过滤器,请声明 tool_search 并设置 "execution": "client",并定义搜索参数。客户端检索每次搜索会增加一次往返,因为应用运行查找。工具无需预先声明,因此可以在用户连接集成时在对话中途暴露新工具。
tools = [{
"type": "tool_search",
"execution": "client",
"name": "search_tools",
"description": "Search the internal tool registry for functions.",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
}]
# When the model emits a function_call for "search_tools",
# return matching {"type": "function", ...} schemas in function_result:
followup = client.interactions.create(
model="gemini-4-hydrogen",
previous_interaction_id=interaction.id,
tools=tools,
input=[{
"type": "function_result",
"name": "search_tools",
"call_id": step.id,
"result": my_registry.search(**step.arguments),
}],
)定价与支持的模型
工具检索没有单独收费。输入和输出令牌按标准费率计费。该功能适用于 Gemini 3.6 及更高版本的模型。
限制与范围
- 该功能处于预览阶段,可能发生变化。
- 客户端检索每次搜索会增加一次往返。
- 需要 Gemini 3.6 或更高版本模型。
- 延迟工具的参数模式不会进入上下文,但名称和描述仍可见。
- 本指南仅基于官方文档,不涉及性能基准或具体成本节省数值。
📚 来源与延伸
本页内容基于以下权威来源整理,可验证、可引用。
引用规范:请注明来源为老金出海(laojinchuhai.com)并保留原文链接。
老金出品 · 用 AI 提效
AllModelsAPI一个 key,调多家大模型
AllModelsAPI 是多模型 API 中转站:一个 key 调多家模型,OpenAI 兼容接口,现有代码改个 base_url 就能迁。它不是演示品——我们自己的生产环境每天都在用。
相关内容
按主题 / 人物 / 专区自动互通