文章目录
- [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



