Multiple rounds of conversations
/chat/completions
API for multiple rounds of conversations./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.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}")
messages
passed to the API are:[
{"role": "user", "content": "What's the highest mountain in the world?"}
]
1.
messages
2.
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?"}
]
Modified atΒ 2025-02-06 02:52:30