Install
openclaw skills install forgetting-curve独立的 Ebbinghaus 遗忘曲线模块,提供记忆衰减计算和间隔重复调度
openclaw skills install forgetting-curve独立的 Ebbinghaus 遗忘曲线实现,为记忆系统提供标准化衰减计算。
decay = 2^(-age_days / half_life_days)
其中:
age_days: 距离最后一次复习的天数half_life_days: 半衰期(默认 30 天)next_review_days = base_interval * (strength ^ factor)
其中:
base_interval: 基础间隔(如 1 天)strength: 当前记忆强度(0.0-1.0)factor: 强度因子(默认 1.5)from forgetting_curve import ForgettingCurve
# 创建衰减器(默认半衰期 30 天)
curve = ForgettingCurve(half_life_days=30.0)
# 计算衰减因子
age_days = 7 # 7 天前记忆
decay = curve.calculate_decay(age_days) # 返回 0.82
# 应用衰减到记忆强度
original_strength = 0.9
decayed_strength = curve.apply_decay(original_strength, age_days) # 0.74
from forgetting_curve import SpacedRepetitionScheduler
scheduler = SpacedRepetitionScheduler()
# 计算下一次复习时间
current_strength = 0.6
next_review_days = scheduler.next_review_interval(current_strength) # 3.1 天
# 更新记忆强度(复习后)
new_strength = scheduler.update_strength(current_strength, success=True) # 0.75
# 批量计算衰减
import pandas as pd
from forgetting_curve import batch_decay
memories = [
{"id": "mem1", "strength": 0.9, "age_days": 3},
{"id": "mem2", "strength": 0.7, "age_days": 15},
{"id": "mem3", "strength": 0.5, "age_days": 60}
]
decayed = batch_decay(memories, half_life_days=30)
# 返回带衰减后强度的列表
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
half_life_days | float | 30.0 | 半衰期(天) |
initial_strength | float | 1.0 | 初始记忆强度 |
minimum_strength | float | 0.1 | 最低强度阈值 |
base_interval | float | 1.0 | 基础复习间隔(天) |
strength_factor | float | 1.5 | 强度因子 |
easy_factor | float | 1.3 | 简单记忆因子 |
hard_factor | float | 0.8 | 困难记忆因子 |
# co_occurrence_tracker.py 中替换硬编码衰减
# 旧代码:
# decay = math.pow(2, -age_days / 30.0)
# 新代码:
from forgetting_curve import ForgettingCurve
curve = ForgettingCurve(half_life_days=30.0)
decay = curve.calculate_decay(age_days)
# 在检索时应用遗忘曲线过滤
from forgetting_curve import ForgettingCurve
def retrieve_memories(query, top_k=10):
# ... 语义搜索 ...
for mem in results:
age_days = (datetime.now() - mem.last_used).days
decay = curve.calculate_decay(age_days)
mem.score *= decay
# ... 返回结果 ...
from forgetting_curve import ForgettingCurve
# 自定义衰减函数
def custom_decay(age_days, strength):
return strength * math.exp(-age_days / 45.0)
curve = ForgettingCurve(decay_function=custom_decay)
# 不同记忆类型使用不同半衰期
short_term = ForgettingCurve(half_life_days=3.0) # 短期记忆
long_term = ForgettingCurve(half_life_days=90.0) # 长期记忆
procedural = ForgettingCurve(half_life_days=7.0) # 程序性记忆
1000 次衰减计算: 0.8ms
10000 次批量衰减: 5.2ms
内存占用: < 1MB
# 从当前目录安装
pip install -e .
# 或直接复制文件
cp forgetting_curve.py /your/project/
# 通过 ClawHub 发布后
clawhub install forgetting-curve
forgetting-curve/
├── forgetting_curve.py # 核心模块
├── test_decay.py # 单元测试
├── examples/ # 使用示例
├── config/ # 配置文件
└── SKILL.md # 本文档
python test_decay.py
版本: 0.1.0 独立的 Ebbinghaus 遗忘曲线模块