feat: Adds changes to cognee graph part

This commit is contained in:
hajdul88 2025-08-13 13:07:04 +02:00
parent 91b9c11cd0
commit 6dcd59c73c
4 changed files with 32 additions and 18 deletions

View file

@ -2,8 +2,7 @@ import time
from cognee.shared.logging_utils import get_logger from cognee.shared.logging_utils import get_logger
from typing import List, Dict, Union, Optional, Type from typing import List, Dict, Union, Optional, Type
from cognee.exceptions import InvalidValueError from cognee.modules.graph.exceptions import EntityNotFoundError, EntityAlreadyExistsError, InvalidDimensionsError
from cognee.modules.graph.exceptions import EntityNotFoundError, EntityAlreadyExistsError
from cognee.infrastructure.databases.graph.graph_db_interface import GraphDBInterface from cognee.infrastructure.databases.graph.graph_db_interface import GraphDBInterface
from cognee.modules.graph.cognee_graph.CogneeGraphElements import Node, Edge from cognee.modules.graph.cognee_graph.CogneeGraphElements import Node, Edge
from cognee.modules.graph.cognee_graph.CogneeAbstractGraph import CogneeAbstractGraph from cognee.modules.graph.cognee_graph.CogneeAbstractGraph import CogneeAbstractGraph
@ -66,8 +65,7 @@ class CogneeGraph(CogneeAbstractGraph):
node_name: Optional[List[str]] = None, node_name: Optional[List[str]] = None,
) -> None: ) -> None:
if node_dimension < 1 or edge_dimension < 1: if node_dimension < 1 or edge_dimension < 1:
raise InvalidValueError(message="Dimensions must be positive integers") raise InvalidDimensionsError()
try: try:
import time import time

View file

@ -1,8 +1,6 @@
import numpy as np import numpy as np
from typing import List, Dict, Optional, Any, Union from typing import List, Dict, Optional, Any, Union
from cognee.modules.graph.exceptions import InvalidDimensionsError, DimensionOutOfRangeError
from cognee.exceptions import InvalidValueError
class Node: class Node:
""" """
@ -24,7 +22,7 @@ class Node:
self, node_id: str, attributes: Optional[Dict[str, Any]] = None, dimension: int = 1 self, node_id: str, attributes: Optional[Dict[str, Any]] = None, dimension: int = 1
): ):
if dimension <= 0: if dimension <= 0:
raise InvalidValueError(message="Dimension must be a positive integer") raise InvalidDimensionsError()
self.id = node_id self.id = node_id
self.attributes = attributes if attributes is not None else {} self.attributes = attributes if attributes is not None else {}
self.attributes["vector_distance"] = float("inf") self.attributes["vector_distance"] = float("inf")
@ -58,9 +56,7 @@ class Node:
def is_node_alive_in_dimension(self, dimension: int) -> bool: def is_node_alive_in_dimension(self, dimension: int) -> bool:
if dimension < 0 or dimension >= len(self.status): if dimension < 0 or dimension >= len(self.status):
raise InvalidValueError( raise DimensionOutOfRangeError(dimension=dimension, max_index=len(self.status) - 1)
message=f"Dimension {dimension} is out of range. Valid range is 0 to {len(self.status) - 1}."
)
return self.status[dimension] == 1 return self.status[dimension] == 1
def add_attribute(self, key: str, value: Any) -> None: def add_attribute(self, key: str, value: Any) -> None:
@ -110,7 +106,7 @@ class Edge:
dimension: int = 1, dimension: int = 1,
): ):
if dimension <= 0: if dimension <= 0:
raise InvalidValueError(message="Dimensions must be a positive integer.") InvalidDimensionsError()
self.node1 = node1 self.node1 = node1
self.node2 = node2 self.node2 = node2
self.attributes = attributes if attributes is not None else {} self.attributes = attributes if attributes is not None else {}
@ -120,9 +116,7 @@ class Edge:
def is_edge_alive_in_dimension(self, dimension: int) -> bool: def is_edge_alive_in_dimension(self, dimension: int) -> bool:
if dimension < 0 or dimension >= len(self.status): if dimension < 0 or dimension >= len(self.status):
raise InvalidValueError( raise DimensionOutOfRangeError(dimension=dimension, max_index=len(self.status) - 1)
message=f"Dimension {dimension} is out of range. Valid range is 0 to {len(self.status) - 1}."
)
return self.status[dimension] == 1 return self.status[dimension] == 1
def add_attribute(self, key: str, value: Any) -> None: def add_attribute(self, key: str, value: Any) -> None:

View file

@ -7,4 +7,6 @@ This module defines a set of exceptions for handling various graph errors
from .exceptions import ( from .exceptions import (
EntityNotFoundError, EntityNotFoundError,
EntityAlreadyExistsError, EntityAlreadyExistsError,
InvalidDimensionsError,
DimensionOutOfRangeError,
) )

View file

@ -1,8 +1,8 @@
from cognee.exceptions import CogneeApiError from cognee.exceptions import CogneeValidationError
from fastapi import status from fastapi import status
class EntityNotFoundError(CogneeApiError): class EntityNotFoundError(CogneeValidationError):
"""Database returns nothing""" """Database returns nothing"""
def __init__( def __init__(
@ -14,7 +14,7 @@ class EntityNotFoundError(CogneeApiError):
super().__init__(message, name, status_code) super().__init__(message, name, status_code)
class EntityAlreadyExistsError(CogneeApiError): class EntityAlreadyExistsError(CogneeValidationError):
"""Conflict detected, like trying to create a resource that already exists""" """Conflict detected, like trying to create a resource that already exists"""
def __init__( def __init__(
@ -24,3 +24,23 @@ class EntityAlreadyExistsError(CogneeApiError):
status_code=status.HTTP_409_CONFLICT, status_code=status.HTTP_409_CONFLICT,
): ):
super().__init__(message, name, status_code) super().__init__(message, name, status_code)
class InvalidDimensionsError(CogneeValidationError):
def __init__(
self,
name: str = "InvalidDimensionsError",
status_code: int = status.HTTP_400_BAD_REQUEST,
):
message = "Dimensions must be positive integers."
super().__init__(message, name, status_code)
class DimensionOutOfRangeError(CogneeValidationError):
def __init__(
self,
dimension: int,
max_index: int,
name: str = "DimensionOutOfRangeError",
status_code: int = status.HTTP_400_BAD_REQUEST,
):
message = f"Dimension {dimension} is out of range. Valid range is 0 to {max_index}."
super().__init__(message, name, status_code)