`Reflect` 是 JavaScript 中的一个内置对象,它提供了许多用于操作对象、函数和类的高级方法。这些方法允许开发者以更灵活、更抽象的方式访问和修改程序的结构。例如,`Reflect.get()` 可以用来获取对象的属性值,`Reflect.set()` 可以用来设置对象的属性值,`Reflect.hasOwnProperty()` 可以用来检查对象是否具有某个属性,`Reflect.prototype()` 可以用来获取对象的原型,等等。然后重试工具插件(Plugin)

class ReflectAndRetryToolPlugin(val maxRetries: Int = 3, val throwExceptionIfRetryExceeded: Boolean = true, val trackingScope: ReflectAndRetryToolPlugin.TrackingScope = TrackingScope.INVOCATION, val resultErrorExtractor: ReflectAndRetryToolPlugin.ResultErrorExtractor? = null, val retryPredicate: ReflectAndRetryToolPlugin.RetryPredicate? = null, val name: String = DEFAULT_NAME) : Plugin

Answers a failed tool call with guidance for the model to reflect on the error and retry, allowing up to maxRetries consecutive failures per tool.

A failure is an exception from the tool, a call to an unregistered tool name, or an error that resultErrorExtractor finds in a result; a success resets that tool's count. Mirrors Python ADK's ReflectAndRetryToolPlugin.

InMemoryRunner(agent = agent, plugins = listOf(ReflectAndRetryToolPlugin(maxRetries = 3)))

构造函数(Constructors)

Link copied to clipboard
constructor(maxRetries: Int = 3, throwExceptionIfRetryExceeded: Boolean = true, trackingScope: ReflectAndRetryToolPlugin.TrackingScope = TrackingScope.INVOCATION, resultErrorExtractor: ReflectAndRetryToolPlugin.ResultErrorExtractor? = null, retryPredicate: ReflectAndRetryToolPlugin.RetryPredicate? = null, name: String = DEFAULT_NAME)

类型

Link copied to clipboard
class Builder

Fluent Builder for ReflectAndRetryToolPlugin该功能主要为 Java 语言的调用者提供支持;如果某个属性未被设置,其值将默认使用构造函数中指定的默认值。

Link copied to clipboard
object Companion
Link copied to clipboard

当某个工具在其结果中检测到错误时,系统会直接显示该错误信息,而不会直接终止该工具的运行。

Link copied to clipboard

决定是否允许模型重试那些失败的工具调用(即那些未能成功完成任务的工具操作)。

Link copied to clipboard

需要多长时间? ReflectAndRetryToolPlugin 该工具会持续记录某个设备(或工具)的连续故障次数。

属性(Properties)

Link copied to clipboard

某个工具连续失败次数(这些失败都得到了重试建议的提示);之后的第 n 次失败超出了规定的限制。该数值不能为负数。 0 禁用重试功能。

Link copied to clipboard
open override val name: String

该插件实例的唯一名称。

Link copied to clipboard

该功能会查找工具在结果中报告的错误(而不是直接抛出异常),并在发现这些错误时采取相应的处理措施。 null如果没有找到任何结果,那么这次尝试就被视为失败。

Link copied to clipboard

该机制负责决定模型应该尝试重试哪些错误(即哪些失败操作)。被模型拒绝的错误不会被计入统计结果,并且会立即向用户发出提示,建议用户停止使用该工具;这种提示不会通过抛出异常(error)的方式来传递。 null每次失败都可以重新尝试。

Link copied to clipboard

一旦超过了限制,就需要决定是停止处理该错误,还是向模型发送指导信息,让其停止使用该工具。当该限制被设置后,任何被抛出的错误都会被“原样”传递给后续的处理流程(就好像该插件不存在一样);如果在处理结果中发现错误,也会将该错误继续传递下去。如果另一个回调函数处理了这些被“原样”传递过来的错误,插件会直接将该处理结果传递出去,同时继续记录错误发生的次数。

Link copied to clipboard

是否将工具的故障次数按每次调用单独统计,还是将所有调用中的故障次数合并后进行统计。

函数

Link copied to clipboard
open suspend fun afterAgent(context: CallbackContext): CallbackChoice<Unit, Content>

当某个特定的代理完成其处理任务后,会执行相应的回调函数。

Link copied to clipboard
open suspend fun afterModel(context: CallbackContext, response: LlmResponse): LlmResponse

在接收到大型语言模型(LLM)的响应后,会执行该回调函数。

Link copied to clipboard
open suspend override fun afterRun(invocationContext: InvocationContext)

回调函数在 ADK 运行器完成执行后,或者调用者提前停止数据收集后(例如由于某种原因)被执行。 first() 或者 take(n))或者它的收集器抛出异常(exception)。

Link copied to clipboard
open suspend override fun afterTool(context: ToolContext, tool: BaseTool, args: Map<String, Any?>, result: Map<String, Any?>): Map<String, Any?>

回调函数在工具运行完成后执行,或者在该工具运行之前的回调函数完成其任务后执行。

Link copied to clipboard

回调函数在某个特定代理开始处理任务之前被执行。

Link copied to clipboard

回调函数在向大型语言模型(LLM)发送请求之前被执行。

Link copied to clipboard
open suspend fun beforeRun(invocationContext: InvocationContext): CallbackChoice<Unit, Content>

回调函数在 ADK 运行器开始执行主循环之前被调用。

Link copied to clipboard
open suspend fun beforeTool(context: ToolContext, tool: BaseTool, args: Map<String, Any?>): CallbackChoice<Map<String, Any?>, Map<String, Any?>>

回调函数在工具被调用之前执行。

Link copied to clipboard
open override fun close()

当运行器(runner)被关闭时,会执行此方法。

Link copied to clipboard
open suspend fun onEvent(invocationContext: InvocationContext, event: Event): Event

当代理在执行过程中触发某个事件时,会执行此回调函数。

Link copied to clipboard
open suspend fun onModelError(context: CallbackContext, request: LlmRequest, error: Throwable): CallbackChoice<Unit, LlmResponse>

当在大型语言模型(LLM)的交互过程中发生错误时,会执行此回调函数。

Link copied to clipboard
open suspend override fun onRunError(invocationContext: InvocationContext, error: Throwable)

当 ADK 运行器运行失败时,或者在其他特定情况下,会执行此回调函数。 afterRun 在调用者提前停止数据收集后,程序会失败(即无法继续执行)。

Link copied to clipboard
open suspend override fun onToolError(context: ToolContext, tool: BaseTool, args: Map<String, Any?>, error: Throwable): CallbackChoice<Unit, Map<String, Any?>>

当工具调用过程中发生错误时,会执行此回调函数。

Link copied to clipboard
open suspend fun onUserMessage(invocationContext: InvocationContext, userMessage: Content): Content

当在调用开始之前接收到用户消息时,会执行此回调函数。