Text generation models generate coherent, contextually relevant text based on natural language prompts, supporting scenarios such as chatbots, content creation, document summarization, and code generation.
The input to a text generation model can be simple keywords, a one-sentence summary, or more complex multi-step instructions and contextual information. Common application scenarios:
The input to a text generation model is a prompt, which consists of one or more message objects. Each message is composed of a role and content, specifically:
The response object in the output contains the
Prerequisites for using the API: You have obtained and configured an API Key and completed configuring the API Key in environment variables. If you call the model through an SDK, you need to install the OpenAI or DashScope SDK. In the example endpoint,
Calling asynchronous interfaces can effectively improve the processing efficiency of high-concurrency requests.
Response
Directly feeding a large amount of raw data into an LLM will lead to increased costs and degraded results due to the context capacity limit. Context Engineering dynamically loads precise knowledge to significantly improve generation quality and efficiency. Core techniques include:
The preceding sections introduced basic interaction methods. For more complex scenarios, see:
For the complete list of parameters for model calls, see the OpenAI-compatible API reference and the DashScope API reference.
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-added markers are also counted as Tokens.
For example, when you send the message
A: The Qwen API itself cannot directly access web page links. You can use the Function Calling feature, or combine it with a web scraping tool such as Python's Beautiful Soup to extract web page content and pass it to the model.
A: Qwen on the web applies additional engineering optimization on top of the Qwen API, which enables features such as parsing web pages, web search, image generation, and creating PPT slides. These capabilities are not inherent to the LLM API, and you can use the Function Calling feature to improve the model's performance.
A: No. The text generation models of Alibaba Cloud Model Studio output plain text only. You need to use code or a third-party library to convert the text to the desired format.
- Content creation: Generate news articles, product descriptions, and short video scripts.
- Customer service: Build chatbots that automatically respond around the clock and answer frequently asked questions.
- Text translation: Support fast and accurate translation between multiple languages.
- Summarization: Extract key information from long documents, reports, and emails.
- Legal document drafting: Generate basic frameworks for contract templates and legal opinions.
Core concepts
The input to a text generation model is a prompt, which consists of one or more message objects. Each message is composed of a role and content, specifically:
- System message: Sets the model's role, behavioral guidelines, or specific task instructions. If not specified, the default is "You are a helpful assistant".
- User message: The questions, instructions, or input content that the user sends to the model.
- Assistant message: The model's replies. In multi-turn conversations, historical assistant messages are passed in to maintain context.
messages array consisting of the message objects described above. A typical request usually consists of a system message that defines behavioral guidelines and a user message submitted by the user.
The system message is optional but recommended. Clearly defining the model's role and behavioral constraints helps obtain more consistent and predictable output.
assistant message replied by the model.
QuickStart
Prerequisites for using the API: You have obtained and configured an API Key and completed configuring the API Key in environment variables. If you call the model through an SDK, you need to install the OpenAI or DashScope SDK. In the example endpoint, {WorkspaceId} is the workspace ID. For how to obtain it, see Region, service deployment scope, and endpoint.
- OpenAI-compatible Chat Completions API
- OpenAI-compatible Responses API
- DashScope API
- Python
- Java
- Node.js
- Go
- C#(HTTP)
- PHP(HTTP)
- curl
- dashscope CLI
Response
Call models asynchronously
Calling asynchronous interfaces can effectively improve the processing efficiency of high-concurrency requests.
- OpenAI-compatible Chat Completions API
- DashScope
Python
Since the calls are asynchronous, the responses may be returned in a different order than in the examples.
Going live
Build high-quality context
Directly feeding a large amount of raw data into an LLM will lead to increased costs and degraded results due to the context capacity limit. Context Engineering dynamically loads precise knowledge to significantly improve generation quality and efficiency. Core techniques include:
- Prompt engineering: By designing and optimizing text instructions (prompts), you can guide the model more precisely so that its output better matches your expectations. To learn more, see the Prompt Engineering Guide page.
- Retrieval augmented generation (RAG): Suitable for scenarios where the model needs to answer questions based on an external knowledge base (such as product documentation or technical manuals).
- Tool calling (Tool): Allows the model to obtain real-time information (such as weather or traffic queries) or perform specific operations (such as calling an API or sending an email).
- Memory: Establishes long-term and short-term memory for the model, enabling it to understand the history of continuous conversations.
Control response diversity
temperature and top_p are used to control the diversity of generated text. The higher the values, the more diverse the content; the lower the values, the more deterministic the content. To accurately evaluate the effect of a parameter, we recommend adjusting only one at a time.
- temperature: Range [0, 2). Focuses on adjusting randomness.
- top_p: Range (0, 1]. Filters responses by a probability threshold.
- High diversity (example
temperature=0.9): Suitable for scenarios that require creativity, imagination, and novel expression, such as creative writing, brainstorming, or marketing copy.
- High determinism (example
temperature=0.1): Suitable for scenarios that require accurate, rigorous, and predictable content, such as factual Q&A, code generation, or legal texts.
How it works
How it works
temperature:
- The higher the temperature, the flatter the Token probability distribution becomes (that is, the probability of high-probability Tokens decreases while the probability of low-probability Tokens increases), making the model more random when selecting the next Token.
- The lower the temperature, the steeper the Token probability distribution becomes (that is, high-probability Tokens are more likely to be selected while low-probability Tokens become less likely), making the model tend to choose a small number of high-probability Tokens.
- The higher the top_p, the more Tokens are considered, so the generated text is more diverse.
- The lower the top_p, the fewer Tokens are considered, so the generated text is more focused and deterministic.
More features
The preceding sections introduced basic interaction methods. For more complex scenarios, see:
- Multi-turn conversations: Suitable for scenarios that require continuous interaction, such as follow-up questions or information gathering.
- Streaming output: Suitable for scenarios that require immediate responses, such as chatbots or real-time code generation. It improves user experience and avoids timeouts caused by long response times.
- Deep thinking: Suitable for scenarios that require high-quality, well-organized in-depth answers, such as complex reasoning or strategy analysis.
- Structured output: Use this when you need the model to reply in a stable JSON format for program calls or data parsing.
- Partial mode: Suitable for scenarios where the model continues existing text, such as code completion or long-form writing.
API reference
For the complete list of parameters for model calls, 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-added markers are also counted as Tokens.
For example, when you send the message {"role": "user", "content": "Hello"} to qwen3.8-max, "Hello" corresponds to only 1 Token after tokenization, but when the system processes it, the actual full input text is <|im_start|>user\nHello<|im_end|>\n<|im_start|>assistant\n<think>, and the total Token count increases to 11 after tokenization.