文档

OpenAI Vision接口兼容

更新时间:

百炼为通义千问视觉模型提供了与OpenAI兼容的使用方式。如果您之前使用OpenAI SDK或者其他OpenAI兼容接口(例如langchain_openai SDK),或者HTTP方式调用OpenAI的视觉模型服务,只需在原有框架下调整API-KEY、BASE_URL、model等参数,就可以直接使用通义千问视觉模型。

兼容OpenAI需要信息

BASE_URL

BASE_URL表示模型服务的网络访问点或地址。通过该地址,您可以访问服务提供的功能或数据。在Web服务或API的使用中,BASE_URL通常对应于服务的具体操作或资源的URL。当您使用OpenAI兼容接口来使用通义千问视觉模型服务时,需要配置BASE_URL。

  • 当您通过OpenAI SDK或其他OpenAI兼容的SDK调用时,需要配置的BASE_URL如下:

    https://dashscope.aliyuncs.com/compatible-mode/v1
  • 当您通过HTTP请求调用时,需要配置的完整访问endpoint如下:

    POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions

获取API-KEY

您需要开通百炼模型服务并获得API-KEY,详情请参考:获取API-KEY

支持的模型列表

当前OpenAI兼容接口支持的通义千问系列模型如下表所示。

通义千问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,增强图像理解和视频推理能力,可以更好地识别图片中的多语言文字和手写体的文字。

32,000

30,000

2,000

0.02元

100万Token

有效期:百炼开通后30天内

2024年9月19日0点后开通百炼的用户,免费额度有效期为180天。

qwen-vl-max-latest

qwen-vl-max模型的最新版本。

0.02元

100万Token

有效期:百炼开通后30天内

2024年9月19日0点后开通百炼的用户,免费额度有效期为180天。

qwen-vl-max-2024-08-09

(qwen-vl-max-0809)

qwen-vl-max的2024年8月9日快照版本,快照版本维护到下个快照版本发布时间(待定)后一个月。

0.02元

100万Token

有效期:已开通百炼的用户,自8月23日0点起30天内有效。

新开通百炼的用户,在开通后180天内有效。

qwen-vl-plus-latest

qwen-vl-plus模型的最新版本。

0.008元

100万Token

有效期:百炼开通后30天内

2024年9月19日0点后开通百炼的用户,免费额度有效期为180天。

qwen-vl-plus-2024-08-09

(qwen-vl-plus-0809)

qwen-vl-plus的2024年8月9日快照版本。

0.008元

100万Token

有效期:百炼开通后30天内

2024年9月19日0点后开通百炼的用户,免费额度有效期为180天。

qwen-vl-max-2024-02-01

(qwen-vl-max-0201)

qwen-vl-max的2024年2月1日快照版本,快照版本维护到下个快照版本发布时间(待定)后一个月。

8,000

6,000

2,000

0.02元

qwen-vl-plus

该模型为稳定版本。

大幅提升细节识别能力和文字识别能力,支持超百万像素分辨率和任意长宽比规格的图像。在广泛的视觉任务中提供卓越性能。

0.008元

通过OpenAI SDK调用

前提条件

  • 请确保您的计算机上安装了Python环境。

  • 请安装最新版OpenAI SDK。

    # 如果下述命令报错,请将pip替换为pip3
    pip install -U openai
  • 您需要开通百炼模型服务并获得API-KEY,详情请参考:获取API-KEY

  • 我们推荐您将API-KEY配置到环境变量中以降低API-KEY的泄露风险,配置方法可参考配置API-KEY到环境变量。您也可以在代码中配置API-KEY,但是泄风险会提高

使用方式

您可以参考以下示例来使用OpenAI SDK访问通义千问视觉模型。

非流式输出

您可以输入单张或多张图片。
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": "text",
                  "text": "这些是什么"
                },
                {
                  "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"
                  }
                }
              ]
            }
          ]
        )
    print(completion.model_dump_json())

if __name__=='__main__':
    get_response()

运行代码可以获得以下结果:

{
    "id": "chatcmpl-3edf4830-fb91-90d2-bec4-d3e97a5910ea",
    "choices": [
        {
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null,
            "message": {
                "content": "图1中是一名女子和她的宠物狗在沙滩上互动,狗狗抬起前爪似乎想要握手。\n图2是CG渲染的一张老虎的图片。",
                "role": "assistant",
                "function_call": null,
                "tool_calls": null
            }
        }
    ],
    "created": 1724638019,
    "model": "qwen-vl-plus",
    "object": "chat.completion",
    "service_tier": null,
    "system_fingerprint": null,
    "usage": {
        "completion_tokens": 33,
        "prompt_tokens": 2509,
        "total_tokens": 2542
    }
}

流式输出

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": "text",
                  "text": "这是什么"
                },
                {
                  "type": "image_url",
                  "image_url": {
                    "url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
                  }
                }
              ]
            }
          ],
        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}}

本地文件

当前API请求负载限制在6M以下。所以VL模型通过base64格式输入的字符串也不能超过此限制。对应的输入图片原始大小需小于4.5Mb。

qwen-vl模型支持通过base64编码的图片输入,您可以将本地图片转换为base64字符串后进行调用。示例图片:test.png

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": "text",
                  "text": "这是什么"
                },
                {
                  "type": "image_url",
                  "image_url": {
                    "url": f"data:image/jpeg;base64,{base64_image}"
                  }
                }
              ]
            }
          ],
          stream=True,
          stream_options={"include_usage":True}
        )
    for chunk in completion:
        print(chunk.model_dump())

if __name__=='__main__':
    get_response("test.png")

如果需要非流式输出,将stream相关配置参数去除,并直接打印completion即可。

返回结果

{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '', 'function_call': None, 'role': 'assistant', 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '这', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '是一', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '只', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '在天空中飞翔的鹰。它有着广阔的翅膀,正在', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '翱翔于云层之间。这种', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '鸟类通常被认为是力量、自由和雄', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '心壮志的象征,在许多文化', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '中都具有重要的意义。', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'stop', 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': {'completion_tokens': 47, 'prompt_tokens': 1254, 'total_tokens': 1301}}

输入参数配置

输入参数与OpenAI的接口参数对齐,当前已支持的参数如下:

参数

类型

默认值

说明

model

string

-

用户使用的模型名称。

messages

array

-

用户与模型的对话历史。array中的每个元素形式为{"role":角色, "content": 内容}。角色当前可选值:system、user、assistant,其中,仅messages[0]中支持role为system,一般情况下,user和assistant需要交替出现,且messages中最后一个元素的role必须为user。

top_p(可选)

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类型。

  • string类型

    当模型将要生成指定的stop词语时停止。

    例如将stop指定为"你好",则模型将要生成“你好”时停止。

  • array类型

    array中的元素可以为token_id或者字符串,或者元素为token_id的array。当模型将要生成的token或其对应的token_id在stop中时,模型生成将会停止。以下为stop为array时的示例(tokenizer对应模型为qwen-turbo):

    1.元素为token_id:

    token_id为108386和104307分别对应token为“你好”和“天气”,设定stop为[108386,104307],则模型将要生成“你好”或者“天气”时停止。

    2.元素为字符串:

    设定stop为["你好","天气"],则模型将要生成“你好”或者“天气”时停止。

    3.元素为array:

    token_id为108386和103924分别对应token为“你好”和“啊”,token_id为35946和101243分别对应token为“我”和“很好”。设定stop为[[108386, 103924],[35946, 101243]],则模型将要生成“你好啊”或者“我很好”时停止。

    说明

    stop为array类型时,不可以将token_id和字符串同时作为元素输入,比如不可以指定stop为["你好",104307]

stream_options(可选)

object

None

该参数用于配置在流式输出时是否展示使用的token数目。只有当stream为True的时候该参数才会激活生效。若您需要统计流式输出模式下的token数目,可将该参数配置为stream_options={"include_usage":True}

通过langchain_openai SDK调用

前提条件

  • 请确保您的计算机上安装了Python环境。

  • 通过运行以下命令安装langchain_openai SDK。

    # 如果下述命令报错,请将pip替换为pip3
    pip install -U langchain_openai
  • 您需要开通百炼模型服务并获得API-KEY,详情请参考:获取API-KEY

  • 我们推荐您将API-KEY配置到环境变量中以降低API-KEY的泄露风险,详情可参考配置API-KEY到环境变量。您也可以在代码中配置API-KEY,但是泄露风险会提高

使用方式

您可以参考以下示例来通过langchain_openai SDK使用通义千问视觉模型。

非流式输出

非流式输出使用invoke方法实现,请参考以下示例代码:

from langchain_openai import ChatOpenAI
import os


def get_response():
    llm = ChatOpenAI(
      # 如果您没有配置环境变量,请在此处用您的API Key进行替换
      api_key=os.getenv("DASHSCOPE_API_KEY"),
      # 填写DashScope base_url
      base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
      model="qwen-vl-plus",
      )
    messages= [
            {
              "role": "user",
              "content": [
                {
                  "type": "text",
                  "text": "这是什么"
                },
                {
                  "type": "image_url",
                  "image_url": {
                    "url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
                  }
                }
              ]
            }
          ]
    response = llm.invoke(messages)
    print(response.json(ensure_ascii=False))

if __name__ == "__main__":
    get_response()

运行代码,可以得到以下结果:

{
  "content": "图中是一名女子和她的狗在沙滩上互动。狗狗坐在地上,伸出爪子像是要握手或者击掌的样子。这名女士穿着格子衬衫,似乎正在与狗狗进行亲密的接触,并且面带微笑。背景是海洋和日出或日落时分的天空。这是一张充满温馨感的照片,展现了人与宠物之间的友谊时刻。",
  "additional_kwargs": {},
  "response_metadata": {
    "token_usage": {
      "completion_tokens": 79,
      "prompt_tokens": 1276,
      "total_tokens": 1355
    },
    "model_name": "qwen-vl-plus",
    "system_fingerprint": null,
    "finish_reason": "stop",
    "logprobs": null
  },
  "type": "ai",
  "name": null,
  "id": "run-c72701d2-e2c6-40a8-9e8b-37b58d53160f-0",
  "example": false,
  "tool_calls": [],
  "invalid_tool_calls": [],
  "usage_metadata": {
    "input_tokens": 1276,
    "output_tokens": 79,
    "total_tokens": 1355
  }
}

流式输出

from langchain_openai import ChatOpenAI
import os


def get_response():
    llm = ChatOpenAI(
        # 如果您没有配置环境变量,请在此处用您的API Key进行替换
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # 填写DashScope base_url
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
        model="qwen-plus",
        # 通过以下设置,在流式输出的最后一行展示token使用信息
        stream_options={"include_usage": True}
    )
    messages= [
            {
              "role": "user",
              "content": [
                {
                  "type": "text",
                  "text": "这是什么"
                },
                {
                  "type": "image_url",
                  "image_url": {
                    "url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
                  }
                }
              ]
            }
          ]
    response = llm.stream(messages)
    for chunk in response:
        print(chunk.json(ensure_ascii=False))

if __name__ == "__main__":
    get_response()

运行以上代码,可得到以下示例结果:

{"content": "", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "这张", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "图片", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "中", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "有一", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "只狗和一个小", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "女孩。狗看起来", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "很友好,可能是", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "宠物,而小女孩", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "似乎在与狗", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "互动或玩耍。", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "这是一幅展示", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "人与动物之间", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "温馨关系的画面。", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "", "additional_kwargs": {}, "response_metadata": {"finish_reason": "stop"}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": {"input_tokens": 23, "output_tokens": 40, "total_tokens": 63}, "tool_call_chunks": []}

关于输入参数的配置,可以参考输入参数配置,相关参数在ChatOpenAI对象中定义。

通过HTTP接口调用

您可以通过HTTP接口来调用通义千问视觉模型,获得与通过HTTP接口调用OpenAI服务相同结构的返回结果。

前提条件

  • 您需要开通百炼模型服务并获得API-KEY,详情请参考:获取API-KEY

  • 我们推荐您将API-KEY配置到环境变量中以降低API-KEY的泄露风险,配置方法可参考配置API-KEY到环境变量。您也可以在代码中配置API-KEY,但是泄露风险会提高

提交接口调用

POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions

请求示例

以下示例展示通过CURL命令来调用API的脚本。

说明

如果您没有配置API-KEY为环境变量,需将$DASHSCOPE_API_KEY更换为您的API-KEY

非流式输出

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": "text",
          "text": "这些是什么"
        },
        {
          "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"
          }
        }
      ]
    }
  ]
}'

运行命令可得到以下结果:

{
  "choices": [
    {
      "message": {
        "content": "图1中是一名女子和她的宠物狗在沙滩上互动,狗狗抬起前爪似乎想要握手。\n图2是CG渲染的一张老虎的图片。",
        "role": "assistant"
      },
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null
    }
  ],
  "object": "chat.completion",
  "usage": {
    "prompt_tokens": 2509,
    "completion_tokens": 34,
    "total_tokens": 2543
  },
  "created": 1724729556,
  "system_fingerprint": null,
  "model": "qwen-vl-plus",
  "id": "chatcmpl-1abb4eb9-f508-9637-a8ba-ac7fc6f73e53"
}

流式输出

如果您需要使用流式输出,请在请求体中指定stream参数为true。

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": "text",
          "text": "这是什么"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
          }
        }
      ]
    }
  ],
    "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":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"finish_reason":null,"delta":{"content":"图"},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"中"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"是一名"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"女子和她的狗在"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"沙滩上互动。狗狗坐在地上,"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"伸出爪子像是要握手或者击"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"掌的样子。这名女士穿着格子"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"衬衫,似乎正在与狗狗进行亲密"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"的接触,并且面带微笑。"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"背景是海洋和日出或日"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"落时分的天空。这是一"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"delta":{"content":"张充满温馨感的照片,展现了人"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[{"finish_reason":"stop","delta":{"content":"与宠物之间的友谊时刻。"},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":1276,"completion_tokens":79,"total_tokens":1355},"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}

data: [DONE]

输入参数的详情请参考输入参数配置

异常响应示例

在访问请求出错的情况下,输出的结果中会通过 code 和 message 指明出错原因。

{
    "error": {
        "message": "Incorrect API key provided. ",
        "type": "invalid_request_error",
        "param": null,
        "code": "invalid_api_key"
    }
}

状态码说明

错误码

说明

400 - Invalid Request Error

输入请求错误,细节请参见具体报错信息。

401 - Incorrect API key provided

API key不正确。

429 - Rate limit reached for requests

QPS、QPM等超限。

429 - You exceeded your current quota, please check your plan and billing details

额度超限或者欠费。

500 - The server had an error while processing your request

服务端错误。

503 - The engine is currently overloaded, please try again later

服务端负载过高,可重试。