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])
相关推荐
jooloo4 小时前
Codex 间歇性 400 之谜:一条对话里,它为什么有时候用 chat/completions,有时候切到 responses?
人工智能
用户5191495848454 小时前
OpenSSL PKCS#12 PBMAC1 堆栈缓冲区溢出漏洞 (CVE-2025-11187) 分析与验证
人工智能·aigc
用户5191495848455 小时前
HP Sound Research SECOMNService 权限提升漏洞利用工具
人工智能·aigc
用户018349301695 小时前
给 AI 智能体能力包一层 BFF,前端只调一个接口
人工智能
黄忠8 小时前
大模型之LangGraph技术体系
python·llm
不好听6138 小时前
Tool:让大模型长出手脚
llm·agent
这token有力气9 小时前
Function Calling 格式漂移
人工智能
onething3659 小时前
Spring Boot + Spring AI 从入门到实战:7天转型计划 Day 5 —— SSE 流式输出 + 打字机效果
人工智能·后端·全栈
onething3659 小时前
Spring Boot + Spring AI 从入门到实战:7天转型计划 Day 6 —— 业务完善 + 会话消息预览
人工智能·后端·全栈
IT_陈寒10 小时前
SpringBoot自动配置的坑,我爬了三天才出来
前端·人工智能·后端