All Products
Search
Document Center

Alibaba Cloud Model Studio:Qwen API reference

Last Updated:Mar 22, 2025

This topic describes the request and response parameters of the Tongyi Qianwen API.

For information about model introduction, selection recommendations, and usage methods, see ,Text generation.

You can call the Qwen API by using OpenAI compatibility mode or DashScope.

OpenAI compatibility

Public cloud

base_url to configure when using SDK: https://dashscope-intl.aliyuncs.com/compatible-mode/v1

endpoint to configure when using HTTP: POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions

You need to obtain an API key and configure the API key in environment variables. If you call through OpenAI SDK, you also need to install the SDK.

Request body

Text input

This example demonstrates a single-round conversation. You can also perform multi-round conversations.

Python

import os
from openai import OpenAI

client = OpenAI(
    # If environment variables are not configured, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"), 
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
    model="qwen-plus", # This example uses qwen-plus. You can change the model name as needed. Model list: https://help.aliyun.com/zh/model-studio/getting-started/models
    messages=[
        {'role': 'system', 'content': 'You are a helpful assistant.'},
        {'role': 'user', 'content': 'Who are you?'}],
    )
    
print(completion.model_dump_json())

Java

Request example

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ChatCompletion;
import com.openai.models.ChatCompletionCreateParams;

public class Main {
    public static void main(String[] args) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .baseUrl("https://dashscope-intl.aliyuncs.com/compatible-mode/v1")
                .build();
        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addUserMessage("Who are you")
                .model("qwen-plus")
                .build();
        ChatCompletion chatCompletion = client.chat().completions().create(params);
        System.out.println(chatCompletion.choices().get(0).message().content().orElse("No returned content"));
    }
}

Node.js

import OpenAI from "openai";

const openai = new OpenAI(
    {
        // If environment variables are not configured, replace the following line with: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
    }
);

async function main() {
    const completion = await openai.chat.completions.create({
        model: "qwen-plus",  // This example uses qwen-plus. You can change the model name as needed. Model list: https://help.aliyun.com/zh/model-studio/getting-started/models
        messages: [
            { role: "system", content: "You are a helpful assistant." },
            { role: "user", content: "Who are you?" }
        ],
    });
    console.log(JSON.stringify(completion))
}

main();

Go

package main

import (
	"context"
	"os"

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

func main() {
	client := openai.NewClient(
		option.WithAPIKey(os.Getenv("DASHSCOPE_API_KEY")), // defaults to os.LookupEnv("OPENAI_API_KEY")
		option.WithBaseURL("https://dashscope-intl.aliyuncs.com/compatible-mode/v1/"),
	)
	chatCompletion, err := client.Chat.Completions.New(
		context.TODO(), openai.ChatCompletionNewParams{
			Messages: openai.F(
				[]openai.ChatCompletionMessageParamUnion{
					openai.UserMessage("Who are you"),
				},
			),
			Model: openai.F("qwen-plus"),
		},
	)

	if err != nil {
		panic(err.Error())
	}

	println(chatCompletion.Choices[0].Message.Content)
}

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)
    {
        // If environment variables are not configured, replace the following line with: string? apiKey = "sk-xxx";
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");

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

        // Configure request URL and content
        string url = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions";
        // This example uses qwen-plus. You can change the model name as needed. Model list: https://help.aliyun.com/zh/model-studio/getting-started/models
        string jsonContent = @"{
            ""model"": ""qwen-plus"",
            ""messages"": [
                {
                    ""role"": ""system"",
                    ""content"": ""You are a helpful assistant.""
                },
                {
                    ""role"": ""user"", 
                    ""content"": ""Who are you?""
                }
            ]
        }";

        // Send request and obtain response
        string result = await SendPostRequestAsync(url, jsonContent, apiKey);

        // Print result
        Console.WriteLine(result);
    }

    private static async Task SendPostRequestAsync(string url, string jsonContent, string apiKey)
    {
        using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
        {
            // Configure request headers
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Send request and obtain response
            HttpResponseMessage response = await httpClient.PostAsync(url, content);

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

PHP (HTTP)

Curl

curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-plus",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user", 
            "content": "Who are you?"
        }
    ]
}'

Streaming output

For more information, see Streaming output.

Python

import os
from openai import OpenAI

client = OpenAI(
    # If environment variables are not configured, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
    model="qwen-plus",  # This example uses qwen-plus. You can change the model name as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': 'Who are you?'}],
    stream=True,
    stream_options={"include_usage": True}
    )
for chunk in completion:
    print(chunk.model_dump_json())

Node.js

import OpenAI from "openai";

const openai = new OpenAI(
    {
        // If environment variables are not configured, replace the following line with: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
    }
);

async function main() {
    const completion = await openai.chat.completions.create({
        model: "qwen-plus", // This example uses qwen-plus. You can change the model name as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
        messages: [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Who are you?"}
        ],
        stream: true,
    });
    for await (const chunk of completion) {
        console.log(JSON.stringify(chunk));
    }
}

main();

Curl

curl --location "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
    "model": "qwen-plus",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user", 
            "content": "Who are you?"
        }
    ],
    "stream":true
}'

Image input

For more information about how models analyze images, see Visual understanding.

Python

import os
from openai import OpenAI

client = OpenAI(
    # If environment variables are not configured, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
    model="qwen-vl-plus",  # This example uses qwen-vl-plus. You can change the model name as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    messages=[{"role": "user","content": [
            {"type": "text","text": "What is this"},
            {"type": "image_url",
             "image_url": {"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"}}
            ]}]
    )
print(completion.model_dump_json())

Node.js

import OpenAI from "openai";

const openai = new OpenAI(
    {
        // If environment variables are not configured, replace the following line with: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
    }
);

async function main() {
    const response = await openai.chat.completions.create({
        model: "qwen-vl-plus", // This example uses qwen-vl-plus. You can change the model name as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
        messages: [{role: "user",content: [
            { type: "text", text: "What is this?" },
            { type: "image_url",image_url: {"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"}}
        ]}]
    });
    console.log(JSON.stringify(response));
}

main();

Curl

curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
  "model": "qwen-vl-plus",
  "messages": [{
      "role": "user",
      "content": 
      [{"type": "text","text": "What is this"},
       {"type": "image_url","image_url": {"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"}}]
    }]
}'

Function calling

For more information, see Function calling.

Python

import os
from openai import OpenAI

client = OpenAI(
    # If environment variables are not configured, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",  # Fill in the base_url of DashScope SDK
)

