1. 本地ollama
1.1 本地安装ollama
方法 1:手动检查最新版本并下载 访问 Ollama 的 GitHub Releases 页面:
打开 https://github.com/ollama/ollama/releases
查看最新的稳定版本(如 v0.7.0 或更高) 手动下载最新版本(替换 v0.7.0 为实际版本):
wget https://github.com/ollama/ollama/releases/download/v0.7.0/ollama-linux-amd64 -O ~/.local/bin/ollama chmod +x ~/.local/bin/ollama
方法 2:使用官方安装脚本(推荐)需要root权限, Ollama 提供了自动安装脚本,无需手动下载:
需要root权限,或者sudo # curl -fsSL https://ollama.com/install.sh | sh >>> Installing ollama to /usr/local >>> Downloading Linux amd64 bundle ######################################################################## 100.0% >>> Creating ollama user... >>> Adding ollama user to render group... >>> Adding ollama user to video group... >>> Adding current user to ollama group... >>> Creating ollama systemd service... >>> Enabling and starting ollama service... Created symlink /etc/systemd/system/default.target.wants/ollama.service → /etc/systemd/system/ollama.service. >>> NVIDIA GPU installed.
该脚本会自动检测系统架构并下载最新版本 安装完成后,运行 ollama pull llama3 测试
方法 3:使用 AppImage(免安装)
如果仍然无法下载,可以直接使用 AppImage 版本:
wget https://ollama.com/download/Ollama-linux-x86_64.AppImage chmod +x Ollama-linux-x86_64.AppImage ./Ollama-linux-x86_64.AppImage
安装完成后查看ollama版本
$ ollama --version
ollama version is 0.6.5
1.2 本地模型导入ollama
$ ollama create Qwen2.5-0.5B-Instruct-f16 -f ./Modelfile2
Modelfile中的内容一定要配置正确,不然模型会胡言乱语。
$ ollama list
NAME ID SIZE MODIFIED
Qwen2.5-0.5B-Instruct-f16:latest b6c92e7256aa 1.4 GB 18 minutes ago
Qwen2.5-0.5B-Instruct-Q4_K_M.gguf:latest b6c92e7256aa 1.4 GB 16 hours ago
$ ollama run qwen2.5-0.5b-instruct-f16
>>> 你叫什么名字
我是阿里云自主研发的超大规模语言模型,我叫通义千问。>>> Send a message (/? for help)
1.3 删除ollama中的模型
$ ollama rm Qwen2.5-0.5B-Instruct-f16
deleted 'Qwen2.5-0.5B-Instruct-f16'
2. docker webui中的ollama
2.1 安装docker
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
2.2 webui docker中安装ollama
启动webui docker
docker run -d -p 3003:8080 --add-host=host.docker.internal:host-gateway -v /home/pretrained_model/output/:/app/backend/output --name open-webui-2 --restart always ghcr.io/open-webui/open-webui:main
安装ollama
# curl -fsSL https://ollama.com/install.sh | sh
docker中运行ollama 模型
但是发现webui界面上找不到模型
用带ollama的webui来启动容器
docker run -d -p 3004:8080 --gpus=all -v ollama:/root/.ollama -v /home/pretrained_model/output/:/app/backend/output --name open-webui-3 --restart always ghcr.io/open-webui/open-webui:ollama
3. docker ollama服务
3.1 启动docker ollama 服务
docker run -d --gpus=all -v ollama:/root/.ollama -p 11435:11434 --name ollama ollama/ollama
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce3281b88186 ollama/ollama "/bin/ollama serve" 4 minutes ago Up 4 minutes 0.0.0.0:11435->11434/tcp, :::11435->11434/tcp ollama
3.2 ollama的几个命令
1. 确认服务状态 首先检查容器是否正常运行:
docker ps | grep ollama
如果状态为 Up ,且端口映射正确( 0.0.0.0:11435->11434/tcp ),说明服务已就绪。
2. 通过命令行与 Ollama 交互 本地直接访问 Ollama 默认监听 11434 端口,但你的容器将 容器内的 11434 映射到了 主机的 11435,因此需要通过 11435 端口访问:
curl http://localhost:11435/api/tags
# 查看已下载的模型
$ curl -s http://localhost:11435/api/tags | python3 -m json.tool {"models": [{"name": "qwen2.5-0.5b-instruct-fp16:latest","model": "qwen2.5-0.5b-instruct-fp16:latest","modified_at": "2025-04-25T08:36:14.196107125Z","size": 994158862,"digest": "1628bedb88991403c55134f79603cd867faa7d064a5209b34c54f4446e38c43e","details": {"parent_model": "","format": "gguf","family": "qwen2","families": ["qwen2"],"parameter_size": "494.03M","quantization_level": "F16"}},
3. 通过 API 调用
Ollama 提供 REST API,可通过代码或工具(如
、Postman)调用:curl
# 生成文本 curl -X POST http://localhost:11435/api/generate -d '{"model": "llama3","prompt": "为什么天空是蓝色的?" }'# 聊天接口 curl -X POST http://localhost:11435/api/chat -d '{"model": "llama3","messages": [{ "role": "user", "content": "解释光的散射现象" }] }'
postman调用:
3.3 流式与非流式调用
流式(逐字返回结果)
$ curl -X POST http://localhost:11435/api/chat -d '{
> "model": "qwen2.5-0.5b-instruct-fp16",
> "messages": [
> { "role": "user", "content": "解释光的散射现象" }
> ]
> }'
{"model":"qwen2.5-0.5b-instruct-fp16","created_at":"2025-04-25T09:56:07.766177288Z","message":{"role":"assistant","content":"光"},"done":false}
{"model":"qwen2.5-0.5b-instruct-fp16","created_at":"2025-04-25T09:56:07.798616753Z","message":{"role":"assistant","content":"的"},"done":false}
{"model":"qwen2.5-0.5b-instruct-fp16","created_at":"2025-04-25T09:56:07.825641344Z","message":{"role":"assistant","content":"散"},"done":false}
{"model":"qwen2.5-0.5b-instruct-fp16","created_at":"2025-04-25T09:56:07.852898933Z","message":{"role":"assistant","content":"射"},"done":false}
{"model":"qwen2.5-0.5b-instruct-fp16","created_at":"2025-04-25T09:56:07.879627403Z","message":{"role":"assistant","content":"是指"},"done":false}
非流式("stream": false)
$ curl -X POST http://localhost:11435/api/chat -d '{
> "model": "qwen2.5-0.5b-instruct-fp16",
> "messages": [
> { "role": "user", "content": "解释光的散射现象" }
> ],
> "stream": false
> }'
{"model":"qwen2.5-0.5b-instruct-fp16","created_at":"2025-04-25T10:14:43.393744802Z","message":{"role":"assistant","content":"光的散射是光在传播过程中,部分光波由于波长、方向、速度等特性不同而相互偏转的现象。这一现象不仅在日常生活中观察到,而且在科学实验中也有应用。\n\n在光学中,光的散射通常发生在大气中的雨滴或尘埃颗粒中,当这些颗粒与光相互作用时,会吸收一部分光波,使其他光波得以通过。这种现象被称为“折射”,即光线在不同介质之间发生改变的方向。\n\n此外,散射还存在于水中、玻璃、塑料等透明介质中。当水中的悬浮颗粒(如小鱼、微小的植物或微粒)与光相互作用时,它们会吸收一部分光波并使其散射出去,导致光线被分散,形成所谓的“光斑”。\n\n在科学研究和工程实践中,理解光的散射现象是非常重要的。例如,在光学设计中,了解如何设计透镜以减少光的散射是提高图像质量、减少反射和减少散射污染的关键。此外,在雷达技术中,散射现象有助于在空中识别目标。\n\n综上所述,光的散射是一个普遍存在的自然现象,它不仅影响我们日常生活中的许多活动,还为科学研究提供了重要的工具。"},"done_reason":"stop","done":true,"total_duration":11849484701,"load_duration":7625965711,"prompt_eval_count":35,"prompt_eval_duration":339442232,"eval_count":259,"eval_duration":3869595024}
格式化输出:
$ curl -X POST http://localhost:11435/api/chat -d '{
> "model": "qwen2.5-0.5b-instruct-fp16",
> "messages": [
> { "role": "user", "content": "解释光的散射现象" }
> ],
> "stream": false
> }' | python3 -m json.tool% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 1643 100 1498 100 145 154 14 0:00:10 0:00:09 0:00:01 333
{"model": "qwen2.5-0.5b-instruct-fp16","created_at": "2025-04-25T10:21:43.376511973Z","message": {"role": "assistant","content": "\u5149\u7684\u6563\u5c04\u662f\u6307\u5149\u5728\u4f20\u64ad\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u4e00\u79cd\u81ea\u7136\u73b0\u8c61\uff0c\u5b83\u662f\u6307\u5149\u6ce2\u5728\u9047\u5230\u7269\u4f53\u65f6\u88ab\u5206\u6563\u6216\u5206\u5f00\u7684\u73b0\u8c61\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u7531\u4e8e\u5149\u6ce2\u7684\u6ce2\u957f\u4e0d\u540c\uff0c\u5bfc\u81f4\u4e0d\u540c\u9891\u7387\u7684\u5149\u5b50\u76f8\u4e92\u5e72\u6270\u800c\u4ea7\u751f\u7684\u3002\n\n\u6563\u5c04\u73b0\u8c61\u4e3b\u8981\u53d1\u751f\u5728\u7a7a\u6c14\u3001\u6c34\u6216\u5176\u4ed6\u900f\u660e\u4ecb\u8d28\u4e2d\uff0c\u5f53\u5149\u7a7f\u8fc7\u8fd9\u4e9b\u4ecb\u8d28\u65f6\uff0c\u7531\u4e8e\u4ecb\u8d28\u4e2d\u7684\u5206\u5b50\u548c\u539f\u5b50\u7ed3\u6784\u5dee\u5f02\uff08\u5982\u5bc6\u5ea6\u3001\u6e29\u5ea6\u3001\u7535\u573a\u7b49\uff09\uff0c\u5149\u6ce2\u88ab\u5206\u6563\u6210\u4e0d\u540c\u7684\u6ce2\u957f\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u201c\u5149\u7684\u6563\u5c04\u201d\u6216\u201c\u5149\u7684\u6298\u5c04\u201d\u3002\n\n\u6563\u5c04\u73b0\u8c61\u662f\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u73b0\u8c61\uff0c\u5c24\u5176\u662f\u5728\u5149\u7ebf\u901a\u8fc7\u4e0d\u540c\u7684\u4ecb\u8d28\u6216\u7269\u4f53\u65f6\u3002\u5b83\u5bf9\u8bb8\u591a\u6280\u672f\u5e94\u7528\uff08\u5982\u5149\u7ea4\u901a\u4fe1\u3001\u96f7\u8fbe\u548c\u6fc0\u5149\uff09\u81f3\u5173\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u5149\u7ea4\u901a\u4fe1\u4e2d\uff0c\u5229\u7528\u591a\u6a21\u5149\u7ea4\u53ef\u4ee5\u4f20\u8f93\u591a\u79cd\u4e0d\u540c\u9891\u7387\u7684\u5149\u4fe1\u53f7\uff0c\u4ee5\u63d0\u9ad8\u4fe1\u606f\u4f20\u8f93\u6548\u7387\u548c\u5b89\u5168\u6027\uff1b\u5728\u96f7\u8fbe\u7cfb\u7edf\u4e2d\uff0c\u6563\u5c04\u73b0\u8c61\u4f7f\u5f97\u96f7\u8fbe\u80fd\u591f\u63a2\u6d4b\u5230\u76ee\u6807\uff0c\u5e76\u8fdb\u884c\u7cbe\u786e\u7684\u5b9a\u4f4d\u548c\u8ddf\u8e2a\u3002\n\n\u6b64\u5916\uff0c\u6563\u5c04\u73b0\u8c61\u4e5f\u5bf9\u751f\u7269\u4f53\u6709\u5f71\u54cd\uff0c\u5982\u751f\u7269\u7ec4\u7ec7\u4e2d\u7684\u7ec6\u80de\u819c\u53ef\u80fd\u56e0\u4e3a\u5149\u7684\u6563\u5c04\u800c\u53d7\u5230\u5f71\u54cd\uff0c\u5bfc\u81f4\u4fe1\u53f7\u4f20\u9012\u7684\u969c\u788d\u3002\u56e0\u6b64\uff0c\u7406\u89e3\u5149\u7684\u6563\u5c04\u73b0\u8c61\u5bf9\u4e8e\u79d1\u5b66\u3001\u5de5\u7a0b\u548c\u6280\u672f\u7684\u53d1\u5c55\u90fd\u5177\u6709\u91cd\u8981\u610f\u4e49\u3002"},"done_reason": "stop","done": true,"total_duration": 9697206025,"load_duration": 5521248207,"prompt_eval_count": 35,"prompt_eval_duration": 314856710,"eval_count": 247,"eval_duration": 3845767388
}
3.4 windows命令行调用
调用方法1:直接调用
(py38) C:\Users\PC>curl -X POST http://192.168.x.xxx:11435/api/chat -H "Content-Type: application/json" -d "{\"model\":\"qwen2.5-0.5b-instruct-fp16\",\"messages\":[{\"role\":\"user\",\"content\":\"红楼梦的作者是谁\"}],\"stream\":false}"
{"model":"qwen2.5-0.5b-instruct-fp16","created_at":"2025-04-27T02:02:24.121938447Z","message":{"role":"assistant","content":"《红楼梦》是中国古典文学中的巅峰之作,由清代作家曹雪芹所著。曹雪芹,字梦阮,号雪芹,是清代著名小说家。《红楼梦》的 创作时间大约在1745年至1750年间,其完成于1755年。"},"done_reason":"stop","done":true,"total_duration":6566747257,"load_duration":5350087605,"prompt_eval_count":33,"prompt_eval_duration":266226914,"eval_count":66,"eval_duration":935840614}
注意1:添加Header(重要): -H "Content-Type: application/json"
注意2:使用双引号包裹 JSON,并转义内部双引号(推荐)
调用方法2:将 JSON 保存到文件再发送
创建一个文件
request.json
内容如下:
{
"model": "qwen2.5-0.5b-instruct-fp16",
"messages": [{
"role": "user",
"content": "红楼梦的作者是谁"
}],
"stream": false
}然后执行:
curl -X POST http://192.168.x.xxx:11435/api/chat -H "Content-Type: application/json" -d "@request.json"
(py38) D:\test>curl -X POST http://192.168.x.xxx:11435/api/chat -H "Content-Type: application/json" -d "@request.json"
{"model":"qwen2.5-0.5b-instruct-fp16","created_at":"2025-04-27T02:15:49.4885271Z","message":{"role":"assistant","content":"《红楼梦》是中国古典四大名著之一,由清代作家曹雪芹创作。曹雪芹,字梦阮,号雪芹,别署芹圃、芹溪,原籍阳曲县(今属河北 ),后迁居于山阴(今浙江绍兴)。他出生于一个世代书香门第的大家庭中,自幼受到良好的教育和家教熏陶,对文学、历史、哲学等多方面都有浓厚的兴趣。曹雪芹在创作《红楼梦》的过程中,历时十年之久,耗时四年完成,最终于1740年(乾隆二十七年)正式出版。"},"done_reason":"stop","done":true,"total_duration":6489315899,"load_duration":5293753071,"prompt_eval_count":33,"prompt_eval_duration":228518150,"eval_count":132,"eval_duration":952374614}
postman中的调用错误:
'{ "model": "qwen2.5-0.5b-instruct-fp16", "messages": [{ "role": "user", "content": "请回答一个中文文学问题:红楼梦的作者是谁?" }], "options": { "temperature": 0.7, "repeat_penalty": 1.1 }, "stream": false }'
{ "error": "invalid character '\'' looking for beginning of value" }
在 Postman 中遇到 "invalid character '\\'' looking for beginning of value"
错误,通常是因为 JSON 格式不正确或引号转义问题。
JSON 格式问题
错误:在 Postman 中直接复制了外层单引号(
'{}'
),导致解析失败。解决:JSON body 必须严格用
{}
包裹,不能有外层单引号。
如:
{"model": "qwen2.5-0.5b-instruct-fp16","messages": [{"role": "user","content": "请回答一个中文文学问题:红楼梦的作者是谁?"}],"options": {"temperature": 0.7,"repeat_penalty": 1.1},"stream": false
}
3.5 jupyter notebook调用
import requestsurl = '192.168.x.xxx:11435' # ollama 服务params = {"model": "qwen2.5-0.5b-instruct-fp16","messages": [{"role": "user","content": "红楼梦的作者是谁"}],"stream": False
}x = requests.post(rf'http://{url}/api/chat', json=params) print(x.status_code)
# print(x.text)
data = x.json()
print(data)
输出
200 {'model': 'qwen2.5-0.5b-instruct-fp16', 'created_at': '2025-04-27T05:56:10.132446588Z', 'message': {'role': 'assistant', 'content': '《红楼梦》是中国古典文学的巅峰之作,由清代作家曹雪芹创作。曹雪芹是清代著名小说家,字梦阮,号雪芹,别号芹溪,世称芹溪先生。他在创作过程中,经历了长期的思虑和奋斗,最终完成了一部宏大的小说巨著。《红楼梦》以贾、王、史、薛四大家族为背景,讲述了以贾宝玉、林黛玉、薛宝钗为代表的青年男女的爱情、婚姻、悲剧以及家族兴衰的故事。这部作品以其深刻的人物刻画、丰富的社会历史背景和精湛的文学技巧,被誉为中国古代小说的巅峰之作。'}, 'done_reason': 'stop', 'done': True, 'total_duration': 1072925721, 'load_duration': 41171766, 'prompt_eval_count': 33, 'prompt_eval_duration': 19348930, 'eval_count': 137, 'eval_duration': 999026759}
参考文章:
https://github.com/ollama/ollama/blob/main/docs/linux.md
Ubuntu | Docker Docs
https://github.com/open-webui/open-webui