深度分析与代码生成

在 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."

 

令牌用量增加

响应更慢

在 Python 中使用 DeepSeek

3. 避免简单任务!

 

聊天模式

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

 

推理模式

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

在 Python 中使用 DeepSeek

3. 避免简单任务!

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4.1-Flash",
    messages=[{"role": "user", "content": "Return the result of 1+1."}]
)
print(response.choices[0].message.content)
Let's count step by step.
- Start with 1.
- Add another 1.
- 1 + 1 = 2.

**Final Answer: 2**
  • 对于琐碎查找,结构化推理大材小用
  • 相比聊天式回答,耗费更多令牌且延迟更高
在 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)
Let me walk through the function step by step.

The function uses a while loop: while start < 10. Then it prints start. But wait, inside the loop,
there's no increment for the start variable — that's the issue.

The fix is to add an increment to start inside the loop. Like start += 1.

Here is the corrected code:

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

count_to_ten(1)
在 Python 中使用 DeepSeek

让我们一起练习吧!

在 Python 中使用 DeepSeek

Preparing Video For Download...