AI学习:图片对话 -通义千问VL

通义千问VL

通义千问VL是阿里云研发的大规模视觉语言模型(Large Vision Language Model, LVLM),可以以图像、文本、检测框作为输入,并以文本和检测框作为输出,支持中文多模态对话及多图对话。

特点:

  1. 强大的性能:在四大类多模态任务的标准英文测评中(Zero-shot Captioning/VQA/DocVQA/Grounding)上,均取得同等通用模型大小下最好效果;
  2. 多语言对话模型:天然支持英文、中文等多语言对话,端到端支持图片里中英双语的长文本识别;
  3. 多图交错对话:支持多图输入和比较,指定图片问答,多图文学创作等;
  4. 首个支持中文开放域定位的通用模型:通过中文开放域语言表达进行检测框标注;
  5. 细粒度识别和理解:相比于目前其它开源LVLM使用的224分辨率,Qwen-VL是首个开源的448分辨率的LVLM模型。更高分辨率可以提升细粒度的文字识别、文档问答和检测框标注。

-这个过程基本上是将用户提供的文本和图片信息转换成模型可以理解的格式,也就是token序列。所谓token,可以简单理解为文本的基本单元,比如中文里的一个字或者英文里的一个单词。对于图片,也会被转换成一系列固定长度的token。

用户输入的信息包括多轮对话历史和当前指令,这些文本信息会被拆分成token序列。举例来说,中文文本“你好,我是通义千问”会被分成[‘你’, ‘好’, ‘,’, ‘我’, ‘是’, ‘通’, ‘义’, ‘千’, ‘问’],而英文文本"Nice to meet you."则会被分成[‘Nice’, ’ to’, ’ meet’, ’ you’, ‘.’]。

此外,如果用户提供了图片,也会被转换成一系列固定长度的token序列,以便模型能够处理。

整个过程的目标是将用户输入的多样化信息转化成模型能够理解和处理的格式,然后模型会生成相应的回复,最终以token序列的形式返回给用户。

由于模型调用的计算量与token序列长度相关,输入或输出token数量越多,模型的计算时间越长,我们将根据模型输入和输出的token数量计费。可以从API返回结果的 usage 字段中了解到您每次调用时使用的token数量。

模型概述

模型名模型简介
qwen-vl-v1以 Qwen-7B 语言模型初始化,添加图像模型,图像输入分辨率为448的预训练模型。
qwen-vl-chat-v1通义千问VL支持灵活的交互方式,包括多图、多轮问答、创作等能力的模型。

SDK使用

前提条件

API-KEY设置

export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY

简单示例

from http import HTTPStatus
import dashscope


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": "这是什么?"}
            ]
        }
    ]
    response = dashscope.MultiModalConversation.call(model=dashscope.MultiModalConversation.Models.qwen_vl_chat_v1,
                                                     messages=messages)
    # The response status_code is HTTPStatus.OK indicate success,
    # otherwise indicate request is failed, you can get error code
    # and message from code and message.
    if response.status_code == HTTPStatus.OK:
        print(response)
    else:
        print(response.code)  # The error code.
        print(response.message)  # The error message.


