OpenClaw 的持久记忆配置方法主要涉及 记忆存储、检索和管理 的设置,以下是关键配置步骤和方法:

配置记忆存储后端
OpenClaw 支持多种记忆存储方式:
A. 本地文件存储(JSON/文本)
storage_type: "local"
file_path: "./memory.json"
# 或使用目录存储分片记忆
directory: "./memory_chunks/"
B. 数据库存储
# 使用 PostgreSQL(带向量扩展) memory: storage_type: "postgres" connection_string: "postgresql://user:pass@localhost:5432/memory_db" table_name: "agent_memories" # 使用 MongoDB memory: storage_type: "mongodb" connection_string: "mongodb://localhost:27017" database_name: "openclaw" collection_name: "memories"
C. 向量数据库(推荐)
# Pinecone
memory:
storage_type: "pinecone"
api_key: "${PINECONE_API_KEY}"
environment: "us-west1-gcp"
index_name: "openclaw-index"
# ChromaDB(本地)
memory:
storage_type: "chromadb"
path: "./chroma_data"
collection_name: "agent_memory"
记忆检索策略配置
memory:
retrieval:
# 检索类型:similarity / time_weighted / 混合
strategy: "hybrid"
# 相似度检索设置
similarity:
top_k: 5
threshold: 0.7 # 最小相似度
# 时间加权检索
recency_weight: 0.3
importance_weight: 0.4
relevance_weight: 0.3
# 混合检索权重
hybrid_weights:
vector: 0.6
keyword: 0.3
time: 0.1
记忆处理流水线配置
memory:
processing_pipeline:
- name: "summarizer"
model: "gpt-3.5-turbo"
chunk_size: 2000 # 当记忆过长时自动总结
- name: "embedder"
model: "text-embedding-3-small" # 或本地模型
- name: "importance_scorer"
# 自动计算记忆重要性(1-10)
- name: "metadata_extractor"
# 提取时间、实体、情感等元数据
记忆生命周期管理
memory:
retention:
max_items: 10000 # 最大记忆数量
ttl_days: 30 # 过期时间
compression:
enabled: true
# 当记忆超过阈值时自动压缩
threshold: 1000
compression_ratio: 0.3
# 分层次存储
hierarchical:
levels:
- name: "working_memory"
capacity: 100
ttl_hours: 24
- name: "short_term"
capacity: 1000
ttl_days: 7
- name: "long_term"
capacity: 10000
ttl_days: 365
完整配置示例
# openclaw_config.yaml
memory:
enabled: true
storage:
type: "chromadb"
persist_directory: "./memory_db"
collection: "main_memory"
embedding:
model: "BAAI/bge-small-zh-v1.5"
device: "cuda" # 或 "cpu"
retrieval:
default_top_k: 7
strategies:
- type: "similarity"
weight: 0.6
- type: "recency"
weight: 0.25
- type: "importance"
weight: 0.15
summarization:
model: "gpt-4-mini"
trigger_length: 1500
max_summary_length: 300
indexing:
auto_index: true
batch_size: 100
update_interval: 3600 # 每小时更新索引
环境变量配置
# .env 文件 MEMORY_STORAGE_TYPE=pinecone PINECONE_API_KEY=your_key_here OPENAI_API_KEY=sk-... # 用于嵌入/ # 本地模型路径 LOCAL_EMBEDDING_MODEL_PATH=./models/bge-small
初始化代码示例
from openclaw import OpenClawAgent
from openclaw.memory import MemoryConfig
# 方法1:通过配置文件
agent = OpenClawAgent(
config_file="config.yaml",
memory_config="memory_config.yaml"
)
# 方法2:代码配置
memory_config = MemoryConfig(
storage_type="chromadb",
embedding_model="text-embedding-3-small",
retrieval_strategy="hybrid",
persist_directory="./memory_data"
)
agent = OpenClawAgent(
name="assistant",
memory_config=memory_config
)
# 使用记忆
agent.remember("用户喜欢咖啡")
memories = agent.recall("用户偏好")
关键注意事项
- 隐私与安全:敏感数据建议使用本地存储和本地嵌入模型
- 性能优化:定期清理和索引记忆,避免存储膨胀
- 混合检索:结合向量搜索和关键字搜索效果最佳
- 版本控制:定期备份记忆数据库
- 模型选择:中文任务建议使用
bge-zh系列嵌入模型
根据具体需求调整配置参数,可通过 OpenClaw 的 memory.evaluate() 方法测试不同配置的效果。
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。