深度分析與程式碼產生

使用 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. 避免用於過簡任務!

 

Chat 模式

聊天模型輕鬆完成簡單任務。

 

Reasoning 模式

推理模型在簡單問題上反而吃力。

使用 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**
  • 對瑣碎查詢,結構化推理是大材小用
  • 比起 chat 風格回答,耗費更多 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)
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...