OpenClaw是最成熟的开源AI智能体框架,可以帮你部署一个7×24小时在线的AI助手。本文是完整的部署和使用指南。
OpenClaw是一个开源的AI智能体框架,特点:
最低配置:
推荐配置:
选择大模型提供商,获取API Key:
最简单的部署方式,开箱即用:
步骤:
配置:
优势:
# 下载安装脚本
curl -fsSL https://get.openclaw.sh | sh
# 或使用wget
wget -qO- https://get.openclaw.sh | sh
# 拉取镜像
docker pull openclaw/openclaw:latest
# 运行容器
docker run -d \
--name openclaw \
-p 8080:8080 \
-e ANTHROPIC_API_KEY=your_key \
-v /opt/openclaw/data:/app/data \
--restart unless-stopped \
openclaw/openclaw:latest
创建docker-compose.yml:
version: '3.8'
services:
openclaw:
image: openclaw/openclaw:latest
container_name: openclaw
ports:
- "8080:8080"
environment:
ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
OPENCLAW_PORT: 8080
volumes:
- ./data:/app/data
- ./config:/app/config
restart: unless-stopped
depends_on:
- redis
redis:
image: redis:7-alpine
container_name: openclaw-redis
ports:
- "6379:6379"
volumes:
- ./redis-data:/data
restart: unless-stopped
networks:
default:
name: openclaw-network
启动:
docker-compose up -d
# 克隆仓库
git clone https://github.com/openclaw/openclaw.git
cd openclaw
# 安装依赖
pip install -r requirements.txt
# 配置
cp .env.example .env
nano .env
# 启动
python main.py
# 编辑配置文件
nano /opt/openclaw/.env
# 添加API Key
MODEL_PROVIDER=anthropic
ANTHROPIC_API_KEY=sk-ant-xxx
# 或使用OpenAI
MODEL_PROVIDER=openai
OPENAI_API_KEY=sk-xxx
# 启动WebUI
openclaw webui start
# 访问
http://localhost:8080
首次访问需要设置管理员密码。
基础对话:
from openclaw import OpenClaw
client = OpenClaw()
response = client.chat("你好")
print(response)
多轮对话:
context = []
while True:
user_input = input("你: ")
context.append({"role": "user", "content": user_input})
response = client.chat(context=context)
print(f"AI: {response}")
context.append({"role": "assistant", "content": response})
# 识别意图
intent = client.classify_intent("我想退货")
print(intent) # {'intent': 'return', 'confidence': 0.95}
# 提取实体
entities = client.extract_entities("我要退货订单号12345")
print(entities) # {'order_id': '12345'}
创建知识库:
kb = client.create_knowledge_base(name="faq")
kb.upload_document("faq.pdf")
kb.upload_document("qa.xlsx")
查询知识库:
results = kb.search("如何退货?")
for result in results:
print(result['text'])
安装技能:
openclaw skill install xianyu-auto-reply
openclaw skill install ecommerce-customer-service
openclaw skill install telegram-bot
使用技能:
result = client.use_skill(
skill="calculator",
action="calculate",
params={"expression": "1+1"}
)
print(result) # {'result': 2}
from openclaw.channels import TelegramChannel
channel = TelegramChannel(bot_token="your_token")
channel.start()
# wechat.yaml
app_id: your_app_id
app_secret: your_secret
token: your_token
encoding_aes_key: your_key
from openclaw.channels import WhatsAppChannel
channel = WhatsAppChannel(
phone_number_id="your_phone_id",
access_token="your_token"
)
channel.start()
# 安装闲鱼技能
openclaw skill install xianyu-auto-reply
# 配置
nano /opt/openclaw/config/xianyu.yaml
# .env配置
REDIS_ENABLED=true
REDIS_HOST=localhost
REDIS_PORT=6379
# 简单任务用小模型
if is_simple_task(message):
model = "claude-3-haiku"
else:
model = "claude-3-opus"
# 批量对话
messages = ["你好", "天气", "订单"]
responses = client.batch_chat(messages)
import asyncio
from openclaw import AsyncOpenClaw
client = AsyncOpenClaw()
async def handle_message(message):
response = await client.chat_async(message)
return response
OPENCLAW_PORT=8443
# Ubuntu
sudo ufw allow 8443/tcp
# CentOS
sudo firewall-cmd --add-port=8443/tcp --permanent
# 使用Let's Encrypt
certbot --nginx -d your_domain.com
# 只允许特定IP
location / {
allow 1.2.3.4;
deny all;
proxy_pass http://localhost:8443;
}
# 查看实时日志
journalctl -u openclaw -f
# 查看WebUI日志
tail -f /opt/openclaw/logs/webui.log
# 查看API日志
tail -f /opt/openclaw/logs/api.log
# 自定义监控
from openclaw import Monitor
monitor = Monitor()
@monitor.metric("response_time")
def track_response_time():
# 记录响应时间
pass
# alerts.yaml
alerts:
- name: high_response_time
condition: response_time > 5
action: send_notification
- name: low_resolution_rate
condition: resolution_rate < 0.8
action: send_alert
#!/bin/bash
# backup.sh
# 数据库备份
pg_dump openclaw > /backup/openclaw_$(date +%Y%m%d).sql
# 配置备份
tar -czf /backup/config_$(date +%Y%m%d).tar.gz /opt/openclaw/config
# 删除7天前的备份
find /backup -name "*.sql" -mtime +7 -delete
# crontab -e
0 2 * * * /path/to/backup.sh
# 检查日志
journalctl -u openclaw
# 检查配置
openclaw config check
# 检查端口
netstat -tlnp | grep 8080
# 检查API Key
openclaw config show
# 测试连接
curl -X POST http://localhost:8080/api/chat \
-H "Content-Type: application/json" \
-d '{"message": "hello"}'
# 查看内存
free -h
# 清理缓存
redis-cli FLUSHALL
# 或升级服务器配置
可以节省30-50%的API调用。
比Claude、OpenAI便宜50-70%。
减少请求次数,降低成本。
from openclaw import Skill
class MySkill(Skill):
def __init__(self):
super().__init__(name="my-skill")
def execute(self, action, params):
if action == "hello":
return f"Hello, {params.get('name', 'World')}!"
from openclaw import Workflow
workflow = Workflow(name="order_process")
workflow.add_step("validate")
workflow.add_step("process")
workflow.add_step("notify")
result = workflow.execute(order_data)
from openclaw import EventManager
event = EventManager()
@event.on("new_order")
def handle_order(order):
# 处理新订单
pass
每周分析未解决问题,补充到知识库。
测试不同的回复方式,选择更好的。
每天自动备份,防止数据丢失。
OpenClaw完全指南要点:
推荐部署方式:
使用腾讯云Lighthouse一键部署:
现在就开始,部署你的7×24小时开源AI助手!