跳至内容

Model interface

模型追踪

基类: Enum

Source code in src/agents/models/interface.py
class ModelTracing(enum.Enum):
    DISABLED = 0
    """Tracing is disabled entirely."""

    ENABLED = 1
    """Tracing is enabled, and all data is included."""

    ENABLED_WITHOUT_DATA = 2
    """Tracing is enabled, but inputs/outputs are not included."""

    def is_disabled(self) -> bool:
        return self == ModelTracing.DISABLED

    def include_data(self) -> bool:
        return self == ModelTracing.ENABLED

已禁用 class-attribute instance-attribute

DISABLED = 0

追踪功能已完全禁用。

已启用 class-attribute instance-attribute

ENABLED = 1

跟踪功能已启用,所有数据均包含在内。

ENABLED_WITHOUT_DATA class-attribute instance-attribute

ENABLED_WITHOUT_DATA = 2

跟踪功能已启用,但输入/输出未包含在内。

模型

基类: ABC

调用大语言模型(LLM)的基础接口。

Source code in src/agents/models/interface.py
class Model(abc.ABC):
    """The base interface for calling an LLM."""

    @abc.abstractmethod
    async def get_response(
        self,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
        model_settings: ModelSettings,
        tools: list[Tool],
        output_schema: AgentOutputSchema | None,
        handoffs: list[Handoff],
        tracing: ModelTracing,
    ) -> ModelResponse:
        """Get a response from the model.

        Args:
            system_instructions: The system instructions to use.
            input: The input items to the model, in OpenAI Responses format.
            model_settings: The model settings to use.
            tools: The tools available to the model.
            output_schema: The output schema to use.
            handoffs: The handoffs available to the model.
            tracing: Tracing configuration.

        Returns:
            The full model response.
        """
        pass

    @abc.abstractmethod
    def stream_response(
        self,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
        model_settings: ModelSettings,
        tools: list[Tool],
        output_schema: AgentOutputSchema | None,
        handoffs: list[Handoff],
        tracing: ModelTracing,
    ) -> AsyncIterator[TResponseStreamEvent]:
        """Stream a response from the model.

        Args:
            system_instructions: The system instructions to use.
            input: The input items to the model, in OpenAI Responses format.
            model_settings: The model settings to use.
            tools: The tools available to the model.
            output_schema: The output schema to use.
            handoffs: The handoffs available to the model.
            tracing: Tracing configuration.

        Returns:
            An iterator of response stream events, in OpenAI Responses format.
        """
        pass

get_response abstractmethod async

get_response(
    system_instructions: str | None,
    input: str | list[TResponseInputItem],
    model_settings: ModelSettings,
    tools: list[Tool],
    output_schema: AgentOutputSchema | None,
    handoffs: list[Handoff],
    tracing: ModelTracing,
) -> ModelResponse

从模型获取响应。

参数:

名称 类型 描述 默认值
system_instructions str | None

要使用的系统指令。

required
input str | list[TResponseInputItem]

模型的输入项,采用OpenAI响应格式。

required
model_settings ModelSettings

要使用的模型设置。

required
tools list[Tool]

模型可用的工具。

required
output_schema AgentOutputSchema | None

要使用的输出模式。

required
handoffs list[Handoff]

模型可用的交接选项。

required
tracing ModelTracing

追踪配置。

required

返回值:

类型 描述
ModelResponse

完整的模型响应。

Source code in src/agents/models/interface.py
@abc.abstractmethod
async def get_response(
    self,
    system_instructions: str | None,
    input: str | list[TResponseInputItem],
    model_settings: ModelSettings,
    tools: list[Tool],
    output_schema: AgentOutputSchema | None,
    handoffs: list[Handoff],
    tracing: ModelTracing,
) -> ModelResponse:
    """Get a response from the model.

    Args:
        system_instructions: The system instructions to use.
        input: The input items to the model, in OpenAI Responses format.
        model_settings: The model settings to use.
        tools: The tools available to the model.
        output_schema: The output schema to use.
        handoffs: The handoffs available to the model.
        tracing: Tracing configuration.

    Returns:
        The full model response.
    """
    pass

stream_response abstractmethod

stream_response(
    system_instructions: str | None,
    input: str | list[TResponseInputItem],
    model_settings: ModelSettings,
    tools: list[Tool],
    output_schema: AgentOutputSchema | None,
    handoffs: list[Handoff],
    tracing: ModelTracing,
) -> AsyncIterator[TResponseStreamEvent]

从模型流式传输响应。

参数:

名称 类型 描述 默认值
system_instructions str | None

要使用的系统指令。

required
input str | list[TResponseInputItem]

模型的输入项,采用OpenAI响应格式。

required
model_settings ModelSettings

要使用的模型设置。

required
tools list[Tool]

模型可用的工具。

required
output_schema AgentOutputSchema | None

要使用的输出模式。

required
handoffs list[Handoff]

模型可用的交接选项。

required
tracing ModelTracing

追踪配置。

required

返回值:

类型 描述
AsyncIterator[TResponseStreamEvent]

一个响应流事件的迭代器,采用OpenAI响应格式。

Source code in src/agents/models/interface.py
@abc.abstractmethod
def stream_response(
    self,
    system_instructions: str | None,
    input: str | list[TResponseInputItem],
    model_settings: ModelSettings,
    tools: list[Tool],
    output_schema: AgentOutputSchema | None,
    handoffs: list[Handoff],
    tracing: ModelTracing,
) -> AsyncIterator[TResponseStreamEvent]:
    """Stream a response from the model.

    Args:
        system_instructions: The system instructions to use.
        input: The input items to the model, in OpenAI Responses format.
        model_settings: The model settings to use.
        tools: The tools available to the model.
        output_schema: The output schema to use.
        handoffs: The handoffs available to the model.
        tracing: Tracing configuration.

    Returns:
        An iterator of response stream events, in OpenAI Responses format.
    """
    pass

模型提供者

基类: ABC

模型提供者的基础接口。

模型提供者负责按名称查找模型。

Source code in src/agents/models/interface.py
class ModelProvider(abc.ABC):
    """The base interface for a model provider.

    Model provider is responsible for looking up Models by name.
    """

    @abc.abstractmethod
    def get_model(self, model_name: str | None) -> Model:
        """Get a model by name.

        Args:
            model_name: The name of the model to get.

        Returns:
            The model.
        """

get_model abstractmethod

get_model(model_name: str | None) -> Model

通过名称获取模型。

参数:

名称 类型 描述 默认值
model_name str | None

要获取的模型名称。

required

返回值:

类型 描述
Model

模型。

Source code in src/agents/models/interface.py
@abc.abstractmethod
def get_model(self, model_name: str | None) -> Model:
    """Get a model by name.

    Args:
        model_name: The name of the model to get.

    Returns:
        The model.
    """