Request body | Text inputThis example demonstrates single-round conversation. For more information, see multi-round conversations. Pythonimport 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);
}
}
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)
}
Curlcurl --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
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)
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 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);
}
}
Curlcurl --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 models analyze images, see Visual understanding. Pythonimport 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);
}
}
Curlcurl --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 callingFor the complete Function Calling process code, see Text generation. Pythonimport 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)
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 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);
}
}
Curlcurl --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"}} .
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. |
|
|
| |