用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
相关推荐
piglet1213815 小时前
把搜索调到 Claude.ai 的水准
前端·人工智能
Linlingu15 小时前
openClaw不能操作我的电脑提示没有权限如何解决?
人工智能·windows·办公自动化·数字员工·小龙虾
snpgroupcn15 小时前
SNP亮相2026思爱普中国峰会,助力企业加速数据价值兑现
人工智能
IT乐手15 小时前
Anthropic 为何限制中国大陆使用 Claude?
人工智能
To_OC15 小时前
用 ESM 模块化搭建 DeepSeek LLM 调用,顺带用 Prompt 实现轻量 NLP 任务
人工智能·nlp·deepseek
jrjrgood15 小时前
现货黄金和黄金期货的区别有哪些?如何投资?
大数据·人工智能·区块链
属于自己的天空15 小时前
确认弹窗太多?一次配好 Claude Code 权限,安心让 AI 干活
人工智能
dearxue15 小时前
这一次,我们一起把AI的复杂一口吃掉
人工智能·后端
行者-全栈开发15 小时前
深度解析 WWDC 2026:苹果 AI 全栈技术架构与落地实现路径
人工智能·架构·wwdc
企业老板ai培训15 小时前
2026中小企业AI应用落地白皮书:从AI短视频矩阵到数字人获客的破局增长趋势
人工智能·矩阵·音视频