Spaces:
Build error
Build error
Create memory.py
Browse files
memory.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json, os
|
| 2 |
+
|
| 3 |
+
class MemoryManager:
|
| 4 |
+
def __init__(self, agent_id):
|
| 5 |
+
self.agent_id = agent_id
|
| 6 |
+
self.memory_file = f"memory_{agent_id}.json"
|
| 7 |
+
if not os.path.exists(self.memory_file):
|
| 8 |
+
with open(self.memory_file, "w") as f:
|
| 9 |
+
json.dump({"history": []}, f)
|
| 10 |
+
|
| 11 |
+
def save(self, event):
|
| 12 |
+
with open(self.memory_file, "r") as f:
|
| 13 |
+
data = json.load(f)
|
| 14 |
+
data["history"].append(event)
|
| 15 |
+
with open(self.memory_file, "w") as f:
|
| 16 |
+
json.dump(data, f)
|
| 17 |
+
|
| 18 |
+
def recall(self, n=5):
|
| 19 |
+
with open(self.memory_file, "r") as f:
|
| 20 |
+
data = json.load(f)
|
| 21 |
+
return data["history"][-n:]
|
| 22 |
+
|
| 23 |
+
class ContextGraph:
|
| 24 |
+
def __init__(self):
|
| 25 |
+
self.graph_file = "context_graph.json"
|
| 26 |
+
if not os.path.exists(self.graph_file):
|
| 27 |
+
with open(self.graph_file, "w") as f:
|
| 28 |
+
json.dump({"nodes": [], "links": []}, f)
|
| 29 |
+
|
| 30 |
+
def add_link(self, source, target, info):
|
| 31 |
+
with open(self.graph_file, "r") as f:
|
| 32 |
+
data = json.load(f)
|
| 33 |
+
data["links"].append({"source": source, "target": target, "info": info})
|
| 34 |
+
with open(self.graph_file, "w") as f:
|
| 35 |
+
json.dump(data, f)
|