深度分析与代码生成

在 Python 中使用 DeepSeek

James Chapman

AI Curriculum Lead, DataCamp

回顾...

print(response.choices[0].message.content)
Step 1 - ...
Step 2 - ...
Final Answer: ...
  • 默认启用推理 → 可用 reasoning_effort="high""max" 调整
  • 推理时 temperature 无任何作用
在 Python 中使用 DeepSeek

1. 保持简洁

  • 精简提示

What are the differences between lists and tuples in Python?

In Python, there are different data structures. Lists are...

  • 提供示例(如 few-shot)可能会降低表现

Who developed the Python programming language?

Example 1:
Q: Who developed the R programming language?
A: Ross Ihaka and Robert Gentleman
在 Python 中使用 DeepSeek

2. 促进推理

 

"... Take your time and think through each step."

 

❌ token 使用增加

❌ 响应时间更长

在 Python 中使用 DeepSeek

3. 避免简单任务!

 

聊天模式

聊天模型轻松完成简单任务。

 

推理模式

推理模型在简单问题上反而吃力。

在 Python 中使用 DeepSeek

3. 避免简单任务!

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[{"role": "user", "content": "Return the result of 1+1."}]
)
print(response.choices[0].message.content)
让我们逐步计算。
- 从 1 开始。
- 再加 1。
- 1 + 1 = 2。

**最终答案:2**
  • 对于琐碎查询,结构化推理是小题大做
  • 比聊天式回答耗费更多 tokens,延迟更高
在 Python 中使用 DeepSeek

示例:代码调试

一位人类在调试代码。

推理模型的工作流程。

在 Python 中使用 DeepSeek

示例:代码调试

一个推理模型根据错误修复代码。

在 Python 中使用 DeepSeek

示例:代码调试

prompt = """
[Task: Fix the following code.]

Code:
def count_to_ten(start):
    while start < 10:
        print(start)
    return "Done"

count_to_ten(1)
"""
在 Python 中使用 DeepSeek
print(response.choices[0].message.content)
让我逐步分析该函数。

该函数使用 while 循环:while start < 10,然后打印 start。但循环内
没有对 start 递增——这就是问题。

修复方法是在循环中给 start 递增,例如 start += 1。

修正后的代码:

def count_to_ten(start):
    while start < 10:
        print(start)
        start += 1
    return "Done"

count_to_ten(1)
在 Python 中使用 DeepSeek

Passons à la pratique !

在 Python 中使用 DeepSeek

Preparing Video For Download...