DeepSeek官方文档阅读笔记

DeepSeek今天发布了V3.2,深度思考模式首次支持工具调用,并且将最大输出token长度扩充至32k-64k。同时,发布了一个测试版的v3.2-speciale,该模型可以看做一个超级加强版长思考DeepSeek,具有出色的数学推理能力和高复杂任务解决能力。但既不支持工具调用,也没有针对日常应用场景进行专项优化,仅仅供探索模型能力边界研究使用。(消耗token量也显著更多)

image

 关注完了前沿,那么,如何在api调用中开启深度思考?

一. 开启深度思考

1. 设置model为'deepseek-reasoner'

2. model仍然设置成'deepseek-chat',同时设置thinking参数:

'thinking': {'type': 'enabled'}

如果使用openai sdk,需要这样传:

response = client.chat.completions.create(
    model='deepseek-chat',
    # ...
     extra_body={'thinking': {'type': 'enabled'}}
)

当开启深度思考后,返回字段中会比普通模型调用多一个reasoning_content参数(即深度思考的思维链内容),与content同级,在多轮对话时,官方推荐下一轮对话的输入仅包含上一轮的普通输出content,不会包含reasoning_content,来节省带宽。

二. 工具调用

目前DeepSeek仅支持function类型的工具调用。(有模型支持其它方式吗?待学习...)

首先我们要明白一个概念,大模型本身是没有函数执行能力的。所有的工具调用概念,指的是开发者预先定义好可以使用的函数,同时在请求时将可以调用的函数信息以标准json的形式编码,形成函数列表,用特定的tools参数传递给大模型,大模型经过思考,不输出文本,而是输出标准的json,内容为提示调用的函数名,和json编码过的函数参数。开发者拿到大模型输出后,解析输出对大模型要调用的方法+参数手动执行,并将结果跟随消息记录一同返回给大模型(以ToolMessage的形式),大模型再进行下一步思考。

重点是:函数是开发者自己定义的,执行也是开发者执行的。(当然现在很多agent框架替我们封装好了这些,比如langchain,google ADK)

image

 整个过程示意图,可以看出在最终答复用户之前,实际上调用了2次大模型(根据思考情况可以重复调用更多次)

image

 多次的示意图。

示例代码:

 1 import os
 2 import json
 3 from openai import OpenAI
 4 
 5 # The definition of the tools
 6 tools = [
 7     {
 8         "type": "function",
 9         "function": {
10             "name": "get_date",
11             "description": "Get the current date",
12             "parameters": { "type": "object", "properties": {} },
13         }
14     },
15     {
16         "type": "function",
17         "function": {
18             "name": "get_weather",
19             "description": "Get weather of a location, the user should supply the location and date.",
20             "parameters": {
21                 "type": "object",
22                 "properties": {
23                     "location": { "type": "string", "description": "The city name" },
24                     "date": { "type": "string", "description": "The date in format YYYY-mm-dd" },
25                 },
26                 "required": ["location", "date"]
27             },
28         }
29     },
30 ]
31 
32 # The mocked version of the tool calls
33 def get_date_mock():
34     return "2025-12-01"
35 
36 def get_weather_mock(location, date):
37     return "Cloudy 7~13°C"
38 
39 TOOL_CALL_MAP = {
40     "get_date": get_date_mock,
41     "get_weather": get_weather_mock
42 }
43 
44 def clear_reasoning_content(messages):
45     for message in messages:
46         if hasattr(message, 'reasoning_content'):
47             message.reasoning_content = None
48 
49 def run_turn(turn, messages):
50     sub_turn = 1
51     while True:
52         response = client.chat.completions.create(
53             model='deepseek-chat',
54             messages=messages,
55             tools=tools,
56             extra_body={ "thinking": { "type": "enabled" } }
57         )
58         messages.append(response.choices[0].message)
59         reasoning_content = response.choices[0].message.reasoning_content
60         content = response.choices[0].message.content
61         tool_calls = response.choices[0].message.tool_calls
62         print(f"Turn {turn}.{sub_turn}\n{reasoning_content=}\n{content=}\n{tool_calls=}")
63         # If there is no tool calls, then the model should get a final answer and we need to stop the loop
64         if tool_calls is None:
65             break
66         for tool in tool_calls:
67             tool_function = TOOL_CALL_MAP[tool.function.name]
68             tool_result = tool_function(**json.loads(tool.function.arguments))
69             print(f"tool result for {tool.function.name}: {tool_result}\n")
70             messages.append({
71                 "role": "tool",
72                 "tool_call_id": tool.id,
73                 "content": tool_result,
74             })
75         sub_turn += 1
76 
77 client = OpenAI(
78     api_key=os.environ.get('DEEPSEEK_API_KEY'),
79     base_url=os.environ.get('DEEPSEEK_BASE_URL'),
80 )
81 
82 # The user starts a question
83 turn = 1
84 messages = [{
85     "role": "user",
86     "content": "How's the weather in Hangzhou Tomorrow"
87 }]
88 run_turn(turn, messages)
89 
90 # The user starts a new question
91 turn = 2
92 messages.append({
93     "role": "user",
94     "content": "How's the weather in Hangzhou Tomorrow"
95 })
96 # We recommended to clear the reasoning_content in history messages so as to save network bandwidth
97 clear_reasoning_content(messages)
98 run_turn(turn, messages)
View Code

