深度思考
更新时间: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,请求示例如下:
Bash
1curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer bce-v3/ALTAK-*********/614fb**********' \
4--data '{
5 "model": "qwen3-235b-a22b",
6 "messages": [
7 {
8 "role": "user",
9 "content": "你好"
10 }
11 ],
12 "stream": false,
13 "enable_thinking":true // 表示开启深度思考
14}'
注意:Qwen3系列模型开启深度思考,输出token价格会更贵,详细请参考价格文档。
快速开始
您可以通过文本生成API 调用深度思考模型,以下是使用深度思考模型的简单调用示例。
Bash
1curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer bce-v3/ALTAK-*********/614fb**********' \
4--data '{
5 "model": "deepseek-r1",
6 "messages": [
7 {
8 "role": "user",
9 "content": "9.11 and 9.8, which is greater?"
10 }
11 ]
12}'
深度思考模型多轮对话请求
多轮对话上下文拼接
除用户提问和模型回答外,深度思考模型还会输出思维链内容,帮助模型拆解用户问题、生成多种回复综合得到更好的答案。在每一轮对话中,模型都会输出思维链内容和最终回答。但在下一轮对话时,之前轮次输出的思维链内容不会被拼接的上下文中。以下是用户和模型多轮对话的示例。
输出的 reasoning_content
长度不计入上下文长度中,但输出 token 数包含了思维链(reasoning_content
)和最终答案(content
)的所有 token。
您可以在请求完成后响应对象的 “usage” 中查看 token 的具体用量。思维链的 token 消耗量可在 completion_tokens_details
中查看。
JSON
1{
2 "usage": {
3 "prompt_tokens": 2,
4 "completion_tokens": 544,
5 "total_tokens": 546,
6 "completion_tokens_details": {
7 "reasoning_tokens": 446
8 }
9 }
10 }
多轮对话调用示例
可以使用 OpenAI SDK 请求千帆平台推理接口。
Python
1pip3 install-U openai
下面的代码以 Python 语言为例,展示了如何访问思维链和最终回答,以及如何在多轮对话中进行上下文拼接。
流式请求
Python
1from openai import OpenAI
2
3client = OpenAI(
4 api_key="your-api_key",
5 base_url="https://qianfan.baidubce.com/v2"
6)
7
8# Round 1
9messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]
10response = client.chat.completions.create(
11 model="qwen3-235b-a22b",
12 messages=messages,
13 # enable_thinking 参数开启思考过程,仅针对Qwen3系列有效,deepseek-r1不需要此参数
14 extra_body={"enable_thinking": True},
15 stream=True
16)
17
18reasoning_content = ""
19content = ""
20
21for chunk in response:
22 if chunk.choices[0].delta.reasoning_content:
23 print(chunk.choices[0].delta.reasoning_content, end="")
24 reasoning_content += chunk.choices[0].delta.reasoning_content
25 else:
26 print(chunk.choices[0].delta.content, end="")
27 content += chunk.choices[0].delta.content
28
29# Round 2
30messages.append({"role": "assistant", "content": content})
31messages.append({'role': 'user', 'content': "How many Rs are there in the word 'strawberry'?"})
32response = client.chat.completions.create(
33 model="qwen3-235b-a22b",
34 messages=messages,
35 # enable_thinking 参数开启思考过程,仅针对Qwen3系列有效,deepseek-r1不需要此参数
36 extra_body={"enable_thinking": True},
37 stream=True
38)
39
40for chunk in response:
41 if chunk.choices[0].delta.reasoning_content:
42 print(chunk.choices[0].delta.reasoning_content, end="")
43 reasoning_content += chunk.choices[0].delta.reasoning_content
44 else:
45 print(chunk.choices[0].delta.content, end="")
46 content += chunk.choices[0].delta.content
非流式请求
Python
1from openai import OpenAI
2
3client = OpenAI(
4 api_key="your-api_key",
5 base_url="https://qianfan.baidubce.com/v2"
6)
7
8# Round 1
9messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]
10response = client.chat.completions.create(
11 model="deepseek-r1",
12 messages=messages
13 # enable_thinking 参数开启思考过程,仅针对Qwen3系列有效,deepseek-r1不需要此参数
14 extra_body={"enable_thinking": True},
15)
16
17print(response.choices[0].message)
18
19reasoning_content = response.choices[0].message.reasoning_content
20content = response.choices[0].message.content
21
22# Round 2
23messages.append({'role': 'assistant', 'content': content})
24messages.append({'role': 'user', 'content': "How many Rs are there in the word 'strawberry'?"})
25response = client.chat.completions.create(
26 model="deepseek-r1",
27 messages=messages
28 # enable_thinking 参数开启思考过程,仅针对Qwen3系列有效,deepseek-r1不需要此参数
29 extra_body={"enable_thinking": True},
30)
31print(response.choices[0].message)