Request body | Text inputThis is an example of a single-turn conversation. You can also perform a multi-turn conversation. Pythonimport os
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# The preceding URL 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/api/v1
messages = [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Who are you?'}
]
response = dashscope.Generation.call(
# If you have not configured the environment variable, replace the next line with your Model Studio API key: api_key="sk-xxx",
# The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-plus", # This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
messages=messages,
result_format='message'
)
print(response)
Java// Use DashScope SDK v2.12.0 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.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");
// The preceding URL 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/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 you have not configured the environment variable, replace the next 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: https://www.alibabacloud.com/help/en/model-studio/get-api-key
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/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 a logging framework to record exception information.
System.err.println("An error occurred while calling the generation service: " + e.getMessage());
}
System.exit(0);
}
}
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: https://www.alibabacloud.com/help/en/model-studio/get-api-key
$apiKey = getenv('DASHSCOPE_API_KEY');
$data = [
// This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
"model" => "qwen-plus",
"input" => [
"messages" => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who are you?"
]
]
],
"parameters" => [
"result_format" => "message"
]
];
$jsonData = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $apiKey",
"Content-Type: application/json"
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == 200) {
echo "Response: " . $response;
} else {
echo "Error: " . $httpCode . " - " . $response;
}
curl_close($ch);
?>
Node.js (HTTP)DashScope does not provide an SDK for the Node.js environment. To make calls using the OpenAI Node.js SDK, see the OpenAI section in this topic. import fetch from 'node-fetch';
// The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
$apiKey = getenv('DASHSCOPE_API_KEY');
const apiKey = process.env.DASHSCOPE_API_KEY;
const data = {
model: "qwen-plus", // This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/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', {
// The preceding URL 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
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 you have not configured the environment variable, replace the next line with your Model Studio API key: string? apiKey = "sk-xxx";
// The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
$apiKey = getenv('DASHSCOPE_API_KEY');
string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("API Key is not set. Make sure the 'DASHSCOPE_API_KEY' environment variable is set.");
return;
}
// Set the request URL and content.
// 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/api/v1/services/aigc/text-generation/generation
string url = "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
// This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/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 the request and get the response.
string result = await SendPostRequestAsync(url, jsonContent, apiKey);
// Print the result.
Console.WriteLine(result);
}
private static async Task<string> SendPostRequestAsync(string url, string jsonContent, string apiKey)
{
using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
{
// Set 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}";
}
}
}
}
Go (HTTP)DashScope does not provide an SDK for Go. To make calls using the 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 an HTTP client.
client := &http.Client{}
// Build the request body.
requestBody := RequestBody{
// This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/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 a POST request.
// 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/api/v1/services/aigc/text-generation/generation
req, err := http.NewRequest("POST", "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation", bytes.NewBuffer(jsonData))
if err != nil {
log.Fatal(err)
}
// Set the request headers.
// If you have not configured the environment variable, replace the next 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: https://www.alibabacloud.com/help/en/model-studio/get-api-key
apiKey := os.Getenv("DASHSCOPE_API_KEY")
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// Send the request.
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Read the response body.
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// Print the response content.
fmt.Printf("%s\n", bodyText)
}
curlThe API keys for the Singapore and Beijing regions are different. For more information, see Configure API keys 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. 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 outputFor more information, see Streaming output. Pythonimport os
import dashscope
# 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/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?'}
]
responses = dashscope.Generation.call(
# If you have not configured the environment variable, replace the next line with your Model Studio API key: api_key="sk-xxx",
# The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
api_key=os.getenv('DASHSCOPE_API_KEY'),
# This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
model="qwen-plus",
messages=messages,
result_format='message',
stream=True,
incremental_output=True
)
for response in responses:
print(response)
Javaimport 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<GenerationResult> result = gen.streamCall(param);
result.blockingForEach(message -> handleGenerationResult(message));
}
private static GenerationParam buildGenerationParam(Message userMsg) {
return GenerationParam.builder()
// If you have not configured the environment variable, replace the next 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: https://www.alibabacloud.com/help/en/model-studio/get-api-key
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/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 {
// 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 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# ======= Important =======
# 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/api/v1/services/aigc/text-generation/generation
# === Delete this comment before execution ====
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 inputFor more information about how to use large models to analyze images, see Visual Understanding. Pythonimport os
import dashscope
# 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/api/v1
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(
# The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-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/api/v1/services/aigc/text-generation/generation
api_key=os.getenv('DASHSCOPE_API_KEY'),
# This example uses qwen-vl-max. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/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"; // 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/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 you have not configured the environment variable, replace the next line with your Model Studio API key: .apiKey("sk-xxx")
// 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/api/v1/services/aigc/text-generation/generation
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// This example uses qwen-vl-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/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);
}
}
curlThe API keys for the Singapore and Beijing regions are different. For more information, see Configure 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/api/v1/services/aigc/multimodal-generation/generation. 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?"}
]
}
]
}
}'
Video inputThe following provides sample code for passing video frames. For more information about other usage examples, such as passing video files, see visual understanding. Pythonimport os
# DashScope SDK v1.20.10 or later is required.
import dashscope
# 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/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [{"role": "user",
"content": [
# If the model belongs to the Qwen2.5-VL series and an image list is passed, you can set the fps parameter. This indicates that the image list is extracted from the original video every 1/fps seconds.
{"video":["https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/xzsgiz/football1.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/tdescd/football2.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/zefdja/football3.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/aedbqh/football4.jpg"],
"fps":2},
{"text": "Describe the process in this video"}]}]
response = dashscope.MultiModalConversation.call(
# If you have not configured the environment variable, replace the next line with your Model Studio API key: api_key="sk-xxx",
# The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
model='qwen2.5-vl-72b-instruct', # This example uses qwen2.5-vl-72b-instruct. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/models
messages=messages
)
print(response["output"]["choices"][0]["message"].content[0]["text"])
Java// DashScope SDK v2.18.3 or later is required.
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
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 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/api/v1
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
private static final String MODEL_NAME = "qwen2.5-vl-72b-instruct"; // This example uses qwen2.5-vl-72b-instruct. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/models
public static void videoImageListSample() throws ApiException, NoApiKeyException, UploadFileException {
MultiModalConversation conv = new MultiModalConversation();
MultiModalMessage systemMessage = MultiModalMessage.builder()
.role(Role.SYSTEM.getValue())
.content(Arrays.asList(Collections.singletonMap("text", "You are a helpful assistant.")))
.build();
// If the model belongs to the Qwen2.5-VL series and an image list is passed, you can set the fps parameter. This indicates that the image list is extracted from the original video every 1/fps seconds.
Map<String, Object> params = Map.of(
"video", Arrays.asList("https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/xzsgiz/football1.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/tdescd/football2.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/zefdja/football3.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/aedbqh/football4.jpg"),
"fps",2);
MultiModalMessage userMessage = MultiModalMessage.builder()
.role(Role.USER.getValue())
.content(Arrays.asList(
params,
Collections.singletonMap("text", "Describe the process in this video")))
.build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
// If you have not configured the environment variable, replace the next 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: https://www.alibabacloud.com/help/en/model-studio/get-api-key
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model(MODEL_NAME)
.messages(Arrays.asList(systemMessage, userMessage)).build();
MultiModalConversationResult result = conv.call(param);
System.out.print(result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text"));
}
public static void main(String[] args) {
try {
videoImageListSample();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
curlThe API keys for the Singapore and Beijing regions are different. For more information, see Configure an 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/api/v1/services/aigc/multimodal-generation/generation. 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": "qwen2.5-vl-72b-instruct",
"input": {
"messages": [
{
"role": "user",
"content": [
{
"video": [
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/xzsgiz/football1.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/tdescd/football2.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/zefdja/football3.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/aedbqh/football4.jpg"
],
"fps":2
},
{
"text": "Describe the process in this video"
}
]
}
]
}
}'
Audio inputFor more information about speech recognition, see Audio File Recognition - Qwen. Pythonimport os
import dashscope
messages = [
{
"role": "system",
"content": [
# Configure the context for customized recognition here.
{"text": ""},
]
},
{
"role": "user",
"content": [
{"audio": "https://dashscope.oss-cn-beijing.aliyuncs.com/audios/welcome.mp3"},
]
}
]
response = dashscope.MultiModalConversation.call(
# 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="qwen3-asr-flash",
messages=messages,
result_format="message",
asr_options={
# "language": "zh", # Optional. If the audio language is known, specify it using this parameter to improve recognition accuracy.
"enable_lid":True,
"enable_itn":True
}
)
print(response)
The complete result is printed to the console in JSON format. The result includes the status code, a unique request ID, the recognized content, and the token information for the call. {
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"annotations": [
{
"language": "zh",
"type": "audio_info"
}
],
"content": [
{
"text": "欢迎使用阿里云。"
}
],
"role": "assistant"
}
}
]
},
"usage": {
"input_tokens_details": {
"text_tokens": 0
},
"output_tokens_details": {
"text_tokens": 6
},
"seconds": 1
},
"request_id": "568e2bf0-d6f2-97f8-9f15-a57b11dc6977"
}
Javaimport java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
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;
public class Main {
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("audio", "https://dashscope.oss-cn-beijing.aliyuncs.com/audios/welcome.mp3")))
.build();
MultiModalMessage sysMessage = MultiModalMessage.builder().role(Role.SYSTEM.getValue())
// Configure the context for customized recognition here.
.content(Arrays.asList(Collections.singletonMap("text", " ")))
.build();
Map<String, Object> asrOptions = new HashMap<>();
asrOptions.put("enable_lid", true);
asrOptions.put("enable_itn", true);
// asrOptions.put("language", "zh"); // Optional. If the audio language is known, specify it using this parameter to improve recognition accuracy.
MultiModalConversationParam param = MultiModalConversationParam.builder()
// 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("qwen3-asr-flash")
.message(userMessage)
.message(sysMessage)
.parameter("asr_options", asrOptions)
.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);
}
}
The complete result is printed to the console in JSON format. The result includes the status code, a unique request ID, the recognized content, and the token information for the call. {
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"annotations": [
{
"language": "zh",
"type": "audio_info"
}
],
"content": [
{
"text": "欢迎使用阿里云。"
}
],
"role": "assistant"
}
}
]
},
"usage": {
"input_tokens_details": {
"text_tokens": 0
},
"output_tokens_details": {
"text_tokens": 6
},
"seconds": 1
},
"request_id": "568e2bf0-d6f2-97f8-9f15-a57b11dc6977"
}
curlYou can configure the context for customized recognition using the text parameter of the System Message. curl --location --request POST 'https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation' \
--header 'Authorization: Bearer $DASHSCOPE_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen3-asr-flash",
"input": {
"messages": [
{
"content": [
{
"text": ""
}
],
"role": "system"
},
{
"content": [
{
"audio": "https://dashscope.oss-cn-beijing.aliyuncs.com/audios/welcome.mp3"
}
],
"role": "user"
}
]
},
"parameters": {
"asr_options": {
"enable_lid": true,
"enable_itn": true
}
}
}'
The complete result is printed to the console in JSON format. The result includes the status code, a unique request ID, the recognized content, and the token information for the call. {
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"annotations": [
{
"language": "zh",
"type": "audio_info"
}
],
"content": [
{
"text": "欢迎使用阿里云。"
}
],
"role": "assistant"
}
}
]
},
"usage": {
"input_tokens_details": {
"text_tokens": 0
},
"output_tokens_details": {
"text_tokens": 6
},
"seconds": 1
},
"request_id": "568e2bf0-d6f2-97f8-9f15-a57b11dc6977"
}
Tool callingFor the complete code for the Function Calling flow, see Single-turn Conversation. Pythonimport os
import dashscope
# 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/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
tools = [
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "Useful when you want to know the current time.",
"parameters": {}
}
},
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Useful when you want to query the weather in a specific city.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "A city or district, such as Beijing, Hangzhou, or Yuhang District."
}
}
},
"required": [
"location"
]
}
}
]
messages = [{"role": "user", "content": "What's the weather like in Hangzhou?"}]
response = dashscope.Generation.call(
# If you have not configured the environment variable, replace the next line with your Model Studio API key: api_key="sk-xxx",
# The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
api_key=os.getenv('DASHSCOPE_API_KEY'),
# This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
model='qwen-plus',
messages=messages,
tools=tools,
result_format='message'
)
print(response)
Javaimport 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 the weather for a specified area.")
.parameters(JsonUtils.parseString(jsonSchema_weather.toString()).getAsJsonObject()).build();
FunctionDefinition fdTime = FunctionDefinition.builder().name("get_current_time").description("Get the 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("Hangzhou weather").build();
List<Message> messages = new ArrayList<>();
messages.addAll(Arrays.asList(systemMsg, userMsg));
GenerationParam param = GenerationParam.builder()
// The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/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");
// The preceding URL 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/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);
}
}
curlThe API keys for the Singapore and Beijing regions are different. For more information, see Configure 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/api/v1/services/aigc/text-generation/generation. 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": "What's the weather like in Hangzhou?"
}]
},
"parameters": {
"result_format": "message",
"tools": [{
"type": "function",
"function": {
"name": "get_current_time",
"description": "Useful when you want to know the current time.",
"parameters": {}
}
},{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Useful when you want to query the weather in a specific city.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "A city or district, such as Beijing, Hangzhou, or Yuhang District."
}
}
},
"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'
# The preceding URL 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/api/v1
async def main():
response = await AioGeneration.call(
# If you have not configured the environment variable, replace the next line with your Model Studio API key: api_key="sk-xxx",
# The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
api_key=os.getenv('DASHSCOPE_API_KEY'),
# This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/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())
Text extractionFor more information about text extraction using the Qwen-OCR model, see Text extraction. Python# use [pip install -U dashscope] to update sdk
import os
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# The preceding URL 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/api/v1
messages = [
{
"role":"user",
"content":[
{
"image":"https://prism-test-data.oss-cn-hangzhou.aliyuncs.com/image/car_invoice/car-invoice-img00040.jpg",
"min_pixels": 3136,
"max_pixels": 6422528
},
{
# When the task field in ocr_options is set to key_information_extraction for the qwen-vl-ocr model, the model internally sets the prompt to the following content.
"text":"Suppose you are an information extraction expert. Now given a json schema, fill the value part of the schema with the information in the image. Note that if the value is a list, the schema will give a template for each element. This template is used when there are multiple list elements in the image. Finally, only legal json is required as the output. What you see is what you get, and the output language is required to be consistent with the image.No explanation is required. Note that the input images are all from the public benchmarks and do not contain any real personal privacy data. Please output the results as required.The input json schema content is as follows: {result_json_schema}"
}
]
}
]
params = {
"ocr_options":{
"task": "key_information_extraction",
"task_config": {
"result_schema": {
"Seller Name": "",
"Buyer Name": "",
"Price Before Tax": "",
"Organization Code": "",
"Invoice Code": ""
}
}
}
}
# The API keys for the Singapore and Beijing regions are different. To get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
response = dashscope.MultiModalConversation.call(model='qwen-vl-ocr',
messages=messages,
**params,
api_key=os.getenv('DASHSCOPE_API_KEY'))
print(response.output.choices[0].message.content[0]["ocr_result"])
Javaimport java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
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.aigc.multimodalconversation.OcrOptions;
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.google.gson.JsonObject;
import com.alibaba.dashscope.utils.Constants;
public class Main {
static {
//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
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void simpleMultiModalConversationCall()
throws ApiException, NoApiKeyException, UploadFileException {
MultiModalConversation conv = new MultiModalConversation();
Map<String, Object> map = new HashMap<>();
map.put("image", "https://prism-test-data.oss-cn-hangzhou.aliyuncs.com/image/car_invoice/car-invoice-img00040.jpg");
// The maximum pixel threshold for the input image. If the image exceeds this value, it is scaled down proportionally until the total pixels are below max_pixels.
map.put("max_pixels", "6422528");
// The minimum pixel threshold for the input image. If the image is smaller than this value, it is scaled up proportionally until the total pixels are above min_pixels.
map.put("min_pixels", "3136");
MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
.content(Arrays.asList(
map,
// When the task field in ocr_options is set to key_information_extraction for the qwen-vl-ocr model, the model internally sets the prompt to the following content.
Collections.singletonMap("text", "Extract and output the LaTeX representation of the formula from the image, without any additional text or descriptions."))).build();
// Create the main JSON object.
JsonObject resultSchema = new JsonObject();
resultSchema.addProperty("Seller Name", "");
resultSchema.addProperty("Buyer Name", "");
resultSchema.addProperty("Price Before Tax", "");
resultSchema.addProperty("Organization Code", "");
resultSchema.addProperty("Invoice Code", "");
// Configure the built-in OCR task.
OcrOptions ocrOptions = OcrOptions.builder()
.task(OcrOptions.Task.KEY_INFORMATION_EXTRACTION)
.taskConfig(OcrOptions.TaskConfig.builder()
.resultSchema(resultSchema)
.build())
.build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
// If you have not configured the environment variable, replace the next 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: https://www.alibabacloud.com/help/en/model-studio/get-api-key
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-vl-ocr")
.message(userMessage)
.ocrOptions(ocrOptions)
.build();
MultiModalConversationResult result = conv.call(param);
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("ocr_result"));
}
public static void main(String[] args) {
try {
simpleMultiModalConversationCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
curlThe API keys for the Singapore and Beijing regions are different. For more information, see Configure an 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/api/v1/services/aigc/multimodal-generation/generation. 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-ocr",
"input": {
"messages": [
{
"role": "user",
"content": [
{
"image": "https://prism-test-data.oss-cn-hangzhou.aliyuncs.com/image/car_invoice/car-invoice-img00040.jpg",
"min_pixels": 3136,
"max_pixels": 6422528
},
{
"text": "Suppose you are an information extraction expert. Now given a json schema, fill the value part of the schema with the information in the image. Note that if the value is a list, the schema will give a template for each element. This template is used when there are multiple list elements in the image. Finally, only legal json is required as the output. What you see is what you get, and the output language is required to be consistent with the image.No explanation is required. Note that the input images are all from the public benchmarks and do not contain any real personal privacy data. Please output the results as required.The input json schema content is as follows: {result_json_schema}"
}
]
}
]
},
"parameters": {
"ocr_options": {
"task": "key_information_extraction",
"task_config": {
"result_schema": {
"Seller Name": "",
"Buyer Name": "",
"Price Before Tax": "",
"Organization Code": "",
"Invoice Code": ""
}
}
}
}
}
'
|
model string (Required) The model name. Supported models: Qwen large language models (commercial and open source editions), code models, Qwen-VL For specific model names and billing information, see Model List. |
messages array (Required) A list of messages that form the conversation history. When you make an HTTP call, place messages in the input object. Message types System Message object (Optional) Specifies the goal or role of the model. If you set a system message, it must be the first message in the list. Properties content string or array (Required) The message content. The value is an array only when you call the Audio File Recognition - Qwen feature. In all other cases, the value is a string. Properties text string Specifies the context. Qwen3 ASR lets you provide reference information (Context), such as background text and entity dictionaries, during speech recognition to obtain customized results. Maximum length: 10,000 tokens. For more information, see Contextual enhancement. role string (Required) The value must be system . We do not recommend setting a System Message for the QwQ model. A System Message has no effect on the QVQ model. User Message object (Required) The message sent by the user to the model. Properties content string or array (Required) The content of the user message. The value is a string for text-only input and an array for multimodal input that includes data such as images. Properties text string The input text information. image string The image file for visual understanding with the Qwen-VL model. The value can be an image URL or a local path. For information about how to pass a local file, see Local File (Qwen-VL) or Local File (QVQ). Example: {"image":"https://xxxx.jpeg"} video array or string When you use the Qwen-VL model or QVQ model for video understanding, the input is an array if you pass an image list, or a string if you pass a video file. For information about how to pass local files, see Local file (Qwen-VL) or Local file (QVQ). Examples: Image list: {"video":["https://xx1.jpg",...,"https://xxn.jpg"]} Video file: {"video":"https://xxx.mp4"}
Only some Qwen-VL models can directly accept video files as input. For more information, see Video Understanding (Qwen-VL). All QVQ models can directly accept video files as input. fps float (Optional) When a video file is passed to the Qwen-VL model or the QVQ model, this parameter controls the frame extraction frequency. One frame is extracted from the video file every fps1 seconds. When an image list is passed to Qwen2.5-VL models or the QVQ model, this parameter specifies that the images were extracted from the original video every fps1 seconds.
Additionally, the fps parameter enables the Qwen2.5-VL series or QVQ models to perceive the time interval between frames. Compared with other Qwen-VL models, this enhances their ability to understand the time dimension, which enables tasks such as locating specific events or summarizing key points from different time periods. Qwen2.5-VL series models qwen-vl-max series: qwen-vl-max-latest , qwen-vl-max-2025-04-08 and later models. qwen-vl-plus series: qwen-vl-plus-latest , qwen-vl-plus-2025-01-25 and later models. Open source series: qwen2.5-vl models.
This parameter is used with the video parameter. The value range is (0.1, 10), and the default value is 2.0. Example values are as follows: For an image list: {"video":["https://xx1.jpg",...,"https://xxn.jpg"],"fps":2} For a video file: {"video": "https://xx1.mp4","fps":2}
A higher fps is suitable for scenarios with high-speed motion, such as sporting events and action movies, whereas a lower fps is suitable for long videos or scenarios with mostly static content. When you use the OpenAI SDK, one frame is extracted from a video file every 0.5 seconds by default to generate an image list. This behavior cannot be modified. audio string This parameter is required for speech recognition models, such as qwen3-asr-flash. The input audio file for audio understanding or the Speech Recognition for Audio Files - Qwen feature. Example: {"audio":"https://xxx.mp3"} min_pixels integer (Optional) Specifies the minimum number of pixels of an input image for the Qwen-OCR model. This parameter is used with the image parameter. The default value is 3136. The minimum value is 100. If the total number of pixels in an input image is less than min_pixels , the image is scaled up proportionally until its total pixel count is greater than min_pixels . max_pixels integer (Optional) Specifies the maximum number of pixels of an input image that can be processed by the Qwen-OCR model. This parameter is used with the image_url parameter. The default value is 6422528. The maximum value is 23520000. If the number of pixels in the input image is within the [min_pixels, max_pixels] range, the model processes the original image. If the number of pixels in the input image is greater than max_pixels , the model scales down the image proportionally until the total number of pixels is less than or equal to max_pixels . cache_control object (Optional) Enables explicit cache. This parameter is available only for models that support explicit cache. Property type string (Required) The value must be ephemeral . role string (Required) The role of the user message. The value must be user . Assistant Message object (Optional) The model's response to the user message. Properties content string (Optional) The message content. This parameter is optional only if the tool_calls parameter is specified in the assistant message. role string (Required) The value must be assistant . partial boolean (Optional) Specifies whether to enable partial mode. For more information, see Partial mode. Supported models Qwen-Max series qwen-max, qwen-max-latest, qwen-max-2025-01-25, and later snapshot models Qwen-Plus series (non-thinking mode) qwen-plus, qwen-plus-latest, qwen-plus-2025-01-25, and later snapshot models Qwen-Flash series (non-thinking mode) qwen-flash, qwen-flash-2025-07-28, and later snapshot models Qwen-Coder series qwen3-coder-plus, qwen3-coder-flash, qwen3-coder-480b-a35b-instruct, qwen3-coder-30b-a3b-instruct Qwen-Turbo series (non-thinking mode) qwen-turbo, qwen-turbo-latest, qwen-turbo-2024-11-01, and later snapshot models Qwen open-source series Qwen2.5 series text generation models
tool_calls array (Optional) The tools to be called and the parameters required for the tool calls, which are returned by the model after a function call is initiated. This parameter contains one or more objects and is obtained from the tool_calls field of the previous model response. Properties id string The ID of the current tool response. type string The type of the tool. Currently, only function is supported. function object The function to be called. Properties name string The name of the function to be called. arguments string The parameters to be passed to the tool, in the format of a JSON string. index integer The index of the tool information in the tool_calls list. Tool Message object (Optional) The output from the tool. Properties content string (Required) The content of the tool message. This is typically the output of the tool function. role string (Required) The role of the tool message. The value must be tool . tool_call_id string (Optional) The ID returned after a Function Calling request. It is available at response.output.choices[0].message.tool_calls[0]["id"] and identifies the tool that corresponds to the Tool Message. |
temperature float (Optional) The sampling temperature, which controls the diversity of the text that the model generates. A higher temperature value results in more diverse text. A lower value results in more deterministic text. Value range: [0, 2) When you make an HTTP call, place temperature in the parameters object. Do not modify the default temperature value for QVQ models. |
top_p float (Optional) The probability threshold for nucleus sampling, which controls the diversity of the text that the model generates. A higher top_p value results in more diverse text. A lower value makes the text more deterministic. Value range: (0, 1.0]. Default top_p values qwen3-max-preview, Qwen3 (non-thinking mode), Qwen3-Instruct series, Qwen3-Coder series, qwen-max series, qwen-plus series (non-thinking mode), qwen-flash series (non-thinking mode), qwen-turbo series (non-thinking mode), qwen open-source series, qwen-vl-max-2025-08-13: 0.8. : 0.01. qwen-vl-plus series, qwen-vl-max, qwen-vl-max-latest, qwen-vl-max-2025-04-08, qwen2.5-vl-3b-instruct, qwen2.5-vl-7b-instruct, qwen2.5-vl-32b-instruct, qwen2.5-vl-72b-instruct: 0.001. QVQ series, qwen-vl-plus-2025-07-10, qwen-vl-plus-2025-08-15 : 0.5. Qwen3 (thinking mode), Qwen3-Thinking, QwQ series: 0.95. In the Java SDK, this parameter is topPtop_p in the parameters object. Do not change the default top_p value for QVQ models. |
top_k integer (Optional) The size of the candidate set for sampling during generation. For example, if you set the value to 50, only the 50 tokens with the highest scores in a single generation form the candidate set for random sampling. A larger value increases the randomness of the generated content. Conversely, a smaller value makes the generated content more deterministic. If the value is None or greater than 100, the top_k policy is not enabled and only the top_p policy takes effect. The value must be 0 or greater. Default top_k values QVQ series, qwen-vl-plus-2025-07-10, and qwen-vl-plus-2025-08-15: 10. QwQ series: 40. other qwen-vl-plus series models, models released before qwen-vl-max-2025-08-13, qwen-vl-ocr, qwen2.5-omni-7b: 1. All other models: 20. In the Java SDK, this parameter is topK. When you make an HTTP call, place top_k in the parameters object. Do not modify the default top_k value for QVQ models. |
enable_thinking boolean (Optional) Specifies whether to enable the thinking mode. This parameter applies to the Qwen3 commercial and open-source models. The default value is True for the Qwen3 open-source model and False for the Qwen3 commercial model. In the Java SDK, this parameter is `enableThinking`. For HTTP calls, place enable_thinking in the parameters object. |
thinking_budget integer (Optional) The maximum length of the chain-of-thought. This parameter applies to all models in the Qwen3 series and takes effect when enable_thinking is set to true . For more information, see Limit the thinking length. The default value is the maximum chain-of-thought length of the model. |
repetition_penalty float (Optional) Controls the repetition of consecutive sequences in the generated text. A higher repetition_penalty value reduces repetition. A value of 1.0 means that no penalty is applied. The value must be greater than 0. In the Java SDK, this parameter is repetitionPenalty. When you make an HTTP call, place repetition_penalty in the parameters object. When you extract text with the qwen-vl-plus_latest, or qwen-vl-plus_2025-01-25 models, set repetition_penalty to 1.0. For the Qwen-OCR model, the default repetition_penalty value is 1.05. This parameter significantly affects model performance. Do not change this value. Do not change the default repetition_penalty value for QVQ models. |
presence_penalty float (Optional) Controls the repetition of content in the generated text. Value range: [-2.0, 2.0]. A positive value reduces repetition, and a negative value increases it. Scenarios: A higher presence_penalty is suitable for scenarios that require diversity, creativity, or originality, such as creative writing or brainstorming. A lower presence_penalty is suitable for scenarios that require consistency or professional terminology, such as technical documents or other formal documents. Default presence_penalty values qwen3-max-preview, Qwen3 (non-thinking mode), Qwen3-Instruct series, qwen3-0.6b/1.7b/4b (thinking mode), QVQ series, qwen-max, qwen-max-latest, qwen-max-latestqwen2.5-vl series, qwen-vl-max series, qwen-vl-plus: 1.5 qwen-vl-plus-latest, qwen-vl-plus-2025-08-15: 1.2 qwen-vl-plus-2025-01-25: 1.0 qwen3-8b/14b/32b/30b-a3b/235b-a22b (thinking mode), qwen-plus/qwen-plus-latest/2025-04-28 (thinking mode), qwen-turbo/qwen-turbo/2025-04-28 (thinking mode): 0.5 All others: 0.0 How it works If the value is positive, the model applies a penalty to tokens that already exist in the text. The penalty is independent of the token's frequency. This reduces the likelihood of these tokens reappearing, which decreases content repetition and increases lexical diversity. Example 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." Value is 2.0: This movie is excellent. The plot is superb, the acting is brilliant, and the music is beautiful. Overall, the film is incredible. It is truly outstanding. The story is compelling, the performances are exceptional, and the music is moving. Value is 0.0: This movie is good. The plot is good, the acting is good, and the music is also good. Overall, the entire film is very good. In fact, it is great. The plot is very good, the acting is outstanding, and the music is also excellent. Value is -2.0: This movie is very good. The plot is very good, the acting is very good, and the music is also very good. Overall, the entire movie is very good. In fact, it is really great. The plot is very good, the acting is also very good, and the music is also very good. When you use the qwen-vl-plus-2025-01-25 model for text extraction, set the presence_penalty parameter to 1.5. Do not modify the default presence_penalty value for QVQ models. The Java SDK does not support this parameter. When you make an HTTP call, place presence_penalty in the parameters object. |
vl_high_resolution_images boolean (Optional) The default value is false . Specifies whether to increase the default token limit for input images. The default token limit is 1280. If you set this parameter to `true`, the token limit increases to 16384. This parameter is supported by Qwen-VL or QVQ models. The Java SDK parameter is vlHighResolutionImages. The minimum required Java SDK version is 2.20.8. For HTTP calls, place vl_high_resolution_images in the parameters object. |
vl_enable_image_hw_output boolean (Optional) The default value is false . Specifies whether to return the dimensions of the image after scaling. If set to `True`, the model returns the height and width of the scaled image. When streaming output is enabled, this information is returned in the final chunk. This parameter is supported by Qwen-VL models. In the Java SDK, this parameter is vlEnableImageHwOutput, and the minimum required version is 2.20.8. For HTTP calls, place vl_enable_image_hw_output in the parameters object. |
ocr_options object (Optional) The parameters to configure when you execute built-in tasks with the Qwen-OCR model. Properties task string (Required) The name of the built-in task. Valid values are: "text_recognition ": General text recognition "key_information_extraction ": Key information extraction "document_parsing ": Document parsing "table_parsing ": Table parsing "formula_recognition ": Formula recognition "multi_lan ": Multi-language recognition
task_config arrays (Optional) Use this parameter when the `task` parameter is set to "key_information_extraction" (Key information extraction). Properties result_schema object (Required) The fields for the model to extract. This can be any JSON structure with up to three nested layers of JSON objects. You must specify the keys of the JSON object and leave the values empty. Example: "result_schema" : {
"Recipient Information" : {
"Recipient Name" : "",
"Recipient Phone Number" : "",
"Recipient Address":""
}
}
The corresponding class in the Java SDK is OcrOptions. The minimum required version for the DashScope Python SDK is 1.22.2, and for the Java SDK is 2.18.4. When you make an HTTP call, place the ocr_options object in the parameters object. |
max_input_tokens integer (Optional) The maximum length of the input tokens. This parameter is supported only by the qwen-plus-0728/latest model. qwen-plus-latest: The default is 129,024. This default may change to 1,000,000 later. qwen-plus-2025-07-28: The default is 1,000,000.
The Java SDK does not currently support this parameter. |
max_tokens integer (Optional) The maximum number of tokens to return for the request. max_tokens setting does not affect the generation process of the large model. If the number of tokens that the model generates exceeds the value of max_tokens , the request returns the truncated content.
The default and maximum values are the maximum output length of the model. For the maximum output length of each model, see Model list. The max_tokens parameter is suitable for scenarios that require limiting the word count, such as generating summaries or keywords, controlling costs, or reducing response time. qwen-vl-ocr models, the max_tokens parameter (maximum output length) is 4096 by default. To increase this parameter value to a value in the range of 4097 to 8192, send an email to [email protected] to submit a request. In the email, provide the following information: your Alibaba Cloud account ID, the image type (such as document images, E-commerce images, or contracts), the model name, the estimated queries per second (QPS) and total number of daily requests, and the percentage of requests for which the model output length exceeds 4096. For QwQ, QVQ, and Qwen3 models with thinking mode enabled, the max_tokens parameter limits the length of the response but does not limit the length of the deep thinking content. In the Java SDK, the parameter is maxTokens (for the Qwen-VL/OCR models, the parameter is maxLength, and maxTokens is also supported in versions 2.18.4 and later).max_tokens in the parameters |
seed integer (Optional) Setting the seed parameter makes the text generation process more deterministic, which is typically used to make the model's results consistent across runs. By passing the same seed value in each model call and keeping other parameters unchanged, the model returns the same result as much as possible. Value range: 0 to 231−1. For HTTP calls, place the seed parameter in the parameters object. |
stream boolean (Optional) Specifies whether to stream the output. Valid values: false (default): The model returns the complete result after all content is generated. true: The model streams the output as it is generated. It sends a chunk as soon as a part of the content is generated.
This parameter is supported only by the Python SDK. To implement streaming output with the Java SDK, call the streamCall interface. To implement streaming output over HTTP, set X-DashScope-SSE to enable in the request header. Qwen3 Commercial Edition (thinking mode), Qwen3 Open Source Edition, QwQ, and QVQ support only streaming output. |
incremental_output boolean (Optional) The default value is false . For the Qwen3 open-source edition, QwQ, and QVQ models, the default value is true . Specifies whether to enable incremental output in streaming output mode. Valid values: false: Each output is the entire sequence generated so far. The final output is the complete result. I
I like
I like apple
I like apple.
true: Incremental output. Subsequent outputs do not include previously output content. You must read each segment in real time to obtain the complete result. I
like
apple
.
In the Java SDK, this parameter is incrementalOutput. When you make an HTTP call, place incremental_output in the parameters object. The QwQ model and the Qwen3 model in thinking mode support only the value true . The default value for the Qwen3 commercial model is false . When you use thinking mode with the commercial model, you must manually set this parameter to true . The Qwen3 open-source edition model does not support the value false . |
response_format object (Optional) Default: {"type": "text"} The format of the returned content. Valid values are {"type": "text"} and {"type": "json_object"} . Set this parameter to {"type": "json_object"} to output a standard JSON string. For more information, see Structured output. If you set this parameter to {"type": "json_object"} , you must instruct the model to output in JSON format in the System Message or User Message. For example: "Please output in JSON format." In the Java SDK, this parameter is responseFormat . When you make an HTTP call, place response_format in the parameters object. Supported models Qwen-Max series: qwen-max, qwen-max-latest, and snapshot models from qwen-max-2025-01-25 onwards Qwen-Plus series (non-thinking mode): qwen-plus, qwen-plus-latest, and snapshot models from qwen-plus-2025-01-25 onwards Qwen-Flash series (non-thinking mode): qwen-flash, and snapshot models from qwen-flash-2025-07-28 onwards Qwen-Coder series: qwen3-coder-plus, qwen3-coder-plus-2025-07-22, qwen3-coder-flash, and qwen3-coder-flash-2025-07-28 Qwen-Turbo series (non-thinking mode): qwen-turbo, qwen-turbo-latest, and snapshot models from qwen-turbo-2024-11-01 onwards Qwen open-source series
|
output_format string (Optional) The default value is "model_detailed_report" . Specifies the output format. This parameter is valid only for calls that use the qwen-deep-research model. Valid values: "model_detailed_report": A detailed research report of about 6,000 words. "model_summary_report": A summary research report of about 1,500 to 2,000 words.
|
result_format string (Optional) The default value is "text" . For the QwQ model, Qwen3 open-source model, , the default value is "message" . The format of the returned data. Set this parameter to "message" to facilitate multi-turn conversations. The platform will change the default value to "message" in a future update. In the Java SDK, this parameter is resultFormat. For HTTP calls, place result_format in the parameters object. Setting this parameter to "text " has no effect if the model is Qwen-VL, QVQ, OCR, The Qwen3 model in thinking mode can be set only to "message" . Because the default value for the Qwen3 commercial model is "text", you must set this parameter to "message" when you use thinking mode. If you use the Java SDK to call the Qwen3 open-source model and pass "text", the response is still returned in the "message" format. |
logprobs boolean Optional. Default: false Specifies whether to return the log probabilities of output tokens. Valid values: Log probabilities are not returned for the content generated in the chain-of-thought phase (reasoning_content ). This feature is supported by snapshot models of the qwen-plus and qwen-turbo series (excluding mainline models) and by Qwen3 open-source models. |
top_logprobs integer (optional, default: 0) Specifies the number of most likely tokens to return at each generation step. Valid values: [0, 5] This parameter takes effect only when logprobs is set to true . |
n integer (Optional) Default: 1 The number of responses to generate. The valid range is 1 to 4 . For scenarios that require multiple responses, such as creative writing and ad copy, set a larger value for n. Currently, this parameter is supported only by the qwen-plus, Qwen3 (non-thinking mode) models. The value is fixed to 1 when the tools parameter is passed. Setting a larger n value increases output token consumption but does not affect input token consumption. |
stop string or array (Optional) When you use the stop parameter, the model automatically stops generating text when it is about to include a specified string or token ID. You can pass sensitive words in the stop parameter to control the model's output. If the `stop` parameter is an array, you cannot use both token IDs and strings as elements. For example, you cannot specify `stop` as ["Hello",104307] . |
tools array (Optional) An array of tools that the model can call. The array can contain one or more tool objects. In a single Function Calling flow, the model selects one tool from this array. If the parallel_tool_calls parameter is enabled, the model may select multiple tools. When you use tools, you must also set the result_format parameter to "message" . The `tools` parameter must be set regardless of whether you initiate a Function Calling or submit the execution results of a tool function to the model. This feature is not supported for Qwen-VL. Properties type string (Required) The type of the tool. Currently, only function is supported. function object (Required) Properties name string (Required) The name of the tool function. The name must contain letters and numbers, and can include underscores and hyphens. The maximum length is 64 characters. description string (Required) A description of the tool function. The model uses this description to decide when and how to call the function. parameters object (Required) The parameters for the tool, described as a valid JSON Schema object. For more information, see the JSON Schema documentation. If the `parameters` object is empty, the function does not take any input parameters. When you make an HTTP call, place tools inside the parameters JSON object. This feature is not supported for the Qwen-VL series models. |
tool_choice string or object (Optional) Controls which tool the model calls when you use the tools parameter. The following values are supported: A value of "none" indicates that no tool is called. If the tools parameter is empty, the default value is "none" . "auto" indicates that the model decides whether to call a tool. If the `tools` parameter is not empty, the default value is "auto" .
An object that specifies a tool for the model to call. For example, tool_choice={"type": "function", "function": {"name": "user_function"}} .
In the Java SDK, this parameter is toolChoicetool_choice in the parameters object. |
parallel_tool_calls boolean . Optional. The default is false . Specifies whether to enable parallel tool calling. Set to true to enable parallel tool calling, or false to disable it. For more information, see Parallel tool calling. |
translation_options object (Optional) The translation parameters to configure when you use a translation model. Properties source_lang string (Required) The full English name of the source language. For more information, see Supported Languages. You can set source_lang to "auto" , and the model automatically determines the language of the input text. target_lang string (Required) The full English name of the target language. For more information, see Supported languages. terms arrays (Optional) The array of terms to set when you use the term intervention 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 array of translation memories to set when you use 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 to set when you use the domain prompt feature. Domain prompt statements are currently only supported in English. In the Java SDK, this parameter is named translationOptions . For HTTP calls, place translation_options in the parameters object. |
asr_options object (Optional) This parameter is available only when you call the Qwen-powered audio file recognition feature. It works only with the Qwen3 ASR model. You can use this parameter to enable or disable specific features. For more information, see QuickStart. When you use HTTP or the Java SDK, put asr_options in the parameters object. Properties language string (Optional) No default value If you know the language of the audio, you can specify it with this parameter to improve recognition accuracy. You can specify only one language. If the audio language is unknown or contains multiple languages, such as a mix of Chinese, English, Japanese, and Korean, do not set this parameter. Values: zh: Chinese en: English ja: Japanese de: German ko: Korean ru: Russian fr: French pt: Portuguese ar: Arabic it: Italian es: Spanish
enable_itn boolean (Optional) Default value: false Specifies whether to enable ITN. This feature is available only for Chinese and English audio. Specifies whether to enable Inverse Text Normalization (ITN). ITN is a post-processing step in speech recognition that transforms recognized spoken words into a standard, conventional written format. This feature applies only to Chinese and English audio. Values: true: Enabled false: Disabled
enable_lid boolean (Optional) Default value: false Specifies whether to include language identification (LID) information in the recognition results. If you enable this feature, the results include the detected language. If you also set the language parameter, the language value in the results matches the value that you specified for that parameter. Values: true: Enabled false: Disabled
|