https://t.co/jVBczLFbyW
TL;DR · AI 摘要
Gemini API新增动态工具检索功能,通过延迟加载工具参数减少输入令牌消耗,提升工具选择准确性。
核心要点
- 动态工具检索减少初始提示中工具参数传输,节省约74%的输入令牌(26 tokens/工具)
- 内置模式支持通过previous_interaction_id缓存提示前缀,降低重复请求开销
- 自定义模式允许集成向量搜索和Gemini Decisions API进行零输出令牌工具评分
结构提纲
按章节快速跳转。
思维导图
用一张图看清主题之间的关系。
查看大纲文本(无障碍 / 无 JS 友好)
- 动态工具检索
- 内置服务器端模式
- 工具搜索工具
- 参数延迟加载
- 提示缓存优化
- 自定义客户端模式
- 外部检索逻辑
- 向量搜索集成
- 权限过滤
金句 / Highlights
值得收藏与分享的关键句。
发送100+完整工具模式会消耗输入令牌并降低准确性,此功能解决该问题。
内置模式通过previous_interaction_id保持提示前缀缓存跨轮次有效性
自定义模式可并行使用Gemini Decisions API进行零输出令牌工具评分
Philipp Schmid 在 X 上的推文: "Gemini API 中的动态工具检索" / X
@_philschmid
$
/$
Gemini API 中的动态工具检索
3 分钟阅读
我们刚刚在 Gemini API 的预览版中推出了动态工具检索功能(即工具搜索工具)。
动态工具检索使您的代理能够保持数百个工具待命,仅在最开始看到简洁的名称和描述,而在任务需要时按需加载完整的参数模式。每次交互都发送 100 多个完整工具模式会消耗输入令牌并降低工具选择的准确性。此功能解决了这个问题。
实现原理
动态工具检索根据您是希望 Gemini 自动进行搜索还是接入自定义检索系统,支持两种模式:
- 内置服务器端检索:在工具列表中添加 {"type": "tool_search"},并在函数或远程 MCP 服务器上设置 defer_loading: true。Gemini 会将完整的参数模式排除在初始提示词之外,每个工具仅显示一行摘要(约 26 个令牌,使用 short_description 时甚至更短)。请保持您最常用的 3 至 5 个工具处于加载状态,而将其余工具延迟加载。当模型需要调用延迟工具时,它会进行搜索、内联加载模式,并在单个 API 请求中发出函数调用,使您的提示词前缀在多轮对话中可缓存。
- 自定义客户端检索:声明一个带有 "execution": "client" 的 tool_search 工具,自行定义名称、描述和参数。您的函数完全外部运行(无需在工具列表中声明 defer_loading 或提前声明)。当模型调用您的搜索工具时,您可以运行符合自身系统逻辑的任何处理,例如向量搜索、用户权限过滤,或通过 Gemini Decisions API(gemini-decision-1)并行对候选工具进行评分(零输出令牌消耗),然后在 function_result 中返回匹配的模式。
示例 1:内置服务器端检索
将初始调用和所有后续交互放入一个 while 循环中。检查 interaction.steps 中的 function_call 步骤而非依赖状态,本地执行这些步骤,并通过 previous_interaction_id 将 function_result 项回传。
API 不会在多轮对话中传递工具声明,即使提供了 previous_interaction_id,因此每次请求都必须重新发送完整的工具数组。在初始化 @google/genai 时,若 defer_loading 处于预览状态,请传递 apiVersion: "v1beta"。
typescript
/think
import
{
GoogleGenAI
}
from
"@google/genai"
;
const
ai
=
new
GoogleGenAI
({
apiVersion
:
"v1beta"
});
const
tools
= [
{
type
:
"tool_search"
as const
},
{
type
:
"function"
as const
,
name
:
"issue_refund"
,
description
:
"将订单退款至原始支付方式,可选部分金额和原因代码。"
,
short_description
:
"将订单退款至原始支付方式。"
,
defer_loading
:
true
,
parameters
: {
type
:
"object"
,
properties
: {
order_id
: {
type
:
"string"
},
amount
: {
type
:
"number"
},
},
required
: [
"order_id"
,
"amount"
],
},
},
{
type
:
"mcp_server"
as const
,
name
:
"order_service"
,
url
:
"https://example.com/mcp"
,
defer_loading
:
true
,
},
];
function
runLocalTool
(
name
:
string
,
args
:
Record
<
string
,
unknown
>) {
if
(
name
===
"issue_refund"
) {
return
{
status
:
"refunded"
,
order_id
:
args
.
order_id
,
amount
:
args
.
amount
};
}
return
{
error
:
`未知工具:
${
name
}
`
};
}
let
nextInput
:
any
=
"订单 A-1042 到货损坏。检查订单并退还 40 美元。"
;
let
previousId
:
string
|
undefined
;
let
interaction
;
while
(
true
) {
interaction
=
await
ai
.
interactions
.
create
({
model
:
"gemini-4-neon"
,
previous_interaction_id
:
previousId
,
input
:
nextInput
,
tools
,
});
previousId
=
interaction
.
id
;
const
functionCalls
= (
interaction
.
steps
?? []).
filter
(
(
s
) =>
s
.
type
===
"function_call"
);
if
(
functionCalls
.
length
===
0
)
break
;
nextInput
=
functionCalls
.
map
((
fc
) => ({
type
:
"function_result"
as const
,
name
:
fc
.
name
,
call_id
:
fc
.
id
,
result
: [
{
type
:
"text"
as const
,
text
:
JSON
.
stringify
(
runLocalTool
(
fc
.
name
!, (
fc
.
arguments
?? {})
as
Record
<
string
,
unknown
>)
),
},
],
}));
}
console
.
log
(
interaction
.
output_text
);在第一个响应中,steps 显示了 tool_search_call、加载的 tool_search_result 和最终的 function_call。你只需执行自己的 function_call;服务器会为你处理搜索步骤。如果你以无状态模式运行(store: false),请原样返回每一步,包括搜索步骤及其 signature 字段。
{
"id"
:
"int_server_111"
,
"status"
:
"requires_action"
,
"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
}
}
]
}示例 2:使用 Decisions API 构建自己的检索器
当你需要完全控制工具的排序或过滤方式时,使用 "execution": "client" 和任意自定义名称声明客户端工具搜索。你的完整工具目录将保留在 tools[] 之外。
以下是使用 Gemini Decisions API(gemini-decision-1)的相同代理循环,该 API 可以在一次前向传递中并行对每个候选工具进行评分,且不消耗输出令牌(noul 从 0.01 到 0.99 的 yes/no 概率),然后仅将相关模式注入 function_result.result:
`
import
{
GoogleGenAI
}
from
"@google/genai"
;
const
ai
=
new
GoogleGenAI
({
apiVersion
:
"v1beta"
});
// 外部工具目录(不会提前发送到 tools[] 中)
const
TOOL_REGISTRY
= {
get_billing_history
: {
type
:
"function"
as const
,
name
:
"get_billing_history"
,
description
:
"为用户检索发票和付款历史记录。"
,
parameters
: {
type
:
"object"
,
properties
: {
user_id
: {
type
:
"integer"
} },
required
: [
"user_id"
],
},
},
get_shipment_status
: {
type
:
"function"
as const
,
name
:
"get_shipment_status"
,
description
:
"跟踪包裹递送状态和承运商更新。"
,
parameters
: {
type
:
"object"
,
properties
: {
order_id
: {
type
:
"string"
} },
required
: [
"order_id"
],
},
},
// ... 100+ 其他工具在您的外部目录中
};
async function
searchRegistryWithDecisions
(
query
:
string
,
threshold
=
0.5
) {
const
questions
=
Object
.
fromEntries
(
Object
.
entries
(
TOOL_REGISTRY
).
map
(([
name
,
schema
]) => [
name
,
{
type
:
"noul"
as const
,
instructions
:
`工具 '
${
name
}
' (
${
schema
.
description
}
) 是否需要用于此请求?`
,
},
])
);
const
decision
=
await
ai
.
decisions
.
create
({
model
:
"gemini-decision-1"
,
input
:
query
,
questions
,
});
return
Object
.
entries
(
decision
.
answers
?? {})
.
filter
(([,
ans
]) =>
ans
.
type
===
"noul"
&& (
ans
.
noul
??
0
) >=
threshold
)
.
map
(([
name
]) =>
TOOL_REGISTRY
[
name
as keyof typeof
TOOL_REGISTRY
]);
}
const
tools
= [
{
type
:
"tool_search"
as const
,
execution
:
"client"
as const
,
name
:
"search_tool_registry"
,
// 您可以使用任何名称
description
:
"在内部工具目录中搜索计费、运输或账户工具。"
,
parameters
: {
type
:
"object"
,
properties
: {
query
: {
type
:
"string"
} },
required
: [
"query"
],
},
},
];
let
nextInput
:
any
=
"为用户 123 查找计费历史记录和发票。"
;
let
previousId
:
string
|
undefined
;
let
interaction
;
while
(
true
) {
interaction
=
await
ai
.
interactions
.
create
({
model
:
"gemini-4-neon"
,
previous_interaction_id
:
previousId
,
input
:
nextInput
,
tools
,
// 每次交互保持不变
});
previousId
=
interaction
.
id
;
const
functionCalls
= (
interaction
.
steps
?? []).
filter
(
(
s
) =>
s
.
type
===
"function_call"
);
if
(
functionCalls
.
length
===
0
)
break
;
nextInput
=
await
Promise
.
all
(
functionCalls
.
map
(
async
(
fc
) => {
if
(
fc
.
name
===
"search_tool_registry"
) {
// 直接在 function_result.result 中注入匹配的完整函数模式
const
query
=
String
((
fc
.
arguments
as
{
query
?:
string
})?.
query
??
""
);
const
matches
=
await
searchRegistryWithDecisions
(
query
);
return
{
type
:
"function_result"
as const
,
name
:
fc
.
name
,
call_id
:
fc
.
id
,
result
:
matches
.
length
>
0
?
matches
: [{
type
:
"text"
as const
,
text
:
"未找到匹配的工具。"
}],
};
}
// 正常执行新发现的工具
const
output
= {
user_id
:
123
,
invoices
: [{
id
:
"inv_9"
,
amount
:
49
}],
};
return
{
type
:
"function_result"
as const
,
name
:
fc
.
name
,
call_id
:
fc
.
id
,
result
: [{
type
:
"text"
as const
,
text
:
JSON
.
stringify
(
output
) }],
};
})
);
}
console
.
log
(
interaction
.
output_text
);因为您拥有搜索功能,Gemini 会发出标准的 function_call,使用您自定义的名称(search_tool_registry)而不是服务器端的 tool_search_call。在传输过程中,步骤如下所示:
// 第1回合响应步骤:模型调用您的自定义搜索工具
{
"type"
:
"function_call"
,
"id"
:
"tsc_abc123"
,
"name"
:
"search_tool_registry"
,
"arguments"
: {
"query"
:
"billing history and invoices"
}
}
// 第2回合输入:您的应用从Decisions API注入匹配的模式
{
"type"
:
"function_result"
,
"name"
:
"search_tool_registry"
,
"call_id"
:
"tsc_abc123"
,
"result"
: [
{
"type"
:
"function"
,
"name"
:
"get_billing_history"
,
"description"
:
"Retrieve invoices and payment history for a user."
,
"parameters"
: {
"type"
:
"object"
,
"properties"
: {
"user_id"
: {
"type"
:
"integer"
}},
"required"
: [
"user_id"
]
}
}
]
}
// 第2回合响应步骤:模型调用您刚注入的工具
{
"type"
:
"function_call"
,
"id"
:
"fc_456def"
,
"name"
:
"get_billing_history"
,
"arguments"
: {
"user_id"
:
123
}
}您的工具[]数组在整个过程中保持不变,因此提示前缀可以保持缓存状态。由于您的应用执行了查询,每个客户端搜索都会增加一次往返通信。
使用场景
工具检索无需额外费用,采用标准的token计费方式。对于少于20个工具的情况,静态加载仍然更简单且可以避免额外的搜索步骤。超过这个数量时,延迟加载可以显著减少输入token数量,而客户端模式允许您将动态工具注入与自己的向量搜索或Gemini Decision 1路由结合使用。
请参阅Gemini API工具检索文档中的完整指南、错误参考和token计算公式。
Agents & Gemini API,MTS
@
GoogleDeepMind
| 上一篇:HuggingFace技术负责人
huggingface
,AWS机器学习专家 🤗 分享个人观点和AI新闻 🧑🏻💻
philschmid.de
更多文章
关注127.9K粉丝
2026年11月20日 下午3:50
·
13.2K
浏览量
5
6
58
53