ollama部署qwen2.5vl

安装完ollama后,执行:

ollama run qwen2.5vl:7b

后下载qwen2.5vl模型。

安装后可以提供本地服务,配置外部服务需要做如下修改:

1. cd到/etc/systemd/system/目录下,修改ollama.service文件。

2. 增加下面两行配置:

[Service]
Environment="OLLAMA_HOST=0.0.0.0"
Environment="OLLAMA_ORIGINS=*"

3. 放开11434端口后重启电脑:

sudo ufw allow 11434

4. 执行curl看看是否可通:

curl http://127.0.0.1:11434/api/tags

5. 语言模型可以通过curl直接调用,下面是windows的cmd命令,如果是linux,可以把\"去掉:

curl http://127.0.0.1:11434/api/generate -d "{\"model\": \"qwen2.5vl:7b\", \"prompt\": \"你好\"}"

6.如果要调用vl模型,需要对图像做base64编码,下面python代码可以实现该功能:

import requests
import json
import base64   

def vlm():
    with open("1.jpeg","rb") as f: 
    # b64encode是编码,b64decode是解码 
        image_bytes = f.read()
    image_base64 = base64.b64encode(image_bytes).decode("utf-8")  # 转为字符串

    url = "http://127.0.0.1:11434/api/generate"
    data = {
        "model": "qwen2.5vl:7b",
        "prompt": 'describe the image in detail,say chinese',
        "stream": True,  # 关键:明确要求服务端流式输出(如果API支持)
        "images": [image_base64]
    }

    response = requests.post(url, json=data, stream=True)

    for line in response.iter_lines():
        if line:
            # 解析 JSON 数据
            json_data = json.loads(line.decode('utf-8'))
                    
            # 提取 response 字段内容
            current_response = json_data.get("response", "")
                    
            # 实时输出新增内容(非覆盖模式)
            if current_response:
                print(current_response, end='', flush=True)  # 逐词输出
                        

vlm()
posted @ 2025-05-30 20:50  Dsp Tian  阅读(2029)  评论(0)    收藏  举报