Ollama API 拉取模型

2026-10-29 19:01 更新

POST /api/pull 从模型库下载模型到本地,等价于 ollama pull。

版本基线 v0.40.12,官方 docs.ollama.com/api/pull。

一、调用

curl http://localhost:11434/api/pull -d '{
  "model": "gemma4"
}'

二、请求字段

字段 类型 必填 说明
model string 是 要下载的模型名
insecure boolean 允许通过非安全连接下载
stream boolean 默认 true,流式返回进度

三、流式进度:默认行为

拉取大模型时默认流式返回进度,每行一个 JSON:

{"status":"pulling manifest"}
{"status":"pulling c6eb396dbd59","digest":"sha256:c6eb...","total":9608350245,"completed":1048576}
{"status":"pulling c6eb396dbd59","digest":"sha256:c6eb...","total":9608350245,"completed":536870912}
{"status":"verifying sha256 digest"}
{"status":"writing manifest successfully"}
{"status":"success"}
字段 说明
status 当前阶段描述
digest 当前层的内容摘要
total 该层总字节数
completed 已下载字节数

计算进度:completed / total。注意模型是分层下载的,total 是当前层的大小而不是整个模型的总大小。

四、非流式

curl http://localhost:11434/api/pull -d '{
  "model": "gemma4",
  "stream": false
}'

会等到全部下载完才返回 {"status": "success"}。大模型不建议这样用——几 GB 的下载会长时间无响应,容易超时。

五、Python 进度监控

import json, requests

def pull(model):
    with requests.post("http://localhost:11434/api/pull",
                       json={"model": model}, stream=True) as r:
        if r.status_code != 200:
            raise RuntimeError(f"HTTP {r.status_code}: {r.text}")
        for line in r.iter_lines():
            if not line:
                continue
            c = json.loads(line)
            if "error" in c:
                raise RuntimeError(c["error"])
            if c.get("total"):
                pct = c["completed"] / c["total"] * 100
                print(f"\r{c['status']:<30} {pct:5.1f}%", end="")
            else:
                print(f"\r{c['status']:<30}", end="")
            if c.get("status") == "success":
                print("\n完成")
                break

pull("gemma4")

六、私有模型与云端

  • 公开模型:直接 pull
  • 私有模型:需要先 ollama signin 登录,且有访问权限
  • 云端模型(xxx:cloud 后缀):不需要 pull,直接用

常见问题

现象 原因 处理
404 模型名拼错或不存在 到 ollama.com/search 核对
401 私有模型未登录 ollama signin
下载卡在某层不动 网络问题 重试,或配 HTTPS_PROXY
磁盘不足 模型太大 清理旧模型或换小量化版本
非流式长时间无响应 大文件下载被阻塞 用流式

代理配置见 9.1 Ollama 常见问题。

动手试

## 拉一个小模型,观察进度流的结构
curl http://localhost:11434/api/pull -d '{"model":"qwen2.5:0.5b"}'

小模型只有几百 MB,几秒就能下完,用来理解流式输出格式很合适。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号