Tpayne101 commited on
Commit
486a373
·
verified ·
1 Parent(s): b49e0e0

Create memory.py

Browse files
Files changed (1) hide show
  1. memory.py +21 -0
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