🐫 Ollama 基础使用指南(Mac / Linux / Windows)
Ollama 是一个在本地运行大语言模型(LLM)的工具,支持 macOS、Linux 和 Windows。
它让普通用户也能在笔记本上轻松运行 Llama、Qwen、Gemma、Phi 等开源模型。
📦 1. 安装 Ollama
macOS(推荐)
brew install ollama
或访问官网下载:ollama.com
启动服务(macOS 自动后台运行)
bash
ollama serve # 通常不需要手动运行,安装后自动启动
▶️ 2. 基本命令
| 命令 | 说明 |
|---|---|
ollama list |
查看已安装的模型 |
ollama pull <model> |
下载模型(如 qwen3:8b) |
ollama run <model> |
运行模型并进入交互模式 |
ollama rm <model> |
删除模型 |
ollama show <model> |
查看模型信息(如参数、模板) |
🌐 3. 常用模型示例
bash
# 中文强模型(通义千问)
ollama pull qwen3:8b
# 轻量中文模型
ollama pull qwen:4b
# Meta 官方模型
ollama pull llama3.2:1b # 超轻量
ollama pull llama3.2:3b
ollama pull llama3.1:8b # 推荐平衡款
ollama pull llama3.1:70b # 高性能(需大内存)
# Google 轻量模型
ollama pull gemma2:2b
ollama pull gemma2:9b
# 微软小模型
ollama pull phi3:mini
ollama pull phi3:medium
💡 所有模型列表:ollama.com/library
💬 4. 交互式对话
shell
ollama run qwen3:8b
>>> 你好!
>>> 请用 Python 写一个快排。
>>> /bye # 退出
支持多轮对话,上下文自动保留。
🤖 5. 非交互式调用(脚本/自动化)
方式一:管道输入
arduino
echo "1+1等于几?" | ollama run qwen3:8b
方式二:API 调用(本地 HTTP)
Ollama 默认启动 http://localhost:11434 API。
vbnet
curl http://localhost:11434/api/generate -d '{
"model": "qwen3:8b",
"prompt": "解释量子计算",
"stream": false
}'
返回 JSON 格式结果。
🧩 6. 创建自定义模型(Modelfile)
你可以基于现有模型微调提示词、参数等。
示例:Modelfile
python
FROM qwen3:8b
# 设置系统提示
SYSTEM """
你是一个专业、简洁、准确的 AI 助手,擅长技术解答。
回答时避免冗长,优先使用代码或列表。
"""
# 默认参数
PARAMETER temperature 0.5
PARAMETER num_ctx 4096
构建并运行
perl
ollama create my-qwen -f Modelfile
ollama run my-qwen
🗑 7. 清理与管理
bash
# 删除某个模型
ollama rm qwen:7b
# 删除所有未使用的模型(谨慎)
ollama prune
# 查看模型存储位置(macOS)
ls ~/.ollama/models/
模型默认存储在
~/.ollama/models/,占用较大空间(8B 模型约 5GB)。
⚙️ 8. 高级技巧
✅ 在 M 系列 Mac 上加速
Ollama 自动使用 Apple Metal GPU 加速,无需额外配置。
✅ 限制显存/内存使用
通过 num_ctx 和量化版本控制资源:
arduino
ollama pull qwen3:8b-q4_K_M # 4-bit 量化,更省内存
✅ 与 Dify / LM Studio 集成
- Dify :在模型设置中选择 "Ollama",填入
http://localhost:11434和模型名。 - LM Studio:可直接导入 Ollama 模型(部分兼容)。
❓ 常见问题
Q:pull 太慢怎么办?
A:使用代理或国内镜像(如魔搭 ModelScope),但 Ollama 官方暂不支持直接换源。
Q:模型无法启动,报错 out of memory?
A:尝试更小的模型(如 qwen:4b 或 phi3:mini),或使用 4-bit 量化版。
Q:如何查看模型支持的最大上下文?
A:
sql
ollama show qwen3:8b --modelfile
查找 num_ctx 参数。
🔗 参考链接
- 官网:ollama.com
- GitHub:github.com/ollama/olla...
- 模型库:ollama.com/library
- API 文档:github.com/ollama/olla...