Updated on 2025-08-04 GMT+08:00

Creating a Multi-Turn Dialogue in ModelArts Studio (MaaS)

This chapter describes how to use the MaaS chat API to implement multi-turn dialogues.

The MaaS server does not save request details. You must include all conversation history each time you send a new request to the chat API. This example uses Python code. You can change the code as needed.

The following is an example of Python code for context concatenation and request:

from openai import OpenAI
client = OpenAI(api_key="MaaS API Key", base_url="https://xxxxxxxxxxxxxxxx")
# First turn of dialogue
messages = [{"role": "user", "content": "Which number is larger, 9.11 or 9.8?"}]
response = client.chat.completions.create(
    model="DeepSeek-R1",
    messages=messages
)
messages.append(response.choices[0].message)
print(f"Messages Round 1: {messages}")
# Second turn of dialogue
messages.append({"role": "user", "content": "What is the sum of them?"})
response = client.chat.completions.create(
    model="DeepSeek-R1",
    messages=messages
)
messages.append(response.choices[0].message)
print(f"Messages Round 2: {messages}")

The messages in the request body for the first turn of the dialog are as follows:

[
    {"role": "user", "content": "Which number is larger, 9.11 or 9.8?"}
]

The steps for constructing messages in the request body in the second turn of the dialogue are as follows:

  1. Add the output content of the model (role is assistant) in the first turn of the dialogue to the end of messages.
  2. Add the new user question to the end of messages.
  3. The messages in the request body finally transferred to the chat API are as follows:
    [
        {"role": "user", "content": "Which number is larger, 9.11 or 9.8?"},
        {"role": "assistant", "content": "9.8 is larger."},
        {"role": "user", "content": "What is the sum of them?"}
    ]