Graph
NodeType
Bases: str
, Enum
Enumeration of node types in the knowledge graph.
Currently supported node types are: UNKNOWN, DOCUMENT, CHUNK
Node
Bases: BaseModel
Represents a node in the knowledge graph.
Attributes:
Name | Type | Description |
---|---|---|
id |
UUID
|
Unique identifier for the node. |
properties |
dict
|
Dictionary of properties associated with the node. |
type |
NodeType
|
Type of the node. |
add_property
Adds a property to the node.
Raises:
Type | Description |
---|---|
ValueError
|
If the property already exists. |
Source code in src/ragas/testset/graph.py
get_property
Retrieves a property value by key.
Notes
The key is case-insensitive.
Relationship
Bases: BaseModel
Represents a relationship between two nodes in a knowledge graph.
Attributes:
Name | Type | Description |
---|---|---|
id |
(UUID, optional)
|
Unique identifier for the relationship. Defaults to a new UUID. |
type |
str
|
The type of the relationship. |
source |
Node
|
The source node of the relationship. |
target |
Node
|
The target node of the relationship. |
bidirectional |
(bool, optional)
|
Whether the relationship is bidirectional. Defaults to False. |
properties |
(dict, optional)
|
Dictionary of properties associated with the relationship. Defaults to an empty dict. |
get_property
KnowledgeGraph
dataclass
KnowledgeGraph(nodes: List[Node] = list(), relationships: List[Relationship] = list())
Represents a knowledge graph containing nodes and relationships.
Attributes:
Name | Type | Description |
---|---|---|
nodes |
List[Node]
|
List of nodes in the knowledge graph. |
relationships |
List[Relationship]
|
List of relationships in the knowledge graph. |
add
add(item: Union[Node, Relationship])
Adds a node or relationship to the knowledge graph.
Raises:
Type | Description |
---|---|
ValueError
|
If the item type is not Node or Relationship. |
Source code in src/ragas/testset/graph.py
save
Saves the knowledge graph to a JSON file.
Source code in src/ragas/testset/graph.py
load
classmethod
load(path: Union[str, Path]) -> KnowledgeGraph
Loads a knowledge graph from a path.
Source code in src/ragas/testset/graph.py
find_indirect_clusters
find_indirect_clusters(relationship_condition: Callable[[Relationship], bool] = lambda _: True, depth_limit: int = 3) -> List[Set[Node]]
Finds indirect clusters of nodes in the knowledge graph based on a relationship condition. Here if A -> B -> C -> D, then A, B, C, and D form a cluster. If there's also a path A -> B -> C -> E, it will form a separate cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
relationship_condition
|
Callable[[Relationship], bool]
|
A function that takes a Relationship and returns a boolean, by default lambda _: True |
lambda _: True
|
Returns:
Type | Description |
---|---|
List[Set[Node]]
|
A list of sets, where each set contains nodes that form a cluster. |
Source code in src/ragas/testset/graph.py
remove_node
remove_node(node: Node, inplace: bool = True) -> Optional[KnowledgeGraph]
Removes a node and its associated relationships from the knowledge graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Node
|
The node to be removed from the knowledge graph. |
required |
inplace
|
bool
|
If True, modifies the knowledge graph in place. If False, returns a modified copy with the node removed. |
True
|
Returns:
Type | Description |
---|---|
KnowledgeGraph or None
|
Returns a modified copy of the knowledge graph if |
Raises:
Type | Description |
---|---|
ValueError
|
If the node is not present in the knowledge graph. |
Source code in src/ragas/testset/graph.py
find_direct_clusters
find_direct_clusters(relationship_condition: Callable[[Relationship], bool] = lambda _: True) -> Dict[Node, List[Set[Node]]]
Finds direct clusters of nodes in the knowledge graph based on a relationship condition. Here if A->B, and A->C, then A, B, and C form a cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
relationship_condition
|
Callable[[Relationship], bool]
|
A function that takes a Relationship and returns a boolean, by default lambda _: True |
lambda _: True
|
Returns:
Type | Description |
---|---|
List[Set[Node]]
|
A list of sets, where each set contains nodes that form a cluster. |