vLLM代码推理Qwen2-VL多模态

由于近期代码微调以及测试都是在远程服务器上,因此LLamafactory-cli webui 以及vLLM的ui均无法使用,因此不断寻求解决方案,我提供一个解决方案,LLamafactory微调完成的模型需要合并为一个完整模型后再使用vLLM进行代码推理测试微调模型的结果。

由于chat启动的终端互动模式均无法上传图像进行交互,因此需要代码或者参数来上传图像进行理解。

Vision Language --- vLLM

这个链接里有vLLM支持的多模态大模型不同的函数对prompt的处理

我在这里提供一个使用vLLM对Qwen2-VL的多模态图像理解的python代码

python 复制代码
from vllm import LLM, SamplingParams
from PIL import Image

def run_qwen2_vl(questions: str, image_path: str):
    # 模型初始化配置
    llm = LLM(
        model="Qwen/Qwen2-VL-Lora_Sft",
        max_model_len=4096,
        max_num_seqs=5,
        dtype="half"
    )

    # 多模态数据加载
    image = Image.open(image_path)
    question = "What is the content of this image?"
    # 提示词构造
    prompt_template = [(
        "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n"
        "<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>"
        f"{question}<|im_end|>\n"
        "<|im_start|>assistant\n") for question in questions]
    print(prompt_template[0])
    sampling_params = SamplingParams(
        max_tokens=1024,
        temperature=0.8,
        top_p=0.95,
        frequency_penalty=0.2,
        presence_penalty=0.3,
        stop=["<|im_end|>"]
    )
    # 生成请求
    outputs = llm.generate({
    "prompt": prompt_template[0],
    "multi_modal_data": {"image": image},
    }, sampling_params=sampling_params)

    # 结果解析
    return [output.outputs[0].text for output in outputs]

# 使用示例
if __name__ == "__main__":
    response = run_qwen2_vl(
        questions=["请使用中文描述下这个图像并给出中文诊断结果"],
        image_path="aaaa.jpg"
    )
    print("模型输出:", response[0])
相关推荐
Csvn3 小时前
第 28 章 案例四 多智能体协作系统
人工智能·aigc·agent
IT_陈寒3 小时前
Java中equals方法比了个寂寞?原来这才是正确的重写姿势
前端·人工智能·后端
吴佳浩3 小时前
Agent 怎么做自动化评测?构建端到端的 Agent Evaluation 体系
人工智能·agent·ai编程
火山引擎开发者社区3 小时前
火山引擎云数据库 TiDB 版公测开启,MySQL 架构升级的一站式选择
人工智能
代码方舟3 小时前
Java数据工程:利用天远全网运营商三要素优化线上实名认证合规体验
java·人工智能
Csvn3 小时前
第 27 章 案例三 自动化工作流 Agent
人工智能·aigc·agent
知几蜗牛3 小时前
AI眼镜把记忆放上云,怎样证明云端也看不见?
人工智能
知几蜗牛3 小时前
训练数据越多越好吗?用LeRobot讲清数据质量与版本化
人工智能
知几蜗牛3 小时前
PR合并慢,别再只看平均时长:GitHub把等待拆成了三段
人工智能
知几蜗牛3 小时前
AI账单失控前,团队真正缺的不是更便宜的模型
人工智能