深度思考
深度思考模型在传统大语言模型的基础上,强化了推理、逻辑分析和决策能力。在回答用户之前模型会先输出一段思维链内容,以提升最终答案的准确性,适用于复杂推理和深度分析任务,如数理逻辑推理、编程代码等。
深度思考模型API参数特殊说明
本章说明深度思考模型与常规文本生成模型接口字段的差异。
-
输出字段:
reasoning_content
:思维链内容,与content
同级,访问方法见【访问样例】content
:最终回答内容
- 不支持的参数:
temperature
、top_p
、presence_penalty
、frequency_penalty
、logprobs
、top_logprobs
。请注意,为了兼容已有软件,设置temperature
、top_p
、presence_penalty
、frequency_penalty
参数不会报错,但也不会生效。设置logprobs
、top_logprobs
会报错。
快速开始
您可以通过对话 Chat 调用深度思考模型,以下是使用深度思考模型的简单调用示例。
使用前请参考API Key 认证鉴权 获取 API Key ,用于在线推理服务的认证鉴权。
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?"
}
]
}'
深度思考模型多轮对话请求
多轮对话上下文拼接
除用户提问和模型回答外,深度思考模型还会输出思维链内容,帮助模型拆解用户问题、生成多种回复综合得到更好的答案。在每一轮对话中,模型都会输出思维链内容和最终回答。但在下一轮对话时,之前轮次输出的思维链内容不会被拼接的上下文中。以下是用户和模型多轮对话的示例。
请注意,如果您在输入的 messages 序列中,传入了reasoning_content
,API 会返回400
错误。因此,请删除 API 响应中的reasoning_content
字段,再发起 API 请求,方法如下述使用示例所示。
输出的 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
}
}
}
多轮对话调用示例
千帆 V2 版本推理接口兼容 OpenAI 接口规范,因此可以使用 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="deepseek-r1-distill-qwen-32b",
messages=messages,
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="deepseek-r1-distill-qwen-32b",
messages=messages,
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-distill-qwen-32b",
messages=messages
)
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-distill-qwen-32b",
messages=messages
)
print(response.choices[0].message)