最近 OpenClaw 在技术圈很火。这是一个开源的 AI Agent 框架,让你可以用自然语言描述任务,AI 自动调用工具完成。
本文将手把手教你:
- 在本地部署 OpenClaw
- 搭建第一个自动化工作流
- 接入自定义工具(以查询服务器状态为例)
全程 30 分钟,有手就行。
一、OpenClaw 是什么?
核心能力
- 自然语言任务:「帮我查一下服务器负载」→ 自动执行
- 工具调用:代码执行、文件操作、API 调用、浏览器控制
- 多 Agent 协作:复杂任务分解给多个子 Agent
- 安全沙箱:代码在隔离环境运行
适用场景
- DevOps 自动化(监控、部署、排查)
- 数据分析(自动抓取、清洗、可视化)
- 内容运营(自动生成、发布、数据追踪)
- 个人助理(日程管理、信息汇总)
二、环境准备
系统要求
- Linux/macOS/Windows (WSL2)
- Python 3.10+
- Docker(可选,推荐)
安装步骤(5分钟)
# 1. 克隆仓库 git clone https://github.com/openclaw/openclaw.git cd openclaw # 2. 创建虚拟环境 python -m venv venv source venv/bin/activate # 3. 安装依赖 pip install -r requirements.txt # 4. 配置 API Key export OPENAI_API_KEY="your-api-key" # 5. 启动 OpenClaw python -m openclaw
看到以下输出表示成功:
✓ OpenClaw server started on http://localhost:8000 ✓ Web UI available at http://localhost:8000/ui ✓ Ready to accept tasks
三、搭建第一个工作流:服务器监控助手
目标
让 AI 自动查询服务器状态,并在负载过高时发送告警。
Step 1: 创建工具定义
创建 tools/server_monitor.py:
import subprocess
from openclaw import tool
@tool
def get_server_load():
\"\"\"
获取服务器负载信息
Returns: CPU、内存、磁盘使用率
\"\"\"
cpu = subprocess.run(["top", "-bn1"],
capture_output=True, text=True)
# ... 解析逻辑
return {"cpu": 45.2, "memory": 67.8, "disk": 32.1}
@tool
def send_alert(message: str):
\"\"\"发送告警通知\"\"\"
print(f"🚨 ALERT: {message}")
return {"sent": True}
Step 2: 配置 Agent
创建 agents/monitor_agent.yml:
name: ServerMonitor model: gpt-4-turbo system_prompt: | 你是一个服务器监控助手。 定期检查服务器负载,如果超过 80% 发送告警。 tools: - tools.server_monitor.get_server_load - tools.server_monitor.send_alert
Step 3: 运行工作流
# 启动 Agent python -m openclaw.agent --config agents/monitor_agent.yml # 输入任务 >> 检查服务器状态,如果负载过高就发告警
AI 会自动执行:
- 调用
get_server_load()获取状态 - 分析返回数据
- 如果超过阈值,调用
send_alert()
四、进阶:多 Agent 协作
┌─────────────────┐
│ Orchestrator │
│ (任务调度) │
└────────┬────────┘
│
┌────────────────────┼────────────────────┐
│ │ │
┌────▼────┐ ┌────▼────┐ ┌────▼────┐
│ Monitor │ │ Analyst │ │ DevOps │
│ (监控) │ │ (分析) │ │ (执行) │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
└────────────────────┼────────────────────┘
│
┌────────▼────────┐
│ Report/Send │
│ (结果输出) │
└─────────────────┘
五、实战案例
案例 1:自动化数据分析报告
每天早上 9 点,自动查询数据库、生成图表、撰写报告、发送到钉钉群。
案例 2:智能客服
监控客服系统消息,AI 自动回复常见问题,复杂问题转人工。
案例 3:DevOps 自动化
监控 GitHub PR,自动 code review、运行测试、部署到 staging。
六、下一步
- 阅读官方文档:https://docs.openclaw.ai
- 加入社区:Discord / GitHub Discussions
- 贡献工具:提交你的自定义工具到社区
总结
OpenClaw 让 AI Agent 从概念变成了生产力工具。
30 分钟,你已经学会了:
- ✅ 部署 OpenClaw 环境
- ✅ 创建自定义工具
- ✅ 搭建自动化工作流
- ✅ 多 Agent 协作架构
接下来,就看你的想象力了。