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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path where the JSON file should be saved. |
required |
Notes
The file is saved using UTF-8 encoding to ensure proper handling of Unicode characters across different platforms.
Source code in src/ragas/testset/graph.py
load
classmethod
load(path: Union[str, Path]) -> KnowledgeGraph
Loads a knowledge graph from a path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to the JSON file containing the knowledge graph. |
required |
Returns:
| Type | Description |
|---|---|
KnowledgeGraph
|
The loaded knowledge graph. |
Notes
The file is read using UTF-8 encoding to ensure proper handling of Unicode characters across different platforms.
Source code in src/ragas/testset/graph.py
get_node_by_id
get_node_by_id(node_id: Union[UUID, str]) -> Optional[Node]
Retrieves a node by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
UUID
|
The ID of the node to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Node or None
|
The node with the specified ID, or None if not found. |
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. Uses Leiden algorithm for community detection and identifies unique paths within each cluster.
NOTE: "indirect clusters" as used in the method name are "groups of nodes that are not directly connected but share a common relationship through other nodes", while the Leiden algorithm is a "clustering" algorithm that defines neighborhoods of nodes based on their connections -- these definitions of "cluster" are NOT equivalent.
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
|
depth_limit
|
int
|
The maximum depth of relationships (number of edges) to consider for clustering, by default 3. |
3
|
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
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 | |
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_two_nodes_single_rel
find_two_nodes_single_rel(relationship_condition: Callable[[Relationship], bool] = lambda _: True) -> List[Tuple[Node, Relationship, Node]]
Finds nodes in the knowledge graph based on a relationship condition. (NodeA, NodeB, Rel) triples are considered as multi-hop nodes.
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, Relationship, Node]]
|
A list of sets, where each set contains two nodes and a relationship forming a multi-hop node. |