`Class ParallelAgent`
java.lang.Object
com.google.adk.workflow.BaseNode
com.google.adk.agents.BaseAgent
com.google.adk.agents.ParallelAgent
- All Implemented Interfaces:
Node
这是一个 shell 代理程序,它能够以并行方式、并在隔离的环境中运行其下属代理程序(即子代理)。
这种方法适用于那些需要从多个角度或多次尝试来完成同一任务的场景,例如同时运行不同的算法,或生成多个结果供后续的评估人员进行审查。
与……的组合(或搭配) LlmAgents: a ParallelAgent 不会将控制权返回给父进程(或父对象)。 LlmAgent要执行“先扩散(fan-out)再聚合(aggregation)”的操作,需要将这两个步骤封装在一个容器(例如函数或方法)中。
SequentialAgent (用作根节点或转发给代理)。每个并行子代理都会通过相应的渠道进行数据发布。 outputKey 然后,聚合器通过(指定的方式)进行数据读取。 {key} 其指令中的占位符(placeholders in its instruction):
var contacts =
LlmAgent.builder()
.name("contacts")
.model("gemini-flash-latest")
.instruction("List contacts.")
.outputKey("contacts")
.build();
var schedule =
LlmAgent.builder()
.name("schedule")
.model("gemini-flash-latest")
.instruction("List schedule.")
.outputKey("schedule")
.build();
var writer =
LlmAgent.builder()
.name("writer")
.model("gemini-flash-latest")
.instruction("Write: contacts={contacts}, schedule={schedule}")
.build();
var gather =
ParallelAgent.builder().name("gather").subAgents(contacts, schedule).build();
var root = SequentialAgent.builder().name("root").subAgents(gather, writer).build();
-
嵌套类概述
Nested ClassesModifier and TypeClassDescriptionstatic class“Builder for”是一个用于描述某种构建工具或框架的术语。它通常表示该工具/框架用于帮助开发者或系统管理员快速、高效地创建或构建特定类型的应用程序、系统或组件。例如:“Builder for Web Applications”(用于构建Web应用程序的工具)或“Builder for Cloud Services”(用于构建云服务的工具)。ParallelAgent. -
方法概述
Modifier and TypeMethodDescriptionstatic ParallelAgent.Builderbuilder()static ParallelAgentfromConfig(ParallelAgentConfig config, String configAbsPath) 根据配置创建一个 ParallelAgent。protected io.reactivex.rxjava3.core.Flowable<Event> runAsyncImpl(InvocationContext invocationContext) 并行运行子代理(sub-agents),并收集它们产生的事件数据。protected io.reactivex.rxjava3.core.Flowable<Event> runLiveImpl(InvocationContext invocationContext) ParallelAgent 不支持该功能。Methods inherited from class BaseAgent
afterAgentCallback, beforeAgentCallback, canonicalAfterAgentCallbacks, canonicalBeforeAgentCallbacks, close, findAgent, findSubAgent, fromConfig, parentAgent, parentAgent, rootAgent, runAsync, runLive, runNode, subAgents, toolOriginModifier and TypeMethodDescriptioncom.google.common.collect.ImmutableList<? extends Callbacks.AfterAgentCallback> com.google.common.collect.ImmutableList<? extends Callbacks.BeforeAgentCallback> com.google.common.collect.ImmutableList<? extends Callbacks.AfterAgentCallback> `afterAgentCallback` 字段的返回值是一个列表。com.google.common.collect.ImmutableList<? extends Callbacks.BeforeAgentCallback> `beforeAgentCallback` 字段解析后的结果以列表形式返回。io.reactivex.rxjava3.core.Completableclose()关闭所有子代理(sub-agents)。根据名称查找某个代理(该代理本身或其子代理)。findSubAgent(String name) 通过名称递归地搜索子代理(sub-agent)。static BaseAgentfromConfig(BaseAgentConfig config, String configAbsPath) 根据一个配置对象创建一个新的代理实例。从代理树中检索父代理。protected voidparentAgent(BaseAgent parentAgent) 用于设置父代理(即负责管理其他代理的代理)。通过向上遍历父代理链,返回该代理的根代理。io.reactivex.rxjava3.core.Flowable<Event> runAsync(InvocationContext parentContext) 异步运行代理程序。io.reactivex.rxjava3.core.Flowable<Event> runLive(InvocationContext parentContext) 同步运行代理程序。io.reactivex.rxjava3.core.Flowable<Event> 将该代理作为工作流中的一个节点来运行,并将其部署在该节点的分支上;这样,后续的节点就能够接收到该代理产生的事件(即数据或信号)。当该代理被用作工具时,该方法会返回该工具的来源信息。Methods inherited from class BaseNode
config, description, inputSchema, name, outputSchema, rerunOnResume, stateSchema, validateOutgoingEdges, waitForOutputModifier and TypeMethodDescriptionconfig()返回该节点的重试策略(retry policy)和执行超时时间(execution timeout)。该函数会返回节点的具体功能或行为,这些信息既适用于人类用户,也适用于可能调用该节点的模型(即那些需要使用该节点功能的系统或应用程序)。Optional<com.google.genai.types.Schema> 返回该节点的输入数据必须匹配的规范(即数据结构或格式)。final Stringname()返回用于标识该节点在其图中的名称。Optional<com.google.genai.types.Schema> 返回一个规范(schema),该规范要求节点的输出值必须符合这个规范的要求。boolean返回值true如果一个被恢复的节点重新从头开始运行,那么它并不会输出“恢复成功”的结果,而是会继续执行之前的操作。Optional<com.google.genai.types.Schema> 返回用于描述该节点所使用的状态键(state keys)的架构(schema)。protected voidvalidateOutgoingEdges(Collection<Edge> edges) 抛出异常(或错误)GraphValidationException如果这个节点无法提供服务(即无法完成其预定的功能或任务),那么就需要寻找其他节点来替代它。edges这些因素会“留下”某些东西(例如:用于生成某种路径的边缘信息),但这些路径实际上永远不会被真正生成(即:这些边缘信息永远不会被用于实际的路由计算中)。boolean返回值true如果某个节点在产生输出或确定路由之前仍然可以被重新触发(即可以继续被执行),那么它就不会在自身执行完成后立即停止运行。Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Node
requiresAllPredecessorsModifier and TypeMethodDescriptiondefault boolean返回值true如果该节点仅在所有前置节点都完成执行后才会被运行,并且会接收所有以节点名称为键的前置节点的输出结果(就像一个“扇入型”节点一样)。
-
方法详细信息(Method Details)
-
builder
-
fromConfig
public static ParallelAgent fromConfig(ParallelAgentConfig config, String configAbsPath) throws ConfigAgentUtils.ConfigurationException 根据配置创建一个 ParallelAgent。- Parameters:
config- the agent configurationconfigAbsPath- The absolute path to the agent config file.- Returns:
- the configured ParallelAgent
- Throws:
ConfigAgentUtils.ConfigurationException- if the configuration is invalid
-
runAsyncImpl
protected io.reactivex.rxjava3.core.Flowable<Event> runAsyncImpl(InvocationContext invocationContext) 并行运行子代理(sub-agents),并收集它们产生的事件数据。设置分支,并合并来自所有子代理的事件流。
- Specified by:
runAsyncImplin classBaseAgent- Parameters:
invocationContext- Invocation context.- Returns:
- Flowable emitting events from all sub-agents.
-
runLiveImpl
protected io.reactivex.rxjava3.core.Flowable<Event> runLiveImpl(InvocationContext invocationContext) ParallelAgent 不支持该功能。- Specified by:
runLiveImplin classBaseAgent- Parameters:
invocationContext- Invocation context.- Returns:
- Flowable that always throws UnsupportedOperationException.
-