Ollama API 使用指南
📋 服务信息 配置项 值 服务地址 http://192.168.6.9:11434 API 基础路径 http://192.168.6.9:11434/v1 已安装模型 qwen2.5:0.5b (397 MB) API Key ollama(或任意字符串,Ollama 不验证) 🔌 OpenAI 兼容接口 Ollama 提供与 OpenAI API 完全兼容的接口,可直接使用支持 OpenAI 的客户端和工具。 可用端点 端点 说明 GET /v1/models 列出可用模型 POST /v1/chat/completions 聊天完成(推荐) POST /v1/completions 文本补全 POST /v1/embeddings 嵌入向量 🚀 API 调用示例 1. curl 命令行 列出模型 curl http://192.168.6.9:11434/v1/models 聊天请求 curl http://192.168.6.9:11434/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "qwen2.5:0.5b", "messages": [ {"role": "user", "content": "你好,请介绍一下自己"} ], "temperature": 0.7, "max_tokens": 512 }' 流式响应 curl http://192.168.6.9:11434/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "qwen2.5:0.5b", "messages": [{"role": "user", "content": "你好"}], "stream": true }' 2. Python (OpenAI SDK) from openai import OpenAI # 创建客户端 client = OpenAI( base_url="http://192.168.6.9:11434/v1", api_key="ollama" # 任意值即可 ) # 聊天请求 response = client.chat.completions.create( model="qwen2.5:0.5b", messages=[ {"role": "system", "content": "你是一个有帮助的助手"}, {"role": "user", "content": "你好,请介绍一下自己"} ], temperature=0.7, max_tokens=512 ) print(response.choices[0].message.content) 流式响应 from openai import OpenAI client = OpenAI( base_url="http://192.168.6.9:11434/v1", api_key="ollama" ) response = client.chat.completions.create( model="qwen2.5:0.5b", messages=[{"role": "user", "content": "你好"}], stream=True ) for chunk in response: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True) 安装依赖 pip install openai 3. JavaScript/TypeScript (Node.js) import OpenAI from "openai"; const client = new OpenAI({ baseURL: "http://192.168.6.9:11434/v1", apiKey: "ollama" }); const response = await client.chat.completions.create({ model: "qwen2.5:0.5b", messages: [{ role: "user", content: "你好" }], temperature: 0.7, max_tokens: 512 }); console.log(response.choices[0].message.content); 安装依赖 npm install openai 4. HTTP 请求参数说明 请求体参数 参数 类型 说明 默认值 model string 模型名称 必填 messages array 消息列表 必填 temperature float 温度 (0-2) 0.7 max_tokens int 最大输出 token 数 无限制 stream boolean 是否流式输出 false top_p float 核采样阈值 0.9 frequency_penalty float 频率惩罚 0 presence_penalty float 存在惩罚 0 消息格式 { "role": "user" | "assistant" | "system", "content": "消息内容" } 📱 常用客户端配置 ChatGPT-Next-Web / LobeChat / Cherry Studio 配置项 值 API 接口地址 http://192.168.6.9:11434/v1 API Key ollama 模型名称 qwen2.5:0.5b Open WebUI docker run -d -p 3000:8080 \ -e OLLAMA_BASE_URL=http://192.168.6.9:11434 \ --add-host=host.docker.internal:host-gateway \ --name open-webui \ --restart always \ openwebui/open-webui:main 🔧 服务管理命令 # 查看服务状态 sudo systemctl status ollama # 重启服务 sudo systemctl restart ollama # 查看日志 sudo journalctl -u ollama -f # 停止服务 sudo systemctl stop ollama # 启动服务 sudo systemctl start ollama 📦 Ollama 命令行工具 # 列出已安装模型 ollama list # 运行模型(交互式) ollama run qwen2.5:0.5b # 拉取新模型 ollama pull qwen2.5:1.5b # 删除模型 ollama rm qwen2.5:0.5b # 查看模型详情 ollama show qwen2.5:0.5b ⚙️ 配置文件位置 文件/目录 说明 /etc/systemd/system/ollama.service.d/override.conf Ollama 服务配置 /usr/share/ollama/.ollama/models/ 模型存储目录 当前服务环境变量 OLLAMA_HOST=0.0.0.0:11434 HTTP_PROXY=http://127.0.0.1:20171 HTTPS_PROXY=http://127.0.0.1:20171 NO_PROXY=127.0.0.1,localhost,::1,... 🔐 安全建议 局域网访问:当前配置允许局域网内所有设备访问 防火墙设置:如需限制访问,可配置防火墙规则 sudo ufw allow from 192.168.6.0/24 to any port 11434 生产环境:建议使用反向代理(如 Nginx)添加认证和 HTTPS 📊 性能说明 由于运行在 ARM64 Cortex-A53 设备上: ...