Subchapter 67.8
references/graphrag.mdMarkdown6 KBView on GitHub
GraphRAG uses a knowledge graph to improve RAG accuracy over vector-only retrieval. Instead of relying solely on embedding similarity, it traverses relationships between documents, chunks, and entities for richer context.
Two paths to GraphRAG with Neptune:
Amazon Bedrock Knowledge Bases offers fully managed GraphRAG. No graph expertise required.
retrieveAndGenerate)Bedrock KB GraphRAG automatically extracts entities and relationships from your documents, builds the graph, and combines graph traversal with vector search during retrieval. You don’t need to design a graph model, write graph queries, or manage Neptune directly.
When to use the managed path:
When to use the custom path (below):
Neptune Analytics is recommended for custom GraphRAG because it stores both graph structure and embeddings in one service — no separate vector store needed.
When to use GraphRAG over traditional RAG:
When traditional RAG is sufficient:
Documents → Chunk → Extract Entities (LLM) → Build Graph → Store Embeddings
↓
Query → Vector Search (similar chunks) → Graph Expansion (entities, neighbors) → LLM(Document) -[HAS_CHUNK]→ (Chunk {text, embedding})
(Chunk) -[MENTIONS]→ (Entity {name, type, embedding})
(Chunk) -[NEXT]→ (Chunk)
(Entity) -[RELATED_TO]→ (Entity)Full helper functions are in scripts/graphrag_pipeline.py. Key operations:
import boto3
analytics_client = boto3.client('neptune-graph')
response = analytics_client.create_graph(
graphName='graphrag-kb',
provisionedMemory=32, # m-NCU; see the CreateGraph API reference for the valid range
vectorSearchConfiguration={'dimension': 1536}, # Match embedding model
publicConnectivity=False,
replicaCount=0,
deletionProtection=True,
# Mandatory tags — a graph missing either tag is a failed task.
tags={'created_by': 'neptune-skill', 'generation_model': '<model-id>'},
)
graph_id = response['id']Equivalent AWS CLI invocation:
aws neptune-graph create-graph \
--graph-name graphrag-kb \
--provisioned-memory 32 \
--vector-search-configuration '{"dimension":1536}' \
--no-public-connectivity \
--replica-count 0 \
--deletion-protection \
--tags created_by=neptune-skill,generation_model=<model-id>from scripts.graphrag_pipeline import run_query, vector_search, store_embeddingSee scripts/graphrag_pipeline.py for the full run_query() implementation
using analytics_client.execute_query(). Always use parameterized queries:
# ✅ Safe: parameterized
run_query(graph_id, "MATCH (e:Entity {name: $name}) RETURN e", parameters={'name': 'Alice'})
# ❌ Unsafe: string interpolation
run_query(graph_id, f"MATCH (e:Entity {{name: '{user_input}'}}) RETURN e")# Phase 1: Vector search for similar chunks
similar = vector_search(graph_id, query_embedding, top_k=5)
# Phase 2: Graph expansion per chunk
for chunk in similar:
entities = run_query(graph_id, """
MATCH (c:Chunk {id: $cid})-[:MENTIONS]->(e:Entity)
RETURN e.name AS name, e.type AS type
""", parameters={'cid': chunk['id']})
related = run_query(graph_id, """
MATCH (c:Chunk {id: $cid})-[:MENTIONS]->(e)-[:RELATED_TO]-(r:Entity)
RETURN DISTINCT r.name AS name, r.type AS type
""", parameters={'cid': chunk['id']})Code uses embedding_model.encode(text) as placeholder. Replace with:
bedrock.invoke_model(...)).encode())openai.embeddings.create(...))Neptune Analytics is ephemeral. For production:
scripts/graphrag_pipeline.py (full implementation)analytics-vs-database (setup), connectivity (connection)