Spaces:
Sleeping
Sleeping
Update context_graph.py
Browse files- context_graph.py +16 -22
context_graph.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
|
| 4 |
class ContextGraph:
|
| 5 |
def __init__(self, file_path="context_graph.json"):
|
|
@@ -7,42 +6,37 @@ class ContextGraph:
|
|
| 7 |
self._init_graph()
|
| 8 |
|
| 9 |
def _init_graph(self):
|
| 10 |
-
# Create the graph file if it doesn't exist
|
| 11 |
if not os.path.exists(self.file_path):
|
| 12 |
with open(self.file_path, "w") as f:
|
| 13 |
json.dump({"context_links": {}}, f)
|
| 14 |
|
| 15 |
-
def link_context(self, agent_id, key, value):
|
| 16 |
-
# Load the current context graph
|
| 17 |
with open(self.file_path, "r") as f:
|
| 18 |
graph = json.load(f)
|
| 19 |
|
| 20 |
-
# Add the new link under the correct agent ID
|
| 21 |
if agent_id not in graph["context_links"]:
|
| 22 |
graph["context_links"][agent_id] = {}
|
| 23 |
-
graph["context_links"][agent_id]
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Save the updated graph
|
| 26 |
with open(self.file_path, "w") as f:
|
| 27 |
json.dump(graph, f, indent=2)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def query_context(self, agent_id, keyword):
|
| 32 |
-
# Search for any stored memory entries that contain the keyword
|
| 33 |
with open(self.file_path, "r") as f:
|
| 34 |
graph = json.load(f)
|
| 35 |
|
| 36 |
agent_data = graph["context_links"].get(agent_id, {})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
results = [
|
| 38 |
-
f"{k}: {v}" for k, v in
|
| 39 |
-
if keyword.lower() in k.lower() or keyword.lower() in v.lower()
|
| 40 |
]
|
| 41 |
-
|
| 42 |
-
return results if results else ["No matching context found."]
|
| 43 |
-
|
| 44 |
-
def get_context(self, agent_id):
|
| 45 |
-
# Simple alias to maintain backward compatibility with AgentCore
|
| 46 |
-
with open(self.file_path, "r") as f:
|
| 47 |
-
graph = json.load(f)
|
| 48 |
-
return graph["context_links"].get(agent_id, {})
|
|
|
|
| 1 |
+
import json, os
|
|
|
|
| 2 |
|
| 3 |
class ContextGraph:
|
| 4 |
def __init__(self, file_path="context_graph.json"):
|
|
|
|
| 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, category, key, value):
|
|
|
|
| 14 |
with open(self.file_path, "r") as f:
|
| 15 |
graph = json.load(f)
|
| 16 |
|
|
|
|
| 17 |
if agent_id not in graph["context_links"]:
|
| 18 |
graph["context_links"][agent_id] = {}
|
| 19 |
+
if category not in graph["context_links"][agent_id]:
|
| 20 |
+
graph["context_links"][agent_id][category] = {}
|
| 21 |
+
|
| 22 |
+
graph["context_links"][agent_id][category][key] = value
|
| 23 |
|
|
|
|
| 24 |
with open(self.file_path, "w") as f:
|
| 25 |
json.dump(graph, f, indent=2)
|
| 26 |
|
| 27 |
+
def query_context(self, agent_id, category=None, keyword=None):
|
|
|
|
|
|
|
|
|
|
| 28 |
with open(self.file_path, "r") as f:
|
| 29 |
graph = json.load(f)
|
| 30 |
|
| 31 |
agent_data = graph["context_links"].get(agent_id, {})
|
| 32 |
+
if category:
|
| 33 |
+
data = agent_data.get(category, {})
|
| 34 |
+
else:
|
| 35 |
+
# Merge all categories
|
| 36 |
+
data = {k: v for c in agent_data.values() for k, v in c.items()}
|
| 37 |
+
|
| 38 |
results = [
|
| 39 |
+
f"{k}: {v}" for k, v in data.items()
|
| 40 |
+
if not keyword or keyword.lower() in k.lower() or keyword.lower() in v.lower()
|
| 41 |
]
|
| 42 |
+
return results or ["No context found."]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|