
就像组装一只聪明的机器螃蟹 - OpenClaw就像一只可以帮你完成各种任务的智能螃蟹,有眼睛(视觉模块)、大脑(LLM)、钳子(工具使用)和记忆系统!
bash
# 创建一个干净的工作空间(就像给螃蟹准备一个干净的水缸)
python -m venv openclaw_env
# 激活环境(把螃蟹放进水缸)
source openclaw_env/bin/activate # Mac/Linux
# 或
openclaw_env\Scripts\activate # Windows# 从GitHub仓库获取OpenClaw(就像订购螃蟹的零件)
git clone https://github.com/openclawai/openclaw.git
cd openclaw
# 安装所需工具(给螃蟹装上各种功能模块)
pip install -r requirements.txt
pip install -e .生动比喻:这就像我们买了一个乐高螃蟹套装,现在要开始组装了!
python
# config/llm_config.yaml
# 就像给螃蟹选择不同大小的"大脑"
llm:
provider: "openai" # 可以用OpenAI、Claude等
model: "gpt-4" # 大螃蟹(更聪明但耗能)
# 或者用
# model: "gpt-3.5-turbo" # 小螃蟹(经济实惠)
temperature: 0.7 # 螃蟹的"创意程度"(0-1)
max_tokens: 2000 # 螃蟹一次能思考多长python
# config/tools_config.yaml
tools:
# 文件操作钳子
- name: "file_operator"
enabled: true
permissions: ["read", "write"] # 能读写文件
# 网络搜索钳子
- name: "web_search"
enabled: true
api_key: "your_search_api_key" # 上网搜索的能力
# 代码执行钳子
- name: "code_executor"
enabled: true
sandbox: true # 安全沙箱,防止螃蟹搞破坏python
# my_first_agent.py
from openclaw import Agent
from openclaw.memory import ShortTermMemory
from openclaw.tools import ToolSet
# 创建一个"秘书螃蟹" 🦀
secretary_crab = Agent(
name="秘书小蟹",
# 给它装大脑
llm_config={
"provider": "openai",
"model": "gpt-3.5-turbo",
"temperature": 0.3 # 秘书要严谨一点
},
# 给它装记忆(就像螃蟹的短期记忆)
memory=ShortTermMemory(
capacity=10 # 记住最近10件事
),
# 给它装钳子(工具)
tools=ToolSet([
"file_operator", # 能读写文件
"calendar" # 能管理日程
])
)
# 给小蟹一个任务
response = secretary_crab.run(
"帮我整理今天的日程,并创建一个会议纪要文件"
)
print("小蟹说:", response)python
# multi_agent_system.py
from openclaw import Agent, AgentTeam
# 创建不同角色的螃蟹
researcher_crab = Agent(
name="研究员小蟹",
role="研究员",
llm_config={"model": "gpt-4"}, # 用最聪明的大脑
tools=["web_search", "file_operator"]
)
writer_crab = Agent(
name="作家小蟹",
role="写作者",
llm_config={"model": "gpt-3.5-turbo"},
tools=["text_editor"]
)
# 组建螃蟹团队
team = AgentTeam([
researcher_crab,
writer_crab
])
# 团队协作完成任务
result = team.collaborate(
"研究人工智能的最新发展,并写一篇总结报告"
)python
# memory_system.py
from openclaw.memory import LongTermMemory, VectorMemory
# 给螃蟹装上海马体(长期记忆)
crab_memory = LongTermMemory(
storage_type="vector", # 用向量数据库存储
db_path="./crab_memory.db"
)
# 让螃蟹记住重要信息
crab_memory.remember(
key="user_preference",
value="用户喜欢简洁的回答,讨厌技术术语"
)
# 下次遇到类似情况,螃蟹会想起
memory = crab_memory.recall("user_preference")python
# deploy_api.py
from fastapi import FastAPI
from openclaw import Agent
app = FastAPI()
crab = Agent(name="API螃蟹")
@app.post("/ask")
async def ask_crab(question: str):
"""让螃蟹回答问题"""
response = crab.run(question)
return {"answer": response}
@app.get("/status")
async def check_status():
"""检查螃蟹是否健康"""
return {
"status": "healthy",
"tasks_completed": crab.stats.total_tasks,
"memory_usage": crab.memory.usage
}python
# monitoring.py
import logging
from datetime import datetime
# 记录螃蟹的一举一动
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("CrabMonitor")
class CrabMonitor:
def __init__(self, crab):
self.crab = crab
self.start_time = datetime.now()
def log_activity(self, task):
logger.info(f"""
🦀 螃蟹活动日志:
- 时间: {datetime.now()}
- 任务: {task}
- 思考时间: {self.crab.thinking_time}s
- 记忆使用: {self.crab.memory.usage}%
""")python
# personal_assistant.py
"""
创建一个能帮你管理日常的个人AI秘书
就像拥有一个24小时待命的智能螃蟹
"""
from openclaw import Agent
from openclaw.tools import (
EmailTool, # 邮件工具
CalendarTool, # 日历工具
WeatherTool, # 天气工具
ReminderTool # 提醒工具
)
from openclaw.memory import HybridMemory
class PersonalAssistant:
def __init__(self, user_name):
self.user_name = user_name
# 创建个性化的助手螃蟹
self.assistant = Agent(
name=f"{user_name}的秘书小蟹",
# 个性配置
personality={
"style": "友好专业",
"language": "中文",
"formality": "中等"
},
# 工具配置
tools=[
EmailTool(),
CalendarTool(),
WeatherTool(),
ReminderTool()
],
# 记忆配置
memory=HybridMemory(
short_term_capacity=20,
long_term_enabled=True
)
)
def start_day(self):
"""开始一天的工作"""
# 获取今日信息
weather = self.assistant.use_tool(
"weather",
action="get_forecast"
)
schedule = self.assistant.use_tool(
"calendar",
action="get_today_events"
)
# 生成早安报告
report = self.assistant.run(f"""
根据以下信息,给{self.user_name}生成一个友好的早安报告:
今日天气:{weather}
今日日程:{schedule}
提醒我要:
1. 语气要温暖
2. 包含穿搭建议
3. 提醒重要会议
""")
return report
def handle_email(self, email_content):
"""智能处理邮件"""
response = self.assistant.run(f"""
帮我处理这封邮件:
{email_content}
请:
1. 判断是否紧急
2. 如果紧急,立即提醒我
3. 否则存入待办清单
4. 草拟回复建议
""")
return response
# 使用示例
if __name__ == "__main__":
# 创建你的专属秘书
my_assistant = PersonalAssistant("小明")
# 开始一天
morning_report = my_assistant.start_day()
print(morning_report)
# 处理邮件
email = "紧急:下午3点的会议改到4点"
result = my_assistant.handle_email(email)
print(result)python
# optimization.py
from openclaw.cache import SemanticCache
# 给螃蟹装个"速记本"
cache = SemanticCache(
capacity=100, # 记住100个常见问题
similarity=0.85 # 85%相似就算重复问题
)
@cache.cache_result
async def get_answer(question):
"""如果问过类似问题,直接给出答案"""
return await crab.run(question)python
# parallel.py
import asyncio
async def parallel_tasks():
"""让螃蟹同时做多件事"""
tasks = [
crab.run("查天气"),
crab.run("读邮件"),
crab.run("写报告")
]
# 同时执行
results = await asyncio.gather(*tasks)
return resultsA: 就像给螃蟹装个马达 - 使用更小的模型(gpt-3.5),开启缓存,并行处理
A: 调低temperature(0.1以下),增加验证步骤,使用更强大的模型
A: 限制记忆容量,使用轻量级存储,定期清理缓存
搭建OpenClaw AI代理就像组装一只智能螃蟹: