跳过内容

模型设置

ModelSettings

调用 LLM 时的设置。

此类保存可选的模型配置参数(例如温度、top_p、惩罚、截断等)。

并非所有模型/提供商都支持所有这些参数,因此请检查您正在使用的特定模型和提供商的 API 文档。

源代码位于 src/agents/model_settings.py
@dataclass
class ModelSettings:
    """Settings to use when calling an LLM.

    This class holds optional model configuration parameters (e.g. temperature,
    top_p, penalties, truncation, etc.).

    Not all models/providers support all of these parameters, so please check the API documentation
    for the specific model and provider you are using.
    """

    temperature: float | None = None
    """The temperature to use when calling the model."""

    top_p: float | None = None
    """The top_p to use when calling the model."""

    frequency_penalty: float | None = None
    """The frequency penalty to use when calling the model."""

    presence_penalty: float | None = None
    """The presence penalty to use when calling the model."""

    tool_choice: ToolChoice | None = None
    """The tool choice to use when calling the model."""

    parallel_tool_calls: bool | None = None
    """Controls whether the model can make multiple parallel tool calls in a single turn.
    If not provided (i.e., set to None), this behavior defers to the underlying
    model provider's default. For most current providers (e.g., OpenAI), this typically
    means parallel tool calls are enabled (True).
    Set to True to explicitly enable parallel tool calls, or False to restrict the
    model to at most one tool call per turn.
    """

    truncation: Literal["auto", "disabled"] | None = None
    """The truncation strategy to use when calling the model.
    See [Responses API documentation](https://platform.openai.com/docs/api-reference/responses/create#responses_create-truncation)
    for more details.
    """

    max_tokens: int | None = None
    """The maximum number of output tokens to generate."""

    reasoning: Reasoning | None = None
    """Configuration options for
    [reasoning models](https://platform.openai.com/docs/guides/reasoning).
    """

    verbosity: Literal["low", "medium", "high"] | None = None
    """Constrains the verbosity of the model's response.
    """

    metadata: dict[str, str] | None = None
    """Metadata to include with the model response call."""

    store: bool | None = None
    """Whether to store the generated model response for later retrieval.
    For Responses API: automatically enabled when not specified.
    For Chat Completions API: disabled when not specified."""

    prompt_cache_retention: Literal["in_memory", "24h"] | None = None
    """The retention policy for the prompt cache. Set to `24h` to enable extended
    prompt caching, which keeps cached prefixes active for longer, up to a maximum
    of 24 hours.
    [Learn more](https://platform.openai.com/docs/guides/prompt-caching#prompt-cache-retention)."""

    include_usage: bool | None = None
    """Whether to include usage chunk.
    Only available for Chat Completions API."""

    # TODO: revisit ResponseIncludable | str if ResponseIncludable covers more cases
    # We've added str to support missing ones like
    # "web_search_call.action.sources" etc.
    response_include: list[ResponseIncludable | str] | None = None
    """Additional output data to include in the model response.
    [include parameter](https://platform.openai.com/docs/api-reference/responses/create#responses-create-include)"""

    top_logprobs: int | None = None
    """Number of top tokens to return logprobs for. Setting this will
    automatically include ``"message.output_text.logprobs"`` in the response."""

    extra_query: Query | None = None
    """Additional query fields to provide with the request.
    Defaults to None if not provided."""

    extra_body: Body | None = None
    """Additional body fields to provide with the request.
    Defaults to None if not provided."""

    extra_headers: Headers | None = None
    """Additional headers to provide with the request.
    Defaults to None if not provided."""

    extra_args: dict[str, Any] | None = None
    """Arbitrary keyword arguments to pass to the model API call.
    These will be passed directly to the underlying model provider's API.
    Use with caution as not all models support all parameters."""

    def resolve(self, override: ModelSettings | None) -> ModelSettings:
        """Produce a new ModelSettings by overlaying any non-None values from the
        override on top of this instance."""
        if override is None:
            return self

        changes = {
            field.name: getattr(override, field.name)
            for field in fields(self)
            if getattr(override, field.name) is not None
        }

        # Handle extra_args merging specially - merge dictionaries instead of replacing
        if self.extra_args is not None or override.extra_args is not None:
            merged_args = {}
            if self.extra_args:
                merged_args.update(self.extra_args)
            if override.extra_args:
                merged_args.update(override.extra_args)
            changes["extra_args"] = merged_args if merged_args else None

        return replace(self, **changes)

    def to_json_dict(self) -> dict[str, Any]:
        dataclass_dict = dataclasses.asdict(self)

        json_dict: dict[str, Any] = {}

        for field_name, value in dataclass_dict.items():
            if isinstance(value, BaseModel):
                json_dict[field_name] = value.model_dump(mode="json")
            else:
                json_dict[field_name] = value

        return json_dict

