类:ReflectAndRetryToolPlugin
java.lang.Object
com.google.adk.plugins.BasePlugin
com.google.adk.plugins.ReflectAndRetryToolPlugin
- All Implemented Interfaces:
Plugin
为工具故障提供了自我修复的错误恢复机制。
该插件可以拦截工具运行时出现的错误,为模型提供结构化的指导,帮助其进行反思和修正,并允许模型重试(重试次数可配置)。每个工具的错误次数都会被单独记录;如果某个工具成功完成了任务,其错误计数会被重置,但这并不会影响其他工具的错误计数。
“失败”是指工具所发出的错误信号(或错误信息)。 Single.error或者出现一个错误…… ReflectAndRetryToolPlugin.ResultErrorExtractor 在搜索结果中找到该内容。 FunctionTool 该方法会抛出一个异常,并将该异常作为返回值返回。 {"status": "error"} 这些错误信息是通过 “result error extractor” 才被传递到该插件的;而代理程序调用的其他工具所产生的错误信息则永远不会被传递到该插件。
Port of adk-python的 ReflectAndRetryToolPlugin (
plugins/reflect_retry_tool_plugin.py).
示例:
Runner runner =
new InMemoryRunner(
agent,
APP_NAME,
ImmutableList.of(ReflectAndRetryToolPlugin.builder().maxRetries(3).build()));
-
嵌套类概述
Nested ClassesModifier and TypeClassDescriptionstatic final class“Builder for”是一个用于描述某种构建工具或框架的术语。它通常表示该工具/框架用于帮助开发者或系统管理员快速、高效地创建或构建特定类型的应用程序、系统或组件。例如:“Builder for Web Applications”(用于构建Web应用程序的工具)或“Builder for Cloud Services”(用于构建云服务的工具)。ReflectAndRetryToolPlugin.static interface当某个工具在其结果中检测到错误时,系统会直接显示该错误信息,而不会直接终止该工具的运行。static enum用于定义用于跟踪工具故障次数的生命周期范围(即工具从启动到关闭的整个使用过程)。 -
字段摘要
Fields inherited from class BasePlugin
name -
方法概述
Modifier and TypeMethodDescriptionio.reactivex.rxjava3.core.CompletableafterRunCallback(InvocationContext invocationContext) 会清除已终止的调用所记录的失败次数(即失败记录)。afterToolCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext, Map<String, Object> result) 如果操作成功,该工具的故障计数会被重置;如果操作本身是成功的,但其中包含错误信息,这些错误信息会被传递到重试逻辑中(即系统会尝试再次执行该操作)。builder()返回一个构建器(builder),该构建器默认设置重试次数为 3 次;如果重试次数超过 3 次,则会抛出异常。每次调用该构建器时,重试次数都会被累加;此外,该构建器不会报告任何与结果相关的错误(即即使重试失败,也不会返回错误信息)。io.reactivex.rxjava3.core.CompletableonRunErrorCallback(InvocationContext invocationContext, Throwable error) 会清除那些失败调用对应的失败记录(即失败次数)。onToolErrorCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext, Throwable error) 将工具使用过程中出现的错误转化为对模型的反馈建议(即指导模型如何改进其行为)。Methods inherited from class BasePlugin
getNameMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Plugin
afterAgentCallback, afterModelCallback, beforeAgentCallback, beforeModelCallback, beforeRunCallback, beforeToolCallback, close, onEventCallback, onModelErrorCallback, onUserMessageCallbackModifier and TypeMethodDescriptiondefault io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> afterAgentCallback(BaseAgent agent, CallbackContext callbackContext) 在代理程序的主要逻辑执行完成后,会触发该回调函数。default io.reactivex.rxjava3.core.Maybe<LlmResponse> afterModelCallback(CallbackContext callbackContext, LlmResponse llmResponse) 在从模型接收到响应后,会执行该回调函数。default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> beforeAgentCallback(BaseAgent agent, CallbackContext callbackContext) 回调函数在代理程序的主要逻辑被执行之前被调用。default io.reactivex.rxjava3.core.Maybe<LlmResponse> beforeModelCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest) 在请求被发送到模型之前,会先执行这个回调函数。default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> beforeRunCallback(InvocationContext invocationContext) 回调函数在 ADK 运行程序启动之前被执行。beforeToolCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext) 回调函数在工具被调用之前执行。default io.reactivex.rxjava3.core.Completableclose()当运行器(runner)被关闭时,会执行此方法。default io.reactivex.rxjava3.core.Maybe<Event> onEventCallback(InvocationContext invocationContext, Event event) 在事件从运行器(runner)中被处理/执行后,会触发相应的回调函数(Callback)。default io.reactivex.rxjava3.core.Maybe<LlmResponse> onModelErrorCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest, Throwable error) 当模型调用遇到错误时,会执行此回调函数。default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> onUserMessageCallback(InvocationContext invocationContext, com.google.genai.types.Content userMessage) 当在调用开始之前接收到用户消息时,会执行此回调函数。
-
方法详细信息(Method Details)
-
builder
返回一个构建器(builder),该构建器默认设置重试次数为 3 次;如果重试次数超过 3 次,则会抛出异常。每次调用该构建器时,重试次数都会被累加;此外,该构建器不会报告任何与结果相关的错误(即即使重试失败,也不会返回错误信息)。 -
afterToolCallback
public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> afterToolCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext, Map<String, Object> result) 如果操作成功,该插件会重置工具的故障计数;如果操作本身成功但携带了错误信息,这些错误信息会被传递到重试逻辑中进行处理。该插件生成的响应数据也会被继续传递下去。- Parameters:
tool- The tool instance that has just been executed.toolArgs- The original arguments that were passed to the tool.toolContext- The context specific to the tool execution.result- The dictionary returned by the tool invocation.- Returns:
- An optional Map to replace the original result from the tool. Returning Empty to use the original result.
-
afterRunCallback
会清除已终止的调用所记录的失败次数(即失败记录)。- Parameters:
invocationContext- The context for the entire invocation.
-
onRunErrorCallback
public io.reactivex.rxjava3.core.Completable onRunErrorCallback(InvocationContext invocationContext, Throwable error) 会清除那些失败调用对应的失败记录(即失败次数)。- Parameters:
invocationContext- The context for the entire invocation.error- The exception that was raised.
-
onToolErrorCallback
public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> onToolErrorCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext, Throwable error) 将工具使用过程中出现的错误转化为对模型的反馈建议(即指导模型如何改进其行为)。- Parameters:
tool- The tool instance that encountered an error.toolArgs- The arguments that were passed to the tool.toolContext- The context specific to the tool execution.error- The exception that was raised during tool execution.- Returns:
- An optional Map to be used as the tool response instead of propagating the error. Returning Empty to allow the original error to be raised.
-