Help Center/ MaaS/ Model Calling/ Text Generation/ Text Generation Overview
Updated on 2026-05-30 GMT+08:00

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 tutorial will guide you on how to use the model service API to call the model for text understanding and content generation. You can also build or extend your own applications or automate tasks using this API, thereby enhancing development efficiency and application quality.

Application Scenarios

You can use the model's text generation capabilities in the following scenarios.

Table 1 Application 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 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 Text Generation Capability row.

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.

Getting Started

Run the code below to begin using the deepseek-v3.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 address
    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": "deepseek-v3.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 an AI assistant created by DeepSeek. It's great to meet you!
Let me introduce myself briefly:
My capabilities include:
·A broad knowledge base (knowledge up to July 2024)
·A text-based dialogue model, skilled in various textual interactions and analysis
 Support for file uploads for image, TXT, PDF, PPT, Word, and Excel formats to extract text.
·Support for Internet search (You must manually enable the Internet search button on the web or app.)
My features include:
· A context length of 128K, enabling handling of long conversations
· A warm and detailed response style, aiming to provide a comfortable communication experience
· Available for download via official app stores
Although I cannot:
· Engage in voice conversations
· Generate images or videos
· Process real-time multimodal information
I will do my best to assist you through text! Whether it's questions about learning, work, life, or just wanting to chat, I'm here to accompany you.
Is there anything specific I can help you with?
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $MAAS_API_KEY" \
  -d '{
    "model": "deepseek-v3.2",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello"}
    ]
  }'

Model response preview:

Hello! I am an AI assistant created by DeepSeek. It's great to meet you!
Let me introduce myself briefly:
My capabilities include:
·A broad knowledge base (knowledge up to July 2024)
·A text-based dialogue model, skilled in various textual interactions and analysis
 Support for file uploads for image, TXT, PDF, PPT, Word, and Excel formats to extract text.
· Support for Internet search (You must manually enable the Internet search button on the web or app.)
My features include:
· A context length of 128K, enabling handling of long conversations
· A warm and detailed response style, aiming to provide a comfortable communication experience
· Available for download via official app stores
Although I cannot:
· Engage in voice conversations
· Generate images or videos
· Process real-time multimodal information
I will do my best to assist you through text! Whether it's questions about learning, work, life, or just wanting to chat, I'm here to accompany you.
Is there anything specific I can help you with?

JDK 15 or later 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 = "deepseek-v3.2";
        // Construct a request body.
        String requestBody = String.format(
            """
                {
                    "model": "%s",
                    "messages": [
                        {"role": "system", "content": "You are a helpful assistant."},
                        {"role": "user", "content": "Hello, please 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 an AI assistant created by DeepSeek. It's great to meet you!
Let me introduce myself briefly:
My capabilities include:
·A broad knowledge base (knowledge up to July 2024)
·A text-based dialogue model, skilled in various textual interactions and analysis
 Support for file uploads for image, TXT, PDF, PPT, Word, and Excel formats to extract text.
·Support for Internet search (You must manually enable the Internet search button on the web or app.)
My features include:
· A context length of 128K, enabling handling of long conversations
· A warm and detailed response style, aiming to provide a comfortable communication experience
· Available for download via official app stores
Although I cannot:
· Engage in voice conversations
· Generate images or videos
· Process real-time multimodal information
I will do my best to assist you through text! Whether it's questions about learning, work, life, or just wanting to chat, I'm here to accompany you.
Is there anything specific I can help you with?

Writing Prompts

Prompt is the text input by users when interacting with LLMs to guide the model in generating the desired response. Properly designed and written prompts can enhance the quality and accuracy of the model's output. The core functions of prompts:

  • Define the task: Instruct the model on what to do (for example: translation, summarization, creation, inference, etc.).
  • Provide context: Add background information to make the response more relevant and tailored to the request.
  • Constraint output format: Specify the structure of the response (such as list, JSON, code, etc.).
  • Control style and tone: Adjust the professionalism, conciseness, or creativity of the response.

In the Chat API, the messages object is used to pass prompt information 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 provided role and generates an appropriate response.

  • User Message

    The end user sends messages to the model, at which point the role field should be set to user. This type of message typically contains specific tasks or information that the user wants the model to process.

    The following is a simple user message requesting the model to introduce itself.

    "messages": [
        { "role": "user", "content": "Introduce yourself. "}
    ]
  • System Message

    Used to set the model's long-term personality, behavioral guidelines, and conversation context. In this case, the role field should be 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 Message

    Used to provide conversation history, in which case the role field should be set to assistant. In multi-turn conversations, you need to pass in 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" }
    ]

Single-Turn Dialogue

Interact with the model in a single turn of dialogue, where the model returns content based on system and user messages.

Since it is non-streaming output, the model needs to process all content before returning it to you in one go, which may introduce some latency.

The following is an example code for a single-turn conversation. You can replace the model by modifying the model parameter. For details about the model parameter, see Text Generation.

import requests
import json
if __name__ == '__main__':
    url = ""https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions"  # API address
    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": "deepseek-v3.2",  # Model
         "messages": [
             {
                 "role": "system",
                 "content": "You are a helpful assistant."
             },
             {
                 "role": "user",
                 "content": "Hello"
             }
         ]
     }
    response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
    # Print result.
    print(response.status_code)
    print(response.text)

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": "deepseek-v3.2",     
    "messages": [       
        {"role": "system", "content": "You are a helpful assistant."},     
        {"role": "user", "content": "Hello"}
    ]
}'

JDK 15 or later 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 = "deepseek-v3.2";
        // Construct a request body.
        String requestBody = String.format(
            """
               {
                  "model": "%s",
                  "messages": [
                      {"role": "system", "content": "You are a helpful assistant."},
                      {"role": "user", "content": "Hello"}
                   ]
               }""", 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();
        }
    }
}

Multi-Turn Dialogue

Combining system messages, model messages, and user messages enables multi-turn conversations, allowing for multiple dialogues on a single topic.

Note: chat.completions API is stateless. In each request, all historical information is included in the messages field and the role field is set to inform the model of previous conversations from different roles, ensuring contextually relevant and continuous dialogue.

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.

import requests
import json
if __name__ == '__main__':
    url = "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions"  # API address
    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": "deepseek-v3.2",  # Model
         "messages": [
             {"role": "system", "content": "You are a helpful assistant."},
             {"role": "user", "content": "Hello"}
             {"role": "assistant", "content": "Hello! Nice to meet you!"},
        {"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)

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": "deepseek-v3.2",     
    "messages": [       
        {"role": "system", "content": "You are a helpful assistant."},     
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hello! Nice to meet you!"},     
        {"role": "user", "content": "Introduce yourself."}
    ]
}'

JDK 15 or later 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 = "deepseek-v3.2";
        // Construct a request body.
        String requestBody = String.format(
                """
                        {
                            "model": "%s",
                            "messages": [
                                {"role": "system", "content": "You are a helpful assistant."},
                                {"role": "user", "content": "Hello"},
                                {"role": "assistant", "content": "Hello! Nice to meet you!"},
                                {"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();
        }
    }
}  

Streaming Output

Large language models return text incrementally by streaming tokens to the client as they are computed, without waiting for the whole sequence to be generated. This step-by-step delivery reduces wait times, optimizes user experience, and improves resource utilization.

The following is an example of stream output code. You can replace the model by modifying the model parameter. For details about the model parameter, see Text Generation.

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": "deepseek-v3.2",     
    "messages": [       
        {"role": "system", "content": "You are a helpful assistant."},     
        {"role": "user", "content": "Hello"}
    ],
    "thinking": {
        "type": "enabled"
    },
    "stream": true
}'
from openai import OpenAI
import httpx

base_url = "https://api-ap-southeast-1.modelarts-maas.com/openai/v1"  # API address
api_key = "MAAS_API_KEY"  # Replace MAAS_API_KEY with the obtained API key.

client = OpenAI(api_key=api_key, base_url=base_url, http_client=httpx.Client(verify=False))
response = client.chat.completions.create(
    model="deepseek-v3.2",  # The following uses deepseek-v3.2 as an example. You can change the model parameter as needed. For details, see Text Generation.
    messages=[
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "Introduce yourself."},
    ],
    extra_body={
         "thinking": {
            "type": "enabled" # Enable deep thinking mode
         }
    },
    stream=True
)
for chunk in response:
    if not chunk.choices:
        continue

    print(chunk.choices[0].delta.content, end="")

Function Calling

Function calling enables models to dynamically call external APIs by defining tool interface specifications, facilitating seamless function expansion.

For models that support function calling, see Text Generation. For more information about function calling, see Function Calling.

Setting the Model Response Length Limit

To control costs or response time, you can set the max_tokens field in your request to explicitly limit the maximum number of tokens generated by the model, thereby controlling the length of the response.

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 address
    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": "deepseek-v3.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": "deepseek-v3.2",     
    "messages": [       
        {"role": "system", "content": "You are a helpful assistant."},     
        {"role": "user", "content": "Hello"}
    ],
    "max_tokens": 1024
}'

JDK 15 or later 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 = "deepseek-v3.2";
        // Construct a request body.
        String requestBody = String.format(
                """
                        {
                            "model": "%s",
                            "messages": [
                                {"role": "system", "content": "You are a helpful assistant."},
                                {"role": "user", "content": "Hello"},
                                {"role": "assistant", "content": "Hello! Nice to meet you!"},
                                {"role": "user", "content": "Introduce yourself."}
                            ],
                            "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();
        }
    }
}  

Best Practices: Creating a Multi-Turn Dialogue

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-250528",
    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-250528",
    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 dialogue 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?"}
    ]

Helpful Links

The above section introduces the basic interaction methods. For more complex scenarios, refer to:

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.

For best practices on text generation with new models, see Overview of Model Calling Best Practices.