temperature 类属性 实例属性

temperature: float | None = None

调用模型时使用的温度。

top_p 类属性 实例属性

top_p: float | None = None

调用模型时使用的 top_p。

frequency_penalty 类属性 实例属性

frequency_penalty: float | None = None

调用模型时使用的频率惩罚。

presence_penalty 类属性 实例属性

presence_penalty: float | None = None

调用模型时使用的存在惩罚。

tool_choice 类属性 实例属性

tool_choice: ToolChoice | None = None

调用模型时使用的工具选择。

parallel_tool_calls 类属性 实例属性

parallel_tool_calls: bool | None = None

控制模型是否可以在单个回合中进行多个并行工具调用。如果未提供(即设置为 None),则此行为将取决于底层模型提供商的默认设置。对于大多数当前提供商(例如 OpenAI),这通常意味着启用并行工具调用(True)。设置为 True 以显式启用并行工具调用,或设置为 False 以限制模型每回合最多只能调用一个工具。

truncation 类属性 实例属性

truncation: Literal['auto', 'disabled'] | None = None

调用模型时使用的截断策略。有关更多详细信息,请参阅 Responses API 文档

max_tokens 类属性 实例属性

max_tokens: int | None = None

要生成的最大输出令牌数。

reasoning 类属性 实例属性

reasoning: Reasoning | None = None

推理模型的配置选项。

verbosity 类属性 实例属性

verbosity: Literal['low', 'medium', 'high'] | None = None

约束模型的响应的详细程度。

metadata 类属性 实例属性

metadata: dict[str, str] | None = None

要包含在模型响应调用中的元数据。

store 类属性 实例属性

store: bool | None = None

是否存储生成的模型响应以供以后检索。对于 Responses API:如果未指定,则自动启用。对于 Chat Completions API:如果未指定,则禁用。

prompt_cache_retention 类属性 实例属性

prompt_cache_retention: (
    Literal["in_memory", "24h"] | None
) = None

提示缓存的保留策略。设置为 24h 以启用扩展提示缓存,这将使缓存的前缀在更长的时间内保持活动状态,最长可达 24 小时。了解更多

include_usage 类属性 实例属性

include_usage: bool | None = None

是否包含使用量块。仅适用于 Chat Completions API。

response_include 类属性 实例属性

response_include: list[ResponseIncludable | str] | None = (
    None
)

要包含在模型响应中的其他输出数据。include 参数

top_logprobs 类属性 实例属性

top_logprobs: int | None = None

要返回 logprobs 的前 N 个令牌数。设置此选项将自动在响应中包含 "message.output_text.logprobs"

extra_query 类属性 实例属性

extra_query: Query | None = None

要与请求一起提供的其他查询字段。如果未提供,则默认为 None。

extra_body 类属性 实例属性

extra_body: Body | None = None

要与请求一起提供的其他正文字段。如果未提供,则默认为 None。

extra_headers 类属性 实例属性

extra_headers: Headers | None = None

要与请求一起提供的其他标头。如果未提供,则默认为 None。

extra_args 类属性 实例属性

extra_args: dict[str, Any] | None = None

要传递给模型 API 调用的任意关键字参数。这些将直接传递给底层模型提供商的 API。请谨慎使用,因为并非所有模型都支持所有参数。

resolve

resolve(override: ModelSettings | None) -> ModelSettings

通过将覆盖项中的任何非 None 值覆盖在此实例之上来生成新的 ModelSettings。

源代码位于 src/agents/model_settings.py
def resolve(self, override: ModelSettings | None) -> ModelSettings:
    """Produce a new ModelSettings by overlaying any non-None values from the
    override on top of this instance."""
    if override is None:
        return self

    changes = {
        field.name: getattr(override, field.name)
        for field in fields(self)
        if getattr(override, field.name) is not None
    }

    # Handle extra_args merging specially - merge dictionaries instead of replacing
    if self.extra_args is not None or override.extra_args is not None:
        merged_args = {}
        if self.extra_args:
            merged_args.update(self.extra_args)
        if override.extra_args:
            merged_args.update(override.extra_args)
        changes["extra_args"] = merged_args if merged_args else None

    return replace(self, **changes)