用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
相关推荐
嚴 帅2 小时前
Pytnon入门学习(一)
python
小兵张健3 小时前
Java + Spring 到 Python + FastAPI (二)
java·python·spring
N 年 后3 小时前
dify的是什么?怎么使用?
人工智能
腾讯云开发者3 小时前
架构火花|产品经理和程序员谁会先被AI淘汰?
人工智能
腾讯云开发者3 小时前
告别 271 万小时重复劳动:银行数字员工如何再造效率奇迹?
人工智能
xiaoxue..3 小时前
Vibe Coding之道:从Hulk扩展程序看Prompt工程的艺术
前端·人工智能·prompt·aigc
大模型真好玩3 小时前
LangChain1.0实战之多模态RAG系统(一)——多模态RAG系统核心架构及智能问答功能开发
人工智能·langchain·agent
聚梦小课堂3 小时前
2025.11.16 AI快讯
人工智能·安全·语言模型
vvoennvv3 小时前
【Python TensorFlow】BiTCN-BiLSTM双向时间序列卷积双向长短期记忆神经网络时序预测算法(附代码)
python·rnn·tensorflow·lstm·tcn