交错思考
更新时间:2026-01-23
千帆提供了多种深度思考模型,部分模型支持交错思考,可以在工具调用之间、以及收到工具调用结果后继续思考。这种思考模式使得模型可以进行更复杂的分步推理:在采取下一步行动前,先理解和分析每一次工具的输出,将多轮工具调用与推理过程结合,基于中间产出的结果做出更加细致的决策。
支持模型
- deepseek-v3.2
- ernie-5.0-thinking-preview
使用说明
- 在回答问题1的过程中(请求 1.1 - 1.3),模型进行了多次思考 + 工具调用后给出答案。在这个过程中,用户需在 API 中回传思维链内容(reasoning_content),让模型继续思考。
- 在回答问题2时(请求 2.1),需删除之前的 reasoning_content,并保留其它内容发送给 API。如果保留了 reasoning_content 并发送给 API,API 将会忽略它们。
使用示例
Plain Text
1import json
2from openai import OpenAI
3
4# The definition of the tools
5tools = [
6 {
7 "type": "function",
8 "function": {
9 "name": "get_date",
10 "description": "Get the current date",
11 "parameters": { "type": "object", "properties": {} },
12 }
13 },
14 {
15 "type": "function",
16 "function": {
17 "name": "get_weather",
18 "description": "Get weather of a location, the user should supply the location and date.",
19 "parameters": {
20 "type": "object",
21 "properties": {
22 "location": { "type": "string", "description": "The city name" },
23 "date": { "type": "string", "description": "The date in format YYYY-mm-dd" },
24 },
25 "required": ["location", "date"]
26 },
27 }
28 },
29]
30
31# The mocked version of the tool calls
32def get_date_mock():
33 return "2025-12-01"
34
35def get_weather_mock(location, date):
36 return "Cloudy 7~13°C"
37
38TOOL_CALL_MAP = {
39 "get_date": get_date_mock,
40 "get_weather": get_weather_mock
41}
42
43def clear_reasoning_content(messages):
44 for message in messages:
45 if hasattr(message, 'reasoning_content'):
46 message.reasoning_content = None
47
48def run_turn(turn, messages):
49 sub_turn = 1
50 while True:
51 response = client.chat.completions.create(
52 model='deepseek-v3.2',
53 messages=messages,
54 tools=tools,
55 extra_body={ "thinking": { "type": "enabled" } }
56 )
57 messages.append(response.choices[0].message)
58 reasoning_content = response.choices[0].message.reasoning_content
59 content = response.choices[0].message.content
60 tool_calls = response.choices[0].message.tool_calls
61 print(f"Turn {turn}.{sub_turn}\n{reasoning_content=}\n{content=}\n{tool_calls=}")
62 # If there is no tool calls, then the model should get a final answer and we need to stop the loop
63 if tool_calls is None:
64 break
65 for tool in tool_calls:
66 tool_function = TOOL_CALL_MAP[tool.function.name]
67 tool_result = tool_function(**json.loads(tool.function.arguments))
68 print(f"tool result for {tool.function.name}: {tool_result}\n")
69 messages.append({
70 "role": "tool",
71 "tool_call_id": tool.id,
72 "content": tool_result,
73 })
74 sub_turn += 1
75
76client = OpenAI(
77 api_key= qianfan_api_key,
78 base_url="https://qianfan.baidubce.com/v2"
79)
80
81# The user starts a question
82turn = 1
83messages = [{
84 "role": "user",
85 "content": "北京明天天气怎么样"
86}]
87run_turn(turn, messages)
88
89# The user starts a new question
90turn = 2
91messages.append({
92 "role": "user",
93 "content": "北京明天天气怎么样"
94})
95# We recommended to clear the reasoning_content in history messages so as to save network bandwidth
96clear_reasoning_content(messages)
97run_turn(turn, messages)
在 Turn 1 的每个子请求中,都携带了该 Turn 下产生的 reasoning_content 给 API,从而让模型继续之前的思考。response.choices[0].message 携带了 assistant 消息的所有必要字段,包括 content、reasoning_content、tool_calls。
在 Turn 2 开始时,建议丢弃之前 Turn 中的 reasoning_content 。
上述代码的输出如下:
Plain Text
1Turn 1.1
2reasoning_content='用户想知道北京明天的天气情况。我需要获取北京的天气预报。首先,我需要知道明天的日期。我可以使用 `get_date` 工具来获取当前日期,然后计算出明天是哪一天。或者,我也可以直接尝试获取明天的天气。但为了精确起见,我应该先获取当前日期,再推算出明天的日期。\n\n让我先获取当前日期。'
3content=''
4tool_calls=[ChatCompletionMessageFunctionToolCall(id='f6ba3436315d4a17813224c9a9c63f11', function=Function(arguments='{}', name='get_date'), type='function')]
5tool result for get_date: 2025-12-01
6
7Turn 1.2
8reasoning_content='今天是2025年12月1日。那么明天就是2025年12月2日。现在我需要获取北京在2025年12月2日的天气预报。我会调用 `get_weather` 工具。'
9content=''
10tool_calls=[ChatCompletionMessageFunctionToolCall(id='010c460664784cd7a673fcfbcbc48db4', function=Function(arguments='{"location": "北京", "date": "2025-12-02"}', name='get_weather'), type='function')]
11tool result for get_weather: Cloudy 7~13°C
12
13Turn 1.3
14reasoning_content='根据天气预报,明天北京天气多云,气温在7到13摄氏度之间。\n\n现在我可以回复用户了。我会用中文告知他们天气情况。'
15content='根据天气预报,北京明天(2025-12-02)的天气为**多云**,气温在 **7~13°C** 之间。'
16tool_calls=None
17Turn 2.1
18reasoning_content='用户再次问同样的问题,但我已经回答过了。让我检查一下当前的日期,看看是不是需要更新信息。今天是2025-12-01,明天是2025-12-02,天气信息已经给出了。不过我注意到回答里用的是"tomorrow",但可能用户现在问的是另一个"明天"?让我重新检查一下日期。刚刚获取的天气信息是2025-12-02的。既然日期没变,我可以直接给出相同的答案。但为了确保万无一失,我可以再获取一次天气信息,确认一下。不过这样可能会有点多余。我应该直接给出之前的回答,或者也许用户希望我用中文再回答一遍?让我直接用中文再回答一次。'
19content='根据天气预报,北京明天(2025年12月2日)的天气为**多云**,气温在 **7~13°C** 之间。'
20tool_calls=None
