All Products
Search
Document Center

Alibaba Cloud Model Studio:Overview of text generation models

Last Updated:Jan 28, 2026

Text generation models create clear and coherent text based on input prompts.

The input for text generation models can be simple keywords, a one-sentence summary, or more complex instructions and context. The models learn language patterns by analyzing large amounts of data and are widely used for:

  • Content creation: Generating news reports, product descriptions, and short video scripts.

  • Customer service: Powering chatbots to provide 24/7 support and answer common questions.

  • Text translation: Achieving fast and accurate cross-language conversion.

  • Summary generation: Extracting the core content of long articles, reports, and emails.

  • Legal document drafting: Generating basic frameworks for contract templates and legal opinions.

Model selection recommendations

Service regions

Alibaba Cloud Model Studio provides model services in the Singapore, Virginia, and Beijing regions. Each region has a different API key. Accessing the service from a nearby region reduces network latency. For more information, see Select a deployment mode.

General-purpose models

Qwen-Max, Qwen-Plus, and Qwen-Flash have all been upgraded to the Qwen3 series and are OpenAI compatible. They are suitable for various scenarios, such as intelligent customer service, text creation, content polishing, and summarization.

  • Qwen-Plus: Balances performance, speed, and cost, making it the recommended choice for most scenarios.

  • Qwen-Max: The best-performing model in the Qwen series, suitable for handling complex, multi-step tasks. qwen3-max-2026-01-23 supports calling built-in tools to achieve higher accuracy when solving complex problems.

  • Qwen-Flash: The fastest and most cost-effective model in the Qwen series, suitable for performing simple jobs.

Scenario-specific models

For specific business needs, Model Studio offers a variety of specialized and optimized models covering areas such as code capabilities, long context, translation, data mining, intention recognition, role-playing, and deep search.

Multimodal models

  • Qwen-VL (Text + Image -> Text): Includes image understanding capabilities and supports tasks such as optical character recognition (OCR), visual reasoning, and image-text understanding.

  • Qwen-Omni (Omni-modal -> Text + Audio): Supports various data inputs, including video, audio, images, and text, and generates text and speech outputs to handle complex cross-modality tasks.

  • Speech recognition models (Audio -> Text): Recognize and transcribe speech from audio. They support languages such as Chinese (including various dialects such as Cantonese), English, Japanese, and Korean.

Third-party models

Model Studio provides well-known third-party large language models, including DeepSeek and Kimi. For a complete list of models, see Text generation - Third-party.

Core concepts

The input for a text generation model is a prompt, which consists of one or more message objects. Each message has a role and content:

  • System message: Defines the model's role or the instructions it must follow. Defaults to "You are a helpful assistant."

  • User message: The question or instruction from the user to the model.

  • Assistant message: The model's reply.

When calling the model, construct an array of these message objects named messages. A typical request consists of a system message that defines the code of conduct and a user message that provides the instruction.

system message is optional, but including it to set the model's role and code of conduct is recommended for achieving more stable and consistent 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 output response object contains the model's reply as an assistant message.

{
    "role": "assistant",
    "content": "Hello! I am Qwen, a large-scale language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text, perform logical reasoning, and program. I can understand and generate multiple languages, and I support multi-turn conversations and complex task processing. If you need any help, just let me know!"
}

Getting started

Prerequisites: Get an API key and set the API key as an environment variable. To use the SDK, install the OpenAI or DashScope SDK.

OpenAI compatible - Responses API

The Responses API is an evolution of the Chat Completions API. For usage instructions, code examples, and migration guides, see OpenAI compatible - Responses.

Python

import os
from openai import OpenAI

try:
    client = OpenAI(
        # Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
        # If you have not configured the environment variable, replace the following line with api_key="sk-xxx", using your Model Studio API key.
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # The Responses API currently supports only the Singapore region.
        base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
    )

    response = client.responses.create(
        # The Responses API currently supports only the qwen3-max-2026-01-23 model.
        model="qwen3-max-2026-01-23",
        input="Hello, please introduce yourself in one sentence."
    )

    # Get model response
    print(response.output_text)
except Exception as e:
    print(f"Error message: {e}")
    print("For more information, see https://www.alibabacloud.com/help/en/model-studio/error-code")

Response

Hello! I'm Qwen, a large language model developed by Alibaba Cloud's Tongyi Lab, designed to assist with answering questions, creating content, coding, and more—always up-to-date as of January 27, 2026.

