Introducing Tool Retrieval in the Gemini API
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Introducing Tool Retrieval in the Gemini API
Gemini API 中工具检索(Tool Retrieval)功能现已推出预览版。与其在每次提示中发送完整的工具库,不如按需加载工具参数架构,此举旨在减少输入 Token 并确保代理(Agent)在扩展时保持工具选择的准确性。
Today we’re launching Tool Retrieval in the Gemini API, available in preview in the Interactions API. When an agent connects to dozens or even hundreds of tools, sending every full parameter schema on every request adds up fast. It inflates input token counts, adds latency, and lowers tool-selection accuracy as the candidate list grows.
当代理连接到数十甚至数百个工具时,在每次请求中发送完整的参数架构会迅速累积。随着候选列表的增加,这会增加输入 Token 数量、增加延迟,并降低工具选择的准确性。
Tool Retrieval lets your agent keep its full tool library on standby and load parameter schemas only when the current task needs them. To turn on Tool Retrieval, add a tool_search tool to your request and set defer_loading: true on the tools you want to hold back.
工具检索功能允许您的代理将完整的工具库保持在待机状态,仅在当前任务需要时才加载参数架构。要开启工具检索,请在请求中添加一个 tool_search 工具,并将您希望延迟加载的工具设置为 defer_loading: true。
The model still sees the name and description of every deferred tool, so it knows what’s available, but the parameter schemas stay out of context. When the model needs a tool, it asks for it by name, the API loads the full schema, and the model calls the function, all inside the same request.
模型仍然可以看到每个延迟加载工具的名称和描述,因此它知道有哪些可用工具,但参数架构不会占用上下文空间。当模型需要某个工具时,它会按名称请求该工具,API 会加载完整架构,随后模型调用该函数,整个过程都在同一个请求内完成。
There’s no index to build or host. Gemini runs the search on the server, so the only changes to your code are the new tool and a flag on the tools you want to defer. You can keep your most-used tools loaded up front and defer the long tail.
无需构建或托管索引。Gemini 在服务器端运行搜索,因此您代码中唯一需要更改的地方就是添加新工具以及为您想要延迟加载的工具设置标志。您可以将最常用的工具预先加载,而将长尾工具延迟加载。
Tool Retrieval also plays well with prefix caching. Deferred schemas stay out of the start of the prompt, and the schemas the model loads are added later in the conversation, so the cached prefix stays the same from turn to turn.
工具检索功能也与前缀缓存(prefix caching)配合良好。延迟加载的架构不会出现在提示词的开头,模型加载的架构是在对话后期添加的,因此缓存的前缀在每一轮对话中都能保持不变。