tools = [
    # Tool 1: obtain the current time
    {
        "type": "function",
        "function": {
            "name": "get_current_time",
            "description": "This tool can help you query the current time.",
            "parameters": {}  # No request parameter is needed. The parameters parameter is left empty.
        }
    },  
    # Tool 2: obtain the weather of a specific city
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "This tool can help you query the weather of a city.",
            "parameters": {  
                "type": "object",
                "properties": {
                    # The location parameter is required to query the weather.
                    "location": {
                        "type": "string",
                        "description": "A city, county, or district, such as Beijing, Hangzhou, or Yuhang."
                    }
                },
                "required": ["location"]
            }
        }
    }
]
messages = [{"role": "user", "content": "What's the weather like in Hangzhou"}]
completion = client.chat.completions.create(
    model="qwen-plus",  # This example uses qwen-plus. You can change the model name as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    messages=messages,
    tools=tools
)

print(completion.model_dump_json())

Node.js

import OpenAI from "openai";

const openai = new OpenAI(
    {
        // If environment variables are not configured, replace the following line with: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
    }
);

const messages = [{"role": "user", "content": "What's the weather like in Hangzhou"}];
const tools = [
// Tool 1: obtain the current time
{
    "type": "function",
    "function": {
        "name": "get_current_time",
        "description": "This tool can help you query the current time.",
        // No request parameter is needed. The parameters parameter is left empty.
        "parameters": {}  
    }
},  
// Tool 2: obtain the weather of a specific city
{
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "This tool can help you query the weather of a city.",
        "parameters": {  
            "type": "object",
            "properties": {
                // The location parameter is required to query the weather.
                "location": {
                    "type": "string",
                    "description": "A city, county, or district, such as Beijing, Hangzhou, or Yuhang."
                }
            },
            "required": ["location"]
        }
    }
}
];

async function main() {
    const response = await openai.chat.completions.create({
        model: "qwen-plus", // This example uses qwen-plus. You can change the model name as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
        messages: messages,
        tools: tools,
    });
    console.log(JSON.stringify(response));
}

main();

Curl

curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-plus",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user", 
            "content": "What's the weather like in Hangzhou"
        }
    ],
    "tools": [
    {
        "type": "function",
        "function": {
            "name": "get_current_time",
            "description": "This tool can help you query the current time.",
            "parameters": {}
        }
    },
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "This tool can help you querythe weather of a city.",
            "parameters": {
                "type": "object",
                "properties": {
                    "location":{
                        "type": "string",
                        "description": "A city, county, or district, such as Beijing, Hangzhou, or Yuhang."
                    }
                },
                "required": ["location"]
            }
        }
    }
  ]
}'

Asynchronous invocation

import os
import asyncio
from openai import AsyncOpenAI
import platform

