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_clusters
find_clusters(relationship_condition: Callable[[Relationship], bool] = lambda _: True) -> List[Set[Node]]
Finds clusters of nodes in the knowledge graph based on a relationship condition.
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. |