目錄
Ollama API 教學:用 curl 呼叫 /api/chat 與 /api/generate
Ollama 在背景提供 HTTP API,預設位址是 http://localhost:11434。CLI、Open WebUI 和各種程式庫都是透過這個 API 和模型溝通。
/api/chat:對話
curl http://localhost:11434/api/chat -d '{
"model": "qwen3:8b",
"messages": [
{ "role": "system", "content": "請用繁體中文回答。" },
{ "role": "user", "content": "為什麼天空是藍色的?用兩句話說明。" }
],
"stream": false
}'
回應的格式如下。這是對 qwen3:0.6b 送出一句 hi 的真實回應(Ollama 0.40):
{
"model": "qwen3:0.6b",
"created_at": "2026-10-12T14:04:31.457272357Z",
"message": {
"role": "assistant",
"content": "Hello! How can I assist you today? 😊 If you have any questions or need help, feel free to ask!",
"thinking": "Okay, the user just said \"hi\" and I need to respond. ..."
},
"done": true,
"done_reason": "stop",
"total_duration": 10507470186,
"load_duration": 26072287,
"prompt_eval_count": 11,
"prompt_eval_duration": 274566000,
"eval_count": 112,
"eval_duration": 10179865000
}
message.content:模型的回答。message.thinking:推理模型的思考過程。Qwen3 預設會思考,不需要時在請求中加上"think": false。done_reason:stop代表正常結束;length代表達到長度上限被截斷。- 時間欄位的單位都是奈秒(10507470186 ≈ 10.5 秒)。
eval_count ÷ eval_duration就是每秒產生的 token 數。
多輪對話
API 本身不會記住之前的對話。要延續對話,必須把歷史訊息全部帶上:
{
"model": "qwen3:8b",
"messages": [
{ "role": "user", "content": "我叫小明" },
{ "role": "assistant", "content": "你好,小明!" },
{ "role": "user", "content": "我叫什麼名字?" }
]
}
role 可以是 system、user、assistant 或 tool(工具結果,見 工具呼叫)。
/api/generate:單次文字生成
不需要對話結構時,可以用 /api/generate:
curl http://localhost:11434/api/generate -d '{
"model": "qwen3:8b",
"prompt": "把這句話翻譯成英文:今天天氣很好",
"stream": false
}'
回答在 response 欄位中。一般建議優先使用 /api/chat。
串流回應
stream 預設是 true:模型每產生一小段文字就回傳一行 JSON,最後一行的 done 為 true。
curl http://localhost:11434/api/chat -d '{
"model": "qwen3:8b",
"messages": [{ "role": "user", "content": "從 1 數到 5" }]
}'
{"message":{"role":"assistant","content":"1"},"done":false}
{"message":{"role":"assistant","content":"、2"},"done":false}
...
{"message":{"role":"assistant","content":""},"done":true,"eval_count":12}
串流能讓使用者馬上看到回答,聊天介面幾乎都會使用。
options:調整參數
{
"model": "qwen3:8b",
"messages": [{ "role": "user", "content": "寫一句標語" }],
"options": {
"temperature": 0.9,
"num_ctx": 8192,
"num_predict": 100,
"seed": 42
},
"stream": false
}
可用的參數與 Modelfile 的 PARAMETER 相同,見 Modelfile 教學。
keep_alive:控制模型常駐
{ "model": "qwen3:8b", "keep_alive": "1h" }
- 只送
model不送訊息,可以預先載入模型。 "keep_alive": 0會在回答後立即卸載。"keep_alive": -1永不卸載。
圖片輸入
視覺模型可以在訊息中加入 base64 編碼的圖片:
IMG=$(base64 -w0 photo.jpg) # macOS 用 base64 -i photo.jpg
curl http://localhost:11434/api/chat -d "{
\"model\": \"gemma3\",
\"messages\": [{
\"role\": \"user\",
\"content\": \"描述這張圖片\",
\"images\": [\"$IMG\"]
}],
\"stream\": false
}"
思考模式
支援思考的模型可以用 think 控制:
{
"model": "qwen3:8b",
"messages": [{ "role": "user", "content": "strawberry 有幾個 r?" }],
"think": true,
"stream": false
}
思考內容在 message.thinking,最終答案在 message.content。think 也可以是 false,部分模型支援 "low"、"medium"、"high" 等層級。
其他常用端點
| 方法與路徑 | 作用 |
|---|---|
GET /api/tags | 列出已下載的模型(等同 ollama list) |
GET /api/ps | 列出已載入的模型 |
POST /api/show | 模型資訊 |
POST /api/pull | 下載模型 |
DELETE /api/delete | 刪除模型 |
POST /api/embed | 產生文字嵌入,見 文字嵌入與 RAG |
GET /api/version | Ollama 版本 |
curl http://localhost:11434/api/tags
curl http://localhost:11434/api/show -d '{"model": "qwen3:8b"}'
OpenAI 相容 API
Ollama 也提供和 OpenAI API 相容的端點,位於 /v1:
| 端點 | 說明 |
|---|---|
/v1/chat/completions | 對話 |
/v1/completions | 文字補全 |
/v1/embeddings | 嵌入 |
/v1/models | 模型列表 |
/v1/responses | Responses API |
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3:8b",
"messages": [{ "role": "user", "content": "你好" }]
}'
許多原本為 OpenAI 撰寫的程式和工具,只要把 base URL 改成 http://localhost:11434/v1、API key 填任意值,就能改用本機模型。Python 範例見下一節。
更新於 2026年10月12日