类:ReflectAndRetryToolPlugin

java.lang.Object
com.google.adk.plugins.BasePlugin
com.google.adk.plugins.ReflectAndRetryToolPlugin
All Implemented Interfaces:
Plugin

@Experimental public final class ReflectAndRetryToolPlugin extends BasePlugin
为工具故障提供了自我修复的错误恢复机制。

该插件可以拦截工具运行时出现的错误,为模型提供结构化的指导,帮助其进行反思和修正,并允许模型重试(重试次数可配置)。每个工具的错误次数都会被单独记录;如果某个工具成功完成了任务,其错误计数会被重置,但这并不会影响其他工具的错误计数。

“失败”是指工具所发出的错误信号(或错误信息)。 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()));
  • 方法详细信息(Method Details)

    • builder

      public static ReflectAndRetryToolPlugin.Builder 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

      public io.reactivex.rxjava3.core.Completable afterRunCallback(InvocationContext invocationContext)
      会清除已终止的调用所记录的失败次数(即失败记录)。
      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.