client = AsyncOpenAI(
    # If environment variables are not configured, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)

async def main():
    response = await client.chat.completions.create(
        messages=[{"role": "user", "content": "Who are you"}],
        model="qwen-plus",  # This example uses qwen-plus. You can change the model name as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    )
    print(response.model_dump_json())

if platform.system() == "Windows":
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())

model string (Required)

Model name.

Supported models: Tongyi Qianwen large language model (Commercial Edition, Open Source Edition), Tongyi Qianwen VL

For more information about model names and billing, see Model List.

messages array (Required)

A list of messages that comprise the conversation history.

Message types

System Message object (Optional)

Defines the objective or role of the model. If specified, it must be placed at the beginning of the messages list.

Properties

content string (Required)

The message content.

role string (Required)

Must be set to system.

User Message object (Required)

The message sent from the user to the model.

Properties

content string or array (Required)

The message content. Use string type for text-only input. Use array type for multi-modal input such as images.

Properties for multi-modal models

type string (Required)

Valid values:

  • "text"

  • "image_url"

text string

Required when type is set to "text".

The input text.

image_url object

Required when type is set to "image_url".

The input image information. Example:

{
    "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"
}

Properties

url string(Required)

The URL or local path of the image. For information about local file input, see Visual understanding.

role string (Required)

Must be set to user.

Assistant Message object (Optional)

The model's response to the user message.

Properties

content string (Optional)

The message content. Optional only when the tool_calls parameter is specified.

role string (Required)

Must be set to assistant.

partial boolean (Optional)

Enables Partial Mode. For more information, see Prefix continuation (Partial Mode).

Supported models

  • Qwen-Max

    qwen-max, qwen-max-latest, qwen-max-0125

  • Qwen-Plus

    qwen-plus, qwen-plus-latest, qwen-plus-0125

  • Qwen-Turbo

    qwen-turbo, qwen-turbo-latest, qwen-turbo-1101

  • Open source Qwen

    qwen2.5 models

tool_calls array (Optional)

Specifies the tools and parameters the model wants to call after Function Calling is initiated. Contains one or more objects. The value comes from the tool_calls field in the previous model response.

Properties

id string

The tool response ID.

type string

The tool type. Only function is supported.

function object

The function to call.

Properties

name string

The name of the function to call.

arguments string

The input parameters for the tool in JSON string format.

index integer

The index of this tool information in the tool_calls list.

Tool Message object (Optional)

The tool output information.

Properties

content string (Required)

The message content, typically the tool function output.

role string (Required)

Must be set to tool.

tool_call_id string (Optional)

The ID returned after Function Calling initiation, obtained from completion.choices[0].message.tool_calls[0].id. Used to identify the corresponding tool for this Tool Message.

stream boolean (Optional) Default value: false

Specifies whether to enable streaming output. Valid values:

  • false: The model returns the complete response after generating all content.

  • true: The model streams the output by returning content chunks as they are generated. You must read these chunks in real time to obtain the complete response.

stream_options object (Optional)

When streaming output is enabled, you can set this parameter to {"include_usage": true} to display the token count in the last line of the output.

If this parameter is set to false, the token count will not appear in the last line.

This parameter is effective only when stream is set to true.

temperature float (Optional)

The sampling temperature parameter that controls the randomness of text generation by the model.

A higher temperature value produces more diverse text, while a lower value produces more focused and deterministic output.

Valid values: [0, 2)

Because temperature and top_p both control output diversity, you should set only one of these parameters. For more information, see Temperature and top_p.

top_p float (Optional)

A probability threshold for nucleus sampling that determines the diversity of generated text.

Higher top_p values produce more diverse text, while lower values produce more focused and deterministic text.

Valid values: (0,1.0]

Because both temperature and top_p control text diversity, we recommend setting only one of these parameters. For more information, see Temperature and top_p.

presence_penalty float (Optional)

Controls the level of repetition in the model-generated text.

Valid values: [-2.0, 2.0]. Positive values reduce repetition. Negative values increase repetition.

Scenarios:

Higher presence_penalty values are appropriate for scenarios that require diversity, creativity, or originality, such as creative writing or brainstorming.

Lower presence_penalty values are suitable for scenarios that require consistency or technical terminology, such as technical documentation or formal writing.

Default presence_penalty values

qwen-max, qwen-max-latest, qwen-vl-max: 1.5

All others: 0.0

Working principle

When the parameter value is positive, the model applies a penalty to tokens that already appear in the current text (regardless of their frequency). This reduces the likelihood of token repetition, resulting in more diverse vocabulary and less repetitive content.

Examples

Prompt: Translate this sentence into Chinese "This movie is good. The plot is good, the acting is good, the music is good, and overall, the whole movie is just good. It is really good, in fact. The plot is so good, and the acting is so good, and the music is so good."

Parameter value 2.0: 这部电影很好。剧情很棒,演技棒,音乐也非常好听,总的来说,整部电影都好得不得了。实际上它真的很优秀。剧情非常精彩,演技出色,音乐也是那么的动听。

Parameter value 0.0: 这部电影很好。剧情好,演技好,音乐也好,总的来说,整部电影都很好。事实上,它真的很棒。剧情非常好,演技也非常出色,音乐也同样优秀。

Parameter value -2.0: 这部电影很好。情节很好,演技很好,音乐也很好,总的来说,整部电影都很好。实际上,它真的很棒。情节非常好,演技也非常好,音乐也非常好。

response_format object (Optional) Default value: {"type": "text"}

The format of the response. Valid values: {"type": "text"} or {"type": "json_object"}. When you set this parameter to {"type": "json_object"}, the output is returned in standard JSON format. For more information, see Structured output (JSON Mode).

If you set this parameter to {"type": "json_object"}, you must include instructions in the System Message or User Message to guide the model to output in JSON format, such as "Please output in JSON format."

Supported models

  • Qwen-Max

    qwen-max, qwen-max-latest, qwen-max-0125

  • Qwen-Plus

    qwen-plus, qwen-plus-latest, qwen-plus-0125

  • Qwen-Turbo

    qwen-turbo, qwen-turbo-latest, qwen-turbo-1101

  • Open source Qwen

    qwen2.5 models

max_tokens integer (Optional)

The maximum number of tokens to return in this request.

max_tokens setting does not affect the generation process of the large model. If the number of tokens generated by the model exceeds max_tokens, the request returns the truncated content.

Both the default value and maximum value are equal to the maximum output length of the model. For more information about the maximum output length of each model, see Model list.

The max_tokens parameter is useful in scenarios that require word count limits (such as generating summaries or keywords), cost control, or response time reduction.

seed integer (Optional)

The seed parameter controls the deterministic behavior of text generation. You can use it to ensure consistent model outputs across different runs.

If you specify the same seed value and maintain other parameters unchanged in each model call, the model will generate the same results.

Valid values: 0 to 231−1.

stop string or array (Optional)

The model automatically stops generating text when the output is about to contain the specified string or token_id.

You can specify sensitive words in the stop parameter to control the model output.

When stop is an array type, all elements must be either token_ids or strings. For example, you cannot specify stop as ["hello",104307].

tools array (Optional)

An array of tools that the model can call. It can include one or more tool objects. The model selects a tool during a Function Calling flow.

This feature is currently not supported for Tongyi Qianwen VL.

Properties

type string (Required)

The type of tools. Currently, only function is supported.

function object (Required)

Properties

name string (Required)

The name of the tool function. It must contain only letters, numbers, underscores, and hyphens. The maximum length is 64 characters.

description string (Required)

The description of the tool function that helps the model determine when and how to call the tool function.

parameters object (Required)

The parameter description of the tool. It must be a valid JSON Schema. For more information, see this link. If the parameters parameter is empty, the function has no request parameters.

tool_choice string or object (Optional) Default value: "auto"

You can use the tool_choice parameter to control tool selection strategies (such as enforcing a specific tool, requiring tool usage, or disabling tools) for different types of questions. Valid values:

  • "auto"

    The large model determines the tool strategy automatically.

  • "none"

    To prevent Function Calling from using any tools regardless of the input question, you can set the tool_choice parameter to "none".

  • {"type": "function", "function": {"name": "the_function_to_call"}}

    To force Function Calling to use a specific tool, you can set the tool_choice parameter to {"type": "function", "function": {"name": "the_function_to_call"}}, where the_function_to_call represents your specified function name.

parallel_tool_calls boolean (Optional) Default value: false

Specifies whether to enable parallel tool invocation. This feature is enabled when the parameter is set to true and disabled when set to false. For more information, see Parallel tool invocation.

translation_options object (Optional)

The translation parameters that you need to configure when using the translation model.

Properties

source_lang string (Required)

The English name of the source language. For more information, see Supported languages. You can set source_lang to "auto" to allow the model to automatically determine the language of the input text.

target_lang string (Required)

The English name of the target language. For more information, see Supported languages.

terms arrays (Optional)

The term array that you need to configure when using the term intervention translation feature.

Properties

source string (Required)

The term in the source language.

target string (Required)

The term in the target language.

tm_list arrays (Optional)

The translation memory array that you need to configure when using the translation memory feature.

Properties

source string (Required)

The statement in the source language.

target string (Required)

The statement in the target language.

domains string (Optional)

The domain prompt statement that you need to configure when using the domain prompt feature.

The domain prompt statement supports only English.
If you make the call through Python SDK, configure through extra_body. The configuration method is: extra_body={"translation_options": xxx}.

Chat response object (non-streaming)

{
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "I am Tongyi Qianwen, a large language model developed by Alibaba Cloud."
            },
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null
        }
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 3019,
        "completion_tokens": 104,
        "total_tokens": 3123,
        "prompt_tokens_details": {
            "cached_tokens": 2048
        }
    },
    "created": 1735120033,
    "system_fingerprint": null,
    "model": "qwen-plus",
    "id": "chatcmpl-6ada9ed2-7f33-9de2-8bb0-78bd4035025a"
}

id string

The unique identifier of this request.

choices array

An array of content generated by the model that contains one or more choices objects.

Properties

finish_reason string

