跳过内容

运行上下文

RunContextWrapper dataclass

基类: Generic[TContext]

它封装了传递给 Runner.run() 的上下文对象。它还包含有关迄今为止的代理运行使用情况的信息。

注意:上下文不会传递给 LLM。它们是一种将依赖项和数据传递给您实现的的代码的方式,例如工具函数、回调、钩子等。

源代码位于 src/agents/run_context.py
@dataclass(eq=False)
class RunContextWrapper(Generic[TContext]):
    """This wraps the context object that you passed to `Runner.run()`. It also contains
    information about the usage of the agent run so far.

    NOTE: Contexts are not passed to the LLM. They're a way to pass dependencies and data to code
    you implement, like tool functions, callbacks, hooks, etc.
    """

    context: TContext
    """The context object (or None), passed by you to `Runner.run()`"""

    usage: Usage = field(default_factory=Usage)
    """The usage of the agent run so far. For streamed responses, the usage will be stale until the
    last chunk of the stream is processed.
    """

context instance-attribute

context: TContext

您传递给 Runner.run() 的上下文对象(或 None)

usage class-attribute instance-attribute

usage: Usage = field(default_factory=Usage)

迄今为止的代理运行使用情况。对于流式响应,使用情况在处理流的最后一个块之前将是陈旧的。

AgentHookContext dataclass

基类: RunContextWrapper[TContext]

传递给代理钩子(on_start、on_end)的上下文。

源代码位于 src/agents/run_context.py
@dataclass(eq=False)
class AgentHookContext(RunContextWrapper[TContext]):
    """Context passed to agent hooks (on_start, on_end)."""

    turn_input: "list[TResponseInputItem]" = field(default_factory=list)
    """The input items for the current turn."""

turn_input class-attribute instance-attribute

turn_input: list[TResponseInputItem] = field(
    default_factory=list
)

当前回合的输入项。

context instance-attribute

context: TContext

您传递给 Runner.run() 的上下文对象(或 None)

usage class-attribute instance-attribute

usage: Usage = field(default_factory=Usage)

迄今为止的代理运行使用情况。对于流式响应,使用情况在处理流的最后一个块之前将是陈旧的。