Deep analysis and code generation

Working with DeepSeek in Python

James Chapman

Curriculum Manager, DataCamp

Recap...

Reasoning output
<think>
Thought process...
</think>

Final response: ...
  • ThinkDoUpdate
  • model="deepseek-ai/DeepSeek-R1"
  • No temperature parameter for reasoning models
Working with DeepSeek in Python

1. Keep it simple

  • Concise prompts

What are the differences between lists and tuples in Python?

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

  • Providing examples (e.g., few-shot) can diminish performance

Who developed the Python programming language?

Example 1:
Q: Who developed the R programming language?
A: Ross Ihaka and Robert Gentleman
Working with DeepSeek in Python

2. Encourage reasoning

 

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

 

Improved accuracy

Increased token usage

Increased time-to-response

Working with DeepSeek in Python

3. Stay away from simple tasks!

 

Chat models

A chat model easily completing a simple task.

 

Reasoning models

A reasoning model struggling with simple problems.

1 Created with GPT-4o
Working with DeepSeek in Python

3. Stay away from simple tasks!

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-R1",
    messages=[{"role": "user", "content": "Return the result of 1+1."}]
)
<think>
Okay, so the user is asking for the result of 1 plus 1. Let me think about this. Well, basic
arithmetic tells me that when you add two numbers together, you combine their values. So 1 added
to another 1 should be... hmm. Let me visualize it. If I have one apple and someone gives me
another apple, I now have two apples. That makes sense. So 1 + 1 equals 2. Wait, is there any
trick here? The question seems straightforward, but maybe I should double-check. Let me count on
my fingers. One finger up, then add another...
</think>

The result of 1 + 1 is **2**.
Working with DeepSeek in Python

3. Stay away from simple tasks!

Chat output

The result of \(1 + 1\) is \(2\). 
\[
1 + 1 = 2
\]
Working with DeepSeek in Python

Example: Code debugging

A human debugging code.

The reasoning model workflow.

1 Created with GPT-4o
Working with DeepSeek in Python

Example: Code debugging

A reasoning model fixing code based on the error.

1 Created with GPT-4o
Working with DeepSeek in Python

Example: Code debugging

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

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

count_to_ten(1)
"""
Working with DeepSeek in Python
<think>
Okay, let's see. The user provided a Python function called count_to_ten...

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. Oh right, that's the issue...

So the fix is to add an increment to start inside the loop. Like start += 1...
</think>

Here is the corrected code:

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

count_to_ten(1)
Working with DeepSeek in Python

Let's practice!

Working with DeepSeek in Python

Preparing Video For Download...