Mulai sekarangMulai gratis

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.

V4-Pro reasons by default, so all you need to do is loop the conversation and append the right field to the history.

Latihan ini merupakan bagian dari kursus

Bekerja dengan DeepSeek di Python

Lihat Kursus

Instruksi latihan

  • Loop over the user questions.
  • Send each user question, q, to the deepseek-ai/DeepSeek-V4-Pro model.
  • Append the assistant's final answer (the .content, not the reasoning) to messages so the conversation history stays lean.

Latihan interaktif langsung praktik

Cobalah latihan ini dengan melengkapi kode contoh ini.

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 — V4-Pro reasons by default
    response = client.chat.completions.create(
        model="deepseek-ai/____",
        messages=____,
        max_tokens=500
    )
    
    # Append only the final answer to the conversation history
    assistant_dict = {"role": "assistant", "content": response.choices[0].message.____}
    messages.append(assistant_dict)
    print("Assistant: ", response.choices[0].message.content, "\n")
Edit dan Jalankan Kode