API调用Qwen系列模型

Qwen-VL的使用

参考教程

官方仓库:https://github.com/QwenLM/Qwen-VL/blob/master/TUTORIAL_zh.md

参考教程: https://blog.csdn.net/qq_41185868/article/details/141789608

环境安装

conda create -n QwenVL_Env python=3.10
pip install torch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0 --index-url https://download.pytorch.org/whl/cu118
git clone https://github.com/QwenLM/Qwen-VL.git
pip install -r requirements.txt
pip install openai

Qwen-VL plus使用,API调用

百炼模型开通:https://bailian.console.aliyun.com/?spm=a2c4g.11186623.0.0.23f61d1cvLyEdQ#/model-market/detail/qwen-vl-plus-latest

模型测试-效果对比

Qwen-VL

The movie poster for “Rebecca” features a man and a woman posing next to each other. The woman is holding a large book with the title “Rebecca” written on it. The man is standing behind her, looking at her. They are both looking at something off-screen to the right. The woman on the left is wearing a white dress.

《丽贝卡》的电影海报上,一男一女并排摆姿势。这个女人拿着一本大书,上面写着“Rebecca”。那个男人站在她身后,看着她。他们都在看屏幕外右边的东西。左边的女人穿着一件白色的连衣裙。

Qwen-VL-Max

The image is a vintage movie poster for the film “Rebecca.” The poster features a large, bold title “Rebecca” in red at the center. Below the title, the names of the lead actors are listed: Laurence Olivier and Joan Fontaine. The poster also mentions that the film is directed by Alfred Hitchcock and produced by David O. Selznick, who was also the producer of “Gone with the Wind.” The poster includes an illustration of a woman in a white dress standing near a mansion, which is likely Manderley, the setting of the story. The background has a greenish hue, and there are images of two people, presumably the main characters, looking into the distance. The overall design evokes a sense of mystery and drama, fitting for the film’s themes.

该图像是电影《丽贝卡》的老式电影海报。海报的中央有一个大而粗的红色标题“Rebecca”。在标题下方,列出了主要演员的名字:劳伦斯·奥利维尔 (Laurence Olivier) 和琼·方丹 (Joan Fontaine)。海报还提到,这部电影由阿尔弗雷德·希区柯克执导,大卫·塞尔兹尼克担任制片人,他也是《乱世佳人》的制片人。海报上有一幅插图,一个穿着白色连衣裙的女人站在一座豪宅附近,这可能是故事的背景曼德利。背景是绿色的,有两个人的图像,大概是主角,望着远方。整体设计唤起了一种神秘感和戏剧性,与电影的主题相得益彰。

Qwen-VL-Plus

This is the poster for the movie “Rebecca”, featuring two main characters, both played by Joan Fontaine and Laurence Olivier. The background color of the image is greenish-blue with white text that reads ‘Selznick International presents Rebecca’. There’s also an illustration in the bottom left corner showing a woman standing on what appears to be a balcony overlooking a large house.

这是电影《丽贝卡》的海报,由琼·方丹和劳伦斯·奥利维尔饰演两个主要角色。图像的背景颜色为蓝绿色,白色文字为“Selznick International presents Rebecca”。左下角还有一幅插图,显示一名女子站在阳台上,俯瞰着一座大房子。

模型调用

  1. 获取API_KEY
  2. 配置API_KEY到环境变量
export DASHSCOPE_API_KEY="--your api_key--"
  1. Qwen-VL-MAX使用代码示例
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")


base64_image = encode_image("test.png")
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": "image_url",
                    # 需要注意,传入BASE64,图像格式(即image/{format})需要与支持的图片列表中的Content Type保持一致。"f"是字符串格式化的方法。
                    # PNG图像:  f"data:image/png;base64,{base64_image}"
                    # JPEG图像: f"data:image/jpeg;base64,{base64_image}"
                    # WEBP图像: f"data:image/webp;base64,{base64_image}"
                    "image_url": {"url": f"data:image/png;base64,{base64_image}"}, 
                },
                {"type": "text", "text": "这是什么?"},
            ],
        }
    ],
)
print(completion.choices[0].message.content)
  1. 回答的信息过于丰富,需要使用大语言模型简化

The image is a vintage movie poster for the film “Rebecca.” The poster features two main characters, a man and a woman, prominently displayed in the foreground. The woman has light hair styled in waves and is wearing a green dress with a white collar. The man has dark hair and is dressed in a suit with a tie.

In the background, there is a ghostly figure of a woman in a white dress, standing near what appears to be a large, imposing mansion or estate. The mansion is situated on a hill and is surrounded by trees, adding to the eerie atmosphere.

The title “Rebecca” is written in large, bold red letters across the center of the poster. Below the title, the names of the lead actors are listed: Laurence Olivier and Joan Fontaine. The director’s name, Alfred Hitchcock, is also mentioned, along with the producer, David O. Selznick. The poster notes that the film is based on the novel by Daphne du Maurier and was released through United Artists.

The overall color scheme of the poster includes shades of green, blue, and white, contributing to a mysterious and suspenseful mood, which is fitting for the film’s genre.

  1. 调用Qwen2.5-7B-Instruct​模型简化描述

环境安装:

git clone https://github.com/QwenLM/Qwen.git
conda create -n Qwen2_Env python=3.10
conda activate Qwen2_Env
pip install torch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0 --index-url https://download.pytorch.org/whl/cu118
git clone https://github.com/QwenLM/Qwen-VL.git
pip install -r requirements.txt
pip install openai

尝试模型调用Qwen2.5-7B模型

API调用Qwen-Max :

https://bailian.console.aliyun.com/?spm=a2c4g.11186623.0.0.23f61d1cvLyEdQ&accounttraceid=9e3357420a5a4dde89df8e2e8fe4859ciiud#/model-market/detail/qwen-max

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值