跳转至

Model interface

ModelTracing

Bases: Enum

Source code in src/agents/models/interface.py
class ModelTracing(enum.Enum):
    DISABLED = 0
    """完全禁用追踪。"""

    ENABLED = 1
    """启用追踪,并包含所有数据。"""

    ENABLED_WITHOUT_DATA = 2
    """启用追踪,但不包含输入/输出数据。"""

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

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

DISABLED class-attribute instance-attribute

DISABLED = 0

完全禁用追踪。

ENABLED class-attribute instance-attribute

ENABLED = 1

启用追踪,并包含所有数据。

ENABLED_WITHOUT_DATA class-attribute instance-attribute

ENABLED_WITHOUT_DATA = 2

启用追踪,但不包含输入/输出数据。

Model

Bases: ABC

调用 LLM 的基础接口。

Source code in src/agents/models/interface.py
class Model(abc.ABC):
    """调用 LLM 的基础接口。"""

    @abc.abstractmethod
    async def get_response(
        self,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
        model_settings: ModelSettings,
        tools: list[Tool],
        output_schema: AgentOutputSchemaBase | None,
        handoffs: list[Handoff],
        tracing: ModelTracing,
        *,
        previous_response_id: str | None,
    ) -> ModelResponse:
        """从模型获取响应。

        参数:
            system_instructions: 要使用的系统指令。
            input: 传递给模型的输入项,采用 OpenAI Responses 格式。
            model_settings: 要使用的模型设置。
            tools: 模型可用的工具列表。
            output_schema: 要使用的输出模式。
            handoffs: 模型可用的 handoff 列表。
            tracing: 追踪配置。
            previous_response_id: 上一个响应的 ID。通常模型不会用到,除了 OpenAI Responses API。

        返回:
            完整的模型响应。
        """
        pass

    @abc.abstractmethod
    def stream_response(
        self,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
        model_settings: ModelSettings,
        tools: list[Tool],
        output_schema: AgentOutputSchemaBase | None,
        handoffs: list[Handoff],
        tracing: ModelTracing,
        *,
        previous_response_id: str | None,
    ) -> AsyncIterator[TResponseStreamEvent]:
        """从模型流式获取响应。

        参数:
            system_instructions: 要使用的系统指令。
            input: 传递给模型的输入项,采用 OpenAI Responses 格式。
            model_settings: 要使用的模型设置。
            tools: 模型可用的工具列表。
            output_schema: 要使用的输出模式。
            handoffs: 模型可用的 handoff 列表。
            tracing: 追踪配置。
            previous_response_id: 上一个响应的 ID。通常模型不会用到,除了 OpenAI Responses API。

        返回:
            响应流事件的迭代器,采用 OpenAI Responses 格式。
        """
        pass

get_response abstractmethod async

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

从模型获取响应。

参数

system_instructions: 要使用的系统指令。 input: 传递给模型的输入项,采用 OpenAI Responses 格式。 model_settings: 要使用的模型设置。 tools: 模型可用的工具列表。 output_schema: 要使用的输出模式。 handoffs: 模型可用的 handoff 列表。 tracing: 追踪配置。 previous_response_id: 上一个响应的 ID。通常模型不会用到,除了 OpenAI Responses API。

返回

完整的模型响应。

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: AgentOutputSchemaBase | None,
    handoffs: list[Handoff],
    tracing: ModelTracing,
    *,
    previous_response_id: str | None,
) -> ModelResponse:
    """从模型获取响应。

    参数:
        system_instructions: 要使用的系统指令。
        input: 传递给模型的输入项,采用 OpenAI Responses 格式。
        model_settings: 要使用的模型设置。
        tools: 模型可用的工具列表。
        output_schema: 要使用的输出模式。
        handoffs: 模型可用的 handoff 列表。
        tracing: 追踪配置。
        previous_response_id: 上一个响应的 ID。通常模型不会用到,除了 OpenAI Responses API。

    返回:
        完整的模型响应。
    """
    pass

stream_response abstractmethod

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

从模型流式获取响应。

参数

system_instructions: 要使用的系统指令。 input: 传递给模型的输入项,采用 OpenAI Responses 格式。 model_settings: 要使用的模型设置。 tools: 模型可用的工具列表。 output_schema: 要使用的输出模式。 handoffs: 模型可用的 handoff 列表。 tracing: 追踪配置。 previous_response_id: 上一个响应的 ID。通常模型不会用到,除了 OpenAI Responses API。

返回

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

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: AgentOutputSchemaBase | None,
    handoffs: list[Handoff],
    tracing: ModelTracing,
    *,
    previous_response_id: str | None,
) -> AsyncIterator[TResponseStreamEvent]:
    """从模型流式获取响应。

    参数:
        system_instructions: 要使用的系统指令。
        input: 传递给模型的输入项,采用 OpenAI Responses 格式。
        model_settings: 要使用的模型设置。
        tools: 模型可用的工具列表。
        output_schema: 要使用的输出模式。
        handoffs: 模型可用的 handoff 列表。
        tracing: 追踪配置。
        previous_response_id: 上一个响应的 ID。通常模型不会用到,除了 OpenAI Responses API。

    返回:
        响应流事件的迭代器,采用 OpenAI Responses 格式。
    """
    pass

ModelProvider

Bases: ABC

模型提供者的基础接口。

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

Source code in src/agents/models/interface.py
class ModelProvider(abc.ABC):
    """模型提供者的基础接口。

    模型提供者负责通过名称查找模型。
    """

    @abc.abstractmethod
    def get_model(self, model_name: str | None) -> Model:
        """通过名称获取模型。

        参数:
            model_name: 要获取的模型名称。

        返回:
            模型实例。
        """

get_model abstractmethod

get_model(model_name: str | None) -> Model

通过名称获取模型。

参数

model_name: 要获取的模型名称。

返回

模型实例。

Source code in src/agents/models/interface.py
@abc.abstractmethod
def get_model(self, model_name: str | None) -> Model:
    """通过名称获取模型。

    参数:
        model_name: 要获取的模型名称。

    返回:
        模型实例。
    """