OpenClaw + Ollama 本地联动部署:私有化 AI 智能体搭建完全指南
想要完全私有化、不上网也能运行专属 AI 智能体?OpenClaw 搭配 Ollama 本地大模型是绝配。
🚀 为什么选择 OpenClaw + Ollama?
在 AI Agent 成为开发主流的今天,隐私安全和完全离线运行变得越来越重要。
核心优势
|
|
|
|
|
|---|---|---|---|
| 隐私保护 |
|
|
|
| 离线可用 |
|
|
|
| 成本控制 |
|
|
|
| 性能灵活 |
|
|
|
| API 支持 |
|
|
|
适用场景
-
• 🔒 数据隐私敏感的开发环境 -
• 📶 网络受限的办公场景 -
• 💰 预算有限的项目起步 -
• 🛡️ 企业内部 AI 服务部署 -
• 🎓 AI 技术学习和研究
📦 环境准备
前置条件
-
1. 操作系统支持 -
• macOS 11+ (Apple Silicon 或 Intel) -
• Linux (Ubuntu 20.04+/Debian 11+) -
• Windows 10/11 + WSL2 -
2.基础工具 -
• Docker Desktop (可选,推荐) -
• curl 或 wget -
• git -
3.硬件要求 -
• 最低配置: 8GB RAM, 4GB 磁盘空间 -
• 推荐配置: 16GB+ RAM, 20GB+ 磁盘空间 -
• GPU 加速: 支持 CUDA 的 NVIDIA 显卡
🐧 macOS 部署
方法一:使用 Homebrew (最简单)
# 1. 安装 OpenClawbrew tap openclaw/openclawbrew install openclaw# 2. 安装 Ollamabrew install ollama# 3. 启动 Ollama 服务ollama serve
方法二:使用 Docker (推荐)
# 1. 启动 OpenClaw 容器docker run -d \ --name openclaw \ -p 3000:3000 \ openclaw/openclaw:latest# 2. 启动 Ollama 容器docker run -d \ --name ollama \ -p 11434:11434 \ -v ollama_data:/root/.ollama \ ollama/ollama
🖥️ Windows 部署
方法一:使用 WSL2 + Docker
# 1. 安装 WSL2wsl --install# 2. 在 WSL 中安装 Dockercurl -fsSL https://get.docker.com -o get-docker.shsudo sh get-docker.sh# 3. 启动 OpenClaw 容器docker run -d \ --name openclaw \ -p 3000:3000 \ openclaw/openclaw:latest# 4. 启动 Ollama 容器docker run -d \ --name ollama \ -p 11434:11434 \ -v ollama_data:/root/.ollama \ ollama/ollama
方法二:原生安装 (需要管理员权限)
# 1. 下载并安装 OpenClaw# 从 https://openclaw.ai 下载安装包# 2. 下载并安装 Ollama# 从 https://ollama.com 下载安装包# 3. 启动服务# 打开终端运行:ollama serve
🧩 模型接入
安装本地模型
Ollama 提供了多种开箱即用的模型,从轻量级到强大的都有:
# 1. 拉取常用模型# - qwen2.5:7b (通义千问,中文优秀)ollama pull qwen2.5:7b# - llama3.2:3b (Meta 最小模型)ollama pull llama3.2:3b# - deepseek-r1:1.5b (深度求索,推理能力)ollama pull deepseek-r1:1.5b# - mistral:7b (Mistral,综合能力强)ollama pull mistral:7b# 2. 验证模型安装ollama list
测试模型推理
# 交互式测试ollama run qwen2.5:7b# 命令行快速测试echo "你好,请介绍一下自己" | ollama run qwen2.5:7b
使用 API 接口
# 简单的 API 调用示例curl http://localhost:11434/api/generate \ -H "Content-Type: application/json" \ -d '{ "model": "qwen2.5:7b", "prompt": "你好,请介绍一下自己", "stream": false }'
🔗 跨服务连通
OpenClaw 连接 Ollama
配置 OpenClaw 使用 Ollama 模型
编辑 OpenClaw 配置文件 (通常是 ~/.openclaw/config.json):
{ "llm": { "provider": "ollama", "base_url": "http://localhost:11434", "model": "qwen2.5:7b", "temperature": 0.7, "max_tokens": 2048 }, "agent": { "enabled":true, "max_iterations": 10 }}
使用环境变量配置
# 设置环境变量export OPENCLAW_LLM_PROVIDER="ollama"export OPENCLAW_LLM_BASE_URL="http://localhost:11434"export OPENCLAW_LLM_MODEL="qwen2.5:7b"export OPENCLAW_LLM_TEMPERATURE="0.7"export OPENCLAW_LLM_MAX_TOKENS="2048"
验证连通性
# 1. 测试 Ollama APIcurl http://localhost:11434/api/tags# 2. 测试 OpenClaw# 在 OpenClaw 中输入:hello
🎯 接口调试实战
场景一:本地 AI 编程助手
# 创建 OpenClaw Agentopenclaw agent create --name "coder" \ --prompt "你是一个专业的编程助手,擅长代码审查、bug 修复和最佳实践建议。"# 启动对话openclaw chat --agent coder
场景二:本地文档分析
# 创建文档分析 Agentopenclaw agent create --name "doc-analyzer" \ --prompt "你是一个文档分析专家,擅长总结、翻译和技术文档撰写。"# 分析本地文档openclaw analyze /path/to/document.md --agent doc-analyzer
场景三:自定义 API 集成
# OpenClaw 支持自定义 API 接口curl -X POST http://localhost:3000/api/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "ollama:qwen2.5:7b", "messages": [ {"role": "system", "content": "你是一个有用的 AI 助手。"}, {"role": "user", "content": "解释一下量子计算的基本原理。"} ] }'
⚙️ 进阶配置
GPU 加速配置
NVIDIA GPU (CUDA):
# 安装 CUDA 驱动 (如果还没有)# 从 NVIDIA 官网下载对应显卡的 CUDA Toolkit# 重启 Ollama 以使用 GPUdocker restart ollama
Apple Silicon (Metal):
Ollama 会自动检测并使用 Mac 的 GPU 加速,无需额外配置。
模型量化与优化
# 拉取量化版本模型 (更小、更快)ollama pull qwen2.5:7b-q4_0 # 4-bit 量化ollama pull qwen2.5:7b-q8_0 # 8-bit 量化# 设置最大上下文长度export OLLAMA_MAX_CONTEXT_LENGTH=8192
性能监控
# 查看 Ollama 资源使用ollama ps# 查看系统日志docker logs ollama# 使用 htop 监控资源 (终端中)htop
🔧 常见问题解决
问题 1: 端口被占用
症状: 启动时报错 “Address already in use”
解决方案:
# macOS/Linux: 查找并杀掉占用端口的进程lsof -ti:3000 | xargs kill -9 # OpenClaw 端口lsof -ti:11434 | xargs kill -9 # Ollama 端口# Windows: 使用 netstat 查找进程netstat -ano | findstr :3000taskkill /PID <进程ID> /F
问题 2: 模型下载失败
症状: ollama pull 命令卡住或失败
解决方案:
# 方法 1: 使用代理export HTTP_PROXY=http://127.0.0.1:7890export HTTPS_PROXY=http://127.0.0.1:7890ollama pull qwen2.5:7b# 方法 2: 手动下载模型文件# 从 https://ollama.com/library 下载模型# 放置到 ~/.ollama/models/
问题 3: 内存不足
症状: 推理时频繁报错 “Out of memory”
解决方案:
# 方法 1: 使用更小的模型ollama pull qwen2.5:3b# 方法 2: 减少上下文长度export OLLAMA_MAX_CONTEXT_LENGTH=4096# 方法 3: 增加系统内存或使用 Swap# macOS: 系统设置 -> 通用 -> 存储 -> 管理# Linux: sudo fallocate -l 4G /swapfile && sudo mkswap /swapfile
问题 4: Docker 容器无法连接
症状: OpenClaw 无法连接到 Ollama 容器
解决方案:
# 1. 确认容器正在运行docker ps | grep ollama# 2. 确认端口映射正确docker port ollama# 3. 检查容器日志docker logs ollama# 4. 测试容器内网络docker exec -it ollama curl http://localhost:11434/api/tags
问题 5: API 返回 401 未授权
症状: 调用 API 时报错 “Unauthorized”
解决方案:
# 检查 Ollama 是否正常运行ollama ps# 重启 Ollama 服务docker restart ollama# 检查配置文件中的认证设置# 编辑 ~/.ollama/.env# OLLAMA_ORIGINS="*"
✅ 验证步骤
完成部署后,按照以下步骤验证:
1. 检查服务状态
# macOSps aux | grep openclawps aux | grep ollama# Dockerdocker ps
2. 测试模型能力
ollama run qwen2.5:7b# 输入:写一个 Python 函数,计算斐波那契数列
3. 验证 OpenClaw 集成
# 在 OpenClaw 中# 输入:你好,请用 Python 写一个快速排序# 检查回复质量和速度
4. API 接口测试
curl http://localhost:3000/health
🎓 学习路径
新手入门
-
1. Day 1: 安装 OpenClaw 和 Ollama -
2. Day 2: 运行第一个本地模型 -
3. Day 3: 使用 OpenClaw 对话 -
4. Day 4: 编写简单的 Python 脚本调用 API
进阶提升
-
1. Docker 部署: 使用 Docker Compose 管理多服务 -
2. 模型优化: 量化、GPU 加速、多模型切换 -
3. 自定义 Agent: 编写专业化的 AI 助手 -
4. CI/CD 集成: 自动化部署和测试
企业级应用
-
1. 安全加固: 网络隔离、访问控制、审计日志 -
2. 高可用: 负载均衡、故障转移、备份恢复 -
3. 性能调优: 资源监控、瓶颈分析、优化配置 -
4. 合规性: 数据脱敏、隐私保护、审计追踪
💡 最佳实践
-
1. 定期备份: 备份模型文件和配置 -
2. 版本控制: 对配置文件使用 Git 管理 -
3. 监控日志: 定期检查系统日志 -
4. 安全更新: 及时更新软件版本 -
5. 文档记录: 记录部署过程中的问题和解决方案
📌 快速命令汇总
# 安装brew install openclaw ollama # macOS# 启动服务ollama serve # 终端运行# 或docker start ollama openclaw # Docker 方式# 安装模型ollama pull qwen2.5:7b# 测试模型ollama run qwen2.5:7b# 查看 APIcurl http://localhost:11434/api/tags
喜欢这篇文章?欢迎点赞、收藏、转发!
关注微信公众号「大强哥爱编程」,获取更多 AI 技术干货!
夜雨聆风