Spaces:
Sleeping
Sleeping
Upload fso_conversational_memory.py with huggingface_hub
Browse files- fso_conversational_memory.py +40 -0
fso_conversational_memory.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hashlib
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
class ConversationalMemory:
|
| 5 |
+
"""
|
| 6 |
+
Law XII Component: Topological Memory
|
| 7 |
+
Stores conversational history as a sequence of coordinates in Fiber 2.
|
| 8 |
+
Allows for temporal context retrieval.
|
| 9 |
+
"""
|
| 10 |
+
def __init__(self, m=256, k=4):
|
| 11 |
+
self.m = m
|
| 12 |
+
self.k = k
|
| 13 |
+
self.history = [] # List of entries
|
| 14 |
+
|
| 15 |
+
def _get_coord(self, text, fiber=2):
|
| 16 |
+
h = hashlib.sha256(text.encode()).digest()
|
| 17 |
+
coords = [h[i % len(h)] % self.m for i in range(self.k - 1)]
|
| 18 |
+
w = (fiber - sum(coords)) % self.m
|
| 19 |
+
return tuple(coords + [w])
|
| 20 |
+
|
| 21 |
+
def record_turn(self, speaker, text):
|
| 22 |
+
coord = self._get_coord(text)
|
| 23 |
+
entry = {
|
| 24 |
+
"timestamp": time.time(),
|
| 25 |
+
"speaker": speaker,
|
| 26 |
+
"text": text,
|
| 27 |
+
"coord": coord
|
| 28 |
+
}
|
| 29 |
+
self.history.append(entry)
|
| 30 |
+
print(f" [MEMORY]: Recorded {speaker} turn @ {coord}")
|
| 31 |
+
return coord
|
| 32 |
+
|
| 33 |
+
def get_recent_context(self, depth=5):
|
| 34 |
+
return "\n".join([f"{h['speaker']}: {h['text']}" for h in self.history[-depth:]])
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
mem = ConversationalMemory()
|
| 38 |
+
mem.record_turn("User", "Hello TGI.")
|
| 39 |
+
mem.record_turn("TGI", "Greetings. I am operational.")
|
| 40 |
+
print(f"\nContext Retrieval:\n{mem.get_recent_context()}")
|