模型概览
您可以通过SDK或HTTP调用的通义千问系列模型如下表所示:
模型名称 | 模型简介 | 模型输入/输出限制 |
qwen-turbo | 通义千问超大规模语言模型,支持中文、英文等不同语言输入。 | 模型支持 8,000 tokens上下文,为了保证正常的使用和输出,API限定用户输入为 6,000 tokens。 |
qwen-plus | 通义千问超大规模语言模型增强版,支持中文、英文等不同语言输入。 | 模型支持 32,000 tokens上下文,为了保证正常的使用和输出,API限定用户输入为30,000 tokens。 |
qwen-max | 通义千问千亿级别超大规模语言模型,支持中文、英文等不同语言输入。随着模型的升级,qwen-max将滚动更新升级,如果希望使用固定版本,请使用下面的历史快照版本。当前qwen-max模型与qwen-max-0428快照版本等价,均为最新版本的qwen-max模型,也是当前通义千问2.5产品版本背后的API模型。 | 模型支持 8,000 tokens上下文,为了保证正常的使用和输出,API限定用户输入为 6,000 tokens。 |
qwen-max-0428 | 通义千问千亿级别超大规模语言模型,支持中文、英文等不同语言输入。该模型与当前的qwen-max版本对齐,为qwen-max的2024年4月28号的历史快照,预期维护到下个快照版本发布时间(待定)后一个月。 | |
qwen-max-0403 | 通义千问千亿级别超大规模语言模型,支持中文、英文等不同语言输入。该模型为qwen-max的2024年4月3号的历史快照固定版本,预期维护到下个快照版本发布时间(待定)后一个月。 | |
qwen-max-0107 | 通义千问千亿级别超大规模语言模型,支持中文、英文等不同语言输入。该模型为qwen-max的2024年1月7号的历史快照固定版本,仅推荐特定需求客户访问。 | |
qwen-max-1201 | 通义千问千亿级别超大规模语言模型,支持中文、英文等不同语言输入。 说明 该模型为qwen-max的2023年12月1号的历史快照固定版本,该版本的维护时间已经到期,将在4月22日下线,请及时迁移到更新版本模型。(4月8日开始模型限流也会逐步调低直至下线) | |
qwen-max-longcontext | 通义千问千亿级别超大规模语言模型,支持中文、英文等不同语言输入。 | 模型支持 30,000 tokens上下文,为了保证正常的使用和输出,API限定用户输入为 28,000 tokens。 |
首先在官网获取api key首次有一定的免费额度开发参考_模型服务灵积(DashScope)-阿里云帮助中心
这里展示通过http请求调用
一.导入依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>the-latest-version</version>
</dependency>
// https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java
implementation group: 'com.alibaba', name: 'dashscope-sdk-java', version: 'the-latest-version'
二.示例代码(http请求)
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Main {
private static final String USER_AGENT = "Java-HttpURLConnection/1.0";
public static void main(String[] args) throws Exception {
String urlStr = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
String apiKey = System.getenv("DASHSCOPE_API_KEY");
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + apiKey);
connection.setDoOutput(true);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
String UserInput = scanner.nextLine();
String jsonInputString = String.format("{\"model\": \"qwen-turbo\", \"input\": {\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"%s\"}]}, \"parameters\": {\"result_format\": \"message\"}}",UserInput);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.write(jsonInputString.getBytes(StandardCharsets.UTF_8));
wr.flush();
}
StringBuilder response = new StringBuilder();
try (BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
}
System.out.println(response);
connection.disconnect();
}
}
请求成功后返回结果示例
{
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "通义千问,是阿里云自主研发的超大规模语言模型,能够回答问题、创作文字,还能表达观点、撰写代码。我是阿里云推出的一种超大规模语言模型,我是一个能够回答问题、创作文字,还能表达观点、撰写代码的AI助手。如果您有任何问题或需要帮助,请随时告诉我,我会尽力提供支持。"
}
}
]
},
"usage": {
"total_tokens": 97,
"output_tokens": 73,
"input_tokens": 24
},
"request_id": "0105f672-d7b4-9172-9d03-51c669ec830a"
}
一般有几个问题会导致请求失败
导入依赖包后没有同步
没有把示例中的api key替换为你获取到的key
模型列表有一些是收费的,初次选择免费模型即可,例如qwen-turbo