Text generation is an AI technology that uses deep learning algorithms to create logical and coherent text content based on given prompts.
The prompt required for text generation can be simple keywords, one-sentence summaries, or more complex instructions and contextual information. Text generation models analyze large amounts of existing data to learn language patterns and can be used in the following scenarios:
Content creation: Generate news reports, product descriptions, short video scripts, and others.
Customer service: Work as chatbots to provide 24-hour customer support and answer frequently asked questions.
Text translation: Quickly and accurately translate texts from one language to another.
Summary generation: Generate summaries for long articles, reports, and customer emails.
Legal document drafting: Generate contract templates and the basic framework of legal opinions.
Example: Extract key information from customer emails based on requirements
Prompt |
|
Output |
|
Text generation models
Model Studio supports Qwen commercial models and open source models. Text generation model list.
How to choose
Qwen-Max, Qwen-Plus, and Qwen-Turbo are all suitable for various scenarios such as customer service, content creation (such as writing articles and copywriting), text polishing, and summarization. If you have no specific needs, start with Qwen-Plus for its balance of effectiveness, speed, and cost.
Reasoning ability: Qwen-Max > Qwen-Plus > Qwen-Turbo
Response speed: Qwen-Turbo > Qwen-Plus > Qwen-Max
All three models are compatible with the OpenAI interface.
For summarization and analysis of long documents, you can try Qwen-Plus, which boasts a context length of up to 131,072 tokens.
You can also try and evaluate the models against specific tasks before making a decision. Quickly and intuitively compare model performance in Playground. You can select multiple text generation models and make a horizontal comparison of model capabilities based on the same input.
For more quantifiable information for comparison, see Model pricing details and Performance and rate limiting.
How to use
The text generation model takes a prompt as input and generates an output based on that prompt. Model Studio supports integration through OpenAI SDK, DashScope SDK, and HTTP interface.
Message types
When you interact with a LLM through API, the input and output are called messages. Each message belongs to one of the following roles: system, user, and assistant.
System message (also known as system prompt): Tells the model the role to play or the behavior to make. The default value is "You are a helpful assistant." You can also place such instructions in the user message, but placing them in the system message is more effective.
User message: The text you input to the model.
Assistant message: The response from the model. You can also preset an assistant message as an example for subsequent assistant messages.
Get started
Prerequisites:
You must have obtained an API Key and set the API key as an environment variable.
You have installed the SDK.
OpenAI
You can use the OpenAI SDK or OpenAI-compatible HTTP method to try Qwen models.
For a complete list of parameters, see OpenAI-compatible.
Python
Sample code
import os
from openai import OpenAI
try:
client = OpenAI(
# If environment variables are not configured, replace the following line with: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
# Model list: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
model="qwen-plus",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who are you?"},
],
)
print(completion.choices[0].message.content)
except Exception as e:
print(f"Error message: {e}")
print("Please refer to the documentation: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code")
Sample response
I am a large language model from Alibaba Cloud, my name is Qwen.
Java
Sample code
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ChatCompletion;
import com.openai.models.ChatCompletionCreateParams;
import java.util.List;
// OpenAI Java SDK is currently in beta stage, the SDK version used in this code example is 0.32.0
public class Main {
public static void main(String[] args) {
// Create OpenAI client, connect to Model Studio's compatible interface
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY")) // Or replace with .apiKey("sk-xxx")
.baseUrl("https://dashscope-intl.aliyuncs.com/compatible-mode/v1")
.build();
// Create ChatCompletion parameters
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.model("qwen-plus") // Specify model
.addSystemMessage("You are a helpful assistant.")
.addUserMessage("Who are you?")
.build();
// Send request and get response
ChatCompletion chatCompletion = client.chat().completions().create(params);
// Extract and print content field
List<ChatCompletion.Choice> choices = chatCompletion.choices();
if (!choices.isEmpty()) {
String content = choices.get(0).message().content().orElse("No response content");
System.out.println(content);
}
}
}
Node.js
Sample code
import OpenAI from "openai";
const openai = new OpenAI(
{
// If environment variables are not configured, replace the following line with: apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen-plus", //Model List: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Who are you?" }
],
});
console.log(JSON.stringify(completion))
Sample response
{
"choices": [
{
"message": {
"role": "assistant",
"content": "I am a large-scale language model from Alibaba Cloud, my name is Qwen."
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 22,
"completion_tokens": 17,
"total_tokens": 39
},
"created": 1728455191,
"system_fingerprint": null,
"model": "qwen-plus",
"id": "chatcmpl-3a8c00cc-9c9f-9aba-b2d9-dc431e27d1b5"
}
HTTP
Sample code
curl
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-plus",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Who are you?"
}
]
}'
PHP
<?php
// Set the request URL
$url = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions';
// If environment variables are not configured, replace the following line with: $apiKey = "sk-xxx";
$apiKey = getenv('DASHSCOPE_API_KEY');
// Set request headers
$headers = [
'Authorization: Bearer '.$apiKey,
'Content-Type: application/json'
];
// Set request body
$data = [
"model" => "qwen-plus",
"messages" => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who are you?"
]
]
];
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute cURL session
$response = curl_exec($ch);
// Check if any error occurred
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
// Close cURL resource
curl_close($ch);
// Output the response
echo $response;
?>
C#
using System.Net.Http.Headers;
using System.Text;
class Program
{
private static readonly HttpClient httpClient = new HttpClient();
static async Task Main(string[] args)
{
// If environment variables are not configured, replace the following line with: string? apiKey = "sk-xxx";
string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("API Key not set. Please ensure the environment variable 'DASHSCOPE_API_KEY' is set.");
return;
}
// Set request URL and content
string url = "https://dashscope.aliyuncs-intl.com/compatible-mode/v1/chat/completions";
// Model list: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
string jsonContent = @"{
""model"": ""qwen-plus"",
""messages"": [
{
""role"": ""system"",
""content"": ""You are a helpful assistant.""
},
{
""role"": ""user"",
""content"": ""Who are you?""
}
]
}";
// Send request and get response
string result = await SendPostRequestAsync(url, jsonContent, apiKey);
// Output result
Console.WriteLine(result);
}
private static async Task<string> SendPostRequestAsync(string url, string jsonContent, string apiKey)
{
using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
{
// Set request headers
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Send request and get response
HttpResponseMessage response = await httpClient.PostAsync(url, content);
// Handle response
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
return $"Request failed: {response.StatusCode}";
}
}
}
}
Go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type RequestBody struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
}
func main() {
// Create HTTP client
client := &http.Client{}
// Build request body
requestBody := RequestBody{
// Model list: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
Model: "qwen-plus",
Messages: []Message{
{
Role: "system",
Content: "You are a helpful assistant.",
},
{
Role: "user",
Content: "Who are you?",
},
},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
log.Fatal(err)
}
// Create POST request
req, err := http.NewRequest("POST", "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions", bytes.NewBuffer(jsonData))
if err != nil {
log.Fatal(err)
}
// Set request headers
// If environment variables are not configured, replace the following line with: apiKey := "sk-xxx"
apiKey := os.Getenv("DASHSCOPE_API_KEY")
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// Send request
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Read response body
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// Print response content
fmt.Printf("%s\n", bodyText)
}
Sample response
{
"choices": [
{
"message": {
"role": "assistant",
"content": "I am Qwen, a large language model developed by Alibaba Cloud. How can I assist you today?"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 22,
"completion_tokens": 17,
"total_tokens": 39
},
"created": 1726127645,
"system_fingerprint": null,
"model": "qwen-plus",
"id": "chatcmpl-81951b98-28b8-9659-ab07-cd30d25600e7"
}
DashScope
You can use the DashScope SDK or HTTP method to try Qwen models.
For a complete list of parameters, see DashScope.
Python
Sample code
import os
from dashscope import Generation
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who are you?"},
]
response = Generation.call(
# If the environment variable is not configured, replace the following line with: api_key = "sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="qwen-plus",
messages=messages,
result_format="message"
)
if response.status_code == 200:
print(response.output.choices[0].message.content)
else:
print(f"HTTP return code: {response.status_code}")
print(f"Error code: {response.code}")
print(f"Error message: {response.message}")
print("For more information, see: https://www.alibabacloud.com/help/en/model-studio/error-code")
Sample response
I am Qwen, an AI assistant developed by Alibaba Cloud. I am designed to answer various questions, provide information, and engage in conversations with users. How can I help you?
Java
Sample code
import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.protocol.Protocol;
public class Main {
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
Message systemMsg = Message.builder()
.role(Role.SYSTEM.getValue())
.content("You are a helpful assistant.")
.build();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("Who are you?")
.build();
GenerationParam param = GenerationParam.builder()
// If the environment variable is not configured, replace the following line with: .apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-plus")
.messages(Arrays.asList(systemMsg, userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.build();
return gen.call(param);
}
public static void main(String[] args) {
try {
GenerationResult result = callWithMessage();
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("Error message: "+e.getMessage());
System.out.println("For more information, see: https://www.alibabacloud.com/help/en/model-studio/error-code");
}
System.exit(0);
}
}
Sample response
I am a large language model developed by Alibaba Cloud, my name is Qwen.
HTTP
Sample code
curl
curl --location "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-plus",
"input":{
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Who are you?"
}
]
},
"parameters": {
"result_format": "message"
}
}'
PHP
<?php
$url = "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
$apiKey = getenv('DASHSCOPE_API_KEY');
$data = [
"model" => "qwen-plus",
"input" => [
"messages" => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who are you?"
]
]
],
"parameters" => [
"result_format" => "message"
]
];
$jsonData = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $apiKey",
"Content-Type: application/json"
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == 200) {
echo "Response: " . $response;
} else {
echo "Error: " . $httpCode . " - " . $response;
}
curl_close($ch);
?>
Node.js
DashScope does not provide SDK for Node.js. You can use OpenAI SDK for Node.js instead.
import fetch from 'node-fetch';
const apiKey = process.env.DASHSCOPE_API_KEY;
const data = {
model: "qwen-plus",
input: {
messages: [
{
role: "system",
content: "You are a helpful assistant."
},
{
role: "user",
content: "Who are you?"
}
]
},
parameters: {
result_format: "message"
}
};
fetch('https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
console.log(JSON.stringify(data));
})
.catch(error => {
console.error('Error:', error);
});
C#
using System.Net.Http.Headers;
using System.Text;
class Program
{
private static readonly HttpClient httpClient = new HttpClient();
static async Task Main(string[] args)
{
// If environment variables are not configured, replace the following line with: string? apiKey = "sk-xxx";
string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("API Key not set. Please ensure the environment variable 'DASHSCOPE_API_KEY' is set.");
return;
}
// Set request URL and content
string url = "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
string jsonContent = @"{
""model"": ""qwen-plus"",
""input"": {
""messages"": [
{
""role"": ""system"",
""content"": ""You are a helpful assistant.""
},
{
""role"": ""user"",
""content"": ""Who are you?""
}
]
},
""parameters"": {
""result_format"": ""message""
}
}";
// Send request and get response
string result = await SendPostRequestAsync(url, jsonContent, apiKey);
// Output result
Console.WriteLine(result);
}
private static async Task<string> SendPostRequestAsync(string url, string jsonContent, string apiKey)
{
using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
{
// Set request headers
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Send request and get response
HttpResponseMessage response = await httpClient.PostAsync(url, content);
// Handle response
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
return $"Request failed: {response.StatusCode}";
}
}
}
}
Go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Input struct {
Messages []Message `json:"messages"`
}
type Parameters struct {
ResultFormat string `json:"result_format"`
}
type RequestBody struct {
Model string `json:"model"`
Input Input `json:"input"`
Parameters Parameters `json:"parameters"`
}
func main() {
// Create HTTP client
client := &http.Client{}
// Build request body
requestBody := RequestBody{
Model: "qwen-plus",
Input: Input{
Messages: []Message{
{
Role: "system",
Content: "You are a helpful assistant.",
},
{
Role: "user",
Content: "Who are you?",
},
},
},
Parameters: Parameters{
ResultFormat: "message",
},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
log.Fatal(err)
}
// Create POST request
req, err := http.NewRequest("POST", "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation", bytes.NewBuffer(jsonData))
if err != nil {
log.Fatal(err)
}
// Set request headers
// If environment variables are not configured, replace the following line with: apiKey := "sk-xxx"
apiKey := os.Getenv("DASHSCOPE_API_KEY")
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// Send request
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Read response body
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// Print response content
fmt.Printf("%s\n", bodyText)
}
Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import com.google.gson.Gson;
public class Main {
static class Message {
String role;
String content;
public Message(String role, String content) {
this.role = role;
this.content = content;
}
}
static class Input {
Message[] messages;
public Input(Message[] messages) {
this.messages = messages;
}
}
static class Parameters {
String result_format;
public Parameters(String result_format) {
this.result_format = result_format;
}
}
static class RequestBody {
String model;
Input input;
Parameters parameters;
public RequestBody(String model, Input input, Parameters parameters) {
this.model = model;
this.input = input;
this.parameters = parameters;
}
}
public static void main(String[] args) {
try {
// Create the request body
RequestBody requestBody = new RequestBody(
"qwen-plus",
new Input(new Message[] {
new Message("system", "You are a helpful assistant."),
new Message("user", "Who are you?")
}),
new Parameters("message")
);
// Convert the request body to JSON
Gson gson = new Gson();
String jsonInputString = gson.toJson(requestBody);
// Create a URL object
URL url = new URL("https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// Set the request method to POST
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json; utf-8");
httpURLConnection.setRequestProperty("Accept", "application/json");
// If the environment variable is not configured, replace the following line with Bailian API Key: String apiKey = "sk-xxx";
String apiKey = System.getenv("DASHSCOPE_API_KEY");
String auth = "Bearer " + apiKey;
httpURLConnection.setRequestProperty("Authorization", auth);
// Enable input and output streams
httpURLConnection.setDoOutput(true);
// Write the request body
try (OutputStream os = httpURLConnection.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// Read the response body
try (BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
Sample response
{
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "I am a large language model created by Alibaba Cloud. You can call me Qwen."
}
}
]
},
"usage": {
"total_tokens": 38,
"output_tokens": 16,
"input_tokens": 22
},
"request_id": "09dceb20-ae2e-999b-85f9-c5ab266198c0"
}
Asynchronous invocation
Use the Asyncio interface calls to implement concurrency and improve program efficiency. Sample code:
OpenAI
Sample code
import os
import asyncio
from openai import AsyncOpenAI
import platform
# Create asynchronous client instance
client = AsyncOpenAI(
# If environment variables are not configured, replace the following line with: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
)
# Define asynchronous task list
async def task(question):
print(f"Sending question: {question}")
response = await client.chat.completions.create(
messages=[
{"role": "user", "content": question}
],
model="qwen-plus", # Model list: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
)
print(f"Received answer: {response.choices[0].message.content}")
# Main asynchronous function
async def main():
questions = ["Who are you?", "What can you do?", "How's the weather?"]
tasks = [task(q) for q in questions]
await asyncio.gather(*tasks)
if __name__ == '__main__':
# Set event loop policy
if platform.system() == 'Windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Run main coroutine
asyncio.run(main(), debug=False)
DashScope
Sample code
Your DashScope SDK for Python must be at least version 1.19.0.
import asyncio
import platform
from dashscope.aigc.generation import AioGeneration
import os
# Define asynchronous task list
async def task(question):
print(f"Sending question: {question}")
response = await AioGeneration.call(
# If environment variables are not configured, replace the following line with: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="qwen-plus", # Model list: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
prompt=question
)
print(f"Received answer: {response.output.text}")
# Main asynchronous function
async def main():
questions = ["Who are you?", "What can you do?", "How's the weather?"]
tasks = [task(q) for q in questions]
await asyncio.gather(*tasks)
if __name__ == '__main__':
# Set event loop policy
if platform.system() == 'Windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Run main coroutine
asyncio.run(main(), debug=False)
Common parameters generation control
Temperature and top_p
Both parameters are used to control the diversity of text generated by the model. Higher temperature or top_p indicate more diverse text. The lower the values, the more deterministic the text.
Diverse text is suitable for creative writing (such as novels, advertising copy), brainstorming, chatbot, and other scenarios.
Deterministic text is suitable for scenarios with clear answers (such as problem analysis, multiple-choice questions, fact queries) or requiring precise wording (such as technical documentation, legal texts, news reports, academic papers).
API reference
Full parameters for DashScope.
Learn more
Prompt engineering
A prompt is a textual input given to an LLM about the problem to be solved or the task to be completed. Prompt is the foundation for the LLM to comprehend user requirements and generate relevant and precise responses. The process of designing and optimizing prompts to enhance LLM response is called prompt engineering.
If you are interested in prompt engineering, see Best practices for prompt engineering to learn how to build effective prompts to enhance model performance.
You can also go to the Prompts page of the Model Studio console to quickly use templates for text generation.
Multimodal capabilities
Multimodal capability refers to the ability of a model to process and integrate various types of data modalities (such as text, images, audio, and video) for understanding, processing, and generating information. This capability enables the model to understand and generate content more comprehensively, enhance contextual understanding, and improve model performance.
Model Studio supports:
Qwen-VL (Text + Image to Text): A Qwen model with image understanding capabilities that can perform tasks such as OCR, visual inference, and text understanding. It supports resolutions of over a million pixels and images with any aspect ratio.
Qwen-Omni (All modalities to Text + Audio): Supports input of multiple modalities, including video, audio, images, text, and outputs audio and text.