All Products
Search
Document Center

Alibaba Cloud Model Studio:Text generation

Last Updated:Apr 29, 2025

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 of text generation: Extract key information from customer emails based on requirements

Prompt

Please extract the following information from this customer feedback email: 1. Customer information 2. Software version 3. Error code 4. Problem description 5. Operating system 6. Other relevant details
---------------
Email content:
Subject: [Help Request] Smart Assistant Startup Issue
Dear Customer Service and Technical Support Team,
I am Tom from xxx Technology, and my user ID is 12345. I am using the Smart Assistant software version V3.1.8, and I am reporting a technical issue that severely affects work progress. I hope you can assist in resolving it as soon as possible.
Since yesterday afternoon, I have encountered a severe obstacle when starting the Smart Assistant. The software startup process is stuck on the initialization screen, with an error code of ERR-2007, and an error message "Database connection failed," causing all functional modules to be unusable. This situation has persisted until now, severely affecting our office efficiency.
I am currently using a server operating system of Windows 10 version 1909, optimized for 64-bit architecture. After encountering the issue, I have taken several preliminary troubleshooting measures, including completely shutting down and restarting the system to clear any potential temporary software conflicts or system hang states, as well as thoroughly uninstalling and reinstalling the relevant software to eliminate possible software corruption or configuration errors. However, despite these routine solutions, the issue persists without any improvement.
To facilitate a more in-depth diagnosis of the issue, I have provided detailed error screenshots and system log files as attachments. These materials should accurately demonstrate the software state, exception details, and any related error codes at the time of the error, providing key information for quickly locating and resolving the current technical obstacle.
Looking forward to your response!

Output

1. Customer information: Tom from xxx Technology (ID12345)
2. Software version: Smart Assistant V3.1.8
3. Error code: ERR-2007
4. Problem description: The Smart Assistant is stuck on the initialization screen during startup, with an error message "Database connection failed," causing all functional modules to be unusable. The issue has persisted since yesterday afternoon.
5. Operating system: Win10 1909 64-bit
6. Other relevant details: Attempted solutions include restarting the system and reinstalling the software, but the issue remains unresolved. Attachments include error screenshots and log files.

Text generation models

Model Studio supports Qwen commercial models and open source models. Full model list.

Commercial Qwen models

Flagship models

通义new Qwen-Max

Best inference performance

通义new Qwen-Plus

Balanced performance, speed and cost

通义new Qwen-Turbo

Fast speed and low cost

Maximum context

(Tokens)

32,768

131,072

1,008,192

Maximum response

(Tokens)

8,192

8,192

8,192

Minimum input price

(Million tokens)

$1.6

$0.4

$0.05

Minimum output price

(Million tokens)

$6.4

$1.2/$8

$0.2/1

Open-source Qwen models

Open-source models

通义newQwen2.5

通义new Qwen2

通义new Qwen1.5

Maximum context

(Tokens)

131,072 - 1,000,000

65,536 - 131,072

8,000

Output length

(Tokens)

8,192

6,144

2,000

Parameter scale

(B: Billion)

7B - 72B

7B - 72B

7B - 110B

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.

    Inference capabilities: 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 extensive documents, you can try Qwen-Plus, which boasts up to 131,072 context 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.

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

To use the API, you must first obtain an API Key and set the API key as an environment variable.

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 the environment variable is not configured, replace the following line with Bailian API Key: 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?"},
        ],
        # Qwen3 uses enable_thinking to control its thinking process (False by default)
    )
    print(completion.choices[0].message.content)
except Exception as e:
    print(f"Error message: {e}")
    print("For more information, see: https://www.alibabacloud.com/help/zh/model-studio/developer-reference/error-code")

Sample response

I am a large language model created by Alibaba Cloud. You can call me Qwen.

Node.js

Sample code

import OpenAI from "openai";

const openai = new OpenAI(
    {
        // If the environment variable is not configured, replace the following line with Bailian API Key: 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/zh/model-studio/getting-started/models
    messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "Who are you?" }
    ],
    // Qwen3 uses enable_thinking to control its thinking process (False by default)
});
console.log(JSON.stringify(completion))

