Get startedGet started for free

Building a coding chatbot with DeepSeek R1

1. Building a coding chatbot with DeepSeek R1

Congratulations on your first chatbot! This approach will serve you well in many domains, but how about deep research, analysis, or coding? In these cases, we need reasoning models.

2. Current approach

Previously, we built a chatbot conversation history using a mechanism that appended the user input and model output to the messages list of dictionaries to be sent with the next input. But for reasoning models, this becomes problematic.

3. Problems with reasoning

The output from reasoning models contains the model's "thoughts", as well as the final response. These thoughts can be quite long,

4. Problems with reasoning

and sending all of this back to the model goes against the best practice of keeping the reasoning inputs short and simple, as the models are designed to build their own reasoning steps.

5. Problems with reasoning

The quantity of thinking tokens would also quickly clog up the model, increasing runtimes and cost, and degrading performance as the model struggles to consider all of the context provided. So what do we do about this?

6. The reasoning approach

One approach is to trim the thoughts from the conversation history, so only the input and final response is sent with the next request. This second request will generate its own thinking tokens,

7. The reasoning approach

which would also be trimmed before being sent in a third request. We'll use this approach to build a functional reasoning chatbot.

8. Trimming thoughts

We've seen previously how to extract the thinking tokens using regular expressions, and we can take similar approach to exclude them. The re.sub() function can be used to replace the think tokens, and any content between them, with an empty string, deleting them from the response content. Again, the .strip() method is used to remove leading or trailing whitespaces.

9. Coding a reasoning chatbot

Here's the backbone of our chatbot code. We've swapped the model to a reasoning model, and thankfully, this model supports the same chat roles we've used with chat models. The only other change we need to make is to remove the thinking tokens, using the regex code we just saw, before adding them to the assistant_dict. This means we could create a reasoning chatbot for code debugging. We create an empty messages list to populate with new messages. At this point, you might be wondering where our system message is. Well, reasoning models don't tend to work well with system messages, for the same reason as few-shot prompts, they work best unassisted. We provide the code to debug, and any follow-up message to the initial response, which could be additional constraints on the code, error messages, or other code outputs to help the model update its knowledge and respond accordingly.

10. Let's practice!

Time for you to give this a go!