跳转至

Model settings

ModelSettings dataclass

调用 LLM 时使用的设置。

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

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

Source code in agents/model_settings.py
@dataclass
class ModelSettings:
    """调用 LLM 时使用的设置。

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

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

    temperature: float | None = None
    """调用模型时使用的温度参数。"""

    top_p: float | None = None
    """调用模型时使用的 top_p 参数。"""

    frequency_penalty: float | None = None
    """调用模型时使用的频率惩罚项。"""

    presence_penalty: float | None = None
    """调用模型时使用的出现惩罚项。"""

    tool_choice: Literal["auto", "required", "none"] | str | None = None
    """调用模型时使用的工具选择。"""

    parallel_tool_calls: bool | None = None
    """调用模型时是否使用并行工具调用。
    如果未提供,默认为 False。"""

    truncation: Literal["auto", "disabled"] | None = None
    """调用模型时使用的截断策略。"""

    max_tokens: int | None = None
    """生成的最大输出 token 数。"""

    reasoning: Reasoning | None = None
    """[推理模型](https://platform.openai.com/docs/guides/reasoning)的配置选项。
    """

    metadata: dict[str, str] | None = None
    """在模型响应调用中包含的元数据。"""

    store: bool | None = None
    """是否存储生成的模型响应以便后续检索。
    如果未提供,默认为 True。"""

    include_usage: bool | None = None
    """是否包含 usage 块。
    如果未提供,默认为 True。"""

    extra_query: Query | None = None
    """请求时提供的额外查询字段。
    如果未提供,默认为 None。"""

    extra_body: Body | None = None
    """请求时提供的额外 body 字段。
    如果未提供,默认为 None。"""

    extra_headers: Headers | None = None
    """请求时提供的额外 headers 字段。
    如果未提供,默认为 None。"""

    def resolve(self, override: ModelSettings | None) -> ModelSettings:
        """通过将 override 中所有非 None 的值覆盖到当前实例上,生成一个新的 ModelSettings。"""
        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)

    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 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

调用模型时使用的出现惩罚项。

tool_choice 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。

truncation class-attribute instance-attribute

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

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

max_tokens class-attribute instance-attribute

max_tokens: int | None = None

生成的最大输出 token 数。

reasoning class-attribute instance-attribute

reasoning: Reasoning | None = None

推理模型的配置选项。

metadata class-attribute instance-attribute

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

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

store class-attribute instance-attribute

store: bool | None = None

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

include_usage class-attribute instance-attribute

include_usage: bool | None = None

是否包含 usage 块。 如果未提供,默认为 True。

extra_query class-attribute instance-attribute

extra_query: Query | None = None

请求时提供的额外查询字段。 如果未提供,默认为 None。

extra_body class-attribute instance-attribute

extra_body: Body | None = None

请求时提供的额外 body 字段。 如果未提供,默认为 None。

extra_headers class-attribute instance-attribute

extra_headers: Headers | None = None

请求时提供的额外 headers 字段。 如果未提供,默认为 None。

resolve

resolve(override: ModelSettings | None) -> ModelSettings

通过将 override 中所有非 None 的值覆盖到当前实例上,生成一个新的 ModelSettings。

Source code in agents/model_settings.py
def resolve(self, override: ModelSettings | None) -> ModelSettings:
    """通过将 override 中所有非 None 的值覆盖到当前实例上,生成一个新的 ModelSettings。"""
    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)