This skill enables implementation of forgetting mechanisms for AI systems, particularly for OpenClaw agents, to manage memory overload, improve performance, and maintain data privacy.
该技能为AI系统(特别是OpenClaw智能体)实现遗忘机制,用于管理内存过载、提高性能和维护数据隐私。
Forgetting mechanisms are essential for AI systems that accumulate large amounts of data over time. They help:
遗忘机制对于随时间积累大量数据的AI系统至关重要。它们有助于:
Remove data after a specified time period.
在指定时间段后移除数据。
// Example: Remove memories older than 30 days
// 示例:删除超过30天的记忆
const forgetOldMemories = (memoryStore, days = 30) => {
const cutoffTime = Date.now() - (days * 24 * 60 * 60 * 1000);
memoryStore.filter(memory => memory.timestamp > cutoffTime);
};
Remove data based on relevance score.
基于相关性分数移除数据。
// Example: Remove memories with relevance score below threshold
// 示例:删除相关性分数低于阈值的记忆
const forgetIrrelevantMemories = (memoryStore, threshold = 0.3) => {
memoryStore.filter(memory => memory.relevance >= threshold);
};
Remove data based on access frequency.
基于访问频率移除数据。
// Example: Remove memories accessed less than N times
// 示例:删除访问次数少于N次的记忆
const forgetInfrequentMemories = (memoryStore, minAccessCount = 2) => {
memoryStore.filter(memory => memory.accessCount >= minAccessCount);
};
Remove specific data based on user requests.
根据用户请求移除特定数据。
// Example: Remove specific memory by ID
// 示例:通过ID删除特定记忆
const forgetSpecificMemory = (memoryStore, memoryId) => {
memoryStore.filter(memory => memory.id !== memoryId);
};
Remove data based on current context relevance.
基于当前上下文相关性移除数据。
// Example: Remove memories unrelated to current task
// 示例:删除与当前任务无关的记忆
const forgetContextIrrelevant = (memoryStore, currentContext) => {
memoryStore.filter(memory => {
return memory.topics.some(topic => currentContext.topics.includes(topic));
});
};
// Example memory structure
// 示例记忆结构
const memory = {
id: "unique-id-123",
content: "This is a memory content",
timestamp: Date.now(),
relevance: 0.8,
accessCount: 5,
topics: ["project", "meeting", "action-item"],
source: "user-input",
sensitivity: "low" // low, medium, high
// 敏感度:低、中、高
};
// OpenClaw forgetting configuration
// OpenClaw遗忘配置
const forgettingConfig = {
strategies: [
{
type: "time-based",
days: 30,
enabled: true
},
{
type: "relevance-based",
threshold: 0.3,
enabled: true
},
{
type: "frequency-based",
minAccessCount: 2,
enabled: true
}
],
runInterval: "daily", // daily, weekly, monthly
// 运行间隔:每日、每周、每月
dryRun: false, // preview what would be deleted
// 模拟运行:预览将要删除的内容
backupBeforeForgetting: true
// 遗忘前备份:是
};
// Example integration with OpenClaw agent
// 与OpenClaw智能体集成示例
const OpenClawForgettingMechanism = {
init(agent) {
this.agent = agent;
this.memoryStore = agent.memory;
this.config = agent.config.forgettingMechanism || {};
this.setupScheduler();
},
setupScheduler() {
const interval = this.config.runInterval || "daily";
const cronExpression = this.getCronExpression(interval);
// Schedule forgetting based on configuration
// 根据配置安排遗忘任务
this.agent.scheduler.addJob({
name: "forgetting-mechanism",
cron: cronExpression,
task: () => this.runForgetting()
});
},
getCronExpression(interval) {
const expressions = {
daily: "0 0 * * *",
weekly: "0 0 * * 0",
monthly: "0 0 1 * *"
};
return expressions[interval] || "0 0 * * *";
},
async runForgetting() {
this.agent.logger.info("Running forgetting mechanism...");
const originalCount = this.memoryStore.size;
// Apply all enabled strategies
// 应用所有启用的策略
this.config.strategies?.forEach(strategy => {
if (strategy.enabled) {
this.applyStrategy(strategy);
}
});
const finalCount = this.memoryStore.size;
const removedCount = originalCount - finalCount;
this.agent.logger.info(`Forgetting mechanism completed: Removed ${removedCount} memories`);
return {
originalCount,
finalCount,
removedCount
};
},
applyStrategy(strategy) {
switch (strategy.type) {
case "time-based":
this.forgetTimeBased(strategy.days);
break;
case "relevance-based":
this.forgetRelevanceBased(strategy.threshold);
break;
case "frequency-based":
this.forgetFrequencyBased(strategy.minAccessCount);
break;
case "context-based":
this.forgetContextBased(strategy.context);
break;
default:
this.agent.logger.warn(`Unknown forgetting strategy: ${strategy.type}`);
}
},
forgetTimeBased(days = 30) {
const cutoffTime = Date.now() - (days * 24 * 60 * 60 * 1000);
this.memoryStore.filter(memory => memory.timestamp > cutoffTime);
},
forgetRelevanceBased(threshold = 0.3) {
this.memoryStore.filter(memory => memory.relevance >= threshold);
},
forgetFrequencyBased(minAccessCount = 2) {
this.memoryStore.filter(memory => memory.accessCount >= minAccessCount);
},
forgetContextBased(context) {
this.memoryStore.filter(memory => {
return memory.topics.some(topic => context.topics.includes(topic));
});
},
// Manual forgetting methods
// 手动遗忘方法
forgetSpecificMemory(memoryId) {
return this.memoryStore.filter(memory => memory.id !== memoryId);
},
forgetByTopic(topic) {
return this.memoryStore.filter(memory => !memory.topics.includes(topic));
},
forgetAll() {
return this.memoryStore.clear();
}
};
// In agent configuration
// 在智能体配置中
const agentConfig = {
// ... other config
// ... 其他配置
forgettingMechanism: {
strategies: [
{
type: "time-based",
days: 30,
enabled: true
},
{
type: "relevance-based",
threshold: 0.4,
enabled: true
}
],
runInterval: "daily",
backupBeforeForgetting: true
}
};
// In agent code
// 在智能体代码中
await agent.forgettingMechanism.forgetSpecificMemory("memory-id-123");
await agent.forgettingMechanism.forgetByTopic("confidential");
// Trigger forgetting manually
// 手动触发遗忘
const result = await agent.forgettingMechanism.runForgetting();
console.log(`Removed ${result.removedCount} memories`);
// Example test for forgetting mechanism
// 遗忘机制测试示例
const testForgettingMechanism = () => {
// Create test memories
// 创建测试记忆
const testMemories = [
{
id: "mem1",
content: "Old memory",
timestamp: Date.now() - (40 * 24 * 60 * 60 * 1000), // 40 days old
// 40天前
relevance: 0.2,
accessCount: 1
},
{
id: "mem2",
content: "Recent relevant memory",
timestamp: Date.now() - (10 * 24 * 60 * 60 * 1000), // 10 days old
// 10天前
relevance: 0.8,
accessCount: 5
},
{
id: "mem3",
content: "Recent irrelevant memory",
timestamp: Date.now() - (5 * 24 * 60 * 60 * 1000), // 5 days old
// 5天前
relevance: 0.1,
accessCount: 1
}
];
// Initialize memory store
// 初始化记忆存储
const memoryStore = new MemoryStore(testMemories);
// Create forgetting mechanism
// 创建遗忘机制
const forgetting = new OpenClawForgettingMechanism();
forgetting.memoryStore = memoryStore;
forgetting.config = {
strategies: [
{ type: "time-based", days: 30, enabled: true },
{ type: "relevance-based", threshold: 0.3, enabled: true }
]
};
// Run forgetting
// 运行遗忘
forgetting.runForgetting();
// Verify results
// 验证结果
const remainingMemories = memoryStore.getAll();
console.log(`Remaining memories: ${remainingMemories.length}`);
console.log(remainingMemories.map(m => m.content));
// Should only keep mem2
// 应该只保留mem2
return remainingMemories.length === 1 && remainingMemories[0].id === "mem2";
};
共 1 个版本