跳至内容

模型设置

模型设置 dataclass

调用LLM时使用的设置。

该类包含可选的模型配置参数(例如温度、top_p、惩罚项、截断等)。

并非所有模型/提供商都支持所有这些参数,因此请查阅您所使用的具体模型和提供商的API文档。

Source code in 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: Literal["auto", "required", "none"] | str | None = None
    """The tool choice to use when calling the model."""

    parallel_tool_calls: bool | None = None
    """Whether to use parallel tool calls when calling the model.
    Defaults to False if not provided."""

    truncation: Literal["auto", "disabled"] | None = None
    """The truncation strategy to use when calling the model."""

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

    store: bool | None = None
    """Whether to store the generated model response for later retrieval.
    Defaults to True if not provided."""

    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
        }
        return replace(self, **changes)

temperature class-attribute instance-attribute

temperature: float | None = None

调用模型时使用的温度参数。

top_p class-attribute instance-attribute

top_p: float | None = None

调用模型时使用的top_p参数。

frequency_penalty class-attribute instance-attribute

frequency_penalty: float | None = None

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

presence_penalty class-attribute instance-attribute

presence_penalty: float | None = None

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

工具选择 class-attribute instance-attribute

tool_choice: (
    Literal["auto", "required", "none"] | str | None
) = None

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

parallel_tool_calls class-attribute instance-attribute

parallel_tool_calls: bool | None = None

是否在调用模型时使用并行工具调用。 如果未提供,默认为False。

截断 class-attribute instance-attribute

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

调用模型时使用的截断策略。

max_tokens class-attribute instance-attribute

max_tokens: int | None = None

生成的最大输出token数量。

存储 class-attribute instance-attribute

store: bool | None = None

是否存储生成的模型响应以供后续检索。 如果未提供,默认为True。

解决

resolve(override: ModelSettings | None) -> ModelSettings

通过将覆盖中的任何非None值叠加到当前实例上,生成一个新的ModelSettings。

Source code in 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
    }
    return replace(self, **changes)