目录

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/versionOllama 版本
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/responsesResponses 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日