使用 DeepSeek 进行推理

在 Python 中使用 DeepSeek

James Chapman

AI Curriculum Lead, DataCamp

聊天图标

  • 预测下一个词
  • ✅ 适用范围广
  • ❌ 难以处理深度多步任务

推理图标

  • 将复杂任务拆分为子任务
  • ✅ 复杂任务表现更好
  • ❌ 更慢且更昂贵
在 Python 中使用 DeepSeek

推理与数学

一列火车在 1.5 小时内行驶 60 miles。若保持相同速度,4 小时可行驶多远?若 2:15 PM 出发,何时到达?

一列火车在轨道上行驶。

在 Python 中使用 DeepSeek

推理与数学

一列火车在 1.5 小时内行驶 60 miles。若保持相同速度,4 小时可行驶多远?若 2:15 PM 出发,何时到达?

  1. 计算速度(60 ÷ 1.5 = 40 mph

  2. 乘以 4 小时求距离(40 × 4 = 160 miles

  3. 出发时间加 4 小时(2:15 PM + 4 小时 = 6:15 PM

  4. 返回:"160 miles,6:15 PM 到达"

在 Python 中使用 DeepSeek

关于"推理"的推理

高层次推理流程:思考、执行、更新知识。

在 Python 中使用 DeepSeek

默认开启推理

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[{"role": "user", "content": train_problem}]
)

print(response.choices[0].message.content)
  • V4-Pro 默认启用推理 → 无需额外参数
在 Python 中使用 DeepSeek
print(response.choices[0].message.content)
**步骤 1 - 求列车速度**
速度 = 60 / 1.5 = **40 mph**

**步骤 2 - 4 小时内的距离**
距离 = 40 × 4 = **160 miles**

**步骤 3 - 到达时间**
2:15 PM + 4 小时 = **6:15 PM**

**最终答案:**160 miles,6:15 PM 到达
在 Python 中使用 DeepSeek

关闭推理

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    extra_body={"thinking": {"type": "disabled"}},
    messages=[{"role": "user", "content": prompt}]
)
  • 思考默认 "enabled";传入 "disabled" 可关闭
  • 关闭推理后,模型表现如标准聊天模型
在 Python 中使用 DeepSeek

提高推理强度

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    reasoning_effort="max",
    messages=[{"role": "user", "content": train_problem}]
)

print(response.choices[0].message.content)
  • 默认努力级别为 "high" → 难题可设为 "max"
  • 提高推理努力会增加成本
  • 先用较低努力级别测试
在 Python 中使用 DeepSeek

Vamos praticar!

在 Python 中使用 DeepSeek

Preparing Video For Download...