跳过内容

生命周期

RunHooks module-attribute

RunHooks = RunHooksBase[TContext, Agent]

在使用 Agent 时运行的钩子。

AgentHooks module-attribute

AgentHooks = AgentHooksBase[TContext, Agent]

Agent 的代理钩子。

RunHooksBase

Bases: Generic[TContext, TAgent]

一个类,用于接收代理运行中的各种生命周期事件的回调。子类化并覆盖您需要的函数。

on_llm_start async

on_llm_start(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    system_prompt: Optional[str],
    input_items: list[TResponseInputItem],
) -> None

在调用此代理的 LLM 之前调用。

on_llm_end async

on_llm_end(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    response: ModelResponse,
) -> None

在 LLM 调用返回后立即调用,用于此代理。

on_agent_start async

on_agent_start(
    context: AgentHookContext[TContext], agent: TAgent
) -> None

在调用代理之前调用。每次当前代理更改时调用。

参数

名称 类型 描述 默认
context AgentHookContext[TContext]

代理钩子上下文。

required
agent TAgent

即将调用的代理。

required

on_agent_end async

on_agent_end(
    context: AgentHookContext[TContext],
    agent: TAgent,
    output: Any,
) -> None

当代理生成最终输出时调用。

参数

名称 类型 描述 默认
context AgentHookContext[TContext]

代理钩子上下文。

required
agent TAgent

生成输出的代理。

required
output Any

代理生成的最终输出。

required

on_handoff async

on_handoff(
    context: RunContextWrapper[TContext],
    from_agent: TAgent,
    to_agent: TAgent,
) -> None

当发生交接时调用。

on_tool_start async

on_tool_start(
    context: RunContextWrapper[TContext],
    agent: TAgent,
    tool: Tool,
) -> None

在调用本地工具之前立即调用。

on_tool_end async

on_tool_end(
    context: RunContextWrapper[TContext],
    agent: TAgent,
    tool: Tool,
    result: str,
) -> None

在调用本地工具后立即调用。

AgentHooksBase

Bases: Generic[TContext, TAgent]

一个类,用于接收特定代理的各种生命周期事件的回调。您可以将其设置为 agent.hooks,以接收该特定代理的事件。

子类化并覆盖您需要的函数。

on_start async

on_start(
    context: AgentHookContext[TContext], agent: TAgent
) -> None

在调用代理之前调用。每次运行的代理更改为此代理时调用。

参数

名称 类型 描述 默认
context AgentHookContext[TContext]

代理钩子上下文。

required
agent TAgent

此代理实例。

required

on_end async

on_end(
    context: AgentHookContext[TContext],
    agent: TAgent,
    output: Any,
) -> None

当代理生成最终输出时调用。

参数

名称 类型 描述 默认
context AgentHookContext[TContext]

代理钩子上下文。

required
agent TAgent

此代理实例。

required
output Any

代理生成的最终输出。

required

on_handoff async

on_handoff(
    context: RunContextWrapper[TContext],
    agent: TAgent,
    source: TAgent,
) -> None

在将代理交接给时调用。source 是将交接给此代理的代理。

on_tool_start async

on_tool_start(
    context: RunContextWrapper[TContext],
    agent: TAgent,
    tool: Tool,
) -> None

在调用本地工具之前立即调用。

on_tool_end async

on_tool_end(
    context: RunContextWrapper[TContext],
    agent: TAgent,
    tool: Tool,
    result: str,
) -> None

在调用本地工具后立即调用。

on_llm_start async

on_llm_start(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    system_prompt: Optional[str],
    input_items: list[TResponseInputItem],
) -> None

在代理发出 LLM 调用之前立即调用。

on_llm_end async

on_llm_end(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    response: ModelResponse,
) -> None

在代理接收到 LLM 响应后立即调用。