该代码的输出:

Turn 1.1
reasoning_content="The user is asking about the weather in Hangzhou tomorrow. I need to get the current date first, then calculate tomorrow's date, and then call the weather API. Let me start by getting the current date."
content=''
tool_calls=[ChatCompletionMessageToolCall(id='call_00_Tcek83ZQ4fFb1RfPQnsPEE5w', function=Function(arguments='{}', name='get_date'), type='function', index=0)]
tool_result(get_date): 2025-12-01

Turn 1.2
reasoning_content='Today is December 1, 2025. Tomorrow is December 2, 2025. I need to format the date as YYYY-mm-dd: "2025-12-02". Now I can call get_weather with location Hangzhou and date 2025-12-02.'
content=''
tool_calls=[ChatCompletionMessageToolCall(id='call_00_V0Uwt4i63m5QnWRS1q1AO1tP', function=Function(arguments='{"location": "Hangzhou", "date": "2025-12-02"}', name='get_weather'), type='function', index=0)]
tool_result(get_weather): Cloudy 7~13°C

Turn 1.3
reasoning_content="I have the weather information: Cloudy with temperatures between 7 and 13°C. I should respond in a friendly, helpful manner. I'll mention that it's for tomorrow (December 2, 2025) and give the details. I can also ask if they need any other information. Let's craft the response."
content="Tomorrow (Tuesday, December 2, 2025) in Hangzhou will be **cloudy** with temperatures ranging from **7°C to 13°C**.  \n\nIt might be a good idea to bring a light jacket if you're heading out. Is there anything else you'd like to know about the weather?"
tool_calls=None

Turn 2.1
reasoning_content="The user wants clothing advice for tomorrow based on the weather in Hangzhou. I know tomorrow's weather: cloudy, 7-13°C. That's cool but not freezing. I should suggest layered clothing, maybe a jacket, long pants, etc. I can also mention that since it's cloudy, an umbrella might not be needed unless there's rain chance, but the forecast didn't mention rain. I should be helpful and give specific suggestions. I can also ask if they have any specific activities planned to tailor the advice. Let me respond."
content="Based on tomorrow's forecast of **cloudy weather with temperatures between 7°C and 13°C** in Hangzhou, here are some clothing suggestions:\n\n**Recommended outfit:**\n- **Upper body:** A long-sleeve shirt or sweater, plus a light to medium jacket (like a fleece, windbreaker, or light coat)\n- **Lower body:** Long pants or jeans\n- **Footwear:** Closed-toe shoes or sneakers\n- **Optional:** A scarf or light hat for extra warmth, especially in the morning and evening\n\n**Why this works:**\n- The temperature range is cool but not freezing, so layering is key\n- Since it's cloudy but no rain mentioned, you likely won't need an umbrella\n- The jacket will help with the morning chill (7°C) and can be removed if you warm up during the day\n\n**If you have specific plans:**\n- For outdoor activities: Consider adding an extra layer\n- For indoor/office settings: The layered approach allows you to adjust comfortably\n\nWould you like more specific advice based on your planned activities?"
tool_calls=None
View Code

 

posted @ 2025-12-01 23:12  溯光独立开发  阅读(0)  评论(0)    收藏  举报