Get startedGet started for free

Creating a reasoning chatbot for coding

Let's put everything together to build a reasoning chatbot for coding assistance!

You've been provided with two user messages: one to request Python code for a particular task, and a follow-up message requesting that it be written with a particular library.

Let's see how the reasoning model does!

This exercise is part of the course

Working with DeepSeek in Python

View Course

Exercise instructions

  • Loop over the user questions.
  • Send each user question, q, to the deepseek-ai/DeepSeek-R1 model.
  • Extract the response content to strip it of thinking tokens before appending it to messages.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")

messages = []
user_msgs = ["Write some Python code to generate a list of numbers from 1-10.", "Update the code to use the NumPy library."]

# Loop over the user questions
for q in ____:
    print("User: ", q)
    user_dict = {"role": "user", "content": q}
    messages.append(user_dict)
    
    # Create the API request
    response = client.chat.completions.create(
        model="deepseek-ai/____",
        messages=____,
        max_tokens=200
    )
    
    # Extract the response content to strip it of thinking tokens
    final_response = re.sub(r'[\s\S]*?<\/think>\s*', '', ____, re.DOTALL)
    assistant_dict = {"role": "assistant", "content": final_response.strip()}
    messages.append(assistant_dict)
    print("Assistant: ", response.choices[0].message.content, "\n")
Edit and Run Code