Node.js

// Requires Node.js v18+ and must be run in an ES Module environment.
import OpenAI from "openai";

const openai = new OpenAI({
    // Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
    // If you have not configured the environment variable, replace the following line with apiKey: "sk-xxx", using your Model Studio API key.
    apiKey: process.env.DASHSCOPE_API_KEY,
    // The Responses API currently supports only the Singapore region.
    baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    try {
        const response = await openai.responses.create({
            // The Responses API currently supports only the qwen3-max-2026-01-23 model.
            model: "qwen3-max-2026-01-23",
            input: "Hello, please introduce yourself in one sentence."
        });

        // Get model response
        console.log(response.output_text);
    } catch (error) {
        console.error("Error:", error);
    }
}

main();

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group.

curl

The Responses API currently supports only the Singapore region and the qwen3-max-2026-01-23 model.

curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "input": "Hello, please introduce yourself in one sentence."
}'

Response

{
    "id": "351e34cc-5f75-483b-b948-35be954dbxxx",
    "created_at": 1769408284,
    "model": "qwen3-max-2026-01-23",
    "object": "response",
    "status": "completed",
    "output": [
        {
            "type": "message",
            "id": "msg_59a7339e-77d0-4451-8f51-75fb8dbefxxx",
            "role": "assistant",
            "status": "completed",
            "content": [
                {
                    "type": "output_text",
                    "text": "I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group."
                }
            ]
        }
    ],
    "usage": {
        "input_tokens": 39,
        "output_tokens": 46,
        "total_tokens": 85
    }
}

OpenAI compatible - Chat Completions API

Python

import os
from openai import OpenAI

try:
    client = OpenAI(
        # Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
        # If you have not configured the environment variable, replace the following line with api_key="sk-xxx", using your Model Studio API key.
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # Each region has a different base_url.
        base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
    )

    completion = client.chat.completions.create(
        # For a list of models, see https://www.alibabacloud.com/help/en/model-studio/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)
    # 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 https://www.alibabacloud.com/help/en/model-studio/error-code")

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

Java

// We recommend using OpenAI Java SDK version 3.5.0 or later.
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

public class Main {
    public static void main(String[] args) {
        try {
            OpenAIClient client = OpenAIOkHttpClient.builder()
                    // If you have not configured the environment variable, replace the following line with .apiKey("sk-xxx"), using your Model Studio API key.
                    // Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    // Each region has a different base_url.
                    .baseUrl("https://dashscope-intl.aliyuncs.com/compatible-mode/v1")
                    .build();

            // Create ChatCompletion parameters.
            ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                    .model("qwen-plus")  // Specify the model.
                    .addSystemMessage("You are a helpful assistant.")
                    .addUserMessage("Who are you?")
                    .build();

            // Send the request and get the response.
            ChatCompletion chatCompletion = client.chat().completions().create(params);
            String content = chatCompletion.choices().get(0).message().content().orElse("No valid content returned");
            System.out.println(content);
            
        } catch (Exception 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");
        }
    }
}

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

Node.js

// Requires Node.js v18+ and must be run in an ES Module environment.
import OpenAI from "openai";

const openai = new OpenAI(
    {
        // Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
        // If you have not configured the environment variable, replace the following line with apiKey: "sk-xxx", using your Model Studio API key.
        apiKey: process.env.DASHSCOPE_API_KEY,
        // Each region has a different base_url.
        baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
        
    }
);
const completion = await openai.chat.completions.create({
    model: "qwen-plus",  // For a list of models, see https://www.alibabacloud.com/help/en/model-studio/models
    messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "Who are you?" }
    ],
});
console.log(completion.choices[0].message.content);
// To view the full response, uncomment the following line.
// console.log(JSON.stringify(completion, null, 4));

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

Go

// OpenAI Go SDK version v2.4.0 or later.
package main

import (
	"context"
	// To view the full response, uncomment the line below and at the end of the code.
	// "encoding/json"
	"fmt"
	"os"

	"github.com/openai/openai-go/v2"
	"github.com/openai/openai-go/v2/option"
)

