大语言模型
支持的领域 / 任务:aigc
Qwen1.5
Qwen1.5是Qwen开源系列的下一个版本。与之前的版本相比,Qwen1.5显著提升了聊天模型与人类偏好的一致性,改善了它们的多语言能力,并具备了强大的链接外部系统能力。灵积上提供API服务的是新版本qwen模型的chat版本,在chat能力上大幅提升,即便在英文的MT-Bench上,Qwen1.5-Chat系列也取得了优秀的性能。
灵积平台上提供的0.5B模型、1.8B模型、7B模型、14B模型、32B模型和72B模型基于千问开源版本,进行了针对性的推理性能优化,为广大开发者提供便捷的API服务。各个版本均对应魔搭社区开源的各个尺寸的模型版本,详细参考链接。
CodeQwen1.5
CodeQwen1.5是专门针对代码的Qwen1.5版本。与Qwen1.5相比,CodeQwen1.5在大量的代码数据上训练得到。它拥有强大的代码生成能力,在一系列基准测试中展现出不凡的竞争力。其支持92种编程语言,上下文长度可达64K,在SQL生成和bug修复方面均有出色的表现。
灵积平台上提供的7B模型基于千问开源版本,进行了针对性的推理性能优化,为广大开发者提供便捷的API服务。各个版本均对应魔搭社区开源的各个尺寸的模型版本,详细参考链接。
Qwen
通义千问系列模型为阿里云研发的大语言模型。千问模型基于Transformer架构,在超大规模的预训练数据上进行训练得到。预训练数据类型多样,覆盖广泛,包括大量网络文本、专业书籍、代码等。同时,在预训练模型的基础之上,使用对齐机制打造了模型的chat版本。其中千问-1.8B是18亿参数规模的模型,千问-7B是70亿参数规模的模型,千问-14B是140亿参数规模的模型,千问-72B是720亿参数规模的模型。
灵积平台上提供的千问开源模型,进行了针对性的推理性能优化,为广大开发者提供便捷的API服务。其中1.8B模型基于最新在魔搭社区开源的最新版本,7B模型基于最新在魔搭社区上开源的V1.1版本,而14B模型同样基于魔搭社区上最新版本提供, 72B模型基于魔搭社区开源的最新版本提供。
用户以文本形式输入的指令(prompt)以及不定轮次的对话历史(history)作为输入,返回模型生成的回复作为输出。在这一过程中,文本将被转换为语言模型可以处理的token序列。Token是模型用来表示自然语言文本的基本单位,可以直观的理解为“字”或“词”。对于中文文本来说,1个token通常对应一个汉字;对于英文文本来说,1个token通常对应3至4个字母或1个单词。例如,中文文本“你好,我是通义千问”会被转换成序列['你', '好', ',', '我', '是', '通', '义', '千', '问'],而英文文本"Nice to meet you."则会被转换成['Nice', ' to', ' meet', ' you', '.']。
由于模型调用的计算量与token序列长度相关,输入或输出token数量越多,模型的计算时间越长,我们将根据模型输入和输出的token数量计费。可以从API返回结果的 usage 字段中了解到您每次调用时使用的token数量。您也可以使用Token计算器或者调用Token计算API来预估文本对应的token数量。
模型概览
模型名 | 模型简介 | 模型输入输出限制 |
qwen1.5-72b-chat | 通义千问1.5对外开源的72B规模参数量的经过人类指令对齐的chat模型 | 支持32k tokens上下文,输入最大30k,输出最大2k tokens。 |
qwen1.5-32b-chat | 通义千问1.5对外开源的32B规模参数量的经过人类指令对齐的chat模型 | |
qwen1.5-14b-chat | 通义千问1.5对外开源的14B规模参数量的经过人类指令对齐的chat模型 | 模型支持 8k tokens上下文,为了保障正常使用和正常输出,API限定用户输入为6k Tokens。 |
qwen1.5-7b-chat | 通义千问1.5对外开源的7B规模参数量的经过人类指令对齐的chat模型 | |
qwen1.5-1.8b-chat | 通义千问1.5对外开源的1.8B规模参数量的经过人类指令对齐的chat模型 | 支持32k tokens上下文,输入最大30k,输出最大2k tokens。 |
qwen1.5-0.5b-chat | 通义千问1.5对外开源的0.5B规模参数量的经过人类指令对齐的chat模型 | |
codeqwen1.5-7b-chat | 通义千问1.5对外开源的7B规模参数量的经过人类指令对齐的针对代码场景的chat模型 | 支持64k tokens上下文,输入最大56k,输出最大8k tokens。 |
qwen-72b-chat | 通义千问对外开源的72B规模参数量的经过人类指令对齐的chat模型 | 支持32k tokens上下文,输入最大30k,输出最大2k tokens。 |
qwen-14b-chat | 通义千问对外开源的14B规模参数量的经过人类指令对齐的chat模型 | 模型支持 8k tokens上下文,为了保障正常使用和正常输出,API限定用户输入为6k Tokens。 |
qwen-7b-chat | 通义千问对外开源的7B规模参数量的经过人类指令对齐的chat模型 | |
qwen-1.8b-longcontext-chat | 通义千问对外开源的1.8B规模参数量的经过人类指令对齐的chat模型 | 支持32k tokens上下文,输入最大30k,输出最大2k tokens。 |
qwen-1.8b-chat | 通义千问对外开源的1.8B规模参数量的经过人类指令对齐的chat模型 | 模型支持 8k tokens上下文,为了保障正常使用和正常输出,API限定用户输入为6k Tokens。 |
SDK使用
前提条件
已开通服务并获得API-KEY:开通DashScope并创建API-KEY。
已安装最新版SDK:安装DashScope SDK。
单轮问答
以下示例展示了调用通义千问72B模型对一个用户指令进行响应的代码,如需访问其它模型,替换对应的模型名称即可。
需要使用您的API-KEY替换示例中的 YOUR_DASHSCOPE_API_KEY,代码才能正常运行。
API-KEY设置
export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY
import random
from http import HTTPStatus
import dashscope
def call_with_messages():
messages = [
{'role': 'user', 'content': '用萝卜、土豆、茄子做饭,给我个菜谱'}]
response = dashscope.Generation.call(
'qwen1.5-72b-chat',
messages=messages,
# set the random seed, optional, default to 1234 if not set
seed=random.randint(1, 10000),
result_format='message', # set the result to be "message" format.
)
if response.status_code == HTTPStatus.OK:
print(response)
else:
print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
response.request_id, response.status_code,
response.code, response.message
))
if __name__ == '__main__':
call_with_messages()
// Copyright (c) Alibaba, Inc. and its affiliates.
import java.util.Arrays;
import java.util.concurrent.Semaphore;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Main {
public static void callWithMessage()
throws NoApiKeyException, ApiException, InputRequiredException {
Generation gen = new Generation();
Message userMsg = Message.builder().role(Role.USER.getValue()).content("用萝卜、土豆、茄子做饭,给我个菜谱").build();
QwenParam param =
QwenParam.builder().model("qwen-72b-chat")
.messages(Arrays.asList(userMsg))
.resultFormat(QwenParam.ResultFormat.MESSAGE)
.topP(0.8)
.build();
GenerationResult result = gen.call(param);
System.out.println(result);
}
public static void callWithMessageCallback()
throws NoApiKeyException, ApiException, InputRequiredException, InterruptedException {
Generation gen = new Generation();
Message userMsg = Message.builder().role(Role.USER.getValue()).content("用萝卜、土豆、茄子做饭,给我个菜谱").build();
QwenParam param =
QwenParam.builder().model("qwen-14b-chat")
.messages(Arrays.asList(userMsg))
.resultFormat(QwenParam.ResultFormat.MESSAGE)
.topP(0.8)
.build();
Semaphore semaphore = new Semaphore(0);
gen.call(param, new ResultCallback<GenerationResult>() {
@Override
public void onEvent(GenerationResult message) {
System.out.println(message);
}
@Override
public void onError(Exception ex){
System.out.println(ex.getMessage());
semaphore.release();
}
@Override
public void onComplete(){
System.out.println("onComplete");
semaphore.release();
}
});
semaphore.acquire();
}
public static void main(String[] args){
try {
callWithMessage();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
try {
callWithMessageCallback();
} catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
多轮会话
您也可以通过messages参数传入对话历史,以达到与模型进行多轮交互的目的。
import random
from http import HTTPStatus
from dashscope import Generation
from dashscope.api_entities.dashscope_response import Role
def multi_round_conversation():
messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '请介绍一下上海有什么好玩的地方'}]
response = Generation.call(
'qwen1.5-72b-chat',
messages=messages,
# set the random seed, optional, default to 1234 if not set
seed=random.randint(1, 10000),
result_format='message', # set the result to be "message" format.
)
if response.status_code == HTTPStatus.OK:
print(response)
messages.append({'role': response.output.choices[0]['message']['role'],
'content': response.output.choices[0]['message']['content']})
else:
print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
response.request_id, response.status_code,
response.code, response.message
))
messages.append({'role': Role.USER, 'content': '能否缩短一些,只讲三点'})
response = Generation.call(
'qwen1.5-72b-chat',
messages=messages,
result_format='message', # set the result to be "message" format.
)
if response.status_code == HTTPStatus.OK:
print(response)
else:
print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
response.request_id, response.status_code,
response.code, response.message
))
if __name__ == '__main__':
multi_round_conversation()
// Copyright (c) Alibaba, Inc. and its affiliates.
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
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;
public class Main {
public static void callWithMessage()
throws NoApiKeyException, ApiException, InputRequiredException {
Generation gen = new Generation();
MessageManager msgManager = new MessageManager(10);
Message systemMsg =
Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
Message userMsg = Message.builder().role(Role.USER.getValue()).content("就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品").build();
msgManager.add(systemMsg);
msgManager.add(userMsg);
QwenParam param =
QwenParam.builder().model("qwen-72b-chat").messages(msgManager.get())
.resultFormat(QwenParam.ResultFormat.MESSAGE)
.topP(0.8)
/* set the random seed, optional, default to 1234 if not set */
.seed(100)
.build();
GenerationResult result = gen.call(param);
System.out.println(result);
msgManager.add(result);
System.out.println(JsonUtils.toJson(result));
param.setPrompt("能否缩短一些,只讲三点");
param.setMessages(msgManager.get());
result = gen.call(param);
System.out.println(result);
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args){
try {
callWithMessage();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
流式输出
import random
from http import HTTPStatus
from dashscope import Generation
def call_stream_with_messages():
messages = [
{'role': 'user', 'content': '用萝卜、土豆、茄子做饭,给我个菜谱'}]
responses = Generation.call(
'qwen1.5-72b-chat',
messages=messages,
seed=random.randint(1, 10000), # set the random seed, optional, default to 1234 if not set
result_format='message', # set the result to be "message" format.
stream=True,
output_in_full=True # get streaming output incrementally
)
full_content = ''
for response in responses:
if response.status_code == HTTPStatus.OK:
full_content += response.output.choices[0]['message']['content']
print(response)
else:
print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
response.request_id, response.status_code,
response.code, response.message
))
print('Full content: \n' + full_content)
if __name__ == '__main__':
call_stream_with_messages()
// Copyright (c) Alibaba, Inc. and its affiliates.
import java.util.Arrays;
import java.util.concurrent.Semaphore;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.ResultCallback;
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 io.reactivex.Flowable;
public class Main {
public static void streamCallWithMessage()
throws NoApiKeyException, ApiException, InputRequiredException {
Generation gen = new Generation();
Message userMsg = Message.builder().role(Role.USER.getValue()).content("用萝卜、土豆、茄子做饭,给我个菜谱").build();
QwenParam param =
QwenParam.builder().model("qwen1.5-72b-chat")
.messages(Arrays.asList(userMsg))
.resultFormat(QwenParam.ResultFormat.MESSAGE)
.topP(0.8)
.incrementalOutput(true) // get streaming output incrementally
.build();
Flowable<GenerationResult> result = gen.streamCall(param);
StringBuilder fullContent = new StringBuilder();
result.blockingForEach(item->{
fullContent.append(item.getOutput().getChoices().get(0).getMessage().getContent());
System.out.println(item);
});
System.out.println("Full content: \n" + fullContent);
}
public static void streamCallWithMessageCallback()
throws NoApiKeyException, ApiException, InputRequiredException, InterruptedException {
Generation gen = new Generation();
Message userMsg = Message.builder().role(Role.USER.getValue()).content("用萝卜、土豆、茄子做饭,给我个菜谱").build();
QwenParam param =
QwenParam.builder().model("qwen-14b-chat")
.messages(Arrays.asList(userMsg))
.resultFormat(QwenParam.ResultFormat.MESSAGE)
.topP(0.8)
.incrementalOutput(true) // get streaming output incrementally
.build();
Semaphore semaphore = new Semaphore(0);
StringBuilder fullContent = new StringBuilder();
gen.streamCall(param, new ResultCallback<GenerationResult>() {
@Override
public void onEvent(GenerationResult message) {
fullContent.append(message.getOutput().getChoices().get(0).getMessage().getContent());
System.out.println(message);
}
@Override
public void onError(Exception ex){
System.out.println(ex.getMessage());
semaphore.release();
}
@Override
public void onComplete(){
System.out.println("onComplete");
semaphore.release();
}
});
semaphore.acquire();
System.out.println("Full content: \n" + fullContent);
}
public static void main(String[] args){
try {
streamCallWithMessage();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
try {
streamCallWithMessageCallback();
} catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
参数配置
参数 | 类型 | 默认值 | 说明 |
model | string | - | 指定用于对话的通义千问模型名,具体见模型概览模型名一列。 |
messages | array | - |
|
prompt | string | - | |
history (可选) | list[dict] | [] | 即将废弃,请使用messages字段。用户与模型的对话历史,list中的每个元素是形式为{"user":"用户输入","bot":"模型输出"}的一轮对话,多轮对话按时间正序排列。 |
seed (可选) | int | 1234 | 生成时使用的随机数种子,用户控制模型生成内容的随机性。seed支持无符号64位整数,默认值为1234。在使用seed时,模型将尽可能生成相同或相似的结果,但目前不保证每次生成的结果完全相同。 |
max_tokens(可选) | int | 1500 | 用于限制模型生成token的数量,max_tokens设置的是生成上限,并不表示一定会生成这么多的token数量。模型输出限制见模型概览部分。 |
top_p (可选) | float | 0.8 | 生成过程中核采样方法概率阈值,例如,取值为0.8时,仅保留概率加起来大于等于0.8的最可能token的最小集合作为候选集。取值范围为(0,1.0),取值越大,生成的随机性越高;取值越低,生成的确定性越高。 |
top_k (可选) | int | 0 | 生成时,采样候选集的大小。例如,取值为50时,仅将单次生成中得分最高的50个token组成随机采样的候选集。取值越大,生成的随机性越高;取值越小,生成的确定性越高。默认值为0,表示不启用top_k策略,此时,仅有top_p策略生效。 |
repetition_penalty (可选) | float | qwen1.5-72b-chat\qwen1.5-14b-chat\qwen1.5-7b-chat为1.0,其余均为 1.1 | 用于控制模型生成时的重复度。提高repetition_penalty时可以降低模型生成的重复度。1.0表示不做惩罚。 |
temperature (可选) | float | qwen1.5-72b-chat\qwen1.5-32b-chat\qwen1.5-14b-chat\qwen1.5-7b-chat均为0.7,其余均为1 | 用于控制随机性和多样性的程度。具体来说,temperature值控制了生成文本时对每个候选词的概率分布进行平滑的程度。较高的temperature值会降低概率分布的峰值,使得更多的低概率词被选择,生成结果更加多样化;而较低的temperature值则会增强概率分布的峰值,使得高概率词更容易被选择,生成结果更加确定。 取值范围:[0, 2),不建议取值为0,无意义。 python version >=1.10.1 java version >= 2.5.1 |
stop (可选) | str/list[str]用于指定字符串;list[int]/list[list[int]]用于指定token_ids | None | 用于控制生成时遇到某些内容则停止。如果指定了字符串或者token_ids,模型将要生成指定字符串或者token_ids时会停止生成,生成结果不包含指定的内容。例如指定stop为"你好",表示将要生成"你好"时停止;指定stop为[37763, 367],表示将要生成"Observation"时停止。同时,stop参数支持以list方式传入字符串数组或者token_ids数组,以期支持使用多个stop的场景。注意,list模式下不支持字符串和token_ids混用,list模式下元素类型要相同。 |
stream (可选) | bool | False | 是否使用流式输出。当以stream模式输出结果时,接口返回结果为generator,需要通过迭代获取结果,默认每次输出为当前生成的整个序列,最后一次输出为最终全部生成结果,python可以通过sdk参output_in_full为False改变输出模式为非增量输出,参考output_in_full说明,java设置incrementalOutput请求参数为False修改。 |
result_format (可选) | String | text | [text|message],默认为text,当为message时,输出参考message结果示例 |
incremental_output (可选) | bool | False | 控制流式输出模式,即后面内容会包含已经输出的内容;设置为True,将开启增量输出模式,后面输出不会包含已经输出的内容,您需要自行拼接整体输出,参考流式输出示例代码。 默认False: I I like i like apple True: I like apple 该参数只能与stream输出模式配合使用。 |
返回结果
返回message结果示例
{
"status_code": 200,
"request_id": "b3d8bb75-05a2-9044-8e9e-ec8c87689a5e",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "材料:\n- 萝卜:2根\n- 土豆:2个\n- 茄子:2个\n- 大葱:1根\n- 姜:适量\n- 蒜:适量\n- 食用油:适量\n- 盐:适量\n- 生抽:适量\n- 蚝油:适量\n\n做法:\n\n1. 将萝卜、土豆、茄子分别洗净去皮,切成块状备用。\n2. 大葱切断,姜切片,蒜切末备用。\n3. 烧热锅,加入适量的食用油,放入葱段、姜片、蒜末爆香。\n4. 加入萝卜块,翻炒几分钟,加入适量的盐、生抽调味。\n5. 加入土豆块,继续翻炒几分钟,加入适量的盐、生抽调味。\n6. 加入茄子块,继续翻炒几分钟,加入适量的盐、生抽调味。\n7. 加入适量的蚝油,翻炒均匀,让每一块蔬菜都均匀地裹上蚝油。\n8. 翻炒几分钟,让蔬菜熟透,即可出锅。\n\n这道菜色香味俱佳,营养丰富,可以作为主食或配菜食用。"
}
}
]
},
"usage": {
"input_tokens": 31,
"output_tokens": 267
}
}
返回text结果示例
{
"status_code": 200,
"request_id": "446877aa-dbb8-99ca-98eb-d78a5e90fe61",
"code": "",
"message": "",
"output": {
"text": "材料:\n- 萝卜:2根\n- 土豆:2个\n- 茄子:2个\n- 大葱:1根\n- 姜:适量\n- 蒜:适量\n- 食用油:适量\n- 盐:适量\n- 生抽:适量\n- 蚝油:适量\n\n做法:\n\n1. 将萝卜、土豆、茄子分别洗净去皮,切成块状备用。\n2. 大葱切段,姜切片,蒜切末备用。\n3. 烧热锅,加入适量的食用油,放入葱段、姜片、蒜末爆香。\n4. 加入萝卜块,翻炒几分钟,加入适量的盐、生抽调味。\n5. 加入土豆块,继续翻炒几分钟,加入适量的盐、生抽调味。\n6. 加入茄子块,继续翻炒几分钟,加入适量的盐、生抽调味。\n7. 加入适量的蚝油,翻炒均匀,让每一块蔬菜都均匀地裹上蚝油。\n8. 翻炒几分钟,让蔬菜熟透,即可出锅。\n\n这道菜色香味俱佳,营养丰富,可以作为主食或配菜食用。",
"finish_reason": "stop",
"choices": null
},
"usage": {
"input_tokens": 31,
"output_tokens": 267
}
}
返回参数说明
返回参数 | 类型 | 说明 | 备注 |
status_code | int | 200(HTTPStatus.OK)表示请求成功,否则表示请求失败,可以通过code获取错误码,通过message字段获取错误详细信息。 | |
request_Id | string | 系统生成的标志本次调用的id。 | |
code | string | 表示请求失败,表示错误码,成功忽略。 | |
message | string | 失败,表示失败详细信息,成功忽略。 | |
output | dict | 调用结果信息,对于千问模型,包含输出text。 | |
output.usage | dict | 计量信息,表示本次请求计量数据。 | |
output.text | string | 模型生成回复。 | |
output.finish_reason | string | 有三种情况:正在生成时为null,生成结束时如果由于停止token导致则为stop,生成结束时如果因为生成长度过长导致则为length。 | |
usage.input_tokens | int | 用户输入文本转换成Token后的长度。 | |
usage.output_tokens | int | 模型生成回复转换为Token后的长度。 | |
choices | List | [] | 当result_format为message输出choices |
choices[i].finish_reason | String | 有三种情况:正在生成时为null,生成结束时如果由于停止token导致则为stop,生成结束时如果因为生成长度过长导致则为length。 | 当result_format为message输出choices |
choices[i].message | dict | 模型生成消息输出 | 当result_format为message输出choices |
message.role | String | 模型role,固定为assistant | 当result_format为message输出choices |
message.content | String | 模型生成的文本 | 当result_format为message输出choices |
HTTP调用接口
功能描述
通义千问开源模型同时支持 HTTP 调用来完成客户的响应,目前提供普通 HTTP 和 HTTP SSE 两种协议,您可根据自己的需求自行选择。
前提条件
已开通服务并获得API-KEY:开通DashScope并创建API-KEY。
提交接口调用
POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
入参描述
传参方式 | 字段 | 类型 | 必选 | 描述 | 示例值 |
Header | Content-Type | String | 是 | 请求类型:application/json 或者text/event-stream(开启 SSE 响应) | application/json |
Accept | String | 否 | */*,选择text/event-stream则会开启 SSE 响应,默认无设置 | text/event-stream | |
Authorization | String | 是 | API-Key,例如:Bearer d1**2a | Bearer d1**2a | |
X-DashScope-SSE | String | 否 | 跟Accept: text/event-stream 二选一即可启用SSE响应 | enable | |
Body | model | String | 是 | 目前可选模型见模型概览。 | qwen1.5-72b-chat |
input.prompt | String | 与input.messages设置其1即可 | 用户当前输入的期望模型执行指令,支持中英文。 | 哪个公园距离我更近 | |
input.history | List | 否 | 即将废弃,请使用messsages字段。用户与模型的对话历史,list中的每个元素是形式为{"user":"用户输入","bot":"模型输出"}的一轮对话,多轮对话按时间正序排列。 | "history": [ { "user":"今天天气好吗?", "bot":"今天天气不错,要出去玩玩嘛?" }, { "user":"那你有什么地方推荐?", "bot":"我建议你去公园,春天来了,花朵开了,很美丽。" } ] | |
input.messages | List | 与input.prompt设置其1即可 | 用户与模型的对话历史,对话接口未来都会有message传输,不过prompt和history会持续兼容,list中的每个元素形式为{"role":角色, "content": 内容}。角色当前可选值:system、user、assistant。未来可以扩展到更多role。 | "input":{ "messages":[ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "你好,附近哪里有博物馆?" }] } | |
input.messages.role | String | message存在的时候不能缺省 | |||
input.messages.content | String | ||||
parameters.result_format | String | 否 | "text"表示旧版本的text "message"表示兼容openai的message | "text" | |
parameters.seed | Integer | 否 | 生成时使用的随机数种子,用户控制模型生成内容的随机性。seed支持无符号64位整数,默认值为1234。在使用seed时,模型将尽可能生成相同或相似的结果,但目前不保证每次生成的结果完全相同。 | 65535 | |
parameters.max_tokens | Integer | 否 | 用于限制模型生成token的数量,max_tokens设置的是生成上限,并不表示一定会生成这么多的token数量。具体见模型概览部分。 | 1500 | |
parameters.top_p | Float | 否 | 生成时,核采样方法的概率阈值。例如,取值为0.8时,仅保留累计概率之和大于等于0.8的概率分布中的token,作为随机采样的候选集。取值范围为(0,1.0),取值越大,生成的随机性越高;取值越低,生成的随机性越低。默认值 0.8。注意,取值不要大于等于1 | 0.8 | |
parameters.top_k | Integer | 否 | 生成时,采样候选集的大小。例如,取值为50时,仅将单次生成中得分最高的50个token组成随机采样的候选集。取值越大,生成的随机性越高;取值越小,生成的确定性越高。注意:如果top_k参数为空或者top_k的值大于100,表示不启用top_k策略,此时仅有top_p策略生效。 | 50 | |
parameters.repetition_penalty | Float | 否 | 用于控制模型生成时的重复度。提高repetition_penalty时可以降低模型生成的重复度。1.0表示不做惩罚。默认为1.1 | 1.1 | |
parameters.temperature | Float | 否 | 用于控制随机性和多样性的程度。具体来说,temperature值控制了生成文本时对每个候选词的概率分布进行平滑的程度。较高的temperature值会降低概率分布的峰值,使得更多的低概率词被选择,生成结果更加多样化;而较低的temperature值则会增强概率分布的峰值,使得高概率词更容易被选择,生成结果更加确定。 取值范围: [0, 2),不建议取值为0,无意义。系统默认值0.85 | 0.85 | |
parameters.stop | String/List[String]用于指定字符串;List[Integer]/List[List[Integer]]用于指定token_ids | 否 | 用于控制生成时遇到某些内容则停止。如果指定了字符串或者token_ids,模型将要生成指定字符串或者token_ids时会停止生成,生成结果不包含指定的内容。例如指定stop为"你好",表示将要生成"你好"时停止;指定stop为[37763, 367],表示将要生成"Observation"时停止。同时,stop参数支持以list方式传入字符串数组或者token_ids数组,以期支持使用多个stop的场景。注意,list模式下不支持字符串和token_ids混用,list模式下每个元素类型要相同。 | [[37763, 367]] | |
parameters.incremental_output | Bool | 否 | 控制流式输出模式,默认False,即后面内容会包含已经输出的内容;设置为True,将开启增量输出模式,后面输出不会包含已经输出的内容,您需要自行拼接整体输出,参考流式输出示例代码。 默认False: I I like i like apple True: I like apple 该参数只能与stream输出模式配合使用。 |
出参描述
字段 | 类型 | 描述 | 示例值 |
output.text | String | 本次请求的算法输出内容。 | 我建议你去颐和园 |
output.finish_reason | String | 有三种情况:正在生成时为null,生成结束时如果由于停止token导致则为stop,生成结束时如果因为生成长度过长导致则为length。 | stop |
output.choices[list] | List | 入参result_format=message时候的返回值 | 入参result_format=message时候的返回值 |
output.choices[x].finish_reason | String | 停止原因,null:生成过程中 stop:stop token导致结束 length:生成长度导致结束 | |
output.choices[x].message | String | message每个元素形式为{"role":角色, "content": 内容}。角色当前可选值:system、user、assistant。未来可以扩展到更多role,content则包含本次请求算法输出的内容。 | |
output.choices[x].message.role | String | ||
output.choices[x].message.content | String | ||
usage.output_tokens | Integer | 本次请求算法输出内容的 token 数目。 | 380 |
usage.input_tokens | Integer | 本次请求输入内容的 token 数目。在打开了搜索的情况下,输入的 token 数目因为还需要添加搜索相关内容支持,所以会超出客户在请求中的输入。 | 633 |
request_id | String | 本次请求的系统唯一码 | 7574ee8f-38a3-4b1e-9280-11c33ab46e51 |
请求示例(SSE 关闭)
以下示例展示通过CURL命令来调用通义千问14B模型的脚本(SSE 关闭),如需调用7B模型或72B模型替换model参数即可。
需要使用您的API-KEY替换示例中的 your-dashscope-api-key ,代码才能正常运行。
curl --location 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen-14b-chat",
"input":{
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "你好,哪个公园距离我最近?"
}
]
},
"parameters": {
}
}'
响应示例(SSE关闭)
{
"output":{
"text":"最近的公园是公园,它距离你的家大约1.5公里 ... ... 你可以使用Google地图或者百度地图来查看具体的路线和距离。",
"finish_reason":"stop"
},
"usage":{
"output_tokens":51,
"input_tokens":85
},
"request_id":"d89c06fb-46a1-47b6-acb9-bfb17f814969"
}
请求示例(SSE开启)
以下示例展示通过CURL命令来调用通义千问14B模型的脚本(SSE 开启),如需调用72B模型替换model参数即可。
需要使用您的API-KEY替换示例中的 your-dashscope-api-key ,代码才能正常运行。
curl --location 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \
--header 'Content-Type: application/json' \
--header 'X-DashScope-SSE: enable' \
--data '{
"model": "qwen1.5-72b-chat",
"input":{
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "你好,哪个公园距离我最近?"
}
]
},
"parameters": {
}
}'
响应示例(SSE开启)
id:1
event:result
data:{"output":{"finish_reason":"null","text":"最近"},"usage":{"output_tokens":3,"input_tokens":85},"request_id":"1117fb64-5dd9-9df0-a5ca-d7ee0e97032d"}
id:2
event:result
data:{"output":{"finish_reason":"null","text":"最近的公园是公园,它"},"usage":{"output_tokens":11,"input_tokens":85},"request_id":"1117fb64-5dd9-9df0-a5ca-d7ee0e97032d"}
... ... ... ...
... ... ... ...
id:8
event:result
data:{"output":{"finish_reason":"stop","text":"最近的公园是公园,它距离你的家大约1.5公里。你可以使用Google地图或者百度地图来查看具体的路线和距离。"},"usage":{"output_tokens":51,"input_tokens":85},"request_id":"1117fb64-5dd9-9df0-a5ca-d7ee0e97032d"}
异常响应示例
在访问请求出错的情况下,输出的结果中会通过 code 和 message 指明出错原因。
{
"code":"InvalidApiKey",
"message":"Invalid API-key provided.",
"request_id":"fb53c4ec-1c12-4fc4-a580-cdb7c3261fc1"
}
状态码说明
DashScope灵积模型服务通用状态码请查阅:返回状态码说明
- 本页导读 (0)