The DeepSeek /chat/completions API offers powerful capabilities for building conversational AI applications. This article will guide you through utilizing the API for multi-turn conversations, enabling you to create engaging and context-aware chat experiences.
It's crucial to understand that the DeepSeek /chat/completions API is stateless. This means that the server doesn't automatically retain any history of past interactions. To maintain context across multiple turns, you, as the user, are responsible for managing and providing the entire conversation history with each new request.
This might seem like extra work, but offers developers more control over the conversation flow and allows for greater flexibility in designing conversational agents.
The core technique for creating multi-turn conversations with the DeepSeek API lies in concatenating the conversation history. With each new turn, you'll append previous user inputs and model outputs to the message list sent to the API.
The following steps outline the process:
response.choices[0].message
) to the end of the message list. This preserves the context of the first turn.Let's illustrate this with a Python code example:
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.deepseek.com")
# Round 1
messages = [{"role": "user", "content": "What's the highest mountain in the world?"}]
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages
)
messages.append(response.choices[0].message)
print(f"Messages Round 1: {messages}")
# Round 2
messages.append({"role": "user", "content": "What is the second?"})
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages
)
messages.append(response.choices[0].message)
print(f"Messages Round 2: {messages}")
In the first round, the API receives only the initial question. In the second round, the API receives the original question, the model's answer from round one, and the new question. This process keeps repeating. Be mindful of the token limits of the DeepSeek models.
Let's break down the message structure in each round.
Round 1:
[
{"role": "user", "content": "What's the highest mountain in the world?"}
]
Round 2:
[
{"role": "user", "content": "What's the highest mountain in the world?"},
{"role": "assistant", "content": "The highest mountain in the world is Mount Everest."},
{"role": "user", "content": "What is the second?"}
]
As you can see, the second round's message list includes the entire history: the initial user question, the model's response, and the subsequent user question. This allows the DeepSeek model to understand the context and provide more relevant and coherent answers.
Mastering multi-turn conversations with the DeepSeek /chat/completions API involves effectively managing and concatenating the conversation context. By understanding the stateless nature of the API and implementing the techniques described in this guide, you can build powerful and engaging conversational AI applications. Remember to consult the official DeepSeek API Docs for the most up-to-date information and best practices.