深度思考
更新时间:2025-04-30
深度思考模型在传统大语言模型的基础上,强化了推理、逻辑分析和决策能力。在回答用户之前模型会先输出一段思维链内容,以提升最终答案的准确性,适用于复杂推理和深度分析任务,如数理逻辑推理、编程代码等。
深度思考模型API参数特殊说明
本章说明深度思考模型与常规文本生成模型接口字段的差异。
-
输出字段:
reasoning_content
:思维链内容,与content
同级,访问方法见【访问样例】content
:最终回答内容
- 不支持的参数:
temperature
、top_p
、presence_penalty
、frequency_penalty
、logprobs
、top_logprobs
。请注意,为了兼容已有软件,设置temperature
、top_p
、presence_penalty
、frequency_penalty
参数不会报错,但也不会生效。
Qwen3系列模型开启深度思考
qwen3系列模型 深度思考通过enable_thinking
参数控制,默认为false,表示关闭深度思考。如果要开启深度思考,设置为true,请求示例如下:
curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer bce-v3/ALTAK-*********/614fb**********' \
--data '{
"model": "qwen3-235b-a22b",
"messages": [
{
"role": "user",
"content": "你好"
}
],
"stream": false,
"enable_thinking":true // 表示开启深度思考
}'
注意:Qwen3系列模型开启深度思考,输出token价格会更贵,详细请参考价格文档。
快速开始
您可以通过文本生成API 调用深度思考模型,以下是使用深度思考模型的简单调用示例。
curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer bce-v3/ALTAK-*********/614fb**********' \
--data '{
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "9.11 and 9.8, which is greater?"
}
]
}'
深度思考模型多轮对话请求
多轮对话上下文拼接
除用户提问和模型回答外,深度思考模型还会输出思维链内容,帮助模型拆解用户问题、生成多种回复综合得到更好的答案。在每一轮对话中,模型都会输出思维链内容和最终回答。但在下一轮对话时,之前轮次输出的思维链内容不会被拼接的上下文中。以下是用户和模型多轮对话的示例。
输出的 reasoning_content
长度不计入上下文长度中,但输出 token 数包含了思维链(reasoning_content
)和最终答案(content
)的所有 token。
您可以在请求完成后响应对象的 “usage” 中查看 token 的具体用量。思维链的 token 消耗量可在 completion_tokens_details
中查看。
{
"usage": {
"prompt_tokens": 2,
"completion_tokens": 544,
"total_tokens": 546,
"completion_tokens_details": {
"reasoning_tokens": 446
}
}
}
多轮对话调用示例
可以使用 OpenAI SDK 请求千帆平台推理接口。
pip3 install-U openai
下面的代码以 Python 语言为例,展示了如何访问思维链和最终回答,以及如何在多轮对话中进行上下文拼接。
流式请求
from openai import OpenAI
client = OpenAI(
api_key="your-api_key",
base_url="https://qianfan.baidubce.com/v2"
)
# Round 1
messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]
response = client.chat.completions.create(
model="qwen3-235b-a22b",
messages=messages,
# enable_thinking 参数开启思考过程,仅针对Qwen3系列有效,deepseek-r1不需要此参数
extra_body={"enable_thinking": True},
stream=True
)
reasoning_content = ""
content = ""
for chunk in response:
if chunk.choices[0].delta.reasoning_content:
print(chunk.choices[0].delta.reasoning_content, end="")
reasoning_content += chunk.choices[0].delta.reasoning_content
else:
print(chunk.choices[0].delta.content, end="")
content += chunk.choices[0].delta.content
# Round 2
messages.append({"role": "assistant", "content": content})
messages.append({'role': 'user', 'content': "How many Rs are there in the word 'strawberry'?"})
response = client.chat.completions.create(
model="qwen3-235b-a22b",
messages=messages,
# enable_thinking 参数开启思考过程,仅针对Qwen3系列有效,deepseek-r1不需要此参数
extra_body={"enable_thinking": True},
stream=True
)
for chunk in response:
if chunk.choices[0].delta.reasoning_content:
print(chunk.choices[0].delta.reasoning_content, end="")
reasoning_content += chunk.choices[0].delta.reasoning_content
else:
print(chunk.choices[0].delta.content, end="")
content += chunk.choices[0].delta.content
非流式请求
from openai import OpenAI
client = OpenAI(
api_key="your-api_key",
base_url="https://qianfan.baidubce.com/v2"
)
# Round 1
messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]
response = client.chat.completions.create(
model="deepseek-r1",
messages=messages
# enable_thinking 参数开启思考过程,仅针对Qwen3系列有效,deepseek-r1不需要此参数
extra_body={"enable_thinking": True},
)
print(response.choices[0].message)
reasoning_content = response.choices[0].message.reasoning_content
content = response.choices[0].message.content
# Round 2
messages.append({'role': 'assistant', 'content': content})
messages.append({'role': 'user', 'content': "How many Rs are there in the word 'strawberry'?"})
response = client.chat.completions.create(
model="deepseek-r1",
messages=messages
# enable_thinking 参数开启思考过程,仅针对Qwen3系列有效,deepseek-r1不需要此参数
extra_body={"enable_thinking": True},
)
print(response.choices[0].message)