1. Reasoning models and DeepSeek R1
Let's now take a look at DeepSeek's reasoning models.
2. Chatting vs. reasoning
The chat models that we've used up until this point are designed to predict the next word in the conversation. They're great for anything from ad-hoc question-answering all the way to workflow automations.
However, they struggle with more involved tasks that require deep analysis or multiple problem-solving steps, like coding and math.
Reasoning models, like DeepSeek's R1 model break complex tasks in smaller, structured steps that greatly improve the likelihood of arriving at the final answer.
Let's illustrate this difference with an example.
3. 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:15PM?
A chat model might impatiently generate a straight prediction, or perhaps the text to help it find the answer, but reasoning models would likely break it down into the following steps:
4. Reasoning and math
It would first calculate the average speed of the train, as the problem indicates the train continues at this speed.
Then, it would calculate the distance it would cover in that time by multiplying by four hours,
and add four hours to the original departure time.
Finally, it responds with the correct answer.
5. Reasoning about reasoning
Throughout this process, the model is generating "thoughts" on what to do, doing it, and then updating its knowledge to help it move forward or iterate.
Let's give this a go!
6. Requesting DeepSeek models
Using a reasoning model only requires one change, and that is to the model we're requesting. Let's send our train problem to the model.
7. Reasoning model output
Here's the model's response. We've truncated steps two and three, which, like step one, included the mathematical equations used by the model.
We can see that the model followed the steps we expected, which mirror the steps a human would also take to tackle the problem.
Notice also that the response includes <think> tags to separate text involved in the thinking part of the process from the final response.
These tags are included in a consistent way in each model response,
8. Extracting the reasoning content
so we can use regular expressions, or RegEx, to extract only the thinking text using an appropriate pattern.
We'll assign the model response to a variable.
Then, we'll use the built-in re Python module to search for this <think> tag pattern. We can read this string as capture any text between an opening and closing think tag. We apply it to the response_content variable, and re.DOTALL ensures that everything is captured, including new lines.
We extract the group captured by the pattern with the .group() method, and strip any leading or trailing whitespace with .strip().
9. Examining the model's thoughts
There we have it, we have peered directly into the model's thought process!
10. Let's practice!
Now it's your turn to give this a try!