思维过程
思考(或称推理)是模型在给出最终答案之前,一步一步解决问题的过程。
此功能通常默认禁用,并且取决于所使用的具体模型。请参阅以下各节,了解如何为每个提供商启用思考功能。
在内部,如果模型不提供思考对象,Pydantic AI 会将提供商特定文本部分中的思考块("<think>..."</think>")转换为 ThinkingPart。我们还决定在多轮对话中不将 ThinkingPart 发回给提供商——这有助于为用户节省成本。未来,我们计划添加一个设置来自定义此行为。
OpenAI
在使用 OpenAIChatModel 时,默认情况下不会创建思考对象。但是,文本内容可能包含 "<think>" 标签。当发生这种情况时,Pydantic AI 会将它们转换为 ThinkingPart 对象。
相反,OpenAIResponsesModel 确实会生成思考部分。要启用此功能,您需要在 OpenAIResponsesModelSettings 中设置 openai_reasoning_effort 和 openai_reasoning_summary 字段。
openai_thinking_part.py
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIResponsesModel, OpenAIResponsesModelSettings
model = OpenAIResponsesModel('o3-mini')
settings = OpenAIResponsesModelSettings(
openai_reasoning_effort='low',
openai_reasoning_summary='detailed',
)
agent = Agent(model, model_settings=settings)
...
Anthropic
与其他提供商不同,Anthropic 在思考部分包含一个签名。该签名用于确保思考部分未被篡改。要启用思考功能,请在 AnthropicModelSettings 中使用 anthropic_thinking 字段。
anthropic_thinking_part.py
from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel, AnthropicModelSettings
model = AnthropicModel('claude-3-7-sonnet-latest')
settings = AnthropicModelSettings(
anthropic_thinking={'type': 'enabled', 'budget_tokens': 1024},
)
agent = Agent(model, model_settings=settings)
...
Groq
Groq 支持以不同格式接收思考部分
"raw":思考部分以"<think>"标签的形式包含在文本内容中。"hidden":思考部分不包含在文本内容中。"parsed":思考部分有其自己的ThinkingPart对象。
要启用思考功能,请在 GroqModelSettings 中使用 groq_reasoning_format 字段。
groq_thinking_part.py
from pydantic_ai import Agent
from pydantic_ai.models.groq import GroqModel, GroqModelSettings
model = GroqModel('qwen-qwq-32b')
settings = GroqModelSettings(groq_reasoning_format='parsed')
agent = Agent(model, model_settings=settings)
...
要启用思考功能,请在 GoogleModelSettings 中使用 google_thinking_config 字段。
google_thinking_part.py
from pydantic_ai import Agent
from pydantic_ai.models.google import GoogleModel, GoogleModelSettings
model = GoogleModel('gemini-2.5-pro-preview-03-25')
settings = GoogleModelSettings(google_thinking_config={'include_thoughts': True})
agent = Agent(model, model_settings=settings)
...
Mistral / Cohere
Mistral 和 Cohere 都不生成思考部分。