The reason why the model stops generating content. Valid values:

  • stop: The model stops because the stop condition in the input parameters is triggered or the output naturally ends.

  • length: The model stops because the generated content exceeds the length limit.

  • tool_calls: The model stops because it needs to call tools.

index integer

The sequence number of the current response in the choices array.

logprobs object

This parameter is fixed as null.

message object

The message output by the model in this request.

Properties

content string

The text generated by the model in this request.

refusal string

This parameter is fixed as null.

role string

The role of the message, fixed as assistant.

audio object

This parameter is fixed as null.

function_call (to be deprecated) object

This value defaults to null. Please refer to the tool_calls parameter.

tool_calls array

After initiating Function Calling, this contains the tools to be called and their required parameters. It can contain one or more tool response objects.

Properties

id string

The ID of this tool response.

type string

The type of tool. Currently only supports function.

function object

The function to be called.

Properties

name string

The name of the function to be called.

arguments string

The parameters to be input to the tool, in JSON string format.

Because the large language model responses have some randomness, the output JSON string might not match your function requirements. You should validate the parameters before inputting them into the function.

index integer

The index of the tool information in the tool_calls list.

created integer

The timestamp when this chat request was created.

model string

The name of the model used for this chat request.

object string

Always chat.completion.

service_tier string

This parameter is fixed as null.

system_fingerprint string

This parameter is fixed as null.

usage object

The token information used in this chat request.

Properties

completion_tokens integer

The length of the model's response after being converted to tokens.

prompt_tokens integer

The length of the user's input after being converted to tokens.

total_tokens integer

The sum of prompt_tokens and completion_tokens.

completion_tokens_details object

This parameter is fixed as null.

prompt_tokens_details object

The granular classification of input tokens.

Properties

audio_tokens integer

This parameter is fixed as null.

cached_tokens integer

The number of tokens that hit the cache. For more information, see Context Cache.

Chat response chunk object (streaming output)

