用curl实现Ollama API流式调用

文章目录

  • [1 前提准备](#1 前提准备)
    • [1.1 安装jq](#1.1 安装jq)
    • [1.2 安装base64](#1.2 安装base64)
  • [2 流式调用基于openai api的ollama聊天模型](#2 流式调用基于openai api的ollama聊天模型)
  • [3 流式调用ollama聊天模型](#3 流式调用ollama聊天模型)
  • [4 流式调用ollama gemma3多模态模型](#4 流式调用ollama gemma3多模态模型)
  • [5 流式调用ollama qwen3-vl多模态模型](#5 流式调用ollama qwen3-vl多模态模型)
  • [6 附其它curl方法](#6 附其它curl方法)
    • [6.1 查看Ollama的模型信息](#6.1 查看Ollama的模型信息)
    • [6.2 非流式访问ollama聊天模型](#6.2 非流式访问ollama聊天模型)
    • [6.3 非流式访问ollama gemma3多模态模型](#6.3 非流式访问ollama gemma3多模态模型)
    • [6.4 非流式访问ollama qwen3-vl多模态模型](#6.4 非流式访问ollama qwen3-vl多模态模型)

作为码农,多多少少都有点"强迫症"。虽然用 Python 调用 Ollama API 是天经地义的选择,但既然 curl 可以非流式调用,没理由流式就不行----就像职场上领导常PUA的那样,"事事有回应时时有回应"----也必须让curl时时有回应,不能已读不回半天才响应!经过一番探索(主要是折腾 LLM),终于实现了用 curl 流式调用 Ollama API 的方法。

1 前提准备

1.1 安装jq

需有jq工具来处理JSON数据,或者使用以下命令进行安装

shell 复制代码
# ubuntu
sudo apt update
sudo apt install jq -y

1.2 安装base64

需要base64工具来将图片转码,或者使用以下命令进行安装

shell 复制代码
# ubuntu
sudo apt update
sudo apt install base64 -y

2 流式调用基于openai api的ollama聊天模型

shell 复制代码
curl -sN http://127.0.0.1:11434/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
        "model": "qwen3:8b",
        "messages": [{"role": "user", "content": "写一首四句唐诗"}],
        "stream": true
      }' \
| {
c_flag=0; r_flag=0                # 标记是否已打印前缀
while IFS= read -r line; do
    # 去掉 "data: " 前缀
    json=${line#data: }
    # 跳过空行或 [DONE]
    [[ $json == "" || $json == "[DONE]" ]] && continue
    # 提取 delta.content 并打印
    content=$(echo "$json" | jq -r '.choices[0].delta.content // empty')
    reasoning=$(echo "$json" | jq -r '.choices[0].delta.reasoning // empty')
    
    if [[ -n $reasoning ]]; then
	     ((r_flag)) || { printf "\n【reasoning】\n\t"; r_flag=1; }
	      printf "%s" "$reasoning"
    fi
    
    if [[ -n $content ]]; then
	     ((c_flag)) || { printf "\n\n【content】\n\t"; c_flag=1; }
	      printf "%s" "$content"
    fi
done
echo
echo
}

3 流式调用ollama聊天模型

shell 复制代码
curl -sN http://127.0.0.1:11434/api/generate \
  -d '{
    "model": "qwen3:8b",
    "prompt": "写一首四句唐诗",
    "stream": true
  }' \
| {
  t_flag=0; r_flag=0                # 标记是否已打印前缀
  while read -r l; do
    t=$(echo "$l" | jq -r '.thinking//empty')
    r=$(echo "$l" | jq -r '.response//empty')

    if [[ -n $t ]]; then
      ((t_flag)) || { printf "\n【thinking】\n\t"; t_flag=1; }
      printf "%s" "$t"
    fi

    if [[ -n $r ]]; then
      ((r_flag)) || { printf "\n\n【response】\n\t"; r_flag=1; }
      printf "%s" "$r"
    fi
  done
  echo
  echo
}

4 流式调用ollama gemma3多模态模型

shell 复制代码
ip="127.0.0.1:11434"
model="gemma3:4b"
# 将 test.png 替换为你要喂给模型的图片
cat > /tmp/request.json <<EOF
{
  "model": "${model}",
  "prompt": "请描述这张图片",
  "images": ["$(base64 -i test.png)"],
  "stream": true
}
EOF

# 从文件发送请求
curl -sX POST http://${ip}/api/generate \
  -H "Content-Type: application/json" \
  -d @/tmp/request.json | {
  r_flag=0                # 标记是否已打印前缀
  while read -r l; do

    r=$(echo "$l" | jq -r '.response//empty')

    if [[ -n $r ]]; then
      ((r_flag)) || { printf "\n\n【response】\n\t"; r_flag=1; }
      printf "%s" "$r"
    fi
  done
  echo
  echo
}

# 清理
rm /tmp/request.json

5 流式调用ollama qwen3-vl多模态模型

shell 复制代码
ip="127.0.0.1:11434"
model="qwen3-vl:8b"
# 将 test.png 替换为你要喂给模型的图片
cat > /tmp/request.json <<EOF
{
  "model": "${model}",
  "prompt": "请描述这张图片",
  "images": ["$(base64 -i test.png)"],
  "stream": true
}
EOF

# 从文件发送请求
curl -sX POST http://${ip}/api/generate \
  -H "Content-Type: application/json" \
  -d @/tmp/request.json | {
  t_flag=0; r_flag=0                # 标记是否已打印前缀
  while read -r l; do
    t=$(echo "$l" | jq -r '.thinking//empty')
    r=$(echo "$l" | jq -r '.response//empty')

    if [[ -n $t ]]; then
      ((t_flag)) || { printf "\n【thinking】\n\t"; t_flag=1; }
      printf "%s" "$t"
    fi

    if [[ -n $r ]]; then
      ((r_flag)) || { printf "\n\n【response】\n\t"; r_flag=1; }
      printf "%s" "$r"
    fi
    
  done
  echo
  echo
}

# 清理
rm /tmp/request.json

6 附其它curl方法

6.1 查看Ollama的模型信息

shell 复制代码
curl -s http://localhost:11434/v1/models | jq
# 或
curl -s http://127.0.0.1:11434/api/tags | jq

6.2 非流式访问ollama聊天模型

shell 复制代码
curl http://127.0.0.1:11434/api/chat \
  -d '{
        "model": "qwen3:8b",
        "messages": [
          {"role": "system", "content": "你是一位科普助手"},
          {"role": "user",   "content": "用一句话解释量子纠缠"}
        ],
        "stream": false
      }'
shell 复制代码
curl http://127.0.0.1:11434/api/generate \
  -d '{
        "model": "qwen3:8b",
        "prompt": "写一首四句唐诗",
        "stream": false
      }'

6.3 非流式访问ollama gemma3多模态模型

shell 复制代码
# 将 test.png 替换为你要喂给模型的图片
curl -sX POST http://127.0.0.1:11434/api/generate \
-H "Content-Type: application/json" \
-d @- <<EOF
{
  "model": "gemma3:4b",
  "prompt": "请描述这张图片",
  "images": ["$(base64 -i test.png)"],
  "stream": false
}
EOF

6.4 非流式访问ollama qwen3-vl多模态模型

shell 复制代码
# 将 test.png 替换为你要喂给模型的图片
curl -X POST http://127.0.0.1:11434/api/generate -H "Content-Type: application/json" -d @- <<EOF
{
  "model": "qwen3-vl:8b",
  "prompt": "请描述这张图片",
  "images": ["$(base64 -i test.png)"],
  "stream": false
}
EOF
相关推荐
Wang's Blog14 小时前
AI Agent白手起家60: 多智能体架构解析与 LangGraph 入门
人工智能·架构·wpf
十三画者14 小时前
【文献分享】SIMBA:单细胞嵌入与特征共学习
人工智能·信息可视化·数据挖掘·数据分析·数据可视化
张小殊.14 小时前
LoongForge TAOT 训练方案,解决MoE EP不均衡问题
人工智能·python·深度学习·机器学习·ai
eBest数字化转型方案14 小时前
从工程视角拆解冰柜资产管理:拍照识别 pipeline、纯净度算法与 IoT 选型踩坑
人工智能·物联网·算法
月诸清酒14 小时前
我同时在用的 AI Coding Agent 和模型评测,cc/gpt/antigravity(2026.08)
人工智能·gpt·chatgpt
东方小月14 小时前
从零开发一个 Coding Agent(七):实现纯文本 Agent Loop
前端·人工智能
玫幽倩14 小时前
2026黄河流域公安院校-电子物证单项赛(程序逆向分析+服务器取证)
运维·服务器·python·电子取证·逆向·程序分析·服务器取证
熊猫钓鱼>_>14 小时前
AI 3D 虚拟盲盒工坊:用腾讯云混元3D + TTS Skills 打造会说话的三维收藏品
人工智能·大模型·llm·agent·tts·混元3d·多skill协同
恒拓高科WorkPlus14 小时前
从“可选项”到“必选项”——私有化部署即时通讯如何重塑企业协作新底座
大数据·网络·人工智能
北斗落凡尘14 小时前
LangGraph 入门实战(4)
python·langchain