-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmind_palace_api.py
More file actions
81 lines (71 loc) · 2.96 KB
/
mind_palace_api.py
File metadata and controls
81 lines (71 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
from typing import Dict, Any, List
class MindPalaceAPI:
"""
Exposes the ChromaDB vector store as nodes and edges for 3D visualization.
Extracts semantic relationships between stored memories.
"""
def __init__(self, vector_memory):
self.vmem = vector_memory
def get_graph_data(self, tenant_id: int = 1, limit: int = 100) -> Dict[str, Any]:
"""
Retrieves recent memories and attempts to link them based on simple semantic
or timestamp proximity to form a procedural graph.
"""
nodes = []
edges = []
# If no ChromaDB, use fallback
if not self.vmem.active:
raw_entries = self.vmem.fallback_store[-limit:]
else:
try:
# We fetch a chunk of recent documents
res = self.vmem.collection.get(
where={"tenant_id": str(tenant_id)},
limit=limit
)
raw_entries = []
if res and res.get("documents"):
for i in range(len(res["documents"])):
raw_entries.append({
"id": res["ids"][i],
"text": res["documents"][i],
"metadata": res["metadatas"][i] if res.get("metadatas") else {}
})
except Exception as e:
print(f"Mind Palace retrieval error: {e}")
raw_entries = []
# Build nodes
for idx, entry in enumerate(raw_entries):
category = entry.get("metadata", {}).get("category", "unknown")
nodes.append({
"id": entry["id"],
"label": entry["text"][:50] + "..." if len(entry["text"]) > 50 else entry["text"],
"full_text": entry["text"],
"group": category,
"val": 10 if category == "knowledge" else 5
})
# Simple chronological edges: link node to previous node to form a chain
if idx > 0:
edges.append({
"source": raw_entries[idx-1]["id"],
"target": entry["id"],
"value": 1
})
# Simple semantic edges: if they share the same topic
for i, source in enumerate(raw_entries):
src_topic = source.get("metadata", {}).get("topic")
if not src_topic: continue
for j, target in enumerate(raw_entries):
if i >= j: continue
tgt_topic = target.get("metadata", {}).get("topic")
if tgt_topic == src_topic:
edges.append({
"source": source["id"],
"target": target["id"],
"value": 5
})
return {
"nodes": nodes,
"links": edges
}