通义千问视觉理解大模型Qwen-VL不仅大幅提升通用OCR、视觉推理、中文文本理解基础能力,还能处理各种分辨率和规格的图像,支持“看图解题”功能。
灵积服务即将合并到百炼中,关于qwen-vl模型的API使用方法,请参考百炼文档的API详情。
模型概览
通义千问VL模型按输入和输出的总Token数进行计费。图像转换为Token的规则如下:分辨率为512*512像素的图像约等于334个Token,其他分辨率的图像按比例换算;最小单位为28x28像素,即每28x28像素对应一个Token,如果图像的长或宽不是28的整数倍,则向上取整至28的整数倍进行计算;一张图最少4个Token,最多1280个Token(qwen-vl-max、qwen-vl-max-0809、qwen-vl-plus-0809模型可接受单张图片的最大输入为 16384 个 Token)。
模型名称 | 说明 | 上下文长度 | 最大输入 | 最大输出 | 输入输出单价 | 免费额度 |
(Token数) | (每千Token) | |||||
qwen-vl-max | 本次更新上下文支持32k,增强图像理解能力,可以更好地识别图片中的多语言文字和手写体的文字。 | 32k | 30k | 2k | 0.02元 | 100万Token 有效期:百炼开通后30天内 |
qwen-vl-max-0809 | 本模型为qwen-vl-max的2024年8月9日快照版本,快照版本维护到下个快照版本发布时间(待定)后一个月。 本模型等同于模型Qwen2-VL-72B。 | 32k | 30k | 2k | 0.02元 | 100万Token 有效期:已开通百炼的用户,自8月23日0点起30天内有效。 新开通百炼的用户,在开通后30天内有效。 |
qwen-vl-plus-0809 | 本模型为qwen-vl-plus的2024年8月9日快照版本。 | 32k | 30k | 2k | 0.008元 | 100万Token 有效期:百炼开通后30天内 |
qwen-vl-max-0201 | 本模型为qwen-vl-max的2024年2月1日快照版本,快照版本维护到下个快照版本发布时间(待定)后一个月。 | 8k | 6k | 2k | 0.02元 | 100万Token 有效期:百炼开通后30天内 |
qwen-vl-plus | 大幅提升细节识别能力和文字识别能力,支持超百万像素分辨率和任意长宽比规格的图像。在广泛的视觉任务中提供卓越性能。 | 2k | 0.008元 |
支持处理的图片格式
图片格式 | Content Type | 文件扩展名 |
BMP | image/bmp | .bmp |
DIB | image/bmp | .dib |
ICNS | image/icns | .icns |
ICO | image/x-icon | .ico |
JPEG | image/jpeg | .jfif, .jpe, .jpeg, .jpg |
JPEG2000 | image/jp2 | .j2c, .j2k, .jp2, .jpc, .jpf, .jpx |
PNG | image/png | .apng, .png |
SGI | image/sgi | .bw, .rgb, .rgba, .sgi |
TIFF | image/tiff | .tif, .tiff |
WEBP | image/webp | .webp |
对于输入的图片有以下限制:
图片文件大小不超过10MB。
输入
qwen-vl-max-0809
与qwen-vl-plus-0809
模型的单张图片,总的像素数不超过 12M,可以支持标准的 4K 图片;输入qwen-vl-max、qwen-vl-max-0201与qwen-vl-plus模型的单张图片,总的像素数不超过 1048576,相当于一张长宽均为 1024 的图片总像素数。
前提条件
请您参考获取API Key,开通百炼服务并获得API-KEY。
请在模型概览中选择您需要使用的模型。
您可以使用OpenAI Python SDK、DashScope SDK或HTTP接口调用通义千问VL模型,请您根据您的需求,参考以下方式准备您的计算环境。
如果您之前使用OpenAI SDK以及HTTP方式调用OpenAI的服务,只需在原有框架下调整API-KEY、base_url、model等参数,就可以直接调用通义千问VL模型。
调用方式
准备条件
通过OpenAI Python SDK调用
您可以通过以下命令安装或更新OpenAI SDK:
# 如果下述命令报错,请将pip替换为pip3 pip install -U openai
您需要配置的base_url如下:
https://dashscope.aliyuncs.com/compatible-mode/v1
通过OpenAI兼容-HTTP调用
如果您需要通过OpenAI兼容的HTTP方式进行调用,需要配置的完整访问endpoint如下:
POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
通过DashScope SDK调用
DashScope SDK提供了Python和Java两个版本,请参考安装SDK,安装最新版SDK。
通过DashScope HTTP调用
如果您需要通过DashScope的HTTP方式进行调用,需要配置的完整访问endpoint如下:
POST https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation
我们推荐您将API-KEY配置到环境变量中以降低API-KEY的泄露风险,详情可参考配置API Key到环境变量。您也可以在代码中配置API-KEY,但是会存在泄露风险。
场景示例
简单示例
您可以参考以下示例代码,通过OpenAI或者DashScope的方式,调用通义千问VL模型。
您可以输入单张或多张图片。
OpenAI兼容
您可以通过OpenAI SDK或OpenAI兼容的HTTP方式调用通义千问VL模型。
Python
示例代码
from openai import OpenAI
import os
def get_response():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-max",
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"
}
},
{
"type": "text",
"text": "这些是什么"
}
]
}
]
)
print(completion.model_dump_json())
if __name__=='__main__':
get_response()
返回结果
{
"id": "chatcmpl-4b5a3bb9-221f-9687-bdd7-a7d56aae44df",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "图1中是一位女士和一只拉布拉多犬在海滩上互动的场景。女士穿着格子衬衫,坐在沙滩上,与狗进行握手的动作,背景是海浪和天空,整个画面充满了温馨和愉快的氛围。\n\n图2中是一只老虎在森林中行走的场景。老虎的毛色是橙色和黑色相间的条纹,它正向前迈步,周围是茂密的树木和植被,地面上覆盖着落叶,整个画面给人一种野生自然的感觉。",
"role": "assistant",
"function_call": null,
"tool_calls": null
}
}
],
"created": 1725948492,
"model": "qwen-vl-max",
"object": "chat.completion",
"service_tier": null,
"system_fingerprint": null,
"usage": {
"completion_tokens": 106,
"prompt_tokens": 2497,
"total_tokens": 2603
}
}
curl
示例代码
curl --location 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen-vl-max",
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"
}
},
{
"type": "text",
"text": "这些是什么"
}
]
}
]
}'
返回结果
{
"choices": [
{
"message": {
"content": "图1中是一位女士和一只拉布拉多犬在海滩上互动的场景。女士穿着格子衬衫,坐在沙滩上,与狗进行握手的动作,背景是海景和日落的天空,整个画面显得非常温馨和谐。\n\n图2中是一只老虎在森林中行走的场景。老虎的毛色是橙色和黑色条纹相间,它正向前迈步,周围是茂密的树木和植被,地面上覆盖着落叶,整个画面充满了自然的野性和生机。",
"role": "assistant"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 2497,
"completion_tokens": 109,
"total_tokens": 2606
},
"created": 1725948561,
"system_fingerprint": null,
"model": "qwen-vl-max",
"id": "chatcmpl-0fd66f46-b09e-9164-a84f-3ebbbedbac15"
}
DashScope
您可以通过DashScope SDK或HTTP方式调用通义千问VL模型。
Python
示例代码
from http import HTTPStatus
import dashscope
def simple_multimodal_conversation_call():
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": "这些是什么?"}
]
}
]
response = dashscope.MultiModalConversation.call(
model='qwen-vl-plus',
messages=messages
)
if response.status_code == HTTPStatus.OK:
print(response)
else:
print(response.code)
print(response.message)
if __name__ == '__main__':
simple_multimodal_conversation_call()
返回结果
{
"status_code": 200,
"request_id": "3a031529-707f-9b7d-968c-172e7533debc",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "图1中是一名女子和狗在沙滩上玩耍。\n图2是孟加拉虎的插画,它正向镜头走来。\n图3里是一只可爱的小白兔。"
}
]
}
}
]
},
"usage": {
"input_tokens": 3743,
"output_tokens": 41,
"image_tokens": 3697
}
}
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;
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("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", "这些是什么?"))).build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
.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);
}
}
返回结果
{
"requestId": "dcb38a0f-fd69-9071-bcde-c4530f9a7559",
"usage": {
"input_tokens": 3740,
"output_tokens": 48
},
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "图1中是一名女子和一只大金毛在沙滩上玩耍。\n图2是孟加拉虎的写实照片,老虎正向镜头走来。\n图3是一幅插画,主要展示了一只兔子。"
}
]
}
}
]
}
}
curl
示例代码
curl --location 'https://dashscope.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": "这些是什么?"}
]
}
]
}
}'
返回结果
{
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或互动。背景是美丽的日落景色,海浪轻轻拍打着海岸线。\n\n请注意,我提供的描述基于图像中可见的内容,并不包括任何超出视觉信息之外的信息。如果您需要更多关于这个场景的具体细节,请告诉我!"
}
]
}
}
]
},
"usage": {
"output_tokens": 81,
"input_tokens": 1277,
"image_tokens": 1247
},
"request_id": "ccf845a3-dc33-9cda-b581-20fe7dc23f70"
}
本地文件
您可以参考以下示例代码,通过OpenAI或者DashScope的方式,调用通义千问VL模型处理本地文件。以下代码使用的示例图片为:test.png
OpenAI兼容
Python
示例代码
from openai import OpenAI
import os
import base64
# base 64 编码格式
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def get_response(image_path):
base64_image = encode_image(image_path)
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
},
{
"type": "text",
"text": "这是什么"
}
]
}
]
)
print(completion.model_dump_json())
if __name__=='__main__':
get_response("test.png")
返回结果
{
"id": "chatcmpl-7399dbeb-af0c-9fcb-9083-0a836669476d",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "这是一只在天空中飞翔的鹰。它有着广阔的翅膀,正在翱翔于云层之间。这种鸟类通常与自由、力量和高瞻远瞩等概念相关联。",
"role": "assistant",
"function_call": null,
"tool_calls": null
}
}
],
"created": 1725948726,
"model": "qwen-vl-plus",
"object": "chat.completion",
"service_tier": null,
"system_fingerprint": null,
"usage": {
"completion_tokens": 41,
"prompt_tokens": 1253,
"total_tokens": 1294
}
}
HTTP
示例代码
import os
import base64
import requests
# base 64 编码格式
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def get_response(image_path):
base64_image = encode_image(image_path)
api_key = os.getenv("DASHSCOPE_API_KEY")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "qwen-vl-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
},
{
"type": "text",
"text": "这是什么"
}
]
}
]
}
response = requests.post("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions", headers=headers, json=payload)
print(response.json())
if __name__=='__main__':
get_response(image_path="test.png")
返回结果
{
"choices": [
{
"message": {
"content": "这是一只在天空中飞翔的鹰。它有着广阔的翅膀,正在翱翔于云层之间。这种鸟类通常被认为是力量、自由和雄心壮志的象征,在各种文化中有重要的地位。",
"role": "assistant"
},
"finish_reason": "stop",
"index": 0,
"logprobs": None
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 1254,
"completion_tokens": 45,
"total_tokens": 1299
},
"created": 1721732005,
"system_fingerprint": None,
"model": "qwen-vl-plus",
"id": "chatcmpl-13b925d1-ef79-9c15-b890-0079a096d7d3"
}
DashScope
请您参考下表,结合您的使用方式与操作系统进行文件路径的创建。
系统 | SDK | 传入的文件路径 | 示例 |
Linux或macOS系统 | Python SDK | file://{文件的绝对路径} | file:///home/images/test.png |
Java SDK | |||
Windows系统 | Python SDK | file://{文件的绝对路径} | file://D:/images/test.png |
Java SDK | file:///{文件的绝对路径} | file:///D:images/test.png |
Python
示例代码
from dashscope import MultiModalConversation
def call_with_local_file(local_path):
image_path = f"file://{local_path}"
messages = [{'role': 'system',
'content': [{'text': 'You are a helpful assistant.'}]},
{'role':'user',
'content': [{'image': image_path},
{'text': '这是什么'}]}]
response = MultiModalConversation.call(model='qwen-vl-plus', messages=messages)
print(response)
if __name__ == '__main__':
call_with_local_file("test.png")
返回结果
{
"status_code": 200,
"request_id": "65061d9a-d4d9-9d31-9caa-d1c5d9eb3d54",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "这是一只在天空中飞翔的鹰。它有着广阔的翅膀,正在翱翔于云层之间。这种鸟类通常被认为是力量、自由和雄心壮志的象征,在各种文化中有重要的地位。"
}
]
}
}
]
},
"usage": {
"input_tokens": 1254,
"output_tokens": 45,
"image_tokens": 1225
}
}
Java
示例代码
// Copyright (c) Alibaba, Inc. and its affiliates.
import java.util.Arrays;
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.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 callWithLocalFile(String localPath)
throws ApiException, NoApiKeyException, UploadFileException {
String filePath = "file://"+localPath;
MultiModalConversation conv = new MultiModalConversation();
MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
.content(Arrays.asList(new HashMap<String, Object>(){{put("image", filePath);}},
new HashMap<String, Object>(){{put("text", "这是什么?");}})).build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
.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 {
callWithLocalFile("test.png");
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
返回结果
{
"requestId": "c1fde568-c7fe-951c-a7fd-0c356fe04c1d",
"usage": {
"input_tokens": 1255,
"output_tokens": 38
},
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "这是一只在天空中飞翔的鹰。它有着广阔的翅膀,正在翱翔于云层之间。这种景象常常象征着自由、力量和勇气等正面意义。"
}
]
}
}
]
}
}
多轮对话
您可以参考以下示例代码,通过OpenAI或者DashScope的方式,调用通义千问VL模型,实现多轮对话的功能。
OpenAI兼容
您可以通过OpenAI SDK或OpenAI兼容的HTTP方式调用通义千问VL模型,体验多轮对话的功能。
Python
示例代码
from openai import OpenAI
import os
def get_response():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
},
{
"type": "text",
"text": "这是什么"
}
]
}
]
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=messages,
)
print(f"模型第一轮输出:\n{completion.model_dump()}")
assistant_message = completion.choices[0].message
messages.append(assistant_message.model_dump())
messages.append({
"role": "user",
"content": [
{
"type": "text",
"text": "做一首诗描述这个场景"
}
]
})
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=messages,
)
print(f"模型第二轮输出:\n{completion.model_dump()}")
if __name__=='__main__':
get_response()
返回结果
模型第一轮输出:
{
"id": "chatcmpl-afcdac19-9bbf-942b-91ad-04252fe1722c",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": None,
"message": {
"content": "图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击掌的样子。这名女士穿着格子衬衫,似乎正在与狗狗进行亲密的接触,并且面带微笑。背景是海洋和日出或日落时分的天空。这是一幅描绘人与宠物之间温馨时刻的画面。",
"role": "assistant",
"function_call": None,
"tool_calls": None
}
}
],
"created": 1721820065,
"model": "qwen-vl-plus",
"object": "chat.completion",
"service_tier": None,
"system_fingerprint": None,
"usage": {
"completion_tokens": 75,
"prompt_tokens": 1276,
"total_tokens": 1351
}
}
模型第二轮输出:
{
"id": "chatcmpl-3090adc4-91da-95d9-8482-49240d47099a",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": None,
"message": {
"content": "朝阳照海涛,\n沙岸女伴犬。\n欢笑共此时,\n友情深似海。\n\n手握同游步,\n默契心间留。\n潮起潮又落,\n此景久难忘。",
"role": "assistant",
"function_call": None,
"tool_calls": None
}
}
],
"created": 1721820068,
"model": "qwen-vl-plus",
"object": "chat.completion",
"service_tier": None,
"system_fingerprint": None,
"usage": {
"completion_tokens": 44,
"prompt_tokens": 1366,
"total_tokens": 1410
}
}
curl
示例代码
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-vl-max",
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
},
{
"type": "text",
"text": "这是什么"
}
]
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "这是一个女孩和一只狗。"
}
]
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "写一首七言绝句描述这个场景"
}
]
}
]
}'
返回结果
{
"choices": [
{
"message": {
"content": "海风轻拂笑颜开, \n沙滩上与犬相陪。 \n夕阳斜照人影短, \n欢乐时光心自醉。",
"role": "assistant"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 1295,
"completion_tokens": 32,
"total_tokens": 1327
},
"created": 1726324976,
"system_fingerprint": null,
"model": "qwen-vl-max",
"id": "chatcmpl-3c953977-6107-96c5-9a13-c01e328b24ca"
}
DashScope
您可以通过DashScope SDK或HTTP方式调用通义千问VL模型,体验多轮对话的功能。
Python
示例代码
from dashscope import MultiModalConversation
def conversation_call():
messages = [
{
"role": "user",
"content": [
{"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
{"text": "这是什么?"},
]
}
]
response = MultiModalConversation.call(
model='qwen-vl-plus',
messages=messages
)
print(f"模型第一轮输出:{response}")
messages.append(response['output']['choices'][0]['message'])
user_msg = {
"role": "user",
"content": [
{
"text": "做一首诗描述这个场景"
}
]
}
messages.append(user_msg)
response = MultiModalConversation.call(
model='qwen-vl-plus',
messages=messages
)
print(f"模型第二轮输出:{response}")
if __name__ == '__main__':
conversation_call()
返回结果
模型第一轮输出:
{
"status_code": 200,
"request_id": "0468708b-85fb-95d8-a502-bb9098b31b37",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或互动。背景是美丽的日落景色,海浪轻轻拍打着海岸线。\n\n请注意,我提供的描述基于图像中可见的内容,并不包括任何超出视觉信息之外的信息。如果您需要更多关于这个场景的具体细节,请告诉我!"
}
]
}
}
]
},
"usage": {
"input_tokens": 1277,
"output_tokens": 81,
"image_tokens": 1247
}
}
模型第二轮输出:
{
"status_code": 200,
"request_id": "8f236443-7b01-9bad-87be-abff5d3887de",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "夕阳染红了天边,\n波涛轻抚着沙岸。\n人犬共坐此情深,\n\n手握着手心相牵。\n欢笑回荡于风间,\n这一刻永恒不变。\n爱意如潮水般涌动,\n在这片金色的大地上蔓延。"
}
]
}
}
]
},
"usage": {
"input_tokens": 1373,
"output_tokens": 59,
"image_tokens": 1247
}
}
Java
示例代码
// Copyright (c) Alibaba, Inc. and its affiliates.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
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 {
private static final String modelName = "qwen-vl-plus";
public static void MultiRoundConversationCall() 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();
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("text", "这是什么?"))).build();
List<MultiModalMessage> messages = new ArrayList<>();
messages.add(systemMessage);
messages.add(userMessage);
MultiModalConversationParam param = MultiModalConversationParam.builder()
.model(modelName)
.messages(messages)
.build();
MultiModalConversationResult result = conv.call(param);
System.out.println(JsonUtils.toJson(result));
// add the result to conversation
messages.add(result.getOutput().getChoices().get(0).getMessage());
MultiModalMessage msg = MultiModalMessage.builder().role(Role.USER.getValue())
.content(Arrays.asList(Collections.singletonMap("text", "做一首诗描述这个场景"))).build();
messages.add(msg);
// new messages
param.setMessages((List)messages);
result = conv.call(param);
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args) {
try {
MultiRoundConversationCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
返回结果
{
"requestId": "1239e1de-dbd3-9b46-b508-421023ed3053",
"usage": {
"input_tokens": 1277,
"output_tokens": 81
},
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或互动。背景是美丽的日落景色,海浪轻轻拍打着海岸线。\n\n请注意,我提供的描述基于图像中可见的内容,并不包括任何超出视觉信息之外的信息。如果您需要更多关于这个场景的具体细节,请告诉我!"
}
]
}
}
]
}
}
{
"requestId": "045fe96f-26c4-9cfd-b0ad-ec5f1f4033ce",
"usage": {
"input_tokens": 1373,
"output_tokens": 59
},
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "夕阳染红了天边,\n波涛轻抚着沙岸。\n人犬共坐此情深,\n\n手握着手心相牵。\n欢笑回荡于风间,\n这一刻永恒不变。\n爱意如潮水般涌动,\n在这片金色的大地上蔓延。"
}
]
}
}
]
}
}
curl
示例代码
curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-vl-max",
"input":{
"messages":[
{
"role": "user",
"content": [
{"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
{"text": "这是什么?"}
]
},
{
"role": "assistant",
"content": [
{"text": "这是一只狗和一只女孩。"}
]
},
{
"role": "user",
"content": [
{"text": "写一首七言绝句描述这个场景"}
]
}
]
}
}'
返回结果
{
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "海浪轻拍沙滩边,女孩与狗同嬉戏。阳光洒落笑颜开,快乐时光永铭记。"
}
]
}
}
]
},
"usage": {
"output_tokens": 27,
"input_tokens": 1298,
"image_tokens": 1247
},
"request_id": "bdf5ef59-c92e-92a6-9d69-a738ecee1590"
}
流式输出
大模型并不是一次性生成最终结果,而是逐步地生成中间结果,最终结果由中间结果拼接而成。使用非流式输出方式需要等待模型生成结束后再将生成的中间结果拼接后返回,而流式输出可以实时地将中间结果返回,您可以在模型进行输出的同时进行阅读,减少等待模型回复的时间。
OpenAI兼容
您可以通过OpenAI SDK或OpenAI兼容的HTTP方式调用通义千问VL模型,体验流式输出的功能。
Python
示例代码
from openai import OpenAI
import os
def get_response():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
},
{
"type": "text",
"text": "这是什么"
}
]
}
],
stream=True,
stream_options={"include_usage":True}
)
for chunk in completion:
print(chunk.model_dump())
if __name__=='__main__':
get_response()
返回结果
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '', 'function_call': None, 'role': 'assistant', 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '图', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '中', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '是一名', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '女子和她的狗在', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '沙滩上互动。狗狗坐在地上,', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '伸出爪子像是要握手或者击', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '掌的样子。这名女士穿着格子', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '衬衫,似乎正在与狗狗进行亲密', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '的接触,并且面带微笑。', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '背景是海洋和日出或日', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '落时分的天空。这是一', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '幅描绘人与宠物之间温馨时刻', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '的画面。', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'stop', 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': {'completion_tokens': 75, 'prompt_tokens': 1276, 'total_tokens': 1351}}
curl
示例代码
curl --location 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen-vl-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
},
{
"type": "text",
"text": "这是什么"
}
]
}
],
"stream":true,
"stream_options":{"include_usage":true}
}'
返回结果
data: {"choices":[{"delta":{"content":"","role":"assistant"},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"finish_reason":null,"delta":{"content":"图"},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"中"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"是一名"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"女子和她的狗在"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"沙滩上互动。狗狗坐在地上,"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"伸出爪子像是要握手或者击"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"掌的样子。这名女士穿着格子"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"衬衫,似乎正在与狗狗进行亲密"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"的接触,并且面带微笑。"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"他们背后的海浪拍打着海岸线"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":",天空看起来很明亮但有些模糊"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":",可能是日出或日落时"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"delta":{"content":"分拍摄的照片。整体氛围显得非常"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[{"finish_reason":"stop","delta":{"content":"和谐而温馨。"},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":1276,"completion_tokens":85,"total_tokens":1361},"created":1721823635,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-9a9ec75a-3109-9910-b79e-7bcbce81c8f9"}
data: [DONE]
DashScope
您可以通过DashScope SDK或HTTP方式调用通义千问VL模型,体验流式输出的功能。
Python
示例代码
from dashscope import MultiModalConversation
def simple_multimodal_conversation_call():
"""Simple single round multimodal conversation call.
"""
messages = [
{
"role": "user",
"content": [
{"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
{"text": "这是什么?"}
]
}
]
responses = MultiModalConversation.call(model='qwen-vl-plus',
messages=messages,
stream=True)
for response in responses:
print(response)
if __name__ == '__main__':
simple_multimodal_conversation_call()
返回结果
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 1, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 2, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 3, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 8, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 16, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 24, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 32, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或互动。背景是美丽的日落景色"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 40, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或互动。背景是美丽的日落景色,海浪轻轻拍打着海岸线"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 48, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或互动。背景是美丽的日落景色,海浪轻轻拍打着海岸线。\n\n请注意,这个描述基于图像中"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 56, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或互动。背景是美丽的日落景色,海浪轻轻拍打着海岸线。\n\n请注意,这个描述基于图像中可见的内容,并不包括任何超出视觉"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 64, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或互动。背景是美丽的日落景色,海浪轻轻拍打着海岸线。\n\n请注意,这个描述基于图像中可见的内容,并不包括任何超出视觉信息之外的推测性解释。如果您"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 72, "image_tokens": 1247}}
{"status_code": 200, "request_id": "9a752a53-00b4-9241-a61d-bed94f55bd21", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "stop", "message": {"role": "assistant", "content": [{"text": "这张图片显示了一位女士和她的狗在海滩上。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与女士握手或互动。背景是美丽的日落景色,海浪轻轻拍打着海岸线。\n\n请注意,这个描述基于图像中可见的内容,并不包括任何超出视觉信息之外的推测性解释。如果您需要更多关于场景的信息,请告诉我!"}]}}]}, "usage": {"input_tokens": 1277, "output_tokens": 80, "image_tokens": 1247}}
Java
示例代码
// Copyright (c) Alibaba, Inc. and its affiliates.
import java.util.Arrays;
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.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 io.reactivex.Flowable;
public class Main {
public static void streamCall()
throws ApiException, NoApiKeyException, UploadFileException {
MultiModalConversation conv = new MultiModalConversation();
// must create mutable map.
MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
.content(Arrays.asList(new HashMap<String, Object>(){{put("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg");}},
new HashMap<String, Object>(){{put("text", "这是什么");}})).build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
.model("qwen-vl-plus")
.message(userMessage)
.build();
Flowable<MultiModalConversationResult> result = conv.streamCall(param);
result.blockingForEach(item -> {
System.out.println(item);
});
}
public static void main(String[] args) {
try {
streamCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
返回结果
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=1), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=2), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=3), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=8), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=16), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=24), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=32), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击掌的样子。这名女士穿着格子}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=40), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击掌的样子。这名女士穿着格子衬衫,似乎正在与狗狗进行亲密}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=48), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击掌的样子。这名女士穿着格子衬衫,似乎正在与狗狗进行亲密的接触,并且面带微笑。}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=56), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击掌的样子。这名女士穿着格子衬衫,似乎正在与狗狗进行亲密的接触,并且面带微笑。背景是海洋和日出或日}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=64), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击掌的样子。这名女士穿着格子衬衫,似乎正在与狗狗进行亲密的接触,并且面带微笑。背景是海洋和日出或日落时分的天空。这是一}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=72), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=null, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击掌的样子。这名女士穿着格子衬衫,似乎正在与狗狗进行亲密的接触,并且面带微笑。背景是海洋和日出或日落时分的天空。这是一张充满温馨感的照片,展现了人}]))]))
MultiModalConversationResult(requestId=ea2bdb31-604e-9237-a55c-9e3230a10962, usage=MultiModalConversationUsage(inputTokens=1276, outputTokens=79), output=MultiModalConversationOutput(choices=[MultiModalConversationOutput.Choice(finishReason=stop, message=MultiModalMessage(role=assistant, content=[{text=图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击掌的样子。这名女士穿着格子衬衫,似乎正在与狗狗进行亲密的接触,并且面带微笑。背景是海洋和日出或日落时分的天空。这是一张充满温馨感的照片,展现了人与宠物之间的友谊时刻。}]))]))
curl
示例代码
curl --location 'https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--header 'X-DashScope-SSE: enable' \
--data '{
"model": "qwen-vl-plus",
"input":{
"messages":[
{
"role": "system",
"content": [
{"text": "You are a helpful assistant."}
]
},
{
"role": "user",
"content": [
{"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
{"text": "这个图片是哪里?"}
]
}
]
},
"parameters": {
}
}'
返回结果
id:1
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":1,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:2
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":2,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:3
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":3,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:4
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":8,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:5
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只狗在海滩上。由于我无法"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":16,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:6
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只狗在海滩上。由于我无法获取具体的地理位置信息,所以不能确定"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":24,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:7
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只狗在海滩上。由于我无法获取具体的地理位置信息,所以不能确定这是哪个地方的海滩。但是从"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":32,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:8
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只狗在海滩上。由于我无法获取具体的地理位置信息,所以不能确定这是哪个地方的海滩。但是从视觉内容来看,它可能是一个位于"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":40,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:9
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只狗在海滩上。由于我无法获取具体的地理位置信息,所以不能确定这是哪个地方的海滩。但是从视觉内容来看,它可能是一个位于沿海地区的沙滩海岸线,并且有"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":48,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:10
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只狗在海滩上。由于我无法获取具体的地理位置信息,所以不能确定这是哪个地方的海滩。但是从视觉内容来看,它可能是一个位于沿海地区的沙滩海岸线,并且有海浪拍打着岸边。这样的场景"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":56,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:11
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只狗在海滩上。由于我无法获取具体的地理位置信息,所以不能确定这是哪个地方的海滩。但是从视觉内容来看,它可能是一个位于沿海地区的沙滩海岸线,并且有海浪拍打着岸边。这样的场景在全球许多美丽的海滨地区都可以找到。"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":64,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:12
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只狗在海滩上。由于我无法获取具体的地理位置信息,所以不能确定这是哪个地方的海滩。但是从视觉内容来看,它可能是一个位于沿海地区的沙滩海岸线,并且有海浪拍打着岸边。这样的场景在全球许多美丽的海滨地区都可以找到。如果您需要更精确的信息,请提供更多的"}],"role":"assistant"},"finish_reason":"null"}]},"usage":{"input_tokens":1279,"output_tokens":72,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
id:13
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":[{"text":"这张照片显示的是一位女士和一只狗在海滩上。由于我无法获取具体的地理位置信息,所以不能确定这是哪个地方的海滩。但是从视觉内容来看,它可能是一个位于沿海地区的沙滩海岸线,并且有海浪拍打着岸边。这样的场景在全球许多美丽的海滨地区都可以找到。如果您需要更精确的信息,请提供更多的背景或细节描述。"}],"role":"assistant"},"finish_reason":"stop"}]},"usage":{"input_tokens":1279,"output_tokens":78,"image_tokens":1247},"request_id":"ba3c3410-0234-9d56-bd6c-e923255a9695"}
视频理解
qwen-vl-max-latest
、qwen-vl-max-0809
、qwen-vl-plus-latest
、qwen-vl-plus-0809
模型支持对视频内容的理解功能,您可以通过图片列表形式传入。
最少传入4张图片,最多可传入768张图片,暂时只支持通过URL形式传入图片。
如果您需要直接输入视频文件,请提交工单进行申请以及获取使用方式。
OpenAI兼容
您可以通过OpenAI SDK或HTTP方式使用视频理解功能。
Python
示例代码
import os
from openai import OpenAI
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-max-latest",
messages=[{"role": "user","content": [
{"type": "video","video": [
"https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01Ib0clU27vTgBdbVLQ_!!6000000007859-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i1/O1CN01aygPLW1s3EXCdSN4X_!!6000000005710-0-tps-3840-2160.jpg"]},
{"type": "text","text": "描述这个视频的具体过程"},
]}]
)
print(completion.model_dump_json())
返回结果
{
"id": "chatcmpl-1a5e2ed1-d328-9f36-b8c7-9b4bea57c46b",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "这个视频展示了一场足球比赛的瞬间。视频从球门网后方拍摄,可以看到球员们在场上奔跑和比赛。具体过程如下:\n\n1. **球员们在场上奔跑**:视频中可以看到多名球员在场上奔跑,他们穿着不同颜色的球衣,可能是两支不同的球队。\n2. **射门尝试**:一名球员尝试射门,将球踢向球门。\n3. **守门员扑救**:守门员试图扑救,但球已经飞入球门。\n4. **进球瞬间**:球飞入球门,球门网被球击中,显示出进球的瞬间。\n\n整个过程充满了紧张和激动,展示了足球比赛中的精彩瞬间。",
"refusal": null,
"role": "assistant",
"function_call": null,
"tool_calls": null
}
}
],
"created": 1728709606,
"model": "qwen-vl-max-latest",
"object": "chat.completion",
"service_tier": null,
"system_fingerprint": null,
"usage": {
"completion_tokens": 151,
"prompt_tokens": 1465,
"total_tokens": 1616
}
}
curl
示例代码
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-vl-max-latest",
"messages": [{"role": "user",
"content": [{"type": "video",
"video": ["https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01Ib0clU27vTgBdbVLQ_!!6000000007859-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i1/O1CN01aygPLW1s3EXCdSN4X_!!6000000005710-0-tps-3840-2160.jpg"
]},
{"type": "text",
"text": "描述这个视频的具体过程"}]}]
}'
返回结果
{
"choices": [
{
"message": {
"content": "这个视频展示了一场足球比赛的瞬间。具体过程如下:\n\n1. **背景**:视频是在一个大型体育场内拍摄的,观众席上坐满了观众,灯光明亮,气氛热烈。\n2. **球员**:场上有两支队伍,一支穿着红色球衣,另一支穿着蓝色球衣。守门员穿着绿色球衣。\n3. **动作**:一名身穿红色球衣的球员在禁区内接到传球,准备射门。守门员迅速反应,向球的方向扑去,试图阻止进球。\n4. **射门**:红色球衣的球员果断射门,球飞向球门。\n5. **扑救**:守门员尽力扑救,但球还是飞进了球门,球网被球撞得晃动。\n\n整个过程充满了紧张和刺激,展示了足球比赛中的精彩瞬间。",
"role": "assistant"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 1466,
"completion_tokens": 181,
"total_tokens": 1647
},
"created": 1728710375,
"system_fingerprint": null,
"model": "qwen-vl-max-latest",
"id": "chatcmpl-73b2b130-b29a-99db-9eda-4cd45f27d4e0"
}
DashScope
您可以通过DashScope SDK或HTTP方式使用视频理解功能。
Python
示例代码
from http import HTTPStatus
import os
# dashscope版本需要不低于1.20.10
import dashscope
messages = [{"role": "user",
"content": [
{"video":["https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01Ib0clU27vTgBdbVLQ_!!6000000007859-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i1/O1CN01aygPLW1s3EXCdSN4X_!!6000000005710-0-tps-3840-2160.jpg"]},
{"text": "描述这个视频的具体过程"}]}]
response = dashscope.MultiModalConversation.call(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
model='qwen-vl-max-latest',
messages=messages
)
if response.status_code == HTTPStatus.OK:
print(response)
else:
print(response.code)
print(response.message)
返回结果
{
"status_code": 200,
"request_id": "957b3e07-6ed4-9a6c-9edb-b7672e941ef8",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "这个视频展示了一场足球比赛的瞬间。具体过程如下:\n\n1. **背景**:视频是在一个大型体育场拍摄的,观众席上坐满了观众,灯光明亮,气氛热烈。\n2. **球员**:场上有两队球员,一队穿着红色球衣,另一队穿着蓝色球衣。守门员穿着绿色球衣。\n3. **动作**:一名身穿红色球衣的球员在禁区内接到传球,准备射门。守门员迅速反应,向球的方向扑去,试图阻止进球。\n4. **射门**:红色球衣的球员成功将球踢向球门,球飞向守门员的右侧。\n5. **进球**:守门员虽然尽力扑救,但球还是从他的右侧飞入球门,进球成功。\n\n整个过程展示了足球比赛中的紧张和精彩瞬间,尤其是射门和守门员的扑救动作。"
}
]
}
}
]
},
"usage": {
"input_tokens": 1466,
"output_tokens": 194,
"video_tokens": 1440
}
}
curl
示例代码
curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-vl-max-latest",
"input": {
"messages": [
{
"role": "user",
"content": [
{
"video": [
"https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01Ib0clU27vTgBdbVLQ_!!6000000007859-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i1/O1CN01aygPLW1s3EXCdSN4X_!!6000000005710-0-tps-3840-2160.jpg"
]
},
{
"text": "描述这个视频的具体过程"
}
]
}
]
}
}'
返回结果
{
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "这个视频展示了一场足球比赛的瞬间。具体过程如下:\n\n1. **背景**:视频是在一个大型体育场拍摄的,观众席上坐满了观众,灯光明亮,气氛热烈。\n2. **球员**:场上有两队球员,一队穿着红色球衣,另一队穿着蓝色球衣。守门员穿着绿色球衣。\n3. **动作**:一名穿着红色球衣的球员在禁区内接到了队友的传球,准备射门。\n4. **射门**:红色球员用右脚大力射门,球飞向球门。\n5. **扑救**:守门员迅速反应,向球的方向扑去,试图将球扑出。\n6. **进球**:尽管守门员尽力扑救,但球还是飞进了球门,守门员未能阻止进球。\n\n整个过程充满了紧张和激动,展示了足球比赛中的精彩瞬间。"
}
]
}
}
]
},
"usage": {
"output_tokens": 191,
"video_tokens": 1440,
"input_tokens": 1466
},
"request_id": "c728d1e0-79ad-9076-8589-7f072e96bccf"
}
输入与输出参数
您可以通过下表查看不同调用方式的输入参数与输出参数。其中,数据类型列中各字段的含义如下所示:
string:字符串类型。
array:在Python中表示列表,在Java中表示ArrayList。
integer:表示整数型。
float:浮点型。
boolean:布尔型。
object:哈希表。
OpenAI兼容
以下为Python SDK参数,若您通过HTTP方式调用,可以参考代码与下方参数。
输入参数
参数 | 类型 | 默认值 | 说明 |
model | string | - | 用户使用的模型名称。 |
messages | array | - | 用户与模型的对话历史。array中的每个元素形式为 |
| float | - | 生成过程中的核采样方法概率阈值,例如,取值为0.8时,仅保留概率加起来大于等于0.8的最可能token的最小集合作为候选集。取值范围为(0,1.0],取值越大,生成的随机性越高;取值越小,生成的确定性越高。 |
max_tokens(可选) | integer | - | 指定模型可生成的最大token个数。 |
temperature(可选) | float | - | 用于控制模型回复的随机性和多样性。具体来说,temperature值控制了生成文本时对每个候选词的概率分布进行平滑的程度。较高的temperature值会降低概率分布的峰值,使得更多的低概率词被选择,生成结果更加多样化;而较低的temperature值则会增强概率分布的峰值,使得高概率词更容易被选择,生成结果更加确定。 取值范围: [0, 2),不建议取值为0,无意义。 |
presence_penalty (可选) | float | - | 用户控制模型生成时整个序列中的重复度。提高presence_penalty时可以降低模型生成的重复度,取值范围[-2.0, 2.0]。 |
seed(可选) | integer | - | 生成时使用的随机数种子,用于控制模型生成内容的随机性。seed支持无符号64位整数。 |
stream(可选) | boolean | False | 用于控制是否使用流式输出。当以stream模式输出结果时,接口返回结果为generator,需要通过迭代获取结果,每次输出为当前生成的增量序列。 |
stop(可选) | string or array | None | stop参数用于实现内容生成过程的精确控制,在模型生成的内容即将包含指定的字符串或token_id时自动停止。stop可以为string类型或array类型。
|
stream_options(可选) | object | None | 该参数用于配置在流式输出时是否展示使用的token数目。只有当stream为True的时候该参数才会激活生效。若您需要统计流式输出模式下的token数目,可将该参数配置为 |
出参描述
返回参数 | 数据类型 | 说明 | 备注 |
id | string | 系统生成的标识本次调用的id。 | 无 |
model | string | 本次调用的模型名。 | 无 |
system_fingerprint | string | 模型运行时使用的配置版本,当前暂时不支持,返回为空字符串“”。 | 无 |
choices | array | 模型生成内容的详情。 | 无 |
choices[i].finish_reason | string | 有三种情况:
| |
choices[i].message | object | 模型输出的消息。 | |
choices[i].message.role | string | 模型的角色,固定为assistant。 | |
choices[i].message.content | string | 模型生成的文本。 | |
choices[i].index | integer | 生成的结果序列编号,默认为0。 | |
created | integer | 当前生成结果的时间戳(s)。 | 无 |
usage | object | 计量信息,表示本次请求所消耗的token数据。 | 无 |
usage.prompt_tokens | integer | 用户输入文本转换成token后的长度。 | 您可以参考本地tokenizer统计token数据进行token的估计。 |
usage.completion_tokens | integer | 模型生成回复转换为token后的长度。 | 无 |
usage.total_tokens | integer | usage.prompt_tokens与usage.completion_tokens的总和。 | 无 |
DashScope SDK
输入参数
参数 | 类型 | 默认值 | 说明 |
model | string | - | 指定用于对话的通义千问模型名,如:qwen-vl-plus。 |
messages | array | - | 用户输入的历史对话信息和指令组成的完整输入信息。 参考示例,目前对于图片,支持图片url和本地路径 本地路径格式。 |
top_p (可选) | float | 0.1 | 生成过程中核采样方法概率阈值,例如,取值为0.1时,仅保留概率加起来大于等于0.1的最可能token的最小集合作为候选集。取值范围为(0,1.0],取值越大,生成的随机性越高;取值越低,生成的确定性越高。 |
max_tokens(可选) | integer | - | 指定模型可生成的最大token个数。 |
vl_high_resolution_images(可选) | boolean | False | 是否提高输入图片的默认token上限。输入图片的默认token上限为1280,配置为True时输入图片的token上限为16384。 |
temperature(可选) | float | - | 用于控制模型回复的随机性和多样性。具体来说,temperature值控制了生成文本时对每个候选词的概率分布进行平滑的程度。较高的temperature值会降低概率分布的峰值,使得更多的低概率词被选择,生成结果更加多样化;而较低的temperature值则会增强概率分布的峰值,使得高概率词更容易被选择,生成结果更加确定。 取值范围: [0, 2),不建议取值为0,无意义。 |
stream(可选) | boolean | False | 是否使用流式输出。当以stream模式输出结果时,接口返回结果为generator,需要通过迭代获取结果,每个输出为当前生成的整个序列,最后一次输出为最终全部生成结果。 |
stop(可选) | string or array | None | stop参数用于实现内容生成过程的精确控制,在模型生成的内容即将包含指定的字符串或token_id时自动停止。stop可以为string类型或array类型。
|
top_k(可选) | integer | 100 | 生成时,采样候选集的大小。例如,取值为50时,仅将单次生成中得分最高的50个token组成随机采样的候选集。取值越大,生成的随机性越高;取值越小,生成的确定性越高。注意:如果top_k的值大于100,top_k将采用默认值100。 |
seed(可选) | integer | 1234 | 生成时,随机数的种子,用于控制模型生成的随机性。如果使用相同的种子,每次运行生成的结果都将相同;当需要复现模型的生成结果时,可以使用相同的种子。seed参数支持无符号64位整数类型。默认值 1234 |
incremental_output | boolean | False | 是否使用增量输出。当使用增量输出时每次流式返回的序列仅包含最新生成的增量内容,默认输出完整的全量内容。 |
出参描述
返回参数 | 类型 | 说明 | 示例 |
status_code | integer | 200(HTTPStatus.OK)表示请求成功,否则表示请求失败,可以通过code获取错误码,通过message字段获取错误详细信息。 | |
request_id | string | 系统生成的标志本次调用的id。 | |
code | string | 表示请求失败,表示错误码,成功忽略。 | |
output | object | 调用结果信息,对于通义VL模型,包含输出message。 | |
output.choices | array | 模型为输入生成的完成选项列表。 | "choices": [ { "finish_reason": null, "message": { "role": "assistant", "content": [ { "text": "这是一个" } ] } } ] |
output.choices[x].message.role | string | 信息来源。 | |
output.choices[x].message.content | array | 本次请求的算法输出内容,输出可能包含多种模态内容,因此,是一个list,元素为dict,key为内容类型,value为内容值。 | |
usage | object | 计量信息,表示本次请求计量数据。 | |
usage.input_tokens | integer | 用户输入转换成Token后的长度。 | |
usage.output_tokens | integer | 模型生成回复转换为Token后的长度。 | |
usage.image_tokens | integer | 当使用图像理解时,用户输入图像转换成Token后的长度。 |
DashScope HTTP
输入参数
传参方式 | 字段 | 数据类型 | 必选 | 描述 | 示例值 |
Header | Content-Type | string | 是 | 请求类型:application/json或者text/event-stream(开启 SSE 响应) | application/json |
Authorization | string | 是 | API-KEY | Bearer d1**2a | |
X-DashScope-WorkSpace | string | 否 | 指明本次调用需要使用的workspace;需要注意的是,对于子账号Apikey调用,此参数为必选项,子账号必须归属于某个workspace才能调用;对于主账号Apikey此项为可选项,添加则使用对应的workspace身份,不添加则使用主账号身份。 | ws_QTggmeAxxxxx | |
X-DashScope-SSE | string | 否 | 跟Accept: text/event-stream 二选一即可启用SSE响应。 | enable | |
Body | model | string | 是 | 指明需要调用的模型。 | qwen-vl-plus |
input.messages[list] | array | 是 | 多模态场景下的输入信息。 | "messages":[ { "role": "user", "content": [ {"image": "http://XXXX"}, {"text": "这个图片是哪里?"} ] }] | |
input.messages[x].role | string | 是 | 信息来源。 | ||
input.messages[x].content[list] | array | 是 | contents列表中包含本次输入的内容列表,每一个列表可以分别是图像(image)或者文本(text)。 | ||
input.messages[x].content[list].image | string | 至少包含一项 | 本次输入的图像内容的URL链接。图像格式:bmp、jpg、jpeg、png和tiff。 | ||
input.message[x].content[list].text | string | 本次输入的文本内容。支持utf-8编码的中文、英文输入。 | |||
parameters.top_p | float | 否 | 生成时,核采样方法的概率阈值。 取值范围为(0,1.0),取值越大,生成的随机性越高;取值越小,生成的随机性越低。默认值 0.8。 例如,取值为0.8时,仅保留累计概率之和大于等于0.8的概率分布中的token,作为随机采样的候选集。 | 0.8 | |
parameters.max_tokens | integer | 否 | 指定模型可生成的最大token个数。 | 1000 | |
parameters.vl_high_resolution_images | boolean | 否 | 是否提高输入图片的默认token上限。输入图片的默认token上限为1280,配置为True时输入图片的token上限为16384。 | True | |
parameters.temperature | float | 否 | 用于控制模型回复的随机性和多样性。具体来说,temperature值控制了生成文本时对每个候选词的概率分布进行平滑的程度。较高的temperature值会降低概率分布的峰值,使得更多的低概率词被选择,生成结果更加多样化;而较低的temperature值则会增强概率分布的峰值,使得高概率词更容易被选择,生成结果更加确定。 取值范围: [0, 2),不建议取值为0,无意义。 | 0.8 | |
parameters.top_k | integer | 否 | 生成时,采样候选集的大小。取值越大,生成的随机性越高;取值越小,生成的确定性越高。如果top_k的值大于100,top_k将采用默认值100 例如,取值为50时,仅将单次生成中得分最高的50个token组成随机采样的候选集。 | 50 | |
parameters.seed | integer | 否 | 生成时,随机数的种子,用于控制模型生成的随机性。如果使用相同的种子,每次运行生成的结果都将相同;当需要复现模型的生成结果时,可以使用相同的种子。默认值为1234。 seed参数支持无符号64位整数类型。 | 65535 | |
parameters.stop | string or array | 否 | stop参数用于实现内容生成过程的精确控制,在模型生成的内容即将包含指定的字符串或token_id时自动停止。stop可以为string类型或array类型。
|
| |
parameters.incremental_output | boolean | 否 | 是否使用增量输出。当使用增量输出时每次流式返回的序列仅包含最新生成的增量内容,默认值为false,即输出完整的全量内容。 | 无 |
出参描述
字段 | 类型 | 描述 | 示例值 |
output.choices[list] | array | 模型为输入生成的完成选项列表。 | "choices": [ { "finish_reason": null, "message": { "role": "assistant", "content": [ { "text": "这是一个" } ] } } ] |
output.choices[x].message.role | string | 信息来源。 | |
output.choices[x].message.content | array | 本次请求的算法输出内容,为list,内容为多个dict,key为内容类型,value为内容值 | |
output.finish_reason | string | 有三种情况:正在生成时为null,生成结束时如果由于停止token导致则为stop,生成结束时如果因为生成长度过长导致则为length。 | stop |
usage.output_tokens | integer | 输出内容的 token 数目。 | 380 |
usage.input_tokens | integer | 用户输入内容的 token 数目。 | 633 |
usage.image_tokens | integer | 当使用图像理解时,用户输入图像转换成Token后的长度。 | |
request_id | string | 本次请求的系统唯一码 | 7574ee8f-38a3-4b1e-9280-11c33ab46e51 |
状态码说明
大模型服务平台通用状态码详情,请参见返回状态码说明。