Class LoopAgent
java.lang.Object
com.google.adk.workflow.BaseNode
com.google.adk.agents.BaseAgent
com.google.adk.agents.LoopAgent
- All Implemented Interfaces:
Node
一个代理程序,它会按顺序循环执行其下属的子代理程序。
循环会一直持续下去,直到某个子代理向上级报告问题(即“升级”该问题),或者直到达到预定的最大迭代次数(如果事先指定了最大迭代次数的话)。
与……的组合(或搭配) LlmAgents: a LoopAgent 不会将控制权重新转移回父进程(或父对象)。 LlmAgent. 为了对循环结果做出反应,请将相关代码放在此处。 LoopAgent 以及后续行动(或:后续措施)。 LlmAgent 就像一家人中的兄弟姐妹一样…… SequentialAgent循环子代理通过以下方式发布内容: outputKey 接下来的内容是通过(某种方式)读取的。 {key} 其指令中的占位符(placeholders in its instruction):
var refiner =
LlmAgent.builder()
.name("refiner")
.model("gemini-flash-latest")
.instruction("Refine: {draft?}")
.outputKey("draft")
.build();
var publisher =
LlmAgent.builder()
.name("publisher")
.model("gemini-flash-latest")
.instruction("Publish: {draft}")
.build();
var loop =
LoopAgent.builder().name("loop").subAgents(refiner).maxIterations(3).build();
var root = SequentialAgent.builder().name("root").subAgents(loop, publisher).build();
-
嵌套类概述
Nested ClassesModifier and TypeClassDescriptionstatic class“Builder for”是一个用于描述某种构建工具或框架的术语。它通常表示该工具/框架用于帮助开发者或系统管理员快速、高效地创建或构建特定类型的应用程序、系统或组件。例如:“Builder for Web Applications”(用于构建Web应用程序的工具)或“Builder for Cloud Services”(用于构建云服务的工具)。LoopAgent. -
方法概述
Modifier and TypeMethodDescriptionstatic LoopAgent.Builderbuilder()static LoopAgentfromConfig(LoopAgentConfig config, String configAbsPath) 根据配置创建一个 LoopAgent。@Nullable Integerprotected io.reactivex.rxjava3.core.Flowable<Event> runAsyncImpl(InvocationContext invocationContext) 与特定代理(Agent)相关的异步逻辑(Agent-specific asynchronous logic)。protected io.reactivex.rxjava3.core.Flowable<Event> runLiveImpl(InvocationContext invocationContext) 与特定代理相关的同步逻辑(Agent-specific synchronous logic)。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 LoopAgent fromConfig(LoopAgentConfig config, String configAbsPath) throws ConfigAgentUtils.ConfigurationException 根据配置创建一个 LoopAgent。- Parameters:
config- The agent configuration.configAbsPath- The absolute path to the agent config file.- Returns:
- the configured LoopAgent
- Throws:
ConfigAgentUtils.ConfigurationException- if the configuration is invalid
-
runAsyncImpl
protected io.reactivex.rxjava3.core.Flowable<Event> runAsyncImpl(InvocationContext invocationContext) 描述内容复制自相关类:BaseAgent与特定代理(Agent)相关的异步逻辑(Agent-specific asynchronous logic)。- Specified by:
runAsyncImplin classBaseAgent- Parameters:
invocationContext- Current invocation context.- Returns:
- stream of agent-generated events.
-
runLiveImpl
protected io.reactivex.rxjava3.core.Flowable<Event> runLiveImpl(InvocationContext invocationContext) 描述内容复制自相关类:BaseAgent与特定代理相关的同步逻辑(Agent-specific synchronous logic)。- Specified by:
runLiveImplin classBaseAgent- Parameters:
invocationContext- Current invocation context.- Returns:
- stream of agent-generated events.
-
maxIterations
-