Sample response

{
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "I am a large language model created by Alibaba Cloud. You can call me 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 the environment variable is not configured, replace the following line with Bailian API Key: $apiKey = "sk-xxx";
$apiKey = getenv('DASHSCOPE_API_KEY');
// Set request headers
$headers = [
    'Authorization: Bearer '.$apiKey,
    'Content-Type: application/json'
];
// Set request body
$data = [
<?php
// Set the request URL
$url = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions';
// If the environment variable is not configured, replace the following line with Bailian API Key: $apiKey = "sk-xxx";
$apiKey = getenv('DASHSCOPE_API_KEY');
// Set request headers
$headers = [
    'Authorization: Bearer '.$apiKey,
    'Content-Type: application/json'
];
// Set request body
$data = [
// Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    "model" => "qwen-plus",
    "messages" => [
        [
            "role" => "system",
            "content" => "You are a helpful assistant."
        ],
        [
            "role" => "user",
            "content" => "Who are you?"
        ]
    ]
];
// Initialize a 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 the cURL session
$response = curl_exec($ch);
// Check whether an error occurs
if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
}
// Close the cURL resource
curl_close($ch);
// Output the response result
echo $response;
?>
    "model" => "qwen-plus",
    "messages" => [
        [
            "role" => "system",
            "content" => "You are a helpful assistant."
        ],
        [
            "role" => "user",
            "content" => "Who are you?"
        ]
    ]
];
// Initialize a 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 the cURL session
$response = curl_exec($ch);
// Check whether an error occurs
if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
}
// Close the cURL resource
curl_close($ch);
// Output the response result
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 the environment variable is not configured, replace the following line with Bailian API Key: string? apiKey = "sk-xxx";
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");

        if (string.IsNullOrEmpty(apiKey))
        {
            Console.WriteLine("API Key is not set. Please ensure the environment variable 'DASHSCOPE_API_KEY' is set.");
            return;
        }

        // Set the request URL and content
        string url = "https://dashscope.aliyuncs-intl.com/compatible-mode/v1/chat/completions";
        // Model list: https://www.alibabacloud.com/help/zh/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 the request and obtain the response
        string result = await SendPostRequestAsync(url, jsonContent, apiKey);

        // Output the 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 the request and obtain the response
            HttpResponseMessage response = await httpClient.PostAsync(url, content);

            // Process the 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 an HTTP client
	client := &http.Client{}
	// Construct the request body
	requestBody := RequestBody{
		// Model list: https://www.alibabacloud.com/help/zh/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 a 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 the environment variable is not configured, replace the following line with Bailian API Key: apiKey := "sk-xxx"
	apiKey := os.Getenv("DASHSCOPE_API_KEY")
	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")
	// Send the request
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	// Read the response body
	bodyText, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	// Print the content of the response
	fmt.Printf("%s\n", bodyText)
}

Java

OpenAI does not have an SDK for Java. If you need to use Java, see DashScope SDK for 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 RequestBody {
        String model;
        Message[] messages;

        public RequestBody(String model, Message[] messages) {
            this.model = model;
            this.messages = messages;
        }
    }

    public static void main(String[] args) {
        try {
            // Create the request body
            RequestBody requestBody = new RequestBody(
                    "qwen-plus",
                    new Message[] {
                            new Message("system", "You are a helpful assistant."),
                            new Message("user", "Who are you?")
                    }
            );

            // 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/compatible-mode/v1/chat/completions");
            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);
            }

            // Get the response code
            int responseCode = httpURLConnection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 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 Body: " + response);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }
}

Sample response

{
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "I am a large language model created by Alibaba Cloud. You can call me Qwen."
            },
            "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 parameter list, see DashScope. For information about how to install the SDK, see Install the SDK.

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 Bailian API Key: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"), 
    model="qwen-plus",
    messages=messages,
    result_format="message"
    # Qwen3 uses enable_thinking to control its thinking process (False by default)
)

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/zh/model-studio/developer-reference/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 assist 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 Bailian API Key: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                // Qwen3 uses enable_thinking to control its thinking process (False by default)
                .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/zh/model-studio/developer-reference/error-code");
        }
        System.exit(0);
    }
}