{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[{"delta":{"content":"","function_call":null,"refusal":null,"role":"assistant","tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[{"delta":{"content":"I am","function_call":null,"refusal":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[{"delta":{"content":"from","function_call":null,"refusal":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[{"delta":{"content":"Alibaba","function_call":null,"refusal":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[{"delta":{"content":"Cloud's large-scale","function_call":null,"refusal":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[{"delta":{"content":"language model, I am","function_call":null,"refusal":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[{"delta":{"content":"Tongyi Qian","function_call":null,"refusal":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[{"delta":{"content":"wen.","function_call":null,"refusal":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[{"delta":{"content":"","function_call":null,"refusal":null,"role":null,"tool_calls":null},"finish_reason":"stop","index":0,"logprobs":null}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-e30f5ae7-3063-93c4-90fe-beb5f900bd57","choices":[],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":{"completion_tokens":17,"prompt_tokens":22,"total_tokens":39,"completion_tokens_details":null,"prompt_tokens_details":{"audio_tokens":null,"cached_tokens":0}}}

id string

A unique identifier for this call. All chunk objects share the same ID.

choices array

An array containing one or more model-generated content objects. If include_usage is set to true, the last chunk will be empty.

Properties

delta object

The incremental object for the chat request.

Properties

content string

The message content of the chunk.

reasoning_content string

The deep thinking content of the QwQ model.

function_call object

This value defaults to null. For more information, see the tool_calls parameter.

refusal object

This parameter is fixed as null.

role string

The role of the incremental message object. This value appears only in the first chunk.

tools_calls array

The tools to be called by the model response and their required parameters. Can contain one or more tool response objects.

Properties

index integer

The index of the tool information in the tool_calls list.

id string

The ID of this tool response.

function object

The function that needs to be called.

Properties

arguments string

The parameters required by the tool. The complete JSON string is formed by concatenating the arguments of all chunks.

Due to the randomness of large model responses, the output JSON string might not match your function. You should validate the parameters before using them in the function.

name string

The function name. This value appears only in the first chunk.

type string

The type of tool. Currently only supports function.

finish_reason string

There are four possible values:

  • stop: Triggered by stop conditions in input parameters or natural output stop

  • null: Generation is not finished

  • length: Ended due to excessive generation length

  • tool_calls: Ended due to need to call tools

index integer

The sequence number of the current response in the choices list.

created integer

The timestamp when this chat request was created. All chunk objects share the same timestamp.

model string

The model name used for this chat request.

object string

Always chat.completion.chunk.

service_tier string

This parameter is fixed as null.

system_fingerprint string

This parameter is fixed as null.

usage object

The token information used for this chat request. Only appears in the last chunk when include_usage is true.

Properties

completion_tokens integer

The length of the model-generated response in tokens.

prompt_tokens integer

The length of the user input in tokens.

total_tokens integer

The sum of prompt_tokens and completion_tokens.

completion_tokens_details object

This parameter is fixed as null.

prompt_tokens_details object

The detailed classification of token usage.

Properties

audio_tokens integer

This parameter is fixed as null.

cached_tokens integer

The number of tokens that hit the cache. For more information about Context Cache, see Context Cache.

DashScope

Public cloud

  • HTTP Endpoint Requirements

    For Tongyi Qianwen large language model: POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation

    For Tongyi Qianwen VL model: POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation

  • SDK Base URL Requirements

    • Python Code

      dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

    • Java Code

      • Method 1

        import com.alibaba.dashscope.protocol.Protocol;
        Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
      • Method 2

        import com.alibaba.dashscope.utils.Constants;
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
You need to obtain an API key and set it as an environment variable. To call DashScope through its SDK, you also need to install the DashScope SDK.

Request body

Text input

This example demonstrates single-round conversation. For more information, see multi-round conversations.

Python

import os
import dashscope

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [
    {'role': 'system', 'content': 'You are a helpful assistant.'},
    {'role': 'user', 'content': 'Who are you?'}
    ]
response = dashscope.Generation.call(
    # If environment variable is not configured, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model="qwen-plus", # This example uses qwen-plus. You can change to other models as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    messages=messages,
    result_format='message'
    )
print(response)

Java

// Recommended dashscope SDK version >= 2.12.0
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.utils.JsonUtils;
import com.alibaba.dashscope.protocol.Protocol;

public class Main {
    public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("Who are you?")
                .build();
        GenerationParam param = GenerationParam.builder()
                // If environment variable is not configured, replace the following line with: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // This example uses qwen-plus. You can change to other models as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
                .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(JsonUtils.toJson(result));
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            // Use logging framework to record exceptions
            System.err.println("An error occurred while calling the generation service: " + e.getMessage());
        }
        System.exit(0);
    }
}

PHP (HTTP)

Node.js (HTTP)

DashScope does not provide an SDK for Node.js environment. For information about calling using OpenAI Node.js SDK, see the OpenAI section in this topic.

import fetch from 'node-fetch';

const apiKey = process.env.DASHSCOPE_API_KEY;

const data = {
    model: "qwen-plus", // This example uses qwen-plus. You can change to other models as needed. Model list: https://help.aliyun.com/zh/model-studio/getting-started/models
    input: {
        messages: [
            {
                role: "system",
                content: "You are a helpful assistant."
            },
            {
                role: "user",
                content: "Who are you?"
            }
        ]
    },
    parameters: {
        result_format: "message"
    }
};

fetch('https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation', {
    method: 'POST',
    headers: {
        'Authorization': `Bearer ${apiKey}`,
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
    console.log(JSON.stringify(data));
})
.catch(error => {
    console.error('Error:', error);
});

C# (HTTP)

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

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

    static async Task Main(string[] args)
    {
        // If environment variable is not configured, replace the following line with: string? apiKey = "sk-xxx";
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");

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

        // Configure request URL and content
        string url = "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
        // This example uses qwen-plus. You can change to other models as needed. Model list: https://help.aliyun.com/zh/model-studio/getting-started/models
        string jsonContent = @"{
            ""model"": ""qwen-plus"", 
            ""input"": {
                ""messages"": [
                    {
                        ""role"": ""system"",
                        ""content"": ""You are a helpful assistant.""
                    },
                    {
                        ""role"": ""user"",
                        ""content"": ""Who are you?""
                    }
                ]
            },
            ""parameters"": {
                ""result_format"": ""message""
            }
        }";

        // Send request and obtain response
        string result = await SendPostRequestAsync(url, jsonContent, apiKey);

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

    private static async Task SendPostRequestAsync(string url, string jsonContent, string apiKey)
    {
        using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
        {
            // Configure request headers
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Send request and obtain response
            HttpResponseMessage response = await httpClient.PostAsync(url, content);

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

Go (HTTP)

DashScope does not provide an SDK for Go. For information about calling using OpenAI Go SDK, see the OpenAI-Go section in this topic.

package main

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

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type Input struct {
	Messages []Message `json:"messages"`
}

type Parameters struct {
	ResultFormat string `json:"result_format"`
}

type RequestBody struct {
	Model      string     `json:"model"`
	Input      Input      `json:"input"`
	Parameters Parameters `json:"parameters"`
}

func main() {
	// Create HTTP client
	client := &http.Client{}

	// Build request body
	requestBody := RequestBody{
		// This example uses qwen-plus. You can change to other models as needed. Model list: https://help.aliyun.com/zh/model-studio/getting-started/models
		Model: "qwen-plus",
		Input: Input{
			Messages: []Message{
				{
					Role:    "system",
					Content: "You are a helpful assistant.",
				},
				{
					Role:    "user",
					Content: "Who are you?",
				},
			},
		},
		Parameters: Parameters{
			ResultFormat: "message",
		},
	}

	jsonData, err := json.Marshal(requestBody)
	if err != nil {
		log.Fatal(err)
	}

	// Create POST request
	req, err := http.NewRequest("POST", "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation", bytes.NewBuffer(jsonData))
	if err != nil {
		log.Fatal(err)
	}

	// Configure request headers
	// If environment variable is not configured, replace the following line with: apiKey := "sk-xxx"
	apiKey := os.Getenv("DASHSCOPE_API_KEY")
	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")

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

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

	// Print response content
	fmt.Printf("%s\n", bodyText)
}

Curl

curl --location "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
    "model": "qwen-plus",
    "input":{
        "messages":[      
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Who are you?"
            }
        ]
    },
    "parameters": {
        "result_format": "message"
    }
}'

Streaming output

For more information, see Streaming output.

Python

import os
import dashscope

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [
    {'role':'system','content':'you are a helpful assistant'},
    {'role': 'user','content': 'Who are you?'}
    ]
responses = dashscope.Generation.call(
    # If environment variable is not configured, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    # This example uses qwen-plus. You can change to other models as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    model="qwen-plus",
    messages=messages,
    result_format='message',
    stream=True,
    incremental_output=True
    )
for response in responses:
    print(response)  

Java

import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.utils.JsonUtils;
import io.reactivex.Flowable;
import java.lang.System;
import com.alibaba.dashscope.protocol.Protocol;

public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);
    private static void handleGenerationResult(GenerationResult message) {
        System.out.println(JsonUtils.toJson(message));
    }
    public static void streamCallWithMessage(Generation gen, Message userMsg)
            throws NoApiKeyException, ApiException, InputRequiredException {
        GenerationParam param = buildGenerationParam(userMsg);
        Flowable result = gen.streamCall(param);
        result.blockingForEach(message -> handleGenerationResult(message));
    }
    private static GenerationParam buildGenerationParam(Message userMsg) {
        return GenerationParam.builder()
                // If environment variable is not configured, replace the following line with: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // This example uses qwen-plus. You can change to other models as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(Arrays.asList(userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .incrementalOutput(true)
                .build();
    }
    public static void main(String[] args) {
        try {
            Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
            Message userMsg = Message.builder().role(Role.USER.getValue()).content("Who are you?").build();
            streamCallWithMessage(gen, userMsg);
        } catch (ApiException | NoApiKeyException | InputRequiredException  e) {
            logger.error("An exception occurred: {}", e.getMessage());
        }
        System.exit(0);
    }
}

Curl

curl --location "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--header "X-DashScope-SSE: enable" \
--data '{
    "model": "qwen-plus",
    "input":{
        "messages":[      
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Who are you?"
            }
        ]
    },
    "parameters": {
        "result_format": "message",
        "incremental_output":true
    }
}'

Image input

For more information about how models analyze images, see Visual understanding.

Python

