-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.py
More file actions
45 lines (35 loc) · 1.36 KB
/
Copy pathstore.py
File metadata and controls
45 lines (35 loc) · 1.36 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
from __future__ import annotations
from collections import defaultdict
from memory_engine.schema import MemoryEdge, MemoryNode
class MemoryStore:
def __init__(self) -> None:
self._nodes: dict[str, MemoryNode] = {}
self._adjacency: dict[str, list[MemoryEdge]] = defaultdict(list)
def add_node(self, node: MemoryNode) -> None:
self._nodes[node.id] = node
def add_edge(self, edge: MemoryEdge) -> None:
self._adjacency[edge.from_id].append(edge)
if edge.bidirectional:
self._adjacency[edge.to_id].append(
MemoryEdge(
from_id=edge.to_id,
to_id=edge.from_id,
edge_type=edge.edge_type,
weight=edge.weight,
confidence=edge.confidence,
bidirectional=True,
source_ref=edge.source_ref,
)
)
def get_node(self, node_id: str) -> MemoryNode:
return self._nodes[node_id]
def nodes(self) -> list[MemoryNode]:
return list(self._nodes.values())
def edges(self) -> list[MemoryEdge]:
return [
edge
for adjacency in self._adjacency.values()
for edge in adjacency
]
def neighbors(self, node_id: str) -> list[MemoryEdge]:
return list(self._adjacency.get(node_id, []))