Intermediate8 min read29 of 40

Graph RAG - Knowledge Graph Enhanced Retrieval

Enhance RAG with knowledge graphs to capture entity relationships and improve contextual understanding. Learn how to build graph-based retrieval systems.

Read first:Simple RAG

Overview

Graph RAG enhances traditional retrieval-augmented generation by incorporating knowledge graphs to capture relationships between entities. This enables more contextually aware responses by understanding how concepts connect.

Pattern #2 in the 37 RAG Patterns Collection

Architecture

graph TB
    A[User Query] --> B[Entity Extraction]
    B --> C[Graph Traversal]
    C --> D[Retrieve Connected Nodes]
    D --> E[Combine Context]
    E --> F[LLM Generation]
    F --> G[Response]
    
    H[Knowledge Graph] --> C
    I[Vector Store] --> D
    
    style A fill:#f9f,stroke:#333
    style F fill:#9f9,stroke:#333

Key Features

1. Entity Recognition

  • Extract entities from queries and documents
  • Identify relationships between entities
  • Build semantic connections

2. Graph Traversal

  • Navigate knowledge graph structure
  • Find relevant connected information
  • Multi-hop reasoning

3. Hybrid Retrieval

  • Combine graph-based and vector search
  • Leverage both structure and semantics
  • Enhanced context assembly

Implementation

Step 1: Build Knowledge Graph

python
from opensearchpy import OpenSearch
import boto3

# Initialize graph database
bedrock = boto3.client('bedrock-runtime')
opensearch = OpenSearch([{'host': 'your-domain', 'port': 443}])

# Extract entities and relationships
def extract_entities(text):
    prompt = f"Extract entities and relationships from: {text}"
    response = bedrock.invoke_model(
        modelId="meta.llama3-70b-instruct-v1:0",
        body=json.dumps({"prompt": prompt})
    )
    return parse_entities(response)

# Build graph
def build_graph(documents):
    graph = {}
    for doc in documents:
        entities = extract_entities(doc)
        for entity in entities:
            graph[entity['name']] = {
                'type': entity['type'],
                'relationships': entity['relationships']
            }
    return graph

Step 2: Query with Graph Context

python
def graph_rag_query(query, graph, vector_store):
    # Extract entities from query
    query_entities = extract_entities(query)
    
    # Traverse graph for related entities
    related = []
    for entity in query_entities:
        if entity in graph:
            # Multi-hop traversal
            neighbors = graph[entity]['relationships']
            related.extend(neighbors)
    
    # Vector search with graph context
    vector_results = vector_store.search(query, top_k=5)
    
    # Combine results
    context = combine_graph_and_vector(related, vector_results)
    
    # Generate response
    response = bedrock.invoke_model(
        modelId="meta.llama3-70b-instruct-v1:0",
        body=json.dumps({
            "prompt": f"Context: {context}\n\nQuestion: {query}"
        })
    )
    return response

Complete Notebook

🚀 Run the full implementation:

📓 Graph RAG AWS Notebook

The notebook includes:

  • Complete AWS setup with Bedrock & OpenSearch
  • Entity extraction pipeline
  • Knowledge graph construction
  • Multi-hop traversal implementation
  • Performance benchmarks
  • Cost analysis

When to Use

Best For

  • Domain-specific applications where entity relationships matter
  • Multi-hop reasoning requiring connected information
  • Complex queries spanning multiple concepts
  • Research tools exploring relationships

Avoid When

  • Simple Q&A tasks (use Simple RAG)
  • Unstructured, relationship-free content
  • Real-time requirements (graph traversal adds latency)

Performance

MetricValue
Accuracy+15% vs Simple RAG
Latency~500ms
Cost/query$0.12
ComplexityMedium

Related Papers

Next Steps

  1. Try it: Clone and run the notebook
  2. Combine: Try Fusion Retrieval (#3) for hybrid search
  3. Optimize: Add Reranking (#4) for better results
  4. Scale: Explore Hierarchical RAG (#22)

Part of: 37 RAG Patterns Collection
See also: Visual Architecture Guide

Go Deeper With Live Instruction

Covered in depth in our llm engineering program.