
1. Ollama Pull API 测试34-85行核心功能是使用 Ollama 的 /api/pull 接口拉取模型。主要特点- 使用 requests.post 发送 POST 请求到 http://localhost:11434/api/pull- 设置 streamTrue 以支持流式响应- 处理 NDJSON 格式的响应每行一个 JSON 对象- 响应包含状态信息status、digest、total、completed- 使用 tqdm 进度条显示下载进度关键点- 模型名称glm-4.7-flash被注释的有 qwen3-vl:4b, glm-4.6:cloud, glm-5:cloud- 响应用 b\n 分割OLLama 使用换行符分隔 NDJSON- 实时更新 tqdm 进度条这是一个简单的测试脚本不是 pytest 测试文件。这是一个 Ollama 模型拉取脚本用于从本地 Ollama 服务下载模型。主要功能- 向 http://localhost:11434/api/pull 发送 POST 请求- 设置 streamTrue 接收流式 NDJSON 响应- 实时解析下载进度并用 tqdm 显示进度条响应格式示例{status:pulling 9c60bdd691c1,digest:sha256:...,total:3295612832,completed:95273168}关键处理逻辑- 用 b\n 分割流式响应Ollama 使用换行分隔 NDJSON- 提取 status、total、completed 字段- 当获取到 total 大小时创建 tqdm 进度条- 累积更新进度条显示下载进度当前配置- 目标模型glm-4.7-flash- 当前机器上 Ollama 地址localhost:11434# 方式一使用 requests import requests import json from tqdm import tqdm #进度显示 response requests.post(fhttp://localhost:11434/api/pull, jsondata, streamTrue) resBuffer :bytes b res :bytes b ts {} for line in response.iter_content(): #{status:pulling 9c60bdd691c1,digest:sha256:9c60bdd691c1897bbfe5ddbc67336848e18c346b7ee2ab8541b135f208e5bb38,total:3295612832,completed:95273168} 88 {status:pulling 9c60bdd691c1,digest:sha256:9c60bdd691c1897bbfe5ddbc67336848e18c346b7ee2ab8541b135f208e5bb38,total:3295612832} 9a {status:pulling 9c60bdd691c1,digest:sha256:9c60bdd691c1897bbfe5ddbc67336848e18c346b7ee2ab8541b135f208e5bb38,total:3295612832,completed:78020} res line r res.split(b\n) res r.pop() for j in r: if bstatus in j: msg json.loads(j.decode()) status msg.get(status,) total msg.get(total) #提取总大小 completed msg.get(completed) #提取已经下载量 tq ts.get(status) if total and tq is None: tq tqdm(range(total)) ts[status] tq if tq and completed: tq.update(completed) #更新进度只看见端口没有主机登录权限时使用, 所以正常ollama的端口不能暴露出去(别人可以恶意下载,占网速/硬盘)