All Products
Search
Document Center

Alibaba Cloud Model Studio:Overview

Last Updated:Dec 11, 2025

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

The input for a text generation model can be simple keywords, a one-sentence summary, or more complex instructions and context. The model learns language patterns by analyzing massive amounts of data and has a wide range of applications:

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

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

  • Text translation: Perform fast and accurate cross-language translations.

  • Summary generation: Extract the key points from long articles, reports, and emails.

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

Model selection

Service regions

Alibaba Cloud Model Studio provides model services in the Singapore and China (Beijing) regions. Calling the service from a nearby region can reduce network latency.

General-purpose models

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

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

  • Qwen-Max: The best-performing model in the Qwen series. It is suitable for handling complex, multi-step tasks.

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

Scenario-specific models

For specific business needs, Model Studio offers various specialized and optimized models. These models cover domains such as code capabilities, long context, translation, data mining, intention recognition, role assumption, and deep research.

Multimodal models

  • Qwen-VL (Text + Image -> Text): Provides image understanding capabilities. It 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. It generates text and speech outputs to handle complex cross-modal tasks.

  • Speech recognition model (Audio -> Text): Recognizes and transcribes speech content in audio. It supports languages such as Chinese (including Cantonese and other dialects), English, Japanese, and Korean.

Third-party models

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

Core concepts

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

  • System message: Sets the role for the model to play or the instructions to follow. If not specified, it defaults to "You are a helpful assistant."

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

  • Assistant message: The model's response content.

When you call the model, you can construct a messages array with these message objects. A typical request consists of a system message that defines the code of conduct and a user message that provides the user's instruction.

system message is optional. You can use this message to define the model's role and code of conduct to achieve more stable and consistent output.
[
    {"role": "system", "content": "You are a helpful assistant. You need to provide accurate, efficient, and insightful responses, and be ready to help users with various tasks and questions."},
    {"role": "user", "content": "Who are you?"}
]

The output response object contains the model's assistant message.

{
    "role": "assistant",
    "content": "Hello! I am Qwen, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text, perform logical reasoning, and write code. 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: You have created an API key and exported the API key as an environment variable. To use an SDK, you must install the OpenAI or DashScope SDK.

Run the following code to start a conversation with the qwen-plus model. For higher-quality generation results, see Deep thinking.

OpenAI compatible

Python

import os
from openai import OpenAI

try:
    client = OpenAI(
        # The API keys for the Singapore and Beijing regions are different. 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 your Model Studio API key: api_key="sk-xxx",
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1
        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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

Java

// We recommend using OpenAI Java SDK v3.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 your Model Studio API key: .apiKey("sk-xxx")
                    // The API keys for the Singapore and Beijing regions are different. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    // The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1
                    .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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

Node.js

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

const openai = new OpenAI(
    {
        // The API keys for the Singapore and Beijing regions are different. 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 your Model Studio API key: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        // The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1
        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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

Go

// OpenAI Go SDK v2.4.0 or later is required.
package main

import (
	"context"
	// To view the full response, uncomment the following lines and the lines at the end of the code block.
	// "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 your Model Studio API key: apiKey := "sk-xxx"
	apiKey := os.Getenv("DASHSCOPE_API_KEY")
	client := openai.NewClient(
		option.WithAPIKey(apiKey),
		// The API keys for the Singapore and Beijing regions are different. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
		// The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1
		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 information about error messages, 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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

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 your Model Studio API key: string? apiKey = "sk-xxx";
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
        // The following is the base_url for the Singapore region. If you use a model in the Beijing region, replace the base_url with https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
        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 information about error messages, see https://www.alibabacloud.com/help/en/model-studio/error-code
                return $"Request failed: {response.StatusCode}";
            }
        }
    }
}

Response

I am Qwen, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

PHP (HTTP)

<?php
// Set the request URL.
// The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
$url = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions';
// The API keys for the Singapore and Beijing regions are different. 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 your Model Studio API key: $apiKey = "sk-xxx";
$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 if any errors occurred.
// For more information about error messages, 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.
$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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

curl

The base_url in the following example is for the Singapore region. If you are using a model in the Beijing region, replace the base_url with https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions.

The API keys for the Singapore and Beijing regions are different. For more information, see Get and configure 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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!"
            },
            "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

# The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/api/v1
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(
    # The API keys for the Singapore and Beijing regions are different. 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 your Model Studio API key: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model="qwen-plus",
    messages=messages,
    result_format="message",
)

if response.status_code == 200:
    print(response.output.choices[0].message.content)
    # To view the full response, uncomment the following line.
    # print(json.dumps(response, default=lambda o: o.__dict__, indent=4))
else:
    print(f"HTTP status 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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

Java

// We recommend using DashScope Java SDK v2.20.6 or later.
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 {
        // The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/api/v1
        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()
                // The API keys for the Singapore and Beijing regions are different. 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 your Model Studio API key: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }
    public static void main(String[] args) {
        try {
            GenerationResult result = callWithMessage();
            System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
            // 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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

Node.js (HTTP)

// Requires Node.js v18+.
// If you have not configured the environment variable, replace the following line with your Model Studio API key: const apiKey = "sk-xxx";
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 {
        // The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
        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 information about error messages, see https://www.alibabacloud.com/help/en/model-studio/error-code
        console.error('Call failed:', error.message);
    }
}

callApi();

Response

I am Qwen, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

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{}
	// The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
	req, _ := http.NewRequest("POST", "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation", bytes.NewBuffer(jsonData))

	// Set the 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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

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)
    {
        // The API keys for the Singapore and Beijing regions are different. 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 your Model Studio API key: string? apiKey = "sk-xxx";
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
        // Set the request URL and content.
        // The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
        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 the 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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!"
                }
            }
        ]
    },
    "usage": {
        "total_tokens": 92,
        "output_tokens": 66,
        "input_tokens": 26
    },
    "request_id": "09dceb20-ae2e-999b-85f9-xxxxxx"
}

