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
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 | |
find_n_indirect_clusters
find_n_indirect_clusters(n: int, relationship_condition: Callable[[Relationship], bool] = lambda _: True, depth_limit: int = 3) -> List[Set[Node]]
Return n indirect clusters of nodes in the knowledge graph based on a relationship condition. Optimized for large datasets by using an adjacency index for lookups and limiting path exploration relative to n.
A cluster represents a path through the graph. For example, if A -> B -> C -> D exists in the graph, then {A, B, C, D} forms a cluster. If there's also a path A -> B -> C -> E, it forms a separate cluster.
The method returns a list of up to n sets, where each set contains nodes forming a complete path from a starting node to a leaf node or a path segment up to depth_limit nodes long. The result may contain fewer than n clusters if the graph is very sparse or if there aren't enough nodes to form n distinct clusters.
To maximize diversity in the results: 1. Random starting nodes are selected 2. Paths from each starting node are grouped 3. Clusters are selected in round-robin fashion from each group until n unique clusters are found 4. Duplicate clusters are eliminated 5. When a superset cluster is found (e.g., {A,B,C,D}), any existing subset clusters (e.g., {A,B,C}) are removed to avoid redundancy
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Target number of clusters to return. Must be at least 1. Should return n clusters unless the graph is extremely sparse. |
required |
relationship_condition
|
Callable[[Relationship], bool]
|
A function that takes a Relationship and returns a boolean, by default lambda _: True |
lambda _: True
|
depth_limit
|
int
|
Maximum depth for path exploration, by default 3. Must be at least 2 to form clusters by definition. |
3
|
Returns:
| Type | Description |
|---|---|
List[Set[Node]]
|
A list of sets, where each set contains nodes that form a cluster. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If depth_limit < 2, n < 1, or no relationships match the provided condition. |
Source code in src/ragas/testset/graph.py
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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |
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. |