Spaces:
Running
Running
File size: 17,366 Bytes
e70c280 | 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | """
File: /graph.py
Created Date: Sunday October 19th 2025
Author: Christian Nonis <alch.infoemail@gmail.com>
-----
Last Modified: Monday February 2nd 2026 10:02:37 pm
Modified By: Christian Nonis <alch.infoemail@gmail.com>
-----
"""
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Literal, Optional, Tuple, TypedDict
from src.constants.kg import (
IdentificationParams,
Node,
NodeDict,
Predicate,
PredicateDict,
SearchEntitiesResult,
SearchRelationshipsResult,
)
from src.adapters.interfaces.embeddings import VectorStoreClient
class PredicateWithFlowKey(TypedDict):
predicate_uuid: str
flow_key: str
class GraphClient(ABC):
"""
Abstract base class for graph clients.
"""
@property
@abstractmethod
def graphdb_description(self) -> str:
"""
Get the description of the graph database.
"""
raise NotImplementedError("graphdb_description method not implemented")
@property
@abstractmethod
def graphdb_type(self) -> str:
"""
Get the type of graph database.
"""
raise NotImplementedError("graphdb_type method not implemented")
@abstractmethod
def execute_operation(self, operation: str, brain_id: str) -> Any:
"""
Execute a generic graph operation.
"""
raise NotImplementedError("execute_operation method not implemented")
@abstractmethod
def add_nodes(
self,
nodes: list[Node],
brain_id: str,
identification_params: Optional[dict] = None,
metadata: Optional[dict] = None,
) -> list[Node] | str:
"""
Add nodes to the graph.
"""
raise NotImplementedError("add_nodes method not implemented")
@abstractmethod
def add_relationship(
self,
subject: Node,
predicate: Predicate,
to_object: Node,
brain_id: str,
) -> str:
"""
Add a relationship between two nodes to the graph.
"""
raise NotImplementedError("add_relationship method not implemented")
@abstractmethod
def search_graph(
self,
nodes: list[Node],
brain_id: str,
) -> list[Node]:
"""
Search the graph for nodes and 1 degree relationships.
"""
raise NotImplementedError("search_graph method not implemented")
@abstractmethod
def node_text_search(self, text: str, brain_id: str) -> list[Node]:
"""
Search the graph for nodes by partial text match into the name of the nodes.
"""
raise NotImplementedError("node_text_search method not implemented")
@abstractmethod
def get_nodes_by_uuid(
self,
uuids: list[str],
brain_id: str,
with_relationships: Optional[bool] = False,
relationships_depth: Optional[int] = 1,
relationships_type: Optional[list[str]] = None,
preferred_labels: Optional[list[str]] = None,
) -> list[dict]:
"""
Get nodes by their UUIDs with optional relationships and preferred labels.
"""
raise NotImplementedError("get_nodes_by_uuid method not implemented")
@abstractmethod
def get_graph_entities(self, brain_id: str) -> list[str]:
"""
Get the entities of the graph.
"""
raise NotImplementedError("get_graph_entities method not implemented")
@abstractmethod
def get_graph_relationships(self, brain_id: str) -> list[str]:
"""
Retrieve the relationship types present in the specified graph.
Parameters:
brain_id (str): Identifier of the brain/graph to query.
Returns:
list[str]: Relationship type names present in the graph.
"""
raise NotImplementedError("get_graph_relationships method not implemented")
@abstractmethod
def get_by_uuid(
self,
uuid: str,
brain_id: str,
) -> Node:
"""
Retrieve a node identified by its UUID from the specified brain.
Parameters:
uuid (str): The node's UUID.
brain_id (str): Identifier of the brain/graph to query.
Returns:
Node: The node matching the given UUID.
"""
raise NotImplementedError("get_by_uuid method not implemented")
@abstractmethod
def get_by_uuids(
self,
uuids: list[str],
brain_id: str,
) -> list[Node]:
"""
Get nodes by their UUIDs.
"""
raise NotImplementedError("get_by_uuids method not implemented")
@abstractmethod
def get_by_identification_params(
self,
identification_params: IdentificationParams,
brain_id: str,
entity_types: Optional[list[str]] = None,
) -> Node:
"""
Get a node by its identification params.
"""
raise NotImplementedError("get_by_identification_params method not implemented")
@abstractmethod
def get_neighbors(
self,
nodes: list[Node | str],
brain_id: str,
same_type_only: bool = False,
limit: int | None = None,
of_types: Optional[list[str]] = None,
) -> Dict[str, List[Tuple[Predicate, Node]]]:
"""
Get the neighbors of a node with their relationships.
"""
raise NotImplementedError("get_neighbors method not implemented")
@abstractmethod
def get_event_centric_neighbors(
self,
nodes: list[Node | str],
brain_id: str,
) -> List[Tuple[Node, Predicate, Node, Predicate, Node]]:
"""
Get the event-centric neighbors of a node.
"""
raise NotImplementedError("get_event_centric_neighbors method not implemented")
@abstractmethod
def get_node_with_rel_by_uuid(
self,
rel_ids_with_node_ids: list[tuple[str, str]],
brain_id: str,
) -> list[dict]:
"""
Get the node with the relationships by their UUIDs.
"""
raise NotImplementedError("get_node_with_rel_by_uuid method not implemented")
@abstractmethod
def get_neighbor_node_tuples(
self,
a_uuid: str,
b_uuids: list[str],
brain_id: str,
) -> list[Tuple[Node, Predicate, Node]]:
"""
Get the neighbor node tuples by their UUIDs.
"""
raise NotImplementedError("get_neighbor_node_tuples method not implemented")
@abstractmethod
def get_connected_nodes(
self,
brain_id: str,
node: Optional[Node] = None,
uuids: Optional[list[str]] = None,
limit: Optional[int] = 10,
with_labels: Optional[list[str]] = None,
) -> list[Tuple[Node, Predicate, Node]]:
"""
Get the connected nodes by their UUIDs.
"""
raise NotImplementedError("get_connected_nodes method not implemented")
@abstractmethod
def search_relationships(
self,
brain_id: str,
limit: int = 10,
skip: int = 0,
relationship_types: Optional[list[str]] = None,
from_node_labels: Optional[list[str]] = None,
to_node_labels: Optional[list[str]] = None,
relationship_uuids: Optional[list[str]] = None,
query_text: Optional[str] = None,
query_search_target: Optional[
str
] = "all", # Search into the relationship desc or node names or relationship desc
) -> SearchRelationshipsResult:
"""
Search the relationships of the graph.
"""
raise NotImplementedError("search_relationships method not implemented")
@abstractmethod
def search_entities(
self,
brain_id: str,
limit: int = 10,
skip: int = 0,
node_labels: Optional[list[str]] = None,
node_uuids: Optional[list[str]] = None,
query_text: Optional[str] = None,
) -> SearchEntitiesResult:
"""
Search the entities of the graph.
"""
raise NotImplementedError("search_entities method not implemented")
@abstractmethod
def deprecate_relationship(
self,
subject: Node,
predicate: Predicate,
object: Node,
brain_id: str,
) -> Tuple[Node, Predicate, Node] | None:
"""
Deprecate a relationship from the graph.
"""
raise NotImplementedError("deprecate_relationship method not implemented")
@abstractmethod
def update_properties(
self,
uuid: str,
updating: Literal["node", "relationship"],
brain_id: str,
new_properties: dict,
properties_to_remove: list[str],
) -> Node | Predicate | None:
"""
Update properties on a node or relationship in the graph.
Parameters:
uuid (str): UUID of the target node or relationship.
updating (Literal["node", "relationship"]): Which entity type to update.
brain_id (str): Identifier of the graph/brain containing the entity.
new_properties (dict): Properties to set or overwrite on the entity.
properties_to_remove (list[str]): Property keys to remove from the entity.
Returns:
Node | Predicate | None: The updated node or relationship if changes were applied, `None` if the entity was not found or no update occurred.
"""
raise NotImplementedError("update_properties method not implemented")
@abstractmethod
def get_graph_relationship_types(self, brain_id: str) -> list[str]:
"""
List unique relationship types present in the specified graph.
Parameters:
brain_id (str): Identifier of the graph/brain to query.
Returns:
relationship_types (list[str]): Relationship type names present in the graph.
"""
raise NotImplementedError("get_graph_relationship_types method not implemented")
@abstractmethod
def get_graph_node_types(self, brain_id: str) -> list[str]:
"""
Return all unique node types (labels) present in the graph for the given brain.
Parameters:
brain_id (str): Identifier of the brain/graph to query.
Returns:
list[str]: A list of node type names (labels) present in the specified graph.
"""
raise NotImplementedError("get_graph_node_types method not implemented")
@abstractmethod
def get_graph_node_properties(self, brain_id: str) -> list[str]:
"""
Retrieve all unique node property keys present in the graph for the given brain.
Parameters:
brain_id (str): Identifier of the graph/brain to query.
Returns:
list[str]: Unique property key names found on nodes in the specified graph.
"""
raise NotImplementedError("get_graph_node_properties method not implemented")
@abstractmethod
def update_node(
self,
uuid: str,
brain_id: str,
new_name: Optional[str] = None,
new_description: Optional[str] = None,
new_labels: Optional[list[str]] = None,
new_properties: Optional[dict] = None,
properties_to_remove: Optional[list[str]] = None,
) -> Node | None:
"""
Update a node's identifying fields, labels, and properties.
Parameters:
uuid (str): UUID of the node to update.
brain_id (str): Identifier of the graph or brain containing the node.
new_name (Optional[str]): New name for the node.
new_description (Optional[str]): New description for the node.
new_labels (Optional[list[str]]): New labels to replace existing labels.
new_properties (Optional[dict]): Properties to add or update on the node.
properties_to_remove (Optional[list[str]]): Property keys to remove from the node.
Returns:
Node | None: The updated node, or `None` if no matching node exists.
"""
raise NotImplementedError("update_node method not implemented")
def get_schema(self, brain_id: str) -> dict:
"""
Get the schema/ontology of the graph.
"""
raise NotImplementedError("get_schema method not implemented")
@abstractmethod
def get_2nd_degree_hops(
self,
from_: List[str],
flattened: bool,
vector_store_adapter: VectorStoreClient,
brain_id: str,
) -> Dict[str, List[Tuple[Predicate, Node, List[Tuple[Predicate, Node]]]]]:
"""
Return second-degree hops for each starting node.
Parameters:
from_ (List[str]): List of starting node UUIDs or identifiers to expand.
flattened (bool): If true, return lightweight results with non-essential metadata removed.
vector_store_adapter (VectorStoreClient): Adapter used to resolve or enrich nodes via the vector store.
brain_id (str): Identifier of the brain/graph to query.
Returns:
hops_by_start (Dict[str, List[Tuple[Predicate, Node, List[Tuple[Predicate, Node]]]]]):
Mapping from each starting node identifier in `from_` to a list of first-degree entries.
Each first-degree entry is a tuple of:
- `Predicate`: relationship from the starting node to the first-hop node,
- `Node`: the first-hop node,
- `List[Tuple[Predicate, Node]]`: list of second-degree hops where each item is a `(Predicate, Node)` tuple representing the relationship and the second-hop node.
"""
raise NotImplementedError("get_2nd_degree_hops not implemented")
@abstractmethod
def check_node_existence(
self,
uuid: str,
name: str,
labels: list[str],
brain_id: str,
) -> bool:
"""
Determine whether a node with the given identity exists in the specified brain.
Parameters:
uuid (str): UUID of the node to check.
name (str): Name of the node to match.
labels (list[str]): List of labels/types the node must have.
brain_id (str): Identifier of the brain (graph) to search within.
Returns:
true if a node matching the provided uuid, name, and labels exists in the brain, false otherwise.
"""
raise NotImplementedError("check_node_existence method not implemented")
@abstractmethod
def get_neighborhood(
self, node: Node | str, depth: int, brain_id: str
) -> list[dict]:
"""
Retrieve the neighborhood of a node up to a specified depth.
Parameters:
node (Node | str): The starting node or its UUID.
depth (int): Maximum distance from the starting node to include (1 = immediate neighbors).
brain_id (str): Identifier of the brain/graph to query.
Returns:
list[dict]: A nested list of dictionaries representing the neighborhood; each dictionary represents a node and contains its neighbors as nested structures up to the requested depth.
"""
raise NotImplementedError("get_neighborhood method not implemented")
@abstractmethod
def get_nexts_by_flow_key(
self, predicates: list[PredicateWithFlowKey], brain_id: str
) -> Dict[str, List[Tuple[Node, Predicate, Node]]]:
"""
Retrieve the next connected node tuple(s) for a relationship identified by a flow key, grouped by the predicate UUID.
Parameters:
predicates: list[PredicateWithFlowKey]: A list of predicates with their flow keys.
brain_id (str): Identifier of the brain/graph to query.
Returns:
Dict[str, List[Tuple[Node, Predicate, Node]]]: A dictionary mapping predicate UUIDs to lists of (subject node, predicate, object node) tuples that are the next nodes matching the provided flow key; empty dictionary if none are found for any predicate UUID.
"""
raise NotImplementedError("get_nexts_by_flow_key method not implemented")
@abstractmethod
def get_triples_by_uuid(
self, uuids: list[str], brain_id: str
) -> List[Tuple[Node, Predicate, Node]]:
"""
Get triples by its UUIDs.
"""
raise NotImplementedError("get_triples_by_uuid method not implemented")
@abstractmethod
def remove_nodes(self, uuids: list[str], brain_id: str) -> list[Node]:
"""
Remove nodes from the graph.
"""
raise NotImplementedError("remove_nodes method not implemented")
@abstractmethod
def remove_relationships(
self,
relationships: list[Tuple[NodeDict, PredicateDict, NodeDict]],
brain_id: str,
) -> list[Tuple[Node, Predicate, Node]]:
"""
Remove relationships from the graph.
"""
raise NotImplementedError("remove_relationships method not implemented")
@abstractmethod
def list_relationships(
self,
brain_id: str,
subject: str,
object: str,
) -> list[Tuple[Node, Predicate, Node]]:
"""
List the relationships between the subject and object.
"""
raise NotImplementedError("list_relationships method not implemented")
|