PHP (HTTP)

<?php
// The following is the URL for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
$url = "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
// The API keys for the Singapore and Beijing regions are different. 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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!

curl

The base_url in the following example is for the Singapore region. If you are using a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation

The API keys for the Singapore and Beijing regions are different. For more information, see Get and configure 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, an extra-large language model developed by the Tongyi Lab of Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and scripts, perform logical reasoning, and write code. I can also express opinions and play games. If you have any questions or need help, feel free to ask me!"
                }
            }
        ]
    },
    "usage": {
        "total_tokens": 92,
        "output_tokens": 66,
        "input_tokens": 26
    },
    "request_id": "09dceb20-ae2e-999b-85f9-xxxxxx"
}

Image and video processing

Multimodal models support processing non-text data, such as images and videos, for tasks such as visual question answering and event detection. Invoking these models is different from invoking plain text models in the following ways:

  • Structure of a user message: User messages for multimodal models contain not only text but also multimodal information, such as images and audio.

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

For image and video file limitations, see visual understanding.

OpenAI compatible

Python

from openai import OpenAI
import os

client = OpenAI(
    # The API keys for the Singapore and Beijing regions are different. To obtain an API key, visit: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # If an 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"),
    # The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/compatible-mode/v1
    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 multi-modal 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(
    {
        // The API keys for the Singapore and Beijing regions are different. To obtain an API key, visit: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
        // If an environment variable is not configured, replace the following line with your Model Studio API key: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        // The following baseURL is for the Singapore region. If you use a model in the Beijing region, replace the baseURL with: https://dashscope.aliyuncs.com/compatible-mode/v1
        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 multi-modal model and modify the messages as needed.
        messages: messages
    });
    console.log(response.choices[0].message.content);
}

main()

curl

The base_url in the following example is for the Singapore region. If you are using a model in the Beijing region, replace the base_url with https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions.

The API keys for the Singapore and Beijing regions are different. For more information, see Get and configure 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 following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with https://dashscope.aliyuncs.com/api/v1.
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(
    # The API keys for the Singapore and Beijing regions are different. To obtain an API key, visit 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", and use your Model Studio API key.
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model='qwen3-vl-plus',  # You can replace the model with other multimodal models and modify the messages accordingly.
    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 following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with https://dashscope.aliyuncs.com/api/v1.
        Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
    }

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

    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()
                // The API keys for the Singapore and Beijing regions are different. To obtain an API key, visit: https://www.alibabacloud.com/help/en/model-studio/get-api-key
                // If no environment variable is configured, use your Model Studio API key to replace the following line with: .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

The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation

The API keys for the Singapore and Beijing regions are different. For more information, see Obtain and configure 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 calls improve the efficiency of handling high-concurrency requests.

OpenAI compatible

Python

import os
import asyncio
from openai import AsyncOpenAI
import platform

# Create an asynchronous client instance.
client = AsyncOpenAI(
    # API keys for the Singapore and Beijing regions are different. 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 your Model Studio API key: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/compatible-mode/v1
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
)

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

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

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

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 API.
        OpenAIClientAsync client = OpenAIOkHttpClientAsync.builder()
                // If the environment variable is not configured, replace the following line with your Model Studio API key: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1
                .baseUrl("https://dashscope-intl.aliyuncs.com/compatible-mode/v1")
                .build();

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

        // 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/en/model-studio/error-code");
                            return null;
                        });
                }).thenCompose(future -> future))
                .toArray(CompletableFuture[]::new);

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

DashScope

Asynchronous text generation with the DashScope SDK is only supported in Python.

