Spaces:
Sleeping
Sleeping
Create context_graph.py
Browse files- context_graph.py +26 -0
context_graph.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
class ContextMemoryGraph:
|
| 5 |
+
def __init__(self, file_path="context_graph.json"):
|
| 6 |
+
self.file_path = file_path
|
| 7 |
+
self._init_graph()
|
| 8 |
+
|
| 9 |
+
def _init_graph(self):
|
| 10 |
+
if not os.path.exists(self.file_path):
|
| 11 |
+
with open(self.file_path, "w") as f:
|
| 12 |
+
json.dump({"context_links": {}}, f)
|
| 13 |
+
|
| 14 |
+
def link_context(self, agent_id, key, value):
|
| 15 |
+
with open(self.file_path, "r") as f:
|
| 16 |
+
graph = json.load(f)
|
| 17 |
+
if agent_id not in graph["context_links"]:
|
| 18 |
+
graph["context_links"][agent_id] = {}
|
| 19 |
+
graph["context_links"][agent_id][key] = value
|
| 20 |
+
with open(self.file_path, "w") as f:
|
| 21 |
+
json.dump(graph, f, indent=2)
|
| 22 |
+
|
| 23 |
+
def get_context(self, agent_id):
|
| 24 |
+
with open(self.file_path, "r") as f:
|
| 25 |
+
graph = json.load(f)
|
| 26 |
+
return graph["context_links"].get(agent_id, {})
|