Spaces:
Sleeping
Sleeping
| import hashlib | |
| import time | |
| class ConversationalMemory: | |
| """ | |
| Law XII Component: Topological Memory | |
| Stores conversational history as a sequence of coordinates in Fiber 2. | |
| Allows for temporal context retrieval. | |
| """ | |
| def __init__(self, m=256, k=4): | |
| self.m = m | |
| self.k = k | |
| self.history = [] # List of entries | |
| def _get_coord(self, text, fiber=2): | |
| h = hashlib.sha256(text.encode()).digest() | |
| coords = [h[i % len(h)] % self.m for i in range(self.k - 1)] | |
| w = (fiber - sum(coords)) % self.m | |
| return tuple(coords + [w]) | |
| def record_turn(self, speaker, text): | |
| coord = self._get_coord(text) | |
| entry = { | |
| "timestamp": time.time(), | |
| "speaker": speaker, | |
| "text": text, | |
| "coord": coord | |
| } | |
| self.history.append(entry) | |
| print(f" [MEMORY]: Recorded {speaker} turn @ {coord}") | |
| return coord | |
| def get_recent_context(self, depth=5): | |
| return "\n".join([f"{h['speaker']}: {h['text']}" for h in self.history[-depth:]]) | |
| if __name__ == "__main__": | |
| mem = ConversationalMemory() | |
| mem.record_turn("User", "Hello TGI.") | |
| mem.record_turn("TGI", "Greetings. I am operational.") | |
| print(f"\nContext Retrieval:\n{mem.get_recent_context()}") | |