Back to Blog

GraphRAG: Knowledge Graphs for Enhanced Retrieval-Augmented Generation

10 min read

Retrieval-Augmented Generation (RAG) typically uses vector databases to locate text chunks based on semantic similarity. While vector databases are highly efficient for local query-matching, they struggle with global reasoning tasks, such as summarizing themes across an entire document set or answering questions that require multiple logical connections ("multi-hop reasoning").

GraphRAG solves these limitations by combining structured Knowledge Graphs with standard RAG pipelines.

GraphRAG: Entity & Community RetrievalGraphRAG: Entity & Community Retrieval

The Architecture of GraphRAG

GraphRAG structures unstructured text by transforming it into a semantic network:

  1. Entity-Relation Extraction: An LLM scans text documents to extract entities (e.g., people, organizations, concepts) and their relationships.
  2. Knowledge Graph Construction: These entities and relationships are structured as nodes and edges. The graph representation is represented as:
G = (V, E)

where V is the set of entity vertices and E is the set of relationship edges. 3. Community Detection: Graph clustering algorithms (e.g., Leiden or Louvain) partition the graph into hierarchical clusters of related entities. 4. Community Summarization: The LLM writes summaries for each community cluster, creating pre-computed abstract representations of different sectors of the corpus.

Local Search vs. Global Search in GraphRAG

GraphRAG supports two main search configurations:

  • Local Search: Used for queries regarding specific entities. The system finds vector-matched entities and traverses adjacent edges to retrieve related entities, relationships, and source text.
  • Global Search: Used for queries regarding aggregate themes. The system retrieves pre-computed summaries across all community partitions at a specified hierarchical level and synthesizes a global response.

Python Code for Simple Graph Construction

Here is an example using NetworkX to structure a basic knowledge graph extracted from text:

import networkx as nx

# Create a graph
G = nx.Graph()

# Add nodes with attributes
G.add_node("RAG", type="concept", description="Retrieval-Augmented Generation")
G.add_node("GraphRAG", type="framework", description="Graph-based RAG")
G.add_node("Knowledge Graph", type="data_structure", description="Structured node-edge network")

# Add edges with attributes
G.add_edge("GraphRAG", "RAG", relation="extends")
G.add_edge("GraphRAG", "Knowledge Graph", relation="integrates")

# Query the graph
print("Entities connected to GraphRAG:")
for neighbor in G.neighbors("GraphRAG"):
    print(f"- {neighbor} via relationship: {G['GraphRAG'][neighbor]['relation']}")

The Benefits of GraphRAG

  • Multi-Hop Reasoning: By traversing edges, the model can connect disparate facts that are located in separate sections of a corpus.
  • Improved Context Integrity: By using community summaries, GraphRAG avoids missing context that might otherwise be split across vector chunk boundaries.
  • Structured Traceability: Answers are directly traceable to specific paths in the knowledge graph, making evaluation and auditing easier.

Conclusion

GraphRAG marks a significant step forward from standard vector-only RAG pipelines. By combining vector matching with the relational power of Knowledge Graphs, it enables deep, global understanding and precise multi-hop reasoning over large, unstructured document collections.