if __name__ == '__main__':
    simple_multimodal_conversation_call()
{"status_code": 200, "request_id": "f11aa834-1c83-93f3-820f-6aade67ff5b5", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": null, "message": {"role": "assistant", "content": "图中是一 名女子在沙滩上和狗玩耍,旁边的狗是一只拉布拉多犬,它们处于沙滩上。"}}]}, "usage": {"input_tokens": 285, "output_tokens": 25}}

本地文件

在这里插入图片描述

from dashscope import MultiModalConversation
def call_with_local_file():
    """Sample of use local file.
       linux&mac file schema: file:///home/images/test.png
       windows file schema: file://D:/images/abc.png
    """
    local_file_path = 'file://D:/ai/python/aliyun/image.png'
    messages = [{
        'role': 'system',
        'content': [{
            'text': 'You are a helpful assistant.'
        }]
    }, {
        'role':
        'user',
        'content': [
            {
                'image': local_file_path
            },
            {
                'text': '图片里有什么东西?'
            },
        ]
    }]
    response = MultiModalConversation.call(model=MultiModalConversation.Models.qwen_vl_chat_v1, messages=messages)
    print(response)


if __name__ == '__main__':
    call_with_local_file()
{"status_code": 200, "request_id": "f7336d09-a173-91c5-9ec8-644af239d15d", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": null, "message": {"role": "assistant", "content": "图片里有 一只蓝眼睛、蓝鼻头的猫,它正在地毯上坐着。"}}]}, "usage": {"input_tokens": 287, "output_tokens": 19}}

多轮对话

from http import HTTPStatus
from dashscope import MultiModalConversation


def conversation_call():
    """Sample of multiple rounds of conversation.
    """
    messages = [
        {
            "role": "user",
            "content": [
                {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
                {"text": "这是什么?"},
            ]
        }
    ]
    response = MultiModalConversation.call(model=MultiModalConversation.Models.qwen_vl_chat_v1,
                                           messages=messages)
    # The response status_code is HTTPStatus.OK indicate success,
    # otherwise indicate request is failed, you can get error code
    # and message from code and message.
    if response.status_code == HTTPStatus.OK:
        print(response)
    else:
        print(response.code)  # The error code.
        print(response.message)  # The error message.
    messages.append({'role': response.output.choices[0].message.role,
                     'content': [{'text': response.output.choices[0].message.content}]})
    messages.append({"role": "user",
                     "content": [
                         {"text": "她们在干什么?", }
                     ]})

    response = MultiModalConversation.call(model=MultiModalConversation.Models.qwen_vl_chat_v1,
                                           messages=messages)
    # The response status_code is HTTPStatus.OK indicate success,
    # otherwise indicate request is failed, you can get error code
    # and message from code and message.
    if response.status_code == HTTPStatus.OK:
        print(response)
    else:
        print(response.code)  # The error code.
        print(response.message)  # The error message.


if __name__ == '__main__':
    conversation_call()
{"status_code": 200, "request_id": "096e75cb-7ad2-9626-a7cd-673e256537e2", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": null, "message": {"role": "assistant", "content": "图中是一 名女子在沙滩上和狗玩耍,旁边的狗是一只拉布拉多犬,它们处于沙滩上。"}}]}, "usage": {"input_tokens": 285, "output_tokens": 25}}
{"status_code": 200, "request_id": "ee838d79-c456-9924-a3e4-5af0631b20f9", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": null, "message": {"role": "assistant", "content": "她们在和 狗玩耍,女孩坐在沙滩上,带着一只拉布拉多犬,她们可能在欣赏风景,也可能在和狗玩耍。"}}]}, "usage": {"input_tokens": 324, "output_tokens": 30}}

流式输出

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=MultiModalConversation.Models.qwen_vl_chat_v1,
                                           messages=messages,
                                           stream=True)
    for response in responses:
        print(response)


if __name__ == '__main__':
    simple_multimodal_conversation_call()
{"status_code": 200, "request_id": "2d7da2d3-f32b-9e87-a4c9-bfff8d9f304f", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": null, "message": {"role": "assistant", "content": "图中是一 名"}}]}, "usage": {"input_tokens": 285, "output_tokens": 3}}
{"status_code": 200, "request_id": "2d7da2d3-f32b-9e87-a4c9-bfff8d9f304f", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": null, "message": {"role": "assistant", "content": "图中是一 名女子在沙滩上和狗玩耍,"}}]}, "usage": {"input_tokens": 285, "output_tokens": 11}}
{"status_code": 200, "request_id": "2d7da2d3-f32b-9e87-a4c9-bfff8d9f304f", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": null, "message": {"role": "assistant", "content": "图中是一 名女子在沙滩上和狗玩耍,旁边的狗是一只拉布拉多犬"}}]}, "usage": {"input_tokens": 285, "output_tokens": 19}}
{"status_code": 200, "request_id": "2d7da2d3-f32b-9e87-a4c9-bfff8d9f304f", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": null, "message": {"role": "assistant", "content": "图中是一 名女子在沙滩上和狗玩耍,旁边的狗是一只拉布拉多犬,它们处于沙滩上。"}}]}, "usage": {"input_tokens": 285, "output_tokens": 25}}

参数配置

参数类型默认值说明
modelstring-指定用于对话的通义千问模型名,qwen-vl-v1或qwen-vl-chat-v1。
messageslist[dict]-用户输入的历史对话信息和指令组成的完整输入信息。
top_p (可选)float0.8生成过程中核采样方法概率阈值,例如,取值为0.8时,仅保留概率加起来大于等于0.8的最可能token的最小集合作为候选集。取值范围为(0,1.0),取值越大,生成的随机性越高;取值越低,生成的确定性越高。
stream (可选)boolFalse是否使用流式输出。当以stream模式输出结果时,接口返回结果为generator,需要通过迭代获取结果,每个输出为当前生成的整个序列,最后一次输出为最终全部生成结果。
top_kfloat100.0生成时,采样候选集的大小。例如,取值为50时,仅将单次生成中得分最高的50个token组成随机采样的候选集。取值越大,生成的随机性越高;取值越小,生成的确定性越高。注意:如果top_k的值大于100,top_k将采用默认值100
seedint1234生成时,随机数的种子,用于控制模型生成的随机性。如果使用相同的种子,每次运行生成的结果都将相同;当需要复现模型的生成结果时,可以使用相同的种子。seed参数支持无符号64位整数类型。默认值 1234

返回参数

返回参数类型说明
status_codeint200(HTTPStatus.OK)表示请求成功,否则表示请求失败,可以通过code获取错误码,通过message字段获取错误详细信息。
request_Idstring系统生成的标志本次调用的id。
codestring表示请求失败,表示错误码,成功忽略。
outputdict调用结果信息,对于通义VL模型,包含输出message。
output.choicesList模型为输入生成的完成选项列表。
output.choices[x].message.roleString信息来源。
output.choices[x].message.contentString本次请求的算法输出内容。
usagedict计量信息,表示本次请求计量数据。
usage.input_tokensint用户输入文本转换成Token后的长度。
usage.output_tokensint模型生成回复转换为Token后的长度。

参考链接:HTTP调用接口方式等

  • 22
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值