Spaces:
Sleeping
Sleeping
Create memory.py
Browse files
memory.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
class MemoryManager:
|
| 5 |
+
def __init__(self, agent_id):
|
| 6 |
+
self.agent_id = agent_id
|
| 7 |
+
self.file_path = f"memory_{agent_id}.json"
|
| 8 |
+
self._init_memory()
|
| 9 |
+
|
| 10 |
+
def _init_memory(self):
|
| 11 |
+
if not os.path.exists(self.file_path):
|
| 12 |
+
with open(self.file_path, "w") as f:
|
| 13 |
+
json.dump({"history": []}, f)
|
| 14 |
+
|
| 15 |
+
def save(self, data):
|
| 16 |
+
with open(self.file_path, "r") as f:
|
| 17 |
+
memory = json.load(f)
|
| 18 |
+
memory["history"].append(data)
|
| 19 |
+
with open(self.file_path, "w") as f:
|
| 20 |
+
json.dump(memory, f, indent=2)
|
| 21 |
+
os.sync() # Ensures the file actually saves on Hugging Face
|