Sample response

I am a large language model created by Alibaba Cloud. You can call me 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 offer an SDK for the Node.js environment. To make calls using the OpenAI Node.js SDK, refer to the Node.js section in this document.

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 the environment variable is not configured, replace the following line with Bailian API Key: string? apiKey = "sk-xxx";
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");

        if (string.IsNullOrEmpty(apiKey))
        {
            Console.WriteLine("API Key is not set. Please ensure the environment variable 'DASHSCOPE_API_KEY' is set.");
            return;
        }

        // Set the 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 the request and obtain the response
        string result = await SendPostRequestAsync(url, jsonContent, apiKey);

        // Output the result
        Console.WriteLine(result);
    }

    private static async Task 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 the request and obtain the response
            HttpResponseMessage response = await httpClient.PostAsync(url, content);

            // Process the 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 an HTTP client
	client := &http.Client{}

	// Construct the 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 a 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 the environment variable is not configured, replace the following line with Bailian API Key: apiKey := "sk-xxx"
	apiKey := os.Getenv("DASHSCOPE_API_KEY")
	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")

	// Send the request
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	// Read the response body
	bodyText, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	// Print the content of the response
	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 calling

You can use the Asyncio interface to achieve concurrency and enhance program efficiency. Sample code:

OpenAI

Sample code

import os
import asyncio
from openai import AsyncOpenAI
import platform

# Create an asynchronous client instance
client = AsyncOpenAI(
    # If the environment variable is not configured, replace the following line with Bailian API Key: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
)

# Define the 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 is the weather?"]
    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)
    

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
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

# Define the asynchronous task list
async def task(question):
    print(f"Sending question: {question}")
    response = await AioGeneration.call(
        # If the environment variable is not configured, replace the following line with Bailian API Key: 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 is the weather?"]
    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)

Common parameters for generation control

temperature and top_p

These 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 texts are suitable for scenarios such as creative writing (novels or advertisement), brainstorming, chat applications, and more.

  • Deterministic texts are suitable for scenarios with clear answers (such as problem analysis, multiple-choice questions, factual queries) or requiring precise wording (such as technical documents, legal texts, news reports, academic papers).

How they works

temperature

  • The higher the temperature, the flatter the probability distribution of tokens is (The chance of high-probability token decreases, and the chance of low-probability increases), making the model more random in selecting the next token.

  • The lower the temperature, the steeper the probability distribution of tokens is (The chance of high-probability tokens increases, and the chance of low-probability tokens decreases), making the model more likely to choose a few high-probability tokens.

top_p

Top-p sampling refers to sampling from the set of tokens with the highest probabilities (the core set). This sampling method sorts all possible tokens by probability from high to low, then accumulates probabilities starting from the highest probability token until the cumulative probability reaches a threshold (For example, if top_p is set to 0.8, the threshold is 80%). Finally, the model randomly selects one token from these high-probability tokens for output.

  • The higher the top_p parameter, the more tokens are considered, resulting in more diverse generated text.

  • The lower the top_p parameter, the fewer tokens are considered, resulting in more focused and deterministic generated text.

API reference

Parameters for the OpenAI SDK

Parameters for the DashScope SDK

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 Prompt Engineering page of the Model Studio console to quickly use templates for text generation.

Multimodal capability

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 -> 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.

FAQ

Q: When I use Web-based Qwen chat, it can parse the links I enter. Why cannot the Qwen API do the same thing?

A: The web version has additional engineering optimizations on top of the Qwen API, and parsing web links is not a capability of the Qwen API itself. You can use web scraping tools such as Beautiful Soup in Python to read web content.

Q: Why does the Web-based Qwen chat and the Qwen API show significant differences in their responses?

A: The web version has additional engineering optimizations on top of the Qwen API, thus enabling features such as web page parsing, online searching, drawing, and creating slides. These features are not the capabilities of the model API itself.