Tpayne101 commited on
Commit
540c668
·
verified ·
1 Parent(s): f062f85

Create context_memory.py

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