用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
相关推荐
m0_6138562918 分钟前
mysql如何利用事务隔离级别解决特定业务冲突_mysql隔离方案选型
jvm·数据库·python
AI_小站19 分钟前
6个GitHub爆火的免费大模型教程,助你快速进阶AI编程
人工智能·langchain·github·知识图谱·agent·llama·rag
xindoo21 分钟前
GitHub Trending霸榜!深度解析AI Coding辅助神器 Superpowers
人工智能·github
时间之里26 分钟前
【深度学习】:RF-DETR与yolo对比
人工智能·深度学习·yolo
北京阿法龙科技有限公司30 分钟前
数智化升级:AR 智能眼镜驱动工业运维效能革新
人工智能
风落无尘34 分钟前
《智能重生:从垃圾堆到AI工程师》——第二章 概率与生存
大数据·人工智能
j_xxx404_38 分钟前
Linux:静态链接与动态链接深度解析
linux·运维·服务器·c++·人工智能
收获不止数据库41 分钟前
达梦9发布会归来:AI 时代,我们需要一款什么样的数据库?
数据库·人工智能·ai·语言模型·数据分析
hhb_6181 小时前
AI全栈编程生存指南
人工智能
AI-Frontiers1 小时前
transformer进阶之路:#2 工作原理详解
人工智能·深度学习·transformer