import os
import dashscope

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [
    {
        "role": "user",
        "content": [
            {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
            {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"},
            {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/rabbit.png"},
            {"text": "What are these?"}
        ]
    }
]
response = dashscope.MultiModalConversation.call(
    # If environment variable is not configured, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    # This example uses qwen-vl-max. You can change to other models as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    model='qwen-vl-max',
    messages=messages
    )
print(response)

Java

// Copyright (c) Alibaba, Inc. and its affiliates.

import java.util.Arrays;
import java.util.Collections;
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.JsonUtils;
import com.alibaba.dashscope.utils.Constants;
public class Main {
    static {
     Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void simpleMultiModalConversationCall()
            throws ApiException, NoApiKeyException, UploadFileException {
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                .content(Arrays.asList(
                        Collections.singletonMap("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"),
                        Collections.singletonMap("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"),
                        Collections.singletonMap("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/rabbit.png"),
                        Collections.singletonMap("text", "What are these?"))).build();
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                // If environment variable is not configured, replace the following line with: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // This example uses qwen-vl-plus. You can change to other models as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
                .model("qwen-vl-plus")
                .message(userMessage)
                .build();
        MultiModalConversationResult result = conv.call(param);
        System.out.println(JsonUtils.toJson(result));
    }

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

Curl

curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "model": "qwen-vl-plus",
    "input":{
        "messages":[
            {
                "role": "user",
                "content": [
                    {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
                    {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"},
                    {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/rabbit.png"},
                    {"text": "What are these?"}
                ]
            }
        ]
    }
}'

Tool calling

For the complete Function Calling process code, see Text generation.

Python

import os
import dashscope

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_time",
            "description": "This tool can help you query the current time.",
            "parameters": {}
        }
    },
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "This tool can help you query the weather of a city.",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "A city, county, or district, such as Beijing, Hangzhou, or Yuhang."
                    }
                }
            },
            "required": [
                "location"
            ]
        }
    }
]
messages = [{"role": "user", "content": "How's the weather in Hangzhou?"}]
response = dashscope.Generation.call(
    # If environment variable is not configured, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    # This example uses qwen-plus. You can change to other models as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    model='qwen-plus',
    messages=messages,
    tools=tools,
    result_format='message'
)
print(response)

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.alibaba.dashscope.aigc.conversation.ConversationParam.ResultFormat;
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.tools.FunctionDefinition;
import com.alibaba.dashscope.tools.ToolFunction;
import com.alibaba.dashscope.utils.JsonUtils;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.victools.jsonschema.generator.Option;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.alibaba.dashscope.protocol.Protocol;

public class Main {
    public class GetWeatherTool {
        private String location;
        public GetWeatherTool(String location) {
            this.location = location;
        }
        public String call() {
            return location + " is sunny today";
        }
    }
    public class GetTimeTool {
        public GetTimeTool() {
        }
        public String call() {
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String currentTime = "Current time: " + now.format(formatter) + ".";
            return currentTime;
        }
    }
    public static void SelectTool()
            throws NoApiKeyException, ApiException, InputRequiredException {
        SchemaGeneratorConfigBuilder configBuilder =
                new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON);
        SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES)
                .without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build();
        SchemaGenerator generator = new SchemaGenerator(config);
        ObjectNode jsonSchema_weather = generator.generateSchema(GetWeatherTool.class);
        ObjectNode jsonSchema_time = generator.generateSchema(GetTimeTool.class);
        FunctionDefinition fdWeather = FunctionDefinition.builder().name("get_current_weather").description("Get weather for a specific area")
                .parameters(JsonUtils.parseString(jsonSchema_weather.toString()).getAsJsonObject()).build();
        FunctionDefinition fdTime = FunctionDefinition.builder().name("get_current_time").description("Get current time")
                .parameters(JsonUtils.parseString(jsonSchema_time.toString()).getAsJsonObject()).build();
        Message systemMsg = Message.builder().role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant. When asked a question, use tools wherever possible.")
                .build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content("How's the weather in Hangzhou?").build();
        List messages = new ArrayList<>();
        messages.addAll(Arrays.asList(systemMsg, userMsg));
        GenerationParam param = GenerationParam.builder()
                // If environment variable is not configured, replace the following line with: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // This example uses qwen-plus. You can change to other models as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(messages)
                .resultFormat(ResultFormat.MESSAGE)
                .tools(Arrays.asList(
                        ToolFunction.builder().function(fdWeather).build(),
                        ToolFunction.builder().function(fdTime).build()))
                .build();
        Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
        GenerationResult result = gen.call(param);
        System.out.println(JsonUtils.toJson(result));
    }
    public static void main(String[] args) {
        try {
            SelectTool();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.println(String.format("Exception %s", e.getMessage()));
        }
        System.exit(0);
    }
}

Curl

curl --location "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
    "model": "qwen-plus",
    "input": {
        "messages": [{
            "role": "user",
            "content": "How's the weather in Hangzhou?"
        }]
    },
    "parameters": {
        "result_format": "message",
        "tools": [{
            "type": "function",
            "function": {
                "name": "get_current_time",
                "description": "This tool can help you query the current time.",
                "parameters": {}
            }
        },{
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "This tool can help you query the weather of a city.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "A city, county, or district, such as Beijing, Hangzhou, or Yuhang."
                        }
                    }
                },
                "required": ["location"]
            }
        }]
    }
}'

Asynchronous invocation

# Your Dashscope Python SDK version must be 1.19.0 or later.
import asyncio
import platform
import os
import dashscope
from dashscope.aigc.generation import AioGeneration

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
async def main():
    response = await AioGeneration.call(
        # If environment variable is not configured, replace the following line with: api_key="sk-xxx",
        api_key=os.getenv('DASHSCOPE_API_KEY'),
        # This example uses qwen-plus. You can change to other models as needed. Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
        model="qwen-plus",
        messages=[{"role": "user", "content": "Who are you?"}],
        result_format="message",
    )
    print(response)

if platform.system() == "Windows":
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())

model string (Required)

Model name.

Supported models: Tongyi Qianwen large language model (Commercial Edition, Open Source Edition), Tongyi Qianwen VL

For more information about model names and billing, see Model List.

messages array (Required)

A list of historical conversation messages.

When making HTTP calls, place messages in the input object.

Message types

System Message object

The objective or role of the model. If a system message is set, it should be placed at the beginning of the messages list.

Properties

content string (Required)

The message body.

role string (Required)

Fixed value: system.

User Message object

Messages sent by users to the model.

Properties

content string or array (Required)

The content of user messages. If your input contains only text, it is string type. If your input contains multi-modal data such as images, it is array type.

Properties

text string

The input text information.

image string

The image file used for image understanding with Qwen-VL model. It can be a URL or local path of the image. For local file input, see Visual understanding.

Example: {"image":"https://xxxx.jpeg"}

role string (Required)

The role of user messages, fixed value: user.

Assistant Message object

The model's response to user messages.

Properties

content string (Required)

The content of assistant messages.

role string (Required)

The role of assistant messages, fixed value: assistant.

partial boolean (Optional)

