Reasoning models and DeepSeek R1

Working with DeepSeek in Python

James Chapman

Curriculum Manager, DataCamp

chat_icon.png

  • Predict next words
  • ✅ Wide-range of use cases
  • ❌ Struggle with deep multi-step tasks

reasoning_icon.png

  • Break complex tasks into sub-tasks
  • ✅ Better performance on complex tasks
  • ❌ Slower and more expensive
Working with DeepSeek in Python

Reasoning and math

A train travels 60 miles in 1.5 hours. If it continues at the same speed, how far will it travel in 4 hours, and what time will it arrive if it leaves at 2:15 PM?

A train traveling on a track.

Working with DeepSeek in Python

Reasoning and math

A train travels 60 miles in 1.5 hours. If it continues at the same speed, how far will it travel in 4 hours, and what time will it arrive if it leaves at 2:15 PM?

  1. Calculate the speed (60 ÷ 1.5 = 40 mph)

  2. Multiply by 4 hours to find the distance (40 × 4 = 160 miles)

  3. Add 4 hours to the departure time (2:15 PM + 4 hours = 6:15 PM)

  4. Return: "160 miles, arrival at 6:15 PM"

Working with DeepSeek in Python

Reasoning about reasoning

The high-level reasoning workflow of thinking, doing, and updating the knowledge.

1 Image generated with GPT-4o
Working with DeepSeek in Python

Requesting DeepSeek models

response = client.chat.completions.create(

# On DeepSeek's API: model="deepseek-reasoner" model="deepseek-ai/DeepSeek-R1",
messages=[ {"role": "user", "content": train_problem}] ) print(response.choices[0].message.content)
Working with DeepSeek in Python
<think>
Let's solve the problem step by step.

### **1. Calculate the Speed of the Train**
First, determine the speed of the train using the given distance and time.
\[
\text{Speed} = \frac{\text{Distance}}{\text{Time}} = \frac{60 \text{ miles}}{1.5 \text{ hours}} = 40 \text{ mph}
\]

### **2. Determine the Distance Traveled in 4 Hours**
Using the speed calculated above, find out how far the train will travel in 4 hours[...]

### **3. Calculate the Arrival Time**
If the train leaves at **2:15 PM** and travels for **4 hours**, the arrival time is[...]
</think>

### **Final Answers**
- **Distance traveled in 4 hours:** \(\boxed{160 \text{ miles}}\)
- **Arrival time:** \(\boxed{6:15 \text{ PM}}\)
Working with DeepSeek in Python

Extracting the reasoning content

  • Use regular expressions (RegEx)
response_content = response.choices[0].message.content
import re

match = re.search(r'<think>(.*?)</think>', response_content, re.DOTALL)

think_content = match.group(1).strip() print(think_content)
Working with DeepSeek in Python

Examining the model's thoughts

Let's solve the problem step by step.

### **1. Calculate the Speed of the Train**
First, determine the speed of the train using the given distance and time.
\[
\text{Speed} = \frac{\text{Distance}}{\text{Time}} = \frac{60 \text{ miles}}{1.5 \text{ hours}} = 40 \text{ mph}
\]

### **2. Determine the Distance Traveled in 4 Hours**
Using the speed calculated above, find out how far the train will travel in 4 hours[...]

### **3. Calculate the Arrival Time**
If the train leaves at **2:15 PM** and travels for **4 hours**, the arrival time is[...]
Working with DeepSeek in Python

Let's practice!

Working with DeepSeek in Python

Preparing Video For Download...