跳转到内容

pydantic_ai.settings

模型设置

基类:TypedDict

用于配置大语言模型(LLM)的设置。

这里我们仅包含适用于多个模型/模型提供商的设置,但并非所有模型都支持所有这些设置。

源代码位于 pydantic_ai_slim/pydantic_ai/settings.py
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
class ModelSettings(TypedDict, total=False):
    """Settings to configure an LLM.

    Here we include only settings which apply to multiple models / model providers,
    though not all of these settings are supported by all models.
    """

    max_tokens: int
    """The maximum number of tokens to generate before stopping.

    Supported by:

    * Gemini
    * Anthropic
    * OpenAI
    * Groq
    * Cohere
    * Mistral
    * Bedrock
    * MCP Sampling
    """

    temperature: float
    """Amount of randomness injected into the response.

    Use `temperature` closer to `0.0` for analytical / multiple choice, and closer to a model's
    maximum `temperature` for creative and generative tasks.

    Note that even with `temperature` of `0.0`, the results will not be fully deterministic.

    Supported by:

    * Gemini
    * Anthropic
    * OpenAI
    * Groq
    * Cohere
    * Mistral
    * Bedrock
    """

    top_p: float
    """An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass.

    So 0.1 means only the tokens comprising the top 10% probability mass are considered.

    You should either alter `temperature` or `top_p`, but not both.

    Supported by:

    * Gemini
    * Anthropic
    * OpenAI
    * Groq
    * Cohere
    * Mistral
    * Bedrock
    """

    timeout: float | Timeout
    """Override the client-level default timeout for a request, in seconds.

    Supported by:

    * Gemini
    * Anthropic
    * OpenAI
    * Groq
    * Mistral
    """

    parallel_tool_calls: bool
    """Whether to allow parallel tool calls.

    Supported by:

    * OpenAI (some models, not o1)
    * Groq
    * Anthropic
    """

    seed: int
    """The random seed to use for the model, theoretically allowing for deterministic results.

    Supported by:

    * OpenAI
    * Groq
    * Cohere
    * Mistral
    """

    presence_penalty: float
    """Penalize new tokens based on whether they have appeared in the text so far.

    Supported by:

    * OpenAI
    * Groq
    * Cohere
    * Gemini
    * Mistral
    """

    frequency_penalty: float
    """Penalize new tokens based on their existing frequency in the text so far.

    Supported by:

    * OpenAI
    * Groq
    * Cohere
    * Gemini
    * Mistral
    """

    logit_bias: dict[str, int]
    """Modify the likelihood of specified tokens appearing in the completion.

    Supported by:

    * OpenAI
    * Groq
    """

    stop_sequences: list[str]
    """Sequences that will cause the model to stop generating.

    Supported by:

    * OpenAI
    * Anthropic
    * Bedrock
    * Mistral
    * Groq
    * Cohere
    * Google
    """

    extra_headers: dict[str, str]
    """Extra headers to send to the model.

    Supported by:

    * OpenAI
    * Anthropic
    * Groq
    """

    extra_body: object
    """Extra body to send to the model.

    Supported by:

    * OpenAI
    * Anthropic
    * Groq
    """

max_tokens 实例属性

max_tokens: int

在停止生成前所生成的最大令牌(token)数量。

支持的模型

  • Gemini
  • Anthropic
  • OpenAI
  • Groq
  • Cohere
  • Mistral
  • Bedrock
  • MCP 采样

temperature 实例属性

temperature: float

注入到响应中的随机性大小。

对于分析性/多项选择任务,请使用接近 0.0temperature 值;对于创造性和生成性任务,请使用接近模型所允许的最大 temperature 值。

请注意,即使 temperature 值为 0.0,结果也不会完全确定。

支持的模型

  • Gemini
  • Anthropic
  • OpenAI
  • Groq
  • Cohere
  • Mistral
  • Bedrock

top_p 实例属性

top_p: float

一种替代温度采样的方法,称为核采样(nucleus sampling),模型会考虑概率总和为 top_p 的令牌结果。

因此,0.1 意味着只考虑构成最高 10% 概率质量的令牌。

您应该只更改 temperaturetop_p 中的一个,而不是两者都更改。

支持的模型

  • Gemini
  • Anthropic
  • OpenAI
  • Groq
  • Cohere
  • Mistral
  • Bedrock

timeout 实例属性

timeout: float | Timeout

覆盖客户端级别的默认请求超时时间,单位为秒。

支持的模型

  • Gemini
  • Anthropic
  • OpenAI
  • Groq
  • Mistral

parallel_tool_calls 实例属性

parallel_tool_calls: bool

是否允许并行工具调用。

支持的模型

  • OpenAI(某些模型,非 o1)
  • Groq
  • Anthropic

seed 实例属性

seed: int

用于模型的随机种子,理论上可以实现确定性结果。

支持的模型

  • OpenAI
  • Groq
  • Cohere
  • Mistral

presence_penalty 实例属性

presence_penalty: float

根据新令牌是否已在文本中出现过来对其进行惩罚。

支持的模型

  • OpenAI
  • Groq
  • Cohere
  • Gemini
  • Mistral

frequency_penalty 实例属性

frequency_penalty: float

根据新令牌在文本中现有的频率来对其进行惩罚。

支持的模型

  • OpenAI
  • Groq
  • Cohere
  • Gemini
  • Mistral

logit_bias 实例属性

logit_bias: dict[str, int]

修改指定令牌在补全结果中出现的可能性。

支持的模型

  • OpenAI
  • Groq

stop_sequences 实例属性

stop_sequences: list[str]

会导致模型停止生成的序列。

支持的模型

  • OpenAI
  • Anthropic
  • Bedrock
  • Mistral
  • Groq
  • Cohere
  • Google

extra_headers 实例属性

extra_headers: dict[str, str]

发送给模型的额外请求头。

支持的模型

  • OpenAI
  • Anthropic
  • Groq

extra_body 实例属性

extra_body: object

发送给模型的额外请求体。

支持的模型

  • OpenAI
  • Anthropic
  • Groq