Multi-Turn Dialogue
The chat.completions API is stateless and does not store the conversation history. To enable multi-turn dialogue, the model must include the conversation history in the messages parameter for each request. The role field specifies the type of each message (system, user, or assistant), allowing the model to interpret context and continue the conversation seamlessly on the same topic.
| Single-Turn Dialogue | Multi-Turn Dialogue |
|---|---|
"messages":
[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Which number is larger, 9.11 or 9.8?"}
] | "messages":
[
{"role": "system", "content": "You are a helpful assistant."},
{"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?"}
] |
API Description
For the complete parameters for model calls, see Sending a Chat Request (Chat/Post).
Prerequisites
- You have subscribed to the built-in service on the Model Inference > Real-Time Inference > Built-in Services tab page. For details, see Subscribing to a Built-in Service.
- (Optional) To control the service call traffic, you can create a custom endpoint in advance. For details, see Creating an Endpoint.
- The API key has been obtained. For details, see Managing API Keys in MaaS.
- The model parameter value for the model service has been obtained. For details about the supported models and APIs, see Sending a Chat Request (Chat/Post).
Getting Started
The following is an example code for a multi-turn conversation. You can replace the model by modifying the model parameter. For details about the model parameter, see Text Generation.
In thinking mode, reasoning_content is the content that the model is thinking about and should not be used as input in multi-turn conversations. If this field is included in the messages sequence, it will be ignored and not used as model input. During a multi-turn conversation, only the role and content fields returned by the model in the previous turn should be retained.
import requests
import json
if __name__ == '__main__':
url = "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions" # API URL
api_key = "MAAS_API_KEY" # Replace MAAS_API_KEY with the obtained API key.
# Send request.
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
data = {
"model": "glm-5.2", # Model
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"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?"}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
# Print result.
print(response.status_code)
print(response.text) The sum of them is 18.91.
curl -X POST "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MAAS_API_KEY" \
-d '{
"model": "glm-5.2",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"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?"}
]
}' The sum of them is 18.91.
JDK later than 15 is recommended.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class ChatCompletionsExample {
public static void main(String[] args) {
// API URI
String apiUrl = "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions";
// Replace MAAS_API_KEY with the obtained API key.
String apiKey = "MAAS_API_KEY";
// Replace it with the model you want to call, for example, "deepseek-v3.2".
String modelName = "glm-5.2";
// Construct a request body.
String requestBody = String.format(
"""
{
"model": "%s",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"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?"}
]
}""", modelName);
// Create an HttpClient.
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
// Create a request.
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
try {
// Send the request and print the result.
HttpResponse<String> response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println("HTTP Status: " + response.statusCode());
System.out.println("Response Body:\n" + response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
} The sum of them is 18.91.
Setting the Model Response Length Limit
In multi-turn dialog scenarios, the number of tokens consumed per call increases as the conversation length grows, which raises usage costs.
To manage this, you can set the max_tokens field in the request to define the maximum number of tokens the model may generate, thereby limiting the length of its response. For details about the value of max_tokens for each model, see Maximum Output Length on the model details page.
The following is an example code for setting the model response length limit. You can replace the model parameter with your desired model. For details about the model parameter, see Text Generation.
import requests
import json
if __name__ == '__main__':
url = # API URL
api_key = "MAAS_API_KEY" # Replace MAAS_API_KEY with the obtained API key.
# Send request.
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
data = {
"model": "glm-5.2", # Model
"messages": [
{"role": "system", "content": "You are a helpful assistant." },
{"role": "user", "content": "Hello"}
],
"max_tokens": 1024
}
response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
# Print result.
print(response.status_code)
print(response.text) curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MAAS_API_KEY" \
-d '{
"model": "glm-5.2",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello"}
],
"max_tokens": 1024
}' JDK later than 15 is recommended.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class ChatCompletionsExample {
public static void main(String[] args) {
// API URI
String apiUrl = "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions";
// Replace MAAS_API_KEY with the obtained API key.
String apiKey = "MAAS_API_KEY";
// Replace it with the model you want to call, for example, "deepseek-v3.2".
String modelName = "glm-5.2";
// Construct a request body.
String requestBody = String.format(
"""
{
"model": "%s",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello"}
],
"max_tokens": 1024
}""", modelName);
// Create an HttpClient.
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
// Create a request.
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
try {
// Send the request and print the result.
HttpResponse<String> response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println("HTTP Status: " + response.statusCode());
System.out.println("Response Body:\n" + response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
} Error Codes
If an error is reported during model calling, rectify the fault by referring to Error Codes.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot