DeepSeek API
πŸ‡ΊπŸ‡Έ English
  • πŸ‡ΊπŸ‡Έ English
  • πŸ‡―πŸ‡΅ Japanese
  • πŸ‡°πŸ‡· Korea
  • πŸ‡΅πŸ‡Ή Portuguese
  1. API Guides
DeepSeek API
πŸ‡ΊπŸ‡Έ English
  • πŸ‡ΊπŸ‡Έ English
  • πŸ‡―πŸ‡΅ Japanese
  • πŸ‡°πŸ‡· Korea
  • πŸ‡΅πŸ‡Ή Portuguese
  • Start quickly
    • Your First API Call
    • Models & Pricing
    • The Temperature Parameter
    • Token & Token Usage
    • Rate Limit
    • Error Codes
  • API Reference
    • Introduction
    • Create Chat Completion
      POST
    • Create FIM Completion (Beta)
      POST
    • Lists Models
      GET
    • Get User Balance
      GET
  • API Guides
    • Reasoning Model (deepseek-reasoner)
    • Multiple rounds of conversations
    • Chat Prefix Completion (Beta)
    • FIM Completion (Beta)
    • JSON Output
    • Function Calling
    • Context Caching
  • FAQ
    • FAQ
  1. API Guides

Multiple rounds of conversations

This guide will explain how to use the DeepSeek /chat/completions API for multiple rounds of conversations.
The DeepSeek /chat/completions API is a "stateless" API, that is, the server does not record the context of user requests. Each time the user requests, he needs to splice all previous conversation history and pass it to the conversation API.
The following code is in Python and shows how to perform context splicing to achieve multiple rounds of conversations.
from openai import OpenAI
client = OpenAI(api_key="<DeepSeek 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}")

During the first round of requests, messages passed to the API are:
[
    {"role": "user", "content": "What's the highest mountain in the world?"}
]
In the second round of request:
1.
To add the output of the model in the first round to the end of messages
2.
Add new questions to the end of messages
messages are finally passed to the API are:
[
    {"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?"}
]
Previous
Reasoning Model (deepseek-reasoner)
Next
Chat Prefix Completion (Beta)
Built with