Whether to enable Partial Mode. For usage instructions, see Prefix continuation (Partial Mode).

Supported models

The general text models include qwen-max, qwen-max-latest, qwen-plus, qwen-plus-latest, qwen-turbo, qwen-turbo-1101, qwen-turbo-latest, and the qwen2.5 series models.

Tool Message object

The output information of tools.

Properties

content string (Required)

The content of tool messages, typically the output of tool functions.

role string (Required)

The role of tool messages, fixed value: tool.

tool_call_id string (Optional)

The ID returned after initiating Function Calling, which you can obtain through response.output.choices[0].message.tool_calls[0]["id"]. Used to mark which tool the Tool Message corresponds to.

temperature float (Optional)

The sampling temperature that controls the randomness of the model's text generation.

A higher temperature value leads to more diverse text generation. A lower temperature value leads to more deterministic text generation.

Valid values: [0, 2)

When you make an HTTP request, include temperature in the parameters object.

top_p float (Optional)

The probability threshold for nucleus sampling that controls the diversity of the generated text.

A higher top_p value leads to more diverse generated text. A lower value leads to more deterministic generated text.

Valid values: (0,1.0].

In Java SDK, it is topP. When making HTTP calls, include top_p in the parameters object.

top_k integer (Optional)

The size of the sampling candidate set during generation. For example, when the value is 50, only the 50 tokens with the highest scores in a single generation form the random sampling candidate set. A greater value introduces more randomness to the generated content. A smaller value makes the generation more deterministic. When the value is None or when top_k is greater than 100, the top_k policy is disabled, and only the top_p policy takes effect.

The value must be greater than or equal to 0.

qwen-vl editions, and 20 for all other editions.

In Java SDK, it is topK. When making HTTP calls, include top_k in the parameters object.

repetition_penalty float (Optional)

The repetition rate in continuous sequences during model generation. A larger value indicates lower repetition. A value of 1.0 indicates no penalty. There is no strict value range. Any value greater than 0 is valid.

In Java SDK, use repetitionPenalty. For HTTP calls, include repetition_penalty in the parameters object.

vl_high_images boolean (Optional) Default value: false

Specifies whether to increase the default token limit for input images. The default token limit for input images is 1,280. When this parameter is set to true, the token limit is increased to 16,384. This parameter is supported by the qwen-vl-max model.

The Java SDK does not support this parameter. When you make HTTP calls, include vl_high_resolution_images in the parameters object.

presence_penalty float (Optional)

Controls the level of repetition in the model-generated text.

Valid values: [-2.0, 2.0]. Positive values reduce repetition. Negative values increase repetition.

Scenarios:

Higher presence_penalty values are appropriate for scenarios that require diversity, creativity, or originality, such as creative writing or brainstorming.

Lower presence_penalty values are suitable for scenarios that require consistency or technical terminology, such as technical documentation or formal writing.

Default presence_penalty values

qwen-max, qwen-max-latest, qwen-vl-max: 1.5

All others: 0.0

Working principle

When the parameter value is positive, the model applies a penalty to tokens that already appear in the current text (regardless of their frequency). This reduces the likelihood of token repetition, resulting in more diverse vocabulary and less repetitive content.

Examples

Prompt: Translate this sentence into Chinese "This movie is good. The plot is good, the acting is good, the music is good, and overall, the whole movie is just good. It is really good, in fact. The plot is so good, and the acting is so good, and the music is so good."

Parameter value 2.0: 这部电影很好。剧情很棒,演技棒,音乐也非常好听,总的来说,整部电影都好得不得了。实际上它真的很优秀。剧情非常精彩,演技出色,音乐也是那么的动听。

Parameter value 0.0: 这部电影很好。剧情好,演技好,音乐也好,总的来说,整部电影都很好。事实上,它真的很棒。剧情非常好,演技也非常出色,音乐也同样优秀。

Parameter value -2.0: 这部电影很好。情节很好,演技很好,音乐也很好,总的来说,整部电影都很好。实际上,它真的很棒。情节非常好,演技也非常好,音乐也非常好。

The Java SDK does not support this parameter. When making HTTP calls, include presence_penalty in the parameters object.

max_tokens integer (Optional)

The maximum number of tokens to return in this request.

max_tokens setting does not affect the generation process of the large model. If the number of tokens generated by the model exceeds max_tokens, the request returns the truncated content.

Both the default value and maximum value are equal to the maximum output length of the model. For more information about the maximum output length of each model, see Model list.

The max_tokens parameter is useful in scenarios that require word count limits (such as generating summaries or keywords), cost control, or response time reduction.

In Java SDK, it is maxTokens (when the model is Tongyi Qianwen VL, it is maxLength in Java SDK. After version 2.18.4, it also supports setting it as maxTokens). When making HTTP calls, you need to put max_tokens in the parameters object.

seed integer (Optional)

The seed parameter controls the deterministic behavior of text generation. You can use it to ensure consistent model outputs across different runs.

If you specify the same seed value and maintain other parameters unchanged in each model call, the model will generate the same results.

Valid values: 0 to 231−1.

For HTTP calls, include seed in the parameters object.

stream boolean (Optional)

Specifies whether to enable streaming output. Valid values:

  • false (default): The model returns all generated content at once after the generation is complete.

  • true: The content is generated and returned in chunks. Each chunk is returned immediately after it is generated.

This parameter is supported only by Python SDK. To implement streaming output through Java SDK, use the streamCall interface. To implement streaming output through HTTP, set X-DashScope-SSE to enable in the header.

incremental_output boolean (Optional) Default value: false(QwQ model default value: true)

Specifies whether to enable incremental output in streaming mode. Valid values:

  • false: Each output contains the entire sequence generated up to that point. The last output contains the complete result.

    I
    I like
    I like apple
    I like apple.
  • true: Incremental output. Subsequent outputs do not include previously output content. You need to read these segments in real time to obtain the complete result.

    I
    like
    apple
    .
The parameter is incrementalOutput. When you make HTTP calls, include incremental_output in the parameters object.

response_format object (Optional) Default value: {"type": "text"}

The format of the response. Valid values: {"type": "text"} or {"type": "json_object"}. When set to {"type": "json_object"}, the output will be a standard JSON string. For usage instructions, see Structured output (JSON Mode).

If you specify this parameter as {"type": "json_object"}, you need to instruct the model to output in JSON format in the System Message or User Message, such as: "Please output in JSON format."
This parameter cannot be set through the Java SDK. When making HTTP calls, place response_format within the parameters object.

