Skip to content

Advanced Examples - Graph Extraction

Component-level customization and development

The graph extraction is done by GraphGenerator class - a wrapper over 3 core components. This section explains how to customize and extend individual components.

Prerequisites: Read the core documentation guides first: Architecture Overview, Indexing Pipeline, Query System, and Data Flow Examples.

Note: You can pass your own implementations of these components provided they follow the same protocol.


EntityRelationshipExtractor

Purpose: Generates a networkx graph for every text_unit in the dataframe.

Process: This component extracts entities and relationships from individual text units using LLM analysis.

See the Entity Relationship Extraction notebook for a detailed interactive guide on this component.

Customization Options: - Custom entity types and extraction prompts - Different LLM models for extraction - Custom output parsing logic - Domain-specific entity recognition rules


GraphsMerger

Purpose: Merges all the graphs generated by EntityRelationshipExtractor into a single graph.

Process: The merging is done by creating a list of descriptions for nodes and edges that appear in multiple text units.

Key Features: - Consolidates duplicate entities across text units - Aggregates relationship descriptions - Maintains source traceability to original text units - Calculates entity importance (degree) and relationship rankings

Customization Options: - Custom entity matching logic - Different description aggregation strategies - Custom ranking algorithms - Graph sanitization rules


EntityRelationshipDescriptionSummarizer

Purpose: Every node and edge in the merged graph has multiple descriptions from different text units. This component uses LLM to summarize them into clear, unified descriptions.

Process: Takes the aggregated descriptions and creates clean, comprehensive summaries for entities and relationships.

Key Features: - LLM-powered description synthesis - Maintains factual accuracy across sources - Removes redundancy while preserving key information - Creates consistent description format

Customization Options: - Custom summarization prompts - Different LLM models for summarization - Domain-specific description templates - Quality validation rules


Complete GraphGenerator Usage

Bringing it all together: See the Graph Generator notebook for a complete interactive example.

Code Overview:

from langchain_graphrag.indexing.graph_generation import (
    GraphGenerator,
    EntityRelationshipExtractor,
    GraphsMerger,
    EntityRelationshipDescriptionSummarizer
)

# Create components
extractor = EntityRelationshipExtractor.build_default(llm=your_llm)
merger = GraphsMerger()
summarizer = EntityRelationshipDescriptionSummarizer.build_default(llm=your_llm)

# Create graph generator
graph_generator = GraphGenerator(
    er_extractor=extractor,
    graphs_merger=merger,
    er_description_summarizer=summarizer
)

# Process text units
merged_graph, summarized_graph = graph_generator.run(text_units_df)


Additional Resources

These components provide the foundation for knowledge graph construction. Understanding their individual roles helps with system customization and troubleshooting.

Documentation Index
Return to the complete documentation structure