ljsysfurry commited on
Commit
96cf104
·
verified ·
1 Parent(s): b028bfa

Upload aura/memory.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. aura/memory.py +158 -0
aura/memory.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """记忆系统:短期记忆 + 长期记忆 + 遗忘曲线"""
2
+
3
+ import time
4
+ import json
5
+ import heapq
6
+ from dataclasses import dataclass, field
7
+ from typing import List, Dict, Optional, Any
8
+ from collections import deque
9
+ import math
10
+
11
+
12
+ @dataclass
13
+ class MemoryItem:
14
+ """记忆条目"""
15
+ id: str
16
+ content: str
17
+ memory_type: str # "conversation" / "event" / "preference" / "feedback"
18
+ timestamp: float = field(default_factory=time.time)
19
+ importance: float = 0.5 # 0-1
20
+ access_count: int = 0
21
+ last_access: float = field(default_factory=time.time)
22
+ tags: List[str] = field(default_factory=list)
23
+ related_ids: List[str] = field(default_factory=list)
24
+
25
+ @property
26
+ def age(self) -> float:
27
+ return time.time() - self.timestamp
28
+
29
+ def strength(self, half_life: float = 86400.0) -> float:
30
+ """基于遗忘曲线的记忆强度"""
31
+ elapsed = self.age
32
+ # 指数遗忘: S = importance × 2^(-elapsed/half_life)
33
+ decay = math.pow(2, -elapsed / half_life)
34
+ # 访问增强: boost = log2(access_count + 1) × 0.1
35
+ boost = math.log2(self.access_count + 1) * 0.1
36
+ return min(1.0, self.importance * decay + boost)
37
+
38
+
39
+ class ForgettingCurve:
40
+ """
41
+ 遗忘曲线:决定记忆的半衰期和遗忘速度
42
+
43
+ 公式: S(t) = I · 2^(-t / τ)
44
+ 其中:
45
+ S(t) = t时刻的记忆强度
46
+ I = 初始重要性
47
+ τ = 半衰期(新记忆默认1天,高频访问的记忆半衰期延长)
48
+ """
49
+
50
+ def __init__(self, default_half_life: float = 86400.0):
51
+ self.default_half_life = default_half_life
52
+
53
+ def compute_half_life(self, item: MemoryItem) -> float:
54
+ """根据访问频率动态调整半衰期"""
55
+ base = self.default_half_life
56
+ if item.access_count > 10:
57
+ base *= 7 # 高频访问延至1周
58
+ elif item.access_count > 5:
59
+ base *= 3 # 中频延至3天
60
+ # 高重要性记忆更持久
61
+ base *= (0.5 + item.importance)
62
+ return base
63
+
64
+ def should_forget(self, item: MemoryItem, threshold: float = 0.05) -> bool:
65
+ """判断是否应该遗忘(强度低于阈值)"""
66
+ half_life = self.compute_half_life(item)
67
+ return item.strength(half_life) < threshold
68
+
69
+
70
+ class MemoryGraph:
71
+ """
72
+ 异构图记忆网络
73
+
74
+ 节点类型:
75
+ - user: 用户实体
76
+ - conversation: 对话片段
77
+ - event: 事件
78
+ - preference: 偏好
79
+ - topic: 话题
80
+
81
+ 边类型:
82
+ - has_topic: 对话→话题
83
+ - mentioned: 对话→用户实体
84
+ - leads_to: 事件→事件(因果关系)
85
+ - prefers: 用户→偏好
86
+ """
87
+
88
+ def __init__(self, config=None):
89
+ self._items: Dict[str, MemoryItem] = {}
90
+ self._graph: Dict[str, List[tuple]] = {} # node_id -> [(edge_type, target_id)]
91
+ self._forgetting = ForgettingCurve()
92
+ self._short_term: deque = deque(maxlen=100)
93
+ self._long_term_capacity = 10000
94
+
95
+ def add(self, item: MemoryItem, relations: List[tuple] = None) -> str:
96
+ """添加记忆"""
97
+ self._items[item.id] = item
98
+ self._short_term.append(item.id)
99
+ if item.memory_type in ("conversation", "event"):
100
+ self._graph[item.id] = relations or []
101
+ for edge_type, target in (relations or []):
102
+ if target not in self._graph:
103
+ self._graph[target] = []
104
+ self._graph[target].append((f"inverse_{edge_type}", item.id))
105
+ return item.id
106
+
107
+ def get(self, item_id: str) -> Optional[MemoryItem]:
108
+ """获取记忆并更新访问计数"""
109
+ item = self._items.get(item_id)
110
+ if item:
111
+ item.access_count += 1
112
+ item.last_access = time.time()
113
+ return item
114
+
115
+ def recall_by_topic(self, topic: str, top_k: int = 5) -> List[MemoryItem]:
116
+ """按话题召回"""
117
+ results = []
118
+ for item in self._items.values():
119
+ if topic in item.tags or topic in item.content:
120
+ # 跳过已被遗忘的
121
+ if self._forgetting.should_forget(item):
122
+ continue
123
+ results.append((item.strength(), item))
124
+ results.sort(key=lambda x: -x[0])
125
+ return [item for _, item in results[:top_k]]
126
+
127
+ def recall_recent(self, n: int = 10) -> List[MemoryItem]:
128
+ """召回最近的n条记忆"""
129
+ ids = list(self._short_term)[-n:]
130
+ return [self._items[i] for i in ids if i in self._items]
131
+
132
+ def prune(self):
133
+ """清理被遗忘的记忆"""
134
+ to_delete = []
135
+ for item_id, item in self._items.items():
136
+ if self._forgetting.should_forget(item):
137
+ to_delete.append(item_id)
138
+ for item_id in to_delete:
139
+ del self._items[item_id]
140
+ self._graph.pop(item_id, None)
141
+
142
+ def to_json(self) -> str:
143
+ data = {
144
+ "items": {k: v.__dict__ for k, v in self._items.items()},
145
+ "graph": {k: v for k, v in self._graph.items()},
146
+ }
147
+ return json.dumps(data, ensure_ascii=False)
148
+
149
+ @classmethod
150
+ def from_json(cls, json_str: str) -> "MemoryGraph":
151
+ mg = cls()
152
+ data = json.loads(json_str)
153
+ for k, v in data.get("items", {}).items():
154
+ item = MemoryItem(**v)
155
+ mg._items[k] = item
156
+ mg._short_term.append(k)
157
+ mg._graph = data.get("graph", {})
158
+ return mg