Supported models

  • Qwen-Max

    qwen-max, qwen-max-latest, qwen-max-0125

  • Qwen-Plus

    qwen-plus, qwen-plus-latest, qwen-plus-0125

  • Qwen-Turbo

    qwen-turbo, qwen-turbo-latest, qwen-turbo-1101

  • Open source Qwen

    qwen2.5 models

result_format string (Optional) Default value: "text"(Default value for QwQ model is "message")

The format of the returned data. We recommend that you set this parameter to "message" to facilitate multi-round conversations.

In Java SDK, this parameter is resultFormat. When you make HTTP calls, include result_format in the parameters object.

stop string or array (Optional)

The model automatically stops generating text when the output is about to contain the specified string or token_id.

You can specify sensitive words in the stop parameter to control the model output.

When stop is an array type, all elements must be either token_ids or strings. For example, you cannot specify stop as ["hello",104307].

tools array (Optional)

An array of tools that can be called by the model. It can contain one or more tool objects. The model selects one tool during a Function Calling flow. When using tools, you must set the result_format parameter to "message". The tools parameter must be set both when initiating Function Calling and when submitting tool function execution results to the model.

Currently not supported for Tongyi Qianwen VL.

Properties

type string (Required)

The type of tools. Currently, only function is supported.

function object (Required)

Properties

name string (Required)

The name of the tool function. It must contain only letters, numbers, underscores, and hyphens. The maximum length is 64 characters.

description string (Required)

The description of the tool function, which helps the model determine when and how to call the tool function.

parameters objcet (Required)

The parameter description of the tool. It must be a valid JSON Schema. For JSON Schema description, see this link. If the parameters parameter is empty, the function does not have any input parameters.

When making HTTP calls, place tools in the parameters JSON object. Currently not supported for qwen-vl model editions.

tool_choice string or object (Optional)

When using the tools parameter, this parameter controls how the model calls specific tools. There are three possible values:

  • "none" indicates that no tools will be called. When the tools parameter is empty, the default value is "none".

  • "auto" indicates that the model determines whether to call tools. When the tools parameter is not empty, the default value is "auto".

  • object structure can specify which tool the model calls. For example: tool_choice={"type": "function", "function": {"name": "user_function"}}.

    • type only supports "function".

    • function

      • name indicates the name of the tool to be called, such as "get_current_time".

In Java SDK, it is toolChoice. When calling through HTTP, place tool_choice in the parameters object.

translation_options object (Optional)

The translation parameters that you need to configure when using the translation model.

Properties

source_lang string (Required)

The English name of the source language. For more information, see Supported languages. You can set source_lang to "auto" to allow the model to automatically determine the language of the input text.

target_lang string (Required)

The English name of the target language. For more information, see Supported languages.

terms arrays (Optional)

The term array that you need to configure when using the term intervention translation feature.

Properties

source string (Required)

The term in the source language.

target string (Required)

The term in the target language.

tm_list arrays (Optional)

The translation memory array that you need to configure when using the translation memory feature.

Properties

source string (Required)

The statement in the source language.

target string (Required)

The statement in the target language.

domains string (Optional)

The domain prompt statement that you need to configure when using the domain prompt feature.

The domain prompt statement supports only English.
The Java SDK does not support configuring this parameter. When you call through HTTP, place translation_options in the parameters object.

Chat response object (same format for streaming and non-streaming output)

{
  "status_code": 200,
  "request_id": "902fee3b-f7f0-9a8c-96a1-6b4ea25af114",
  "code": "",
  "message": "",
  "output": {
    "text": null,
    "finish_reason": null,
    "choices": [
      {
        "finish_reason": "stop",
        "message": {
          "role": "assistant",
          "content": "I am Tongyi Qianwen, a large-scale language model developed by Alibaba Cloud."
        }
      }
    ]
  },
  "usage": {
    "input_tokens": 22,
    "output_tokens": 17,
    "total_tokens": 39
  }
}

status_code string

The status code of the request. A value of 200 indicates success. Other values indicate failures.

The Java SDK does not return this parameter. When a call fails, an exception is thrown containing the status_code and message information.

request_id string

The unique identifier of this request.

In the Java SDK, this parameter is returned as requestId

code: string

The error code. This value is empty when the call succeeds.

Only the Python SDK returns this parameter.

output object

The response information.

Properties

text string

The response generated by the model. This field contains the response content when the input parameter result_format is set to text.

finish_reason string

This parameter is not empty when the input parameter result_format is set to text.

The possible values are:

  • null: The model has not stopped generating.

  • stop: The model output ended naturally or was stopped by the stop condition in the input parameters.

  • length: The generation was stopped because it reached the maximum length.

  • tool_calls: A tool call occurred.

choices array

The model output information. The choices parameter is returned when result_format is set to message.

Properties

finish_reason string

The possible values are:

  • null: The model has not stopped generating.

  • stop: The model output ended naturally or was stopped by the stop condition in the input parameters.

  • length: The generation was stopped because it reached the maximum length.

  • tool_calls: A tool call occurred.

message object

The message object output by the model.

Properties

role string

The role of the output message, fixed as assistant.

content string or array

The content of the output message. This is an array when using qwen-vl or qwen-audio series models, and a string in other cases.

This value is empty if Function Calling is initiated.

Properties

text string

The content of the output message when using qwen-vl or qwen-audio series models.

reasoning_content string

The deep thinking content of the QwQ model.

tool_calls array

If the model needs to call tools, the tool_calls parameter is generated.

Properties

function object

The name of the tool to call and its input parameters.

Properties

name string

The name of the tool to call.

arguments string

The parameters to input to the tool, in JSON string format.

Because the large model responses have some randomness, the output JSON string may not always match your function. You should validate the parameters before inputting them to your function.

index integer

The index of the current tool_calls object in the tool_calls array.

id string

The ID of this tool response.

type string

The tool type, fixed as function.

usage map

The token information used in this chat request.

Properties

input_tokens integer

The length of the user input content after conversion to tokens.

output_tokens integer

The length of the chat response content after conversion to tokens.

total_tokens integer

When the input is plain text, this field returns the sum of input_tokens and output_tokens.

image_tokens integer

This field is returned when the input content contains image. It represents the length of the user's input image content after conversion to tokens.

prompt_tokens_details object

The granular classification of input tokens.

Properties

cached_tokens integer

The number of tokens that hit the cache. For more information about Context Cache, see Context Cache.

Error codes

If the model call fails and returns an error message, for more information, see Error messages.