# The DashScope Python SDK version must be 1.19.0 or later.
import asyncio
import platform
from dashscope.aigc.generation import AioGeneration
import os
import dashscope
# The following URL is for the Singapore region. To use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

# Define the asynchronous task.
async def task(question):
    print(f"Sending question: {question}")
    response = await AioGeneration.call(
        # If the environment variable is not set, replace the next line with your Model Studio API key: api_key="sk-xxx",
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        model="qwen-plus",  # Model List: 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 response order may differ from the example.
Sending question: Who are you?
Sending question: What can you do?
Sending question: What's the weather like?
Model response: Hello! I am Qwen, a large-scale language model developed by the Tongyi Lab at Alibaba Group. I can help you answer questions, create text such as stories, official documents, emails, and playbooks, 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 conditions.
Model response: I have many skills, such as:

1. **Answering questions**: I can try to help you answer academic questions, common sense questions, or questions about professional knowledge.
2. **Text creation**: 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 code writing, debugging, and optimization.
5. **Multilingual support**: I support multiple languages, including but not limited to Chinese, English, French, and Spanish.
6. **Expressing opinions**: I can 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

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

  • Prompt engineering: This technique involves designing and optimizing text instructions (prompts) to guide the model more precisely. This helps the model produce the desired output. For more information, see the Text-to-text prompt guide.

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

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

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

Control response diversity

temperature and top_p control the diversity of the generated text. A higher value results in more diverse content, while a lower value produces more deterministic content. To accurately evaluate the effect of these parameters, adjust only one at a time.

  • temperature: Range [0, 2). Adjusts randomness.

  • top_p: Range [0, 1]. Filters responses by 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 about a cat and a sunbeam."

  • High diversity (example temperature=0.9): Use for scenarios that require creativity, imagination, and novel expressions, such as creative writing, brainstorming, or marketing copy.

    Sunlight slanted onto the windowsill. The ginger cat crept toward the glowing tile, its fur instantly turning the color of melted honey.
    It extended a paw to tap the patch of light, sinking in as if stepping into a warm pool, and the sunlight flowed up its back from its paw pads.
    The afternoon suddenly grew heavy. The cat, curled in the flowing gold dust, heard time gently dissolve into its purrs.
  • High determinism (example temperature=0.1): Use for scenarios that require accurate, rigorous, and predictable content, such as factual Q&A, code generation, or legal texts.

    In the afternoon, an old cat curled on the windowsill, dozing among the sunbeams.
    Sunlight gently danced across its mottled back, like flipping through an old photo album.
    Dust floated up and settled down, as if time whispered: You were once young, and I was once warm.

How it works

temperature:

  • A higher temperature flattens the token probability distribution. This decreases the probability of high-probability tokens and increases the probability of low-probability tokens. This makes the model's choice for the next token more random.

  • A lower temperature makes the token probability distribution steeper. This makes high-probability tokens more likely to be selected, which causes the model to favor a small set of high-probability tokens.

top_p:

top_p sampling selects from a set of the most probable tokens. This method sorts all possible next tokens by probability in descending order. It then accumulates their probabilities, starting from the most likely token, until the sum reaches the specified top_p threshold. For example, a top_p value of 0.8 corresponds to 80%. The model then randomly selects the next token from this group.

  • A higher top_p value considers more tokens, which makes the generated text more diverse.

  • A lower top_p value considers fewer tokens, which makes the generated text more focused and deterministic.

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

# OpenAI usage example
# completion = client.chat.completions.create(
#     model="qwen-plus",
#     messages=[{"role": "user", "content": "Write a poem about the moon"}],
#     **SCENARIO_CONFIGS["creative_writing"]
# )
# DashScope usage example
# 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 describe basic interaction methods. For more complex scenarios, consider the following features:

  • Multi-turn conversation: This feature is used in scenarios that require continuous communication, such as follow-up questions and information gathering.

  • Streaming output: This feature is used in scenarios that require instant 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: This feature is used in scenarios that require high-quality, structured, and in-depth answers, such as complex reasoning and policy analysis.

  • Structured output: This feature is used when you need the model to respond in a stable JSON format. This is useful for programmatic calls or data parsing.

  • Partial mode: This feature is used in scenarios such as code completion and long-form writing where the model needs to continue from existing text.

API reference

For a complete list of model call parameters, see OpenAI-compatible API reference and DashScope API reference.

FAQ

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

A: The Qwen API cannot directly access and parse web links. You can use features such as Function Calling and , or web scraping tools such as Python's Beautiful Soup to read information from web pages.

Q: Why are the responses from web-based Qwen and the Qwen API inconsistent?

A: Web-based Qwen includes additional engineering optimizations built on top of the Qwen API. This enables features such as web page parsing, web search, graph drawing, and creating PPTs. These features are not native capabilities of the model API. You can use features such as Function calling to optimize the model's results.

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 using code or third-party libraries.