func main() {
	// If you have not configured the environment variable, replace the following line with: apiKey := "sk-xxx", using your Model Studio API key.
	apiKey := os.Getenv("DASHSCOPE_API_KEY")
	client := openai.NewClient(
		option.WithAPIKey(apiKey),
		// Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
		// Each region has a different base_url.
		option.WithBaseURL("https://dashscope-intl.aliyuncs.com/compatible-mode/v1"),
	)
	chatCompletion, err := client.Chat.Completions.New(
		context.TODO(), openai.ChatCompletionNewParams{
			Messages: []openai.ChatCompletionMessageParamUnion{
				openai.SystemMessage("You are a helpful assistant."),
				openai.UserMessage("Who are you?"),
			},
			Model: "qwen-plus",
		},
	)

	if err != nil {
		fmt.Fprintf(os.Stderr, "Request failed: %v\n", err)
		// For more error information, see https://www.alibabacloud.com/help/en/model-studio/error-code
		os.Exit(1)
	}

	if len(chatCompletion.Choices) > 0 {
		fmt.Println(chatCompletion.Choices[0].Message.Content)
	}
	// To view the full response, uncomment the following lines.
	// jsonData, _ := json.MarshalIndent(chatCompletion, "", "  ")
	// fmt.Println(string(jsonData))

}

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

C# (HTTP)

using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

class Program
{
    private static readonly HttpClient httpClient = new HttpClient();

    static async Task Main(string[] args)
    {
        // If you have not configured the environment variable, replace the following line with: string? apiKey = "sk-xxx"; using your Model Studio API key.
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
        // Each region has a different base_url.
        string url = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions";
        // For a list of models, see 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 the request and get the response.
        string result = await SendPostRequestAsync(url, jsonContent, apiKey);
        
        // To view the full response, uncomment the following line.
        // Console.WriteLine(result);

        // Parse the JSON and output only the content part.
        using JsonDocument doc = JsonDocument.Parse(result);
        JsonElement root = doc.RootElement;
        
        if (root.TryGetProperty("choices", out JsonElement choices) && 
            choices.GetArrayLength() > 0)
        {
            JsonElement firstChoice = choices[0];
            if (firstChoice.TryGetProperty("message", out JsonElement message) &&
                message.TryGetProperty("content", out JsonElement content))
            {
                Console.WriteLine(content.GetString());
            }
        }
    }

    private static async Task<string> SendPostRequestAsync(string url, string jsonContent, string apiKey)
    {
        using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await httpClient.PostAsync(url, content);
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            else
            {   
                // For more error information, see https://www.alibabacloud.com/help/en/model-studio/error-code
                return $"Request failed: {response.StatusCode}";
            }
        }
    }
}

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

PHP (HTTP)

<?php
// Set the request URL.
// Each region has a different base_url.
$url = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions';
// Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
// If you have not configured the environment variable, replace the following line with: $apiKey = "sk-xxx"; using your Model Studio API key.
$apiKey = getenv('DASHSCOPE_API_KEY');
// Set the request headers.
$headers = [
    'Authorization: Bearer '.$apiKey,
    'Content-Type: application/json'
];
// Set the request body.
$data = [
    "model" => "qwen-plus",
    "messages" => [
        [
            "role" => "system",
            "content" => "You are a helpful assistant."
        ],
        [
            "role" => "user",
            "content" => "Who are you?"
        ]
    ]
];
// Initialize the cURL session.
$ch = curl_init();
// Set the 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 for errors.
// For more error information, see https://www.alibabacloud.com/help/en/model-studio/error-code
if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
}
// Close the cURL resource.
curl_close($ch);
// Output the response result.
$dataObject = json_decode($response);
$content = $dataObject->choices[0]->message->content;
echo $content;
// To view the full response, uncomment the following line.
//echo $response;
?>

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

curl

Each region has a different base_url and API key. For more information, see OpenAI Chat and Get an API key.

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?"
        }
    ]
}'

Response

{
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!"
            },
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null
        }
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 26,
        "completion_tokens": 66,
        "total_tokens": 92
    },
    "created": 1726127645,
    "system_fingerprint": null,
    "model": "qwen-plus",
    "id": "chatcmpl-81951b98-28b8-9659-ab07-xxxxxx"
}

DashScope

Python

import json
import os
from dashscope import Generation
import dashscope

# Each region has a different base_url.
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(
    # Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If you have not configured the environment variable, replace the following line with: api_key = "sk-xxx", using your Model Studio API key.
    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)
    # To view the full response, uncomment the following line.
    # print(json.dumps(response, default=lambda o: o.__dict__, indent=4))
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")

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

Java

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;
import com.alibaba.dashscope.utils.JsonUtils;

public class Main {
    public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
        // Each region has a different base_url.
        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()
                // Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
                // If you have not configured the environment variable, replace the following line with: .apiKey("sk-xxx"), using your Model Studio API key.
                .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());
            // To view the full response, uncomment the following line.
            // System.out.println(JsonUtils.toJson(result));
        } 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");
        }
    }
}

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

Node.js (HTTP)

// Requires Node.js v18+.
// If you have not configured the environment variable, replace the following line with: const apiKey = "sk-xxx"; using your Model Studio API key.
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"
    }
};

async function callApi() {
    try {
            // Each region has a different base_url.
            const response = await 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)
        });

        const result = await response.json();
        console.log(result.output.choices[0].message.content);
        // To view the full response, uncomment the following line.
        // console.log(JSON.stringify(result));
    } catch (error) {
        // For more error information, see https://www.alibabacloud.com/help/en/model-studio/error-code
        console.error('Call failed:', error.message);
    }
}

callApi();

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

Go (HTTP)

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
)

func main() {
	requestBody := map[string]interface{}{
		"model": "qwen-plus",
		"input": map[string]interface{}{
			"messages": []map[string]string{
				{
					"role":    "system",
					"content": "You are a helpful assistant.",
				},
				{
					"role":    "user",
					"content": "Who are you?",
				},
			},
		},
		"parameters": map[string]string{
			"result_format": "message",
		},
	}

	// Serialize to JSON.
	jsonData, _ := json.Marshal(requestBody)

	// Create an HTTP client and request.
	client := &http.Client{}
	// Each region has a different base_url.
	req, _ := http.NewRequest("POST", "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation", bytes.NewBuffer(jsonData))

	// Set request headers.
	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, _ := io.ReadAll(resp.Body)

	// Parse the JSON and output the content.
	var result map[string]interface{}
	json.Unmarshal(bodyText, &result)
	content := result["output"].(map[string]interface{})["choices"].([]interface{})[0].(map[string]interface{})["message"].(map[string]interface{})["content"].(string)
	fmt.Println(content)

	// To view the full response, uncomment the following line.
	// fmt.Printf("%s\n", bodyText)
}

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

C# (HTTP)

using System.Net.Http.Headers;
using System.Text;

class Program
{
    private static readonly HttpClient httpClient = new HttpClient();

    static async Task Main(string[] args)
    {
        // Each region has a different API key. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
        // If you have not configured the environment variable, replace the following line with: string? apiKey = "sk-xxx"; using your Model Studio API key.
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
        // Set the request URL and content.
        // Each region has a different base_url.
        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 get the response.
        string result = await SendPostRequestAsync(url, jsonContent, apiKey);
        var jsonResult = System.Text.Json.JsonDocument.Parse(result);
        var content = jsonResult.RootElement.GetProperty("output").GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString();
        Console.WriteLine(content);
        // To view the full response, uncomment the following line.
        // 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 get the response.
            HttpResponseMessage response = await httpClient.PostAsync(url, content);

            // Handle the response.
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            else
            {
                return $"Request failed: {response.StatusCode}";
            }
        }
    }
}

Response

{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!"
                }
            }
        ]
    },
    "usage": {
        "total_tokens": 92,
        "output_tokens": 66,
        "input_tokens": 26
    },
    "request_id": "09dceb20-ae2e-999b-85f9-xxxxxx"
}

PHP (HTTP)

<?php
// Each region has a different base_url.
$url = "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
// To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
$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_HTTPHEADER, [
    "Authorization: Bearer $apiKey",
    "Content-Type: application/json"
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($httpCode == 200) {
    $jsonResult = json_decode($response, true);
    $content = $jsonResult['output']['choices'][0]['message']['content'];
    echo $content;
    // To view the full response, uncomment the following line.
    // echo "Model response: " . $response;
} else {
    echo "Request error: " . $httpCode . " - " . $response;
}

curl_close($ch);
?>

Response

I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!

curl

Each region has a different base_url and API key. For more information, see DashScope and Get an API key.

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"
    }
}'

Response

{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "I am Qwen, a large-scale language model independently developed by the Tongyi Lab of Alibaba Group. I can help you answer questions and create text, such as writing stories, official documents, emails, and scripts, along with perform logical reasoning, programming, and more. I can also express opinions and play games. If you have any questions or need help, feel free to ask me at any time!"
                }
            }
        ]
    },
    "usage": {
        "total_tokens": 92,
        "output_tokens": 66,
        "input_tokens": 26
    },
    "request_id": "09dceb20-ae2e-999b-85f9-xxxxxx"
}

Image and video data processing

Multimodal models process non-text data, such as images and videos. Use these models for tasks such as visual question answering and event detection. Calling a multimodal model is different from calling a plain-text model in the following ways:

  • User message construction: User messages include multimodal information, such as images and audio, in addition to text.

  • DashScope SDK: When you use the DashScope Python SDK, call the MultiModalConversation interface. When you use the DashScope Java SDK, call the MultiModalConversation class.

For limitations on image and video files, see Visual understanding.

OpenAI compatible - Chat Completions API

Python

from openai import OpenAI
import os

client = OpenAI(
    # API keys vary by region. To obtain an API key, see https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # If you have not configured an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The base_url varies by region.
    base_url="https://dashscope-intl.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-vl-plus",  # Replace this with another multimodal model and modify the messages as needed.
    messages=messages,
)
print(completion.choices[0].message.content)

Node.js

import OpenAI from "openai";

const openai = new OpenAI(
    {
        // API keys vary by region. To obtain an API key, see https://www.alibabacloud.com/help/zh/model-studio/get-api-key
        // If you have not configured an environment variable, replace the following line with your Model Studio API key: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        // The base_url varies by region.
        baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
    }
);

let 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?" },
        ]
    }]
async function main() {
    let response = await openai.chat.completions.create({
        model: "qwen3-vl-plus",   // Replace this with another multimodal model and modify the messages as needed.
        messages: messages
    });
    console.log(response.choices[0].message.content);
}

main()

curl

Each region has a different base_url and API key. For more information, see OpenAI Chat and Get an API key.

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": "qwen3-vl-plus",
  "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?"
        }
      ]
    }
  ]
}'

DashScope

Python

import os
from dashscope import MultiModalConversation
import dashscope
# The base_url varies by region.
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

messages = [
    {
        "role": "user",
        "content": [
            {
                "image": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png"
            },
            {"text": "What products are shown in the image?"},
        ],
    }
]
response = MultiModalConversation.call(
    # API keys vary by region. To obtain an API key, see https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # If you have not configured an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model='qwen3-vl-plus',  # Replace this with another multimodal model and modify the messages as needed.
    messages=messages
)

print(response.output.choices[0].message.content[0]['text'])

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        // The base_url varies by region.
        Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
    }

    private static final String modelName = "qwen3-vl-plus";  //  Replace this with another multimodal model and modify the messages as needed.

    public static void MultiRoundConversationCall() throws ApiException, NoApiKeyException, UploadFileException {
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                .content(Arrays.asList(Collections.singletonMap("image", "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png"),
                        Collections.singletonMap("text", "What products are shown in the image?"))).build();
        List<MultiModalMessage> messages = new ArrayList<>();
        messages.add(userMessage);
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                // API keys vary by region. To obtain an API key, see https://www.alibabacloud.com/help/zh/model-studio/get-api-key
                // If you have not configured an environment variable, replace the following line with your Model Studio API key: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .model(modelName)
                .messages(messages)
                .build();
        MultiModalConversationResult result = conv.call(param);
        System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text"));        // add the result to conversation
    }

    public static void main(String[] args) {
        try {
            MultiRoundConversationCall();
        } catch (ApiException | NoApiKeyException | UploadFileException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

curl

Each region has a different base_url and API key. For more information, see DashScope and Get an API key.

curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
    "model": "qwen3-vl-plus",
    "input":{
        "messages":[
            {
                "role": "user",
                "content": [
                    {"image": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png"},
                    {"text": "What products are shown in the image?"}
                ]
            }
        ]
    }
}'

Asynchronous invocation

Asynchronous API calls improve processing efficiency for high-concurrency requests.

OpenAI-compatible - Chat Completions API

Python

import os
import asyncio
from openai import AsyncOpenAI
import platform

# Create an asynchronous client instance.
client = AsyncOpenAI(
    # API keys vary by region. To obtain an API key, see https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # If the environment variable is not set, replace the following line with your Model Studio API key: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The base_url varies by region.
    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",  # For a list of models, see https://www.alibabacloud.com/help/zh/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?", "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)
    

Java

import com.openai.client.OpenAIClientAsync;
import com.openai.client.okhttp.OpenAIOkHttpClientAsync;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class Main {
    public static void main(String[] args) {
        // Create an OpenAI client and connect to the DashScope compatible endpoint.
        OpenAIClientAsync client = OpenAIOkHttpClientAsync.builder()
                // API keys vary by region. To obtain an API key, see https://www.alibabacloud.com/help/zh/model-studio/get-api-key
                // If the environment variable is not set, replace the following line with your Model Studio API key: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // The base_url varies by region.
                .baseUrl("https://dashscope-intl.aliyuncs.com/compatible-mode/v1")
                .build();

        // Define the list of questions.
        List<String> questions = Arrays.asList("Who are you?", "What can you do?", "How is the weather?");

        // Create a list of asynchronous tasks.
        CompletableFuture<?>[] futures = questions.stream()
                .map(question -> CompletableFuture.supplyAsync(() -> {
                    System.out.println("Sending question: " + question);
                    // Create ChatCompletion parameters.
                    ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                            .model("qwen-plus")  // Specify the model.
                            .addSystemMessage("You are a helpful assistant.")
                            .addUserMessage(question)
                            .build();

                    // Send an asynchronous request and process the response.
                    return client.chat().completions().create(params)
                        .thenAccept(chatCompletion -> {
                            String content = chatCompletion.choices().get(0).message().content().orElse("No response content");
                            System.out.println("Model response: " + content);
                        })
                        .exceptionally(e -> {
                            System.err.println("Error message: " + e.getMessage());
                            System.out.println("For more information, see https://www.alibabacloud.com/help/zh/model-studio/error-code");
                            return null;
                        });
                }).thenCompose(future -> future))
                .toArray(CompletableFuture[]::new);

        // Wait for all asynchronous operations to complete.
        CompletableFuture.allOf(futures).join();
    }
}

DashScope

Currently, the DashScope SDK supports asynchronous invocation for text generation only in Python.

# DashScope Python SDK version 1.19.0 or later is required.
import asyncio
import platform
from dashscope.aigc.generation import AioGeneration
import os
import dashscope
# The base_url varies by region.
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 set, replace the following line with your Model Studio API key: api_key="sk-xxx",
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        model="qwen-plus",  # For a list of models, see https://www.alibabacloud.com/help/zh/model-studio/models
        messages=[{"role": "system", "content": "You are a helpful assistant."},
                  {"role": "user", "content": question}],
        result_format="message",
    )
    print(f"Model response: {response.output.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)

Response

Because the calls are asynchronous, the order of the responses may differ from the example.
Sending question: Who are you?
Sending question: What can you do?
Sending question: How is the weather?
Model response: Hello! I am Qwen, a large-scale language model developed by the Tongyi Lab under Alibaba Group. I can help you answer questions and create text, such as stories, official documents, emails, and playbooks. I can also perform logical reasoning, write code, express opinions, and play games. If you have any questions or need help, feel free to ask me at any time!
Model response: Hello! I cannot get real-time weather information at the moment. 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 application to check the real-time weather.
Model response: I have many skills, such as:

1. **Answering questions**: I can try to answer your questions, whether they are about academic topics, general knowledge, or professional subjects.
2. **Creating text**: I can write various types of text, such as stories, official documents, emails, and playbooks.
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 writing, debugging, and optimizing code.
5. **Multilingual support**: I support multiple languages, including but not limited to Chinese, English, French, and Spanish.
6. **Expressing opinions**: I can provide you with opinions and suggestions to help you make decisions.
7. **Playing games**: We can play text-based games together, such as riddles and word chain games.

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

Going live

Build high-quality context

Feeding large amounts of raw data directly into a model increases costs and reduces performance because of context size limitations. Context engineering improves generation quality and efficiency by dynamically loading precise knowledge. The core techniques include the following:

  • Prompt engineering: Design and optimize text instructions, called prompts, to guide the model more precisely. This ensures that the output more closely aligns with your expected results. For more information, see Text generation prompt guide.

  • Retrieval-augmented generation (RAG): This is useful when the model needs to answer questions using an external knowledge base, such as product documents or technical manuals.

  • Tool calling: This allows the model to retrieve real-time information, such as weather updates, or perform specific operations, such as calling an API or sending an email.

  • Memory: This technique builds long-term and short-term memory for the model, which allows it to understand the history of a continuous conversation.

Control response diversity

Use temperature and top_p to control the diversity of the generated text. Higher values produce more diverse content. Lower values produce more deterministic content. To accurately assess the effect of a parameter, adjust only one at a time.

  • temperature: The range is [0, 2). This parameter primarily adjusts randomness.

  • top_p: The range is [0, 1]. This parameter filters responses using a probability threshold.

The following examples show how different parameter settings affect the generated content. The input prompt is: "Write a three-sentence short story where the main characters are a cat and a sunbeam."

  • High diversity (temperature = 0.9): This is suitable for scenarios that require creativity, imagination, and novel expression, such as creative writing, brainstorming, or marketing copy.

    Sunlight slanted onto the windowsill. A ginger cat tiptoed to the glowing patch of light, its fur instantly turning the color of melted honey.
    It reached out a paw to tap the sunbeam and sank in as if stepping into warm water, the light flowing up its back from its paw pads.
    The afternoon grew heavy. The cat, curled in the flowing gold, heard time melt away with its purrs.
  • High determinism (temperature = 0.1): This is suitable for scenarios that require accurate, rigorous, and predictable content, such as factual Q&A, code generation, or legal text.

    An old cat dozed on the windowsill in the afternoon sun.
    The light danced on its mottled back like someone flipping through an old photo album.
    Dust motes floated in the air. It was as if time whispered, "You were young once, and I was bright."

How it works

temperature:

  • A higher temperature flattens the token probability distribution. The probability of high-probability tokens decreases, while the probability of low-probability tokens increases. This makes the model's selection of the next token more random.

  • A lower temperature makes the token probability distribution steeper. High-probability tokens are more likely to be selected, and low-probability tokens are less likely. This makes the model tend to choose from a smaller set of high-probability tokens.

top_p:

top_p sampling, also known as nucleus sampling, selects from a core set of tokens based on their cumulative probability. This method sorts all possible next tokens by probability in descending order. It then sums the probabilities of the most likely tokens until the sum reaches the top_p threshold, such as 0.8. The model then randomly selects the next token from this smaller set.

  • A higher top_p value includes more tokens in the set, resulting in more diverse text.

  • A lower top_p value includes fewer tokens, resulting in more focused and deterministic text.

Example parameter configurations for different scenarios

# Recommended parameter configurations for different 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
    }
}

# Example for OpenAI
# completion = client.chat.completions.create(
#     model="qwen-plus",
#     messages=[{"role": "user", "content": "Write a poem about the moon"}],
#     **SCENARIO_CONFIGS["creative_writing"]
# )
# Example for DashScope
# response = Generation.call(
#     # If the environment variable is not configured, replace the following line with your 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 to determine if an input n is a prime number. Do not output non-code content"}],
#     result_format="message",
#     **SCENARIO_CONFIGS["code_generation"]
# )

More features

The previous sections described basic interaction methods. For more complex scenarios:

  • Multi-turn conversation: For scenarios that require continuous communication, such as asking follow-up questions or gathering information.

  • Streaming output: For scenarios that require immediate responses, such as chatbots and real-time code generation. It improves the user experience and helps prevent timeouts caused by long response times.

  • Deep thinking: For scenarios that require high-quality, well-structured, and in-depth answers, such as complex reasoning and policy analysis.

  • Structured output: When you need the model to respond in a stable JSON format for easy parsing by programs or for data processing.

  • Partial mode: For scenarios where the model needs to continue from existing text, such as code completion or long-form writing.

API reference

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

FAQ

Q: Why can't the Qwen API parse web links?

A: The Qwen API cannot directly access and parse web links. Use features such as Function calling or use web scraping tools such as Python's Beautiful Soup to read webpage content.

Q: Why are the responses from Qwen Chat and the Qwen API different?

A: It is because Qwen Chat includes additional engineering optimizations, such as parsing webpages, web search, image creation, and PPT generation, which are not natively supported by the model API. Use features such as web search and Function calling to enhance responses.

Q: Can the model directly generate files in Word, Excel, PDF, or PPT format?

A: No, it cannot. Text generation models in Model Studio output only plain text. To create files in these formats, you can convert the plain text output programmatically or using third-party libraries.