ADK工具的局限性¶
某些 ADK 工具存在局限性,这些局限性可能会影响您在代理工作流程中如何使用这些工具。本页面列出了这些工具的局限性以及相应的解决方法(如果有的话)。
每个代理只能使用一个工具的限制¶
ADK Python:内置的搜索功能(Search)的替代方案需要用户主动选择(即用户需要手动启用该替代方案)。
ADK Python v1.16.0 及更高版本可以绕过这一限制,从而使用 Google 搜索(Google Search)和 Agent Search 工具;不过这并非默认设置。你需要自行编写相应的代码来使用这些工具。 bypass_multi_tools_limit=True;共享的部分
google_search 从……导出的实例 google.adk.tools 请不要关闭该功能(即不要“关闭该标志”)。请参阅下面的解决方法 #2。
一般来说,你可以在一个代理(agent)中使用多个工具;但是,如果某个代理中使用了特定的工具,那么就无法再使用其他工具了。以下这些 ADK 工具只能单独使用(即不能与其他工具结合使用),且必须在一个代理对象中单独使用:
- 代码执行 使用 Gemini API(注意:在 TypeScript 中,这需要 Gemini 2.0 或更高版本;并且不存在此类限制)。
- Google 搜索 使用 Gemini API(注意:该限制仅适用于 TypeScript 中的 Gemini 1.x 版本)
- 代理搜索(Agent Search) (注意:该功能目前在 TypeScript 中尚不可用。)
例如,以下方法是在单个代理中同时使用这些工具中的某一个以及其他工具来实现的: 不受支持:
import {Agent, BuiltInCodeExecutor} from '@google/adk';
const rootAgent = new Agent({
name: 'RootAgent',
model: 'gemini-flash-latest',
description: 'Code Agent',
tools: [myCustomTool], // Assume myCustomTool is defined
codeExecutor: new BuiltInCodeExecutor(), // <-- NOT supported when used with tools
});
解决方法 #1:将代理程序(agent)封装在 AgentTool 中。¶
以下代码示例展示了如何使用多个内置工具,或者如何通过多个代理将这些内置工具与其他工具结合使用:
from google.adk.tools.agent_tool import AgentTool
from google.adk.agents import Agent
from google.adk.tools import google_search
from google.adk.code_executors import BuiltInCodeExecutor
search_agent = Agent(
model='gemini-flash-latest',
name='SearchAgent',
instruction="""
You're a specialist in Google Search
""",
tools=[google_search],
)
coding_agent = Agent(
model='gemini-flash-latest',
name='CodeAgent',
instruction="""
You're a specialist in Code Execution
""",
code_executor=BuiltInCodeExecutor(),
)
root_agent = Agent(
name="RootAgent",
model="gemini-flash-latest",
description="Root Agent",
tools=[AgentTool(agent=search_agent), AgentTool(agent=coding_agent)],
)
import {Agent, AgentTool, BuiltInCodeExecutor, GOOGLE_SEARCH} from '@google/adk';
const searchAgent = new Agent({
model: 'gemini-flash-latest',
name: 'SearchAgent',
instruction: "You're a specialist in Google Search",
tools: [GOOGLE_SEARCH],
});
const codingAgent = new Agent({
model: 'gemini-flash-latest', // Built-in code execution requires Gemini 2.0+ in ADK JS
name: 'CodeAgent',
instruction: "You're a specialist in Code Execution",
codeExecutor: new BuiltInCodeExecutor(),
});
const rootAgent = new Agent({
name: 'RootAgent',
model: 'gemini-flash-latest',
description: 'Root Agent',
tools: [new AgentTool({agent: searchAgent}), new AgentTool({agent: codingAgent})],
});
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.AgentTool;
import com.google.adk.tools.BuiltInCodeExecutionTool;
import com.google.adk.tools.GoogleSearchTool;
import com.google.common.collect.ImmutableList;
public class NestedAgentApp {
private static final String MODEL_ID = "gemini-flash-latest";
public static void main(String[] args) {
// Define the SearchAgent
LlmAgent searchAgent =
LlmAgent.builder()
.model(MODEL_ID)
.name("SearchAgent")
.instruction("You're a specialist in Google Search")
.tools(new GoogleSearchTool()) // Instantiate GoogleSearchTool
.build();
// Define the CodingAgent
LlmAgent codingAgent =
LlmAgent.builder()
.model(MODEL_ID)
.name("CodeAgent")
.instruction("You're a specialist in Code Execution")
.tools(new BuiltInCodeExecutionTool()) // Instantiate BuiltInCodeExecutionTool
.build();
// Define the RootAgent, which uses AgentTool.create() to wrap SearchAgent and CodingAgent
BaseAgent rootAgent =
LlmAgent.builder()
.name("RootAgent")
.model(MODEL_ID)
.description("Root Agent")
.tools(
AgentTool.create(searchAgent), // Use create method
AgentTool.create(codingAgent) // Use create method
)
.build();
// Note: This sample only demonstrates the agent definitions.
// To run these agents, you'd need to integrate them with a Runner and SessionService,
// similar to the previous examples.
System.out.println("Agents defined successfully:");
System.out.println(" Root Agent: " + rootAgent.name());
System.out.println(" Search Agent (nested): " + searchAgent.name());
System.out.println(" Code Agent (nested): " + codingAgent.name());
}
}
// Define the SearchAgent
val searchAgent =
LlmAgent(
name = "SearchAgent",
model = Gemini(name = modelId),
instruction = Instruction("You're a specialist in Google Search"),
tools = listOf(GoogleSearchTool()),
)
// Define another agent (e.g., for specialized tasks)
val taskAgent =
LlmAgent(
name = "TaskAgent",
model = Gemini(name = modelId),
instruction = Instruction("You're a specialist in performing specific tasks."),
)
// Define the RootAgent, which uses AgentTool to wrap SearchAgent and TaskAgent
val rootAgent =
LlmAgent(
name = "RootAgent",
model = Gemini(name = modelId),
description = "Root Agent",
tools =
listOf(
AgentTool(agent = searchAgent),
AgentTool(agent = taskAgent),
),
)
在 ADK Python 中,一个…… AgentTool 该功能无法将元数据(metadata)返回给调用该功能的代理(agent)。请设置相应的参数或代码以解决此问题。 propagate_grounding_metadata=True 在……上;关于…… AgentTool 为了将这些元数据存储在会话状态(session state)中……
temp:_adk_grounding_metadata key. ADK 仅会在父代理拥有名为 “[具体工具名称]” 的工具时,才会将该工具的数据附加到父代理自身的响应中。 google_search_agent这就是下面提到的“解决方案2”(Workaround #2)中为你创建的工具。
临时解决方案 #2:绕过 `multi_tools_limit` 的限制¶
ADK Python 提供了一个内置的解决方案,可以绕过这一限制。
GoogleSearchTool 和 VertexAiSearchTool (使用) bypass_multi_tools_limit=True 为了启用它,
请按照所示的方法操作。
built_in_multi_tools示例代理(Sample Agent)。
例如,你可以在自己创建的工具实例上设置该标志。
GoogleSearchTool(bypass_multi_tools_limit=True) 从……进口的;从……引进的
google.adk.tools.google_search_tool. 共享的 google_search 从……导出 google.adk.tools “leaves the flag off”:这意味着某个标志(或设置)被忽略了;因此,相应的规则或功能不会被执行。
“The bypass takes effect”:当代理(agent)的配置中包含多个相关条目时,旁路(bypass)机制就会生效。 tools 可以列出这些选项,或者将控制权转移给另一个代理。
警告
内置工具无法在子代理(sub-agent)中使用。在 ADK Python 中,
GoogleSearchTool 和 VertexAiSearchTool 只有在次级代理(sub-agent)使用特定方式构建该工具时,这种情况才属于例外。 bypass_multi_tools_limit=True.
例如,以下方法使用了子代理(sub-agents)中内置的工具: 不受支持:
url_context_agent = Agent(
model='gemini-flash-latest',
name='UrlContextAgent',
instruction="""
You're a specialist in URL Context
""",
tools=[url_context],
)
coding_agent = Agent(
model='gemini-flash-latest',
name='CodeAgent',
instruction="""
You're a specialist in Code Execution
""",
code_executor=BuiltInCodeExecutor(),
)
root_agent = Agent(
name="RootAgent",
model="gemini-flash-latest",
description="Root Agent",
sub_agents=[
url_context_agent,
coding_agent
],
)
import {Agent, BuiltInCodeExecutor} from '@google/adk';
const urlContextAgent = new Agent({
model: 'gemini-flash-latest',
name: 'UrlContextAgent',
instruction: "You're a specialist in URL Context",
tools: [myCustomTool], // Assume myCustomTool is defined
});
const codingAgent = new Agent({
model: 'gemini-flash-latest',
name: 'CodeAgent',
instruction: "You're a specialist in Code Execution",
codeExecutor: new BuiltInCodeExecutor(),
});
const rootAgent = new Agent({
name: 'RootAgent',
model: 'gemini-flash-latest',
description: 'Root Agent',
subAgents: [urlContextAgent, codingAgent], // NOT supported when sub-agents use built-in tools
});
LlmAgent searchAgent =
LlmAgent.builder()
.model("gemini-flash-latest")
.name("SearchAgent")
.instruction("You're a specialist in Google Search")
.tools(new GoogleSearchTool())
.build();
LlmAgent codingAgent =
LlmAgent.builder()
.model("gemini-flash-latest")
.name("CodeAgent")
.instruction("You're a specialist in Code Execution")
.tools(new BuiltInCodeExecutionTool())
.build();
LlmAgent rootAgent =
LlmAgent.builder()
.name("RootAgent")
.model("gemini-flash-latest")
.description("Root Agent")
.subAgents(searchAgent, codingAgent) // Not supported, as the sub agents use built in tools.
.build();
val searchAgent = LlmAgent(
model = Gemini(name = "gemini-flash-latest"),
name = "SearchAgent",
instruction = Instruction("You're a specialist in Google Search"),
tools = listOf(GoogleSearchTool())
)
val codingAgent = LlmAgent(
model = Gemini(name = "gemini-flash-latest"),
name = "CodeAgent",
instruction = Instruction("You're a specialist in Code Execution")
// Kotlin currently doesn't have a BuiltInCodeExecutionTool in core
)
val rootAgent = LlmAgent(
name = "RootAgent",
model = Gemini(name = "gemini-flash-latest"),
description = "Root Agent",
subAgents = listOf(searchAgent, codingAgent) // Not supported when sub-agents use built-in tools
)