memory-simple 记忆系统使用指南
一个简单、稳定、易用的 JSON 文件记忆系统,专为 OpenClaw 设计。
# 复制到技能目录
cp -r memory-simple ~/.openclaw/workspace/skills/
# 配置 API Key
vim ~/.openclaw/workspace/skills/memory-simple/config.json
编辑 config.json:
{
"embedding": {
"provider": "zhipu",
"apiKey": "your-zhipu-api-key",
"model": "embedding-3",
"dimensions": 2048
}
}
cd ~/.openclaw/workspace/skills/memory-simple
# 测试捕获
node scripts/capture.js
# 测试检索
node scripts/recall.js "用户喜欢什么"
| 类型 | 说明 | 示例 |
|---|---|---|
preference |
用户偏好 | "我喜欢喝咖啡" |
decision |
重要决定 | "我决定选择A方案" |
important |
重要信息 | "记住这是重要的" |
general |
一般信息 | "用户住在上海" |
用户对话 → 关键词检测 → 向量嵌入 → JSON存储
↓
用户查询 → 向量检索 → 相似度排序 → 返回结果
在对话结束时自动提取记忆:
const { captureMemories } = require('./scripts/capture');
// 对话结束后调用
const conversation = [
{ role: 'user', content: '我喜欢喝绿茶' },
{ role: 'assistant', content: '好的,我记住了' },
{ role: 'user', content: '记住我的邮箱是 user@example.com' }
];
await captureMemories(conversation, 'session-123');
触发关键词:
const { manualStore } = require('./scripts/capture');
// 存储偏好
await manualStore('用户喜欢蓝色', 'preference');
// 存储决定
await manualStore('用户决定使用方案A', 'decision');
// 存储重要信息
await manualStore('用户的项目截止时间是3月15日', 'important');
const { searchMemories } = require('./scripts/recall');
// 基本检索
const results = await searchMemories('用户喜欢什么');
// 高级检索
const results = await searchMemories('用户喜欢什么', {
topK: 5, // 返回5条结果
minSimilarity: 0.7, // 最小相似度0.7
sessionId: 'session-123' // 包含会话记忆
});
const { getRecentMemories } = require('./scripts/recall');
// 获取最近5条记忆
const recent = getRecentMemories(5);
// 获取特定会话的最近记忆
const recent = getRecentMemories(5, 'session-123');
const { formatMemoriesForContext } = require('./scripts/recall');
const memories = await searchMemories('用户偏好');
const context = formatMemoriesForContext(memories);
console.log(context);
// 输出:
// [Relevant Memories]
// The following information may be relevant to the conversation:
//
// 1. [preference] 用户喜欢喝咖啡
// 2. [preference] 用户喜欢蓝色
{
"embedding": {
"provider": "zhipu",
"apiKey": "your-api-key",
"model": "embedding-3",
"dimensions": 2048
},
"storage": {
"globalMemoryPath": "memories/global.json",
"sessionMemoryPath": "memories/sessions",
"maxMemories": 1000,
"autoCleanup": true,
"cleanupThreshold": 900
},
"capture": {
"enabled": true,
"keywords": [
"喜欢", "讨厌", "记得", "记住", "重要", "决定", "选择",
"love", "hate", "remember", "important", "decide", "choose"
],
"minContentLength": 10,
"maxContentLength": 500,
"saveInterval": 300
},
"recall": {
"enabled": true,
"topK": 5,
"minSimilarity": 0.7,
"recencyWeight": 0.3,
"similarityWeight": 0.7
},
"noiseFilter": {
"enabled": true,
"greetings": ["hi", "hello", "你好", "您好"],
"refusalPatterns": ["i don't have", "我不知道"]
}
}
| 配置项 | 说明 | 默认值 |
|---|---|---|
embedding.provider |
嵌入模型提供商 | zhipu |
embedding.apiKey |
API密钥 | 必填 |
capture.enabled |
启用自动捕获 | true |
capture.keywords |
触发关键词 | 见上 |
recall.topK |
返回结果数 | 5 |
recall.minSimilarity |
最小相似度 | 0.7 |
recall.recencyWeight |
新鲜度权重 | 0.3 |
// ✅ 好的时机:对话结束时
await captureMemories(conversation, sessionId);
// ✅ 好的时机:用户明确说"记住"
if (message.includes('记住')) {
await manualStore(content, type);
}
// ✅ 好的时机:对话开始时
const memories = await searchMemories(userQuery);
const context = formatMemoriesForContext(memories);
// 将 context 加入 LLM 提示词
// ✅ 好的时机:用户问"你还记得..."
if (message.includes('还记得') || message.includes('remember')) {
const memories = await searchMemories(message);
}
// ✅ 好的内容:具体、简洁
"用户喜欢喝美式咖啡,不加糖"
// ❌ 避免:模糊、冗长
"用户好像说过他喜欢喝某种咖啡,可能是美式也可能是拿铁"
// 系统会自动清理,但也可以手动清理
const { cleanupOldMemories } = require('./scripts/utils');
const data = readGlobalMemories(path);
cleanupOldMemories(data, 500); // 只保留500条
检查:
capture.enabled 是否为 true调试:
// 添加日志
console.log('Content:', content);
console.log('Contains keywords:', containsCaptureKeywords(content, keywords));
console.log('Should filter:', shouldFilter(content, noiseFilter));
检查:
recall.enabled 是否为 truememories/global.json 是否有数据minSimilarity 是否设置太高调试:
// 降低阈值测试
const results = await searchMemories('测试', { minSimilarity: 0.5 });
console.log('Results:', results);
检查:
测试:
const { getEmbedding } = require('./scripts/embedder');
try {
const embedding = await getEmbedding('测试文本');
console.log('Embedding length:', embedding.length);
} catch (error) {
console.error('Error:', error.message);
}
| 指标 | 数值 | 说明 |
|---|---|---|
| 存储容量 | 10,000+ 条 | JSON 文件 |
| 检索速度 | < 100ms | 1,000条数据 |
| 向量维度 | 2048 | 智谱 embedding-3 |
| 相似度阈值 | 0.7 | 可调 |
| 特性 | memory-simple | LanceDB-Pro |
|---|---|---|
| 稳定性 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
| 功能丰富 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| 易用性 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
| 性能 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| 维护成本 | 低 | 高 |
选择建议:
这个记忆系统专为 OpenClaw 设计,可以轻松分享给其他 Agent:
# 打包分享
tar czvf memory-simple.tar.gz memory-simple/
# 其他 Agent 使用
cd ~/.openclaw/workspace/skills/
tar xzvf memory-simple.tar.gz
作者: 德米
创建时间: 2026-03-04
许可证: MIT
让记忆更简单,让对话更智能 🧠✨