Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Explodinggradients Ragas KnowledgeGraph Enrichment Methods

From Leeroopedia


Knowledge Sources Domains Last Updated
explodinggradients/ragas LLM Evaluation, Test Data Generation, Community Detection, Graph Traversal 2026-02-10

Overview

Description

The KnowledgeGraph Enrichment Methods comprise the find_indirect_clusters() and find_n_indirect_clusters() methods on the KnowledgeGraph class, plus the standalone get_child_nodes() and get_parent_nodes() utility functions. Together, these provide the mechanisms for discovering thematic groupings of nodes and traversing the graph hierarchy -- both essential for assembling context for multi-hop test question generation.

Usage

  • Cluster discovery is used by query synthesizers (e.g., MultiHopAbstractQuerySynthesizer, MultiHopSpecificQuerySynthesizer) to find groups of related nodes that can form the context for multi-hop questions.
  • Graph traversal is used to navigate the document-chunk hierarchy when assembling context for both single-hop and multi-hop scenarios.

Code Reference

Source Location

Component File Lines
find_indirect_clusters src/ragas/testset/graph.py L276-469
find_n_indirect_clusters src/ragas/testset/graph.py L471-645
get_child_nodes src/ragas/testset/graph_queries.py L6-38
get_parent_nodes src/ragas/testset/graph_queries.py L41-73

Signature

# On KnowledgeGraph class
def find_indirect_clusters(
    self,
    relationship_condition: Callable[[Relationship], bool] = lambda _: True,
    depth_limit: int = 3,
) -> List[Set[Node]]:
    ...

def find_n_indirect_clusters(
    self,
    n: int,
    relationship_condition: Callable[[Relationship], bool] = lambda _: True,
    depth_limit: int = 3,
) -> List[Set[Node]]:
    ...

# Standalone functions
def get_child_nodes(
    node: Node,
    graph: KnowledgeGraph,
    level: int = 1,
) -> List[Node]:
    ...

def get_parent_nodes(
    node: Node,
    graph: KnowledgeGraph,
    level: int = 1,
) -> List[Node]:
    ...

Import

from ragas.testset.graph import KnowledgeGraph
from ragas.testset.graph_queries import get_child_nodes, get_parent_nodes

I/O Contract

find_indirect_clusters

Parameter Type Default Description
relationship_condition Callable[[Relationship], bool] lambda _: True Filter function to select which relationships to consider for clustering
depth_limit int 3 Maximum number of edges in a path; must be at least 2
Return Type Description
List[Set[Node]] List of node sets, each representing a cluster of indirectly related nodes forming a path

find_n_indirect_clusters

Parameter Type Default Description
n int (required) Target number of clusters to return; must be at least 1
relationship_condition Callable[[Relationship], bool] lambda _: True Filter function to select which relationships to consider
depth_limit int 3 Maximum path depth; must be at least 2
Return Type Description
List[Set[Node]] Up to n diverse node clusters with superset preference and subset deduplication

get_child_nodes

Parameter Type Default Description
node Node (required) The node whose children to retrieve
graph KnowledgeGraph (required) The knowledge graph containing the node
level int 1 Maximum depth of child traversal
Return Type Description
List[Node] Child nodes reachable via "child"-type relationships up to the specified level

get_parent_nodes

Parameter Type Default Description
node Node (required) The node whose parents to retrieve
graph KnowledgeGraph (required) The knowledge graph containing the node
level int 1 Maximum depth of parent traversal
Return Type Description
List[Node] Parent nodes reachable via reverse "child"-type relationships up to the specified level

Usage Examples

Finding Indirect Clusters

from ragas.testset.graph import KnowledgeGraph

# Assume kg is an enriched KnowledgeGraph with semantic relationships
kg = KnowledgeGraph.load("enriched_graph.json")

# Find all indirect clusters with default settings
clusters = kg.find_indirect_clusters()
print(f"Found {len(clusters)} clusters")
for cluster in clusters[:3]:
    print(f"  Cluster with {len(cluster)} nodes")

# Find clusters filtered by relationship type
semantic_clusters = kg.find_indirect_clusters(
    relationship_condition=lambda rel: rel.type == "semantic_overlap",
    depth_limit=2,
)

Finding a Target Number of Clusters

# Find exactly 10 diverse clusters (or fewer if the graph is too sparse)
clusters = kg.find_n_indirect_clusters(
    n=10,
    relationship_condition=lambda rel: rel.type == "cosine_similarity",
    depth_limit=3,
)
print(f"Got {len(clusters)} clusters (requested 10)")

Traversing the Node Hierarchy

from ragas.testset.graph import KnowledgeGraph, Node, NodeType
from ragas.testset.graph_queries import get_child_nodes, get_parent_nodes

kg = KnowledgeGraph.load("enriched_graph.json")

# Find a document node
doc_nodes = [n for n in kg.nodes if n.type == NodeType.DOCUMENT]
doc = doc_nodes[0]

# Get immediate children (level=1)
children = get_child_nodes(doc, kg, level=1)
print(f"Document has {len(children)} direct children")

# Get children up to 2 levels deep
deep_children = get_child_nodes(doc, kg, level=2)
print(f"Document has {len(deep_children)} children within 2 levels")

# Get parent of a chunk
if children:
    chunk = children[0]
    parents = get_parent_nodes(chunk, kg, level=1)
    print(f"Chunk's parent: {parents[0].type}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment