Skip to main content
Text generation

Overview

A text generation model generates text from natural language prompts for applications such as chatbots, content creation, document summarization, and code generation.

Input can range from a single keyword to complex, multi-step prompts with context. Common use cases include:
  • Content creation: Generate news articles, product descriptions, and short-form video scripts.
  • Customer service: Build 24/7 automated chatbots to answer frequently asked questions.
  • Text translation: Translate text between multiple languages.
  • Summarization: Summarize long articles, reports, and emails.
  • Legal document drafting: Draft contract templates and legal opinions.

Key concepts

The input to a text generation model is a prompt, which consists of one or more message objects each containing a role and content:
  • System message: Sets the model's persona, behavior guidelines, or task-specific instructions. Defaults to "You are a helpful assistant."
  • User message: The user's question, instruction, or input to the model.
  • Assistant message: The model's response. In a multi-turn conversation, pass historical assistant messages to maintain context.
To call the model, construct an array of these message objects named messages. A typical request consists of a system message that defines behavior guidelines and a user message with the user's input.
The system message is optional but recommended. Defining the model's role and behavioral constraints produces more consistent and predictable output.
[
    {"role": "system", "content": "You are a helpful assistant who provides precise, efficient, and insightful responses, ready to assist users with various tasks and questions."},
    {"role": "user", "content": "Who are you?"}
]
The response contains the model's reply in an assistant message.
{
    "role": "assistant",
    "content": "Hello! I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you with tasks like answering questions, creating text, logical reasoning, and coding. I understand and generate multiple languages, and can handle multi-turn conversations and complex instructions. If there is anything you need help with, just let me know!"
}

Quick start

Prerequisites: Get an API key and Configure API key as an environment variable. If using an SDK, also install the OpenAI or DashScope SDK. The {WorkspaceId} in the example base URLs is your workspace ID. For how to obtain it, see Regions and access domains.
  • OpenAI-compatible Chat Completions API
  • OpenAI-Compatible Responses API
  • DashScope
  • Python
  • Java
  • Node.js
  • Go
  • C# (HTTP)
  • PHP (HTTP)
  • curl
import os
from openai import OpenAI

try:
    client = OpenAI(
        # API keys vary by region. To obtain an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
        # If you haven't set the environment variable, replace the following line with your Alibaba Cloud Model Studio API key: api_key="sk-xxx",
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # Endpoint for the Asia Pacific SE 1 (Singapore) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
        base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
    )

    completion = client.chat.completions.create(
        model="qwen3.8-max",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Who are you?"},
        ],
    )
    print(completion.choices[0].message.content)
    # To view the full response, uncomment the following line.
    # print(completion.model_dump_json())
except Exception as e:
    print(f"Error message: {e}")
    print("For more information, see the documentation: https://www.alibabacloud.com/help/en/model-studio/error-code")

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

Image and video data processing

Multimodal models process non-text data (images, videos) for tasks like visual question answering and event detection. They differ from text-only models in two ways:
  • User message construction: Multimodal user messages include text and non-text data such as images and audio.
  • DashScope SDK interfaces: Use the MultiModalConversation interface for the DashScope Python SDK, and the MultiModalConversation class for the DashScope Java SDK.
For limitations on image and video files, see Image and video understanding.
  • OpenAI compatible chat completions
  • DashScope
  • Python
  • Node.js
  • curl
from openai import OpenAI
import os

client = OpenAI(
    # API keys vary by region. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If the environment variable is not set, provide your Model Studio API key directly, for example: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # This is the endpoint for the Singapore region. Replace {WorkspaceId} with your WorkspaceId. Endpoints vary by region.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image_url",
                "image_url": {
                    "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png"
                },
            },
            {"type": "text", "text": "What products are shown in the image?"},
        ],
    }
]
completion = client.chat.completions.create(
    model="qwen3.6-plus",
    messages=messages,
)
print(completion.choices[0].message.content)

Asynchronous calls

Asynchronous calls improve throughput for high-concurrency workloads.
  • OpenAI-compatible chat completions API
  • DashScope
Python
import os
import asyncio
from openai import AsyncOpenAI
import platform

# Create an asynchronous client instance.
client = AsyncOpenAI(
    # API keys vary by region. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If you have not set the environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # This is the URL for the Singapore region. Replace {WorkspaceId} with your workspace ID.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)

# Define an asynchronous task.
async def task(question):
    print(f"Sending question: {question}")
    response = await client.chat.completions.create(
        messages=[
            {"role": "user", "content": question}
        ],
        model="qwen-plus",  # For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
    )
    print(f"Model response: {response.choices[0].message.content}")

# Main asynchronous function.
async def main():
    questions = ["Who are you?", "What can you do?", "What's the weather like?"]
    tasks = [task(q) for q in questions]
    await asyncio.gather(*tasks)

if __name__ == '__main__':
    # Set the event loop policy.
    if platform.system() == 'Windows':
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    # Run the main coroutine.
    asyncio.run(main(), debug=False)

Sample response
Because the calls are asynchronous, the order of the responses may differ from this example.
Sending question: Who are you?
Sending question: What can you do?
Sending question: What's the weather like?
Model response: Hello! I'm Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, programming, share opinions, play games, and more. If you have any questions or need help, feel free to ask!
Model response: Hello! I am currently unable to access real-time weather information. You can tell me your city or region, and I will do my best to provide you with general weather advice or information. Alternatively, you can use a weather app to check the real-time weather conditions.
Model response: I have many skills, for example:

1. Answering questions: Whether it's academic questions, general knowledge, or professional topics, I can try to help you find answers.
2. Creating text: I can write various types of text, such as stories, official documents, emails, and scripts.
3. Logical reasoning: I can help you solve logical reasoning problems, such as math problems and riddles.
4. Programming: I can provide programming assistance, including code writing, debugging, and optimization.
5. Multilingual support: I support multiple languages, including but not limited to Chinese, English, French, and Spanish.
6. Expressing opinions: I can offer you some perspectives and suggestions to help you make decisions.
7. Playing games: We can play text-based games together, such as riddles or idiom solitaire.

If you have any specific needs or questions, feel free to let me know, and I will do my best to help you!

Production use

Building high-quality context

Feeding large amounts of raw data to a model increases costs and can degrade performance due to context window limitations. Context engineering -- dynamically loading precise knowledge -- improves generation quality and efficiency. Key techniques include:
  • prompt engineering: Design and optimize text prompts to guide the model toward the desired output. For more information, see the Prompt guide for text generation page.
  • Retrieval-Augmented Generation (RAG): Lets the model answer questions from an external knowledge base such as product documentation or technical manuals.
  • tool calling: Retrieves real-time information (weather, traffic) or performs actions (API calls, sending emails) on behalf of the model.
  • memory: Provides long-term and short-term memory so the model can recall context across multi-turn conversations.

Controlling response diversity

The temperature and top_p parameters control the diversity of the generated text. Higher values increase diversity; lower values increase determinism. To isolate the effect of each parameter, adjust only one at a time.
  • temperature: Range: [0, 2). Primarily adjusts randomness.
  • top_p: Range: [0, 1]. Filters responses based on a probability threshold.
The following examples show how parameter settings affect output. Input prompt: "Write a three-sentence short story where the main characters are a cat and a sunbeam."
  • High diversity (Example: temperature=0.9): Best for creative writing, brainstorming, or marketing copy.
Sunlight slanted across the windowsill, and the orange cat crept toward the bright patch as its fur turned the color of melted honey.
It reached out and tapped the light, then sank into it as if stepping into a warm pool, and the sunlight flowed up its back in a quiet tide.
The afternoon grew heavy—curled in drifting gold, the cat heard time melt softly inside its purr.
  • High determinism (Example: temperature=0.1): Best for fact-based question answering, code generation, or legal texts.
In the afternoon, an old cat curled on the windowsill and dozed while counting the spots of light.
Sunlight hopped across its mottled back, like turning the pages of an old photo album.
Dust rose and fell, as if time whispered: you were once young, and I was once fierce.
temperature:
  • A higher temperature flattens the token probability distribution, making less likely tokens more probable and increasing output randomness.
  • A lower temperature sharpens the distribution, making high-probability tokens even more likely and reducing output randomness.
top_p:top_p (nucleus) sampling selects from the smallest set of tokens whose cumulative probability meets or exceeds the top_p threshold. Tokens are sorted by probability and accumulated until the threshold is met, then the next token is randomly sampled from this reduced set.
  • A higher top_p widens the token selection pool, producing more diverse text.
  • A lower top_p narrows the pool, producing more focused and deterministic text.
# Recommended parameter settings for common scenarios
SCENARIO_CONFIGS = {
    # Creative writing
    "creative_writing": {
        "temperature": 0.9,
        "top_p": 0.95
    },
    # Code generation
    "code_generation": {
        "temperature": 0.2,
        "top_p": 0.8
    },
    # Factual Q&A
    "factual_qa": {
        "temperature": 0.1,
        "top_p": 0.7
    },
    # Translation
    "translation": {
        "temperature": 0.3,
        "top_p": 0.8
    }
}

# OpenAI example
# completion = client.chat.completions.create(
#     model="qwen-plus",
#     messages=[{"role": "user", "content": "Write a poem about the moon"}],
#     **SCENARIO_CONFIGS["creative_writing"]
# )
# DashScope example
# response = Generation.call(
#     # If you have not set an environment variable, replace the following line with your Alibaba Cloud Model Studio API key: api_key = "sk-xxx",
#     api_key=os.getenv("DASHSCOPE_API_KEY"),
#     model="qwen-plus",
#     messages=[{"role": "user", "content": "Write a Python function that determines whether the input n is a prime number. Output code only."}],
#     result_format="message",
#     **SCENARIO_CONFIGS["code_generation"]
# )

More features

For more complex scenarios, the following features are available:
  • multi-turn conversation: For continuous interaction such as follow-up questions or information gathering.
  • streaming output: Returns tokens incrementally as they are generated, preventing timeouts for chatbots and real-time code generation.
  • deep thinking: Produces higher-quality, more structured answers for complex reasoning or strategic analysis.
  • structured output: Constrains responses to a consistent JSON format for programmatic use and data parsing.
  • prefix completion: Continues generation from existing text, useful for code completion or long-form writing.

API reference

For all parameters, see the OpenAI-compatible API reference and the DashScope API reference.

FAQ

Q: Why is the input token count higher than the token count of the text I sent?

A: When processing a conversation, the system uses a Chat Template to wrap the raw input text, adding control markers such as role identifiers and message boundaries. These system-generated markers are also counted as tokens. For example, when you send the message {"role": "user", "content": "Hi"} to qwen3.8-max, the text "Hi" corresponds to only 1 token after tokenization. However, during system processing, the actual full input text is formatted as follows: <|im_start|>user\nHi<|im_end|>\n<|im_start|>assistant\n<think>. After tokenization, this full text increases the total input token count to 11. A: The Qianwen API cannot access web page content directly. Instead, use function calling, or a web scraping tool like Python's Beautiful Soup to extract the content and pass it to the model.

Q: Response differences:Qianwen (Web)vs. Qianwen API

A: Qianwen (Web) adds features on top of the Qianwen API, including webpage parsing, web search, image creation, and PPT generation. These are not included in the base API, but you can build similar functionality using , function calling.

Q: Generating Word, Excel, PDF, or PPT files

A: No. Text generation models only output plain text. Convert the output to the desired format using your own code or third-party libraries.
Token Plan
Model Playground
Statistics and Monitoring
Support