Text Generation Overview
In the process of developing AI applications, developers often need to handle tasks related to text understanding and generation. However, traditional text processing methods are typically inefficient and yield suboptimal results. LLMs possess capabilities for text understanding and text-based dialogue. For example, when you input text information, the LLM can understand the content and generate appropriate responses based on that information. Nevertheless, efficiently leveraging these LLMs remains a challenge.
This document describes the application scenarios of MaaS text models and how to configure functions such as multi-turn dialogue, streaming output, deep thinking, prefix completion, and tool calling in model APIs. These functions are designed to meet diverse service requirements and improve your work efficiency.
Concepts
In the Chat API, the messages object is used to provide the prompt to the model. The role field defines the role of the message sender, while the content field carries the message content. The model interprets the content based on the role and prompt and generates an appropriate response. There are three types of messages that can be input into the model:
- User messages
Users send messages with the role field set to user. These messages typically contain specific tasks or information for the model to process.
The following is a simple user message requesting the model to introduce itself.
"messages": [ { "role": "user", "content": "Introduce yourself. "} ] - System messages
System messages define the model's long-term personality, behavioral rules, and conversation context. Their role field is set to system. If you configure system messages, place them in the first position of the messages list.
The following is an example of a system message that indicates the model is an assistant and restricts the content of its responses.
"messages": [ {"role": "system", "content": " You are a helpful and concise assistant. Keep your responses to no more than two sentences." } ] - Model messages
Model messages with the role field set to assistant provide the conversation history. In multi-turn conversations, you need to provide the conversation history, so that the model can remember what it said before and maintain coherence.
"messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello"}, {"role": "assistant", "content": " Hello! Nice to meet you! I am your AI assistant and I'm here to help you with anything you need. Whether it's answering questions, assisting with analysis, or just having a chat, I'm ready to assist. Is there anything specific you need help with?"}, {"role": "user","content": "Introduce yourself" } ]
Application Scenarios
You can use the model's text generation capabilities in the following scenarios.
| Scenario | Use Case | Description |
|---|---|---|
| Content creation | Article generation | Automatically generates practical texts such as articles, news, and comments to improve content production efficiency. |
| Text polishing | Aids authors in creative brainstorming and text refinement for news reports and blog posts. | |
| Intelligent interaction | Intelligent customer service | Generates natural and smooth responses in the customer service system to enhance user experience. |
| Chatbot | In fields such as online consultation and English learning, understand user intent, generate responses according to requirements. | |
| Personalized teaching | Subject question answering | Analyzes the question, explain the key points, outline the solution approach, and present the results. |
| Language learning | As required, engages in conversations in certain languages to help users become accustomed to daily communication in the target language. | |
| Machine translation | Automatic translation | Leverages speech models to achieve simultaneous interpretation, daily subtitle generation, and text language translation. |
| Work processing | Data processing | Process the data and tasks as required, such as reading research reports, analyzing news, and evaluating content. |
Billing
Input and output of text-based dialogues are converted into tokens for billing. For details, see MaaS Text Generation Models.
Supported Models
For the models currently supported for text generation, see Text Generation.
Suggestions on selecting a model:
- Select the latest model: If you are choosing a model for the first time, you are advised to select the latest version. This model offers significant improvements in text classification and content creation.
- Select a model with the desired capabilities: You can view the supported capabilities of each model in the Capability row (Text Generation).
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
Run the code below to begin using the GLM-5.2 model for creating content and generating summaries.
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": "Introduce yourself."}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
# Print result.
print(response.status_code)
print(response.text) Model response preview:
Hello! I am a large language model developed by Z.ai. I am designed to answer questions, provide information, and assist with a wide range of tasks. I can help explain complex concepts, generate text, translate languages, and create original content. My knowledge is based on extensive public data, but please note that I do not have access to real-time updates or personal information. How may I assist you today?
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": "Introduce yourself."}
]
}' Model response preview:
Hello! I am a large language model developed by Z.ai. I am designed to answer questions, provide information, and assist with a wide range of tasks. I can help explain complex concepts, generate text, translate languages, and create original content. My knowledge is based on extensive public data, but please note that I do not have access to real-time updates or personal information. How may I assist you today?
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 name of the model you want to call.
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": "Introduce yourself."}
]
}""", 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();
}
}
} Model response preview:
Hello! I am a large language model developed by Z.ai. I am designed to answer questions, provide information, and assist with a wide range of tasks. I can help explain complex concepts, generate text, translate languages, and create original content. My knowledge is based on extensive public data, but please note that I do not have access to real-time updates or personal information. How may I assist you today?
Helpful Links
The above section introduces the basic interaction methods. For more complex scenarios, refer to:
Multi-Turn Dialogue: Suitable for interactive scenarios such as intelligent customer service, conversational Q&A, and role-playing.
Streaming Output: Suitable for scenarios such as long-text generation, code generation, and real-time translation. It reduces TTFT, allowing users to see responses faster and enjoy a smoother interaction experience.
Deep Thinking: Suitable for complex reasoning and strategic analysis scenarios that require high-quality, well-structured in-depth responses.
Prefix Completion: When you provide a prefix (the beginning part) of a text, the LLM can generate coherent and reasonable subsequent content according to your requirements.
Function Calling: This key capability links large models with external tools and APIs. It intelligently translates users' natural language requests into precise tool or API calls, enabling tasks to be completed efficiently to meet users' requirements.
Properly designing and writing prompts can improve the quality and accuracy of the content generated by the model. For details, see Prompt Engineering Practices.
For best practices on text generation with new models, see Overview of Model Calling Best Practices.
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