GraphRAG
Graph-based Retrieval-Augmented Generation that combines Knowledge Graphs with vector search for more accurate, relationship-aware AI responses.
Core Concepts
GraphRAG (Graph-based Retrieval-Augmented Generation) is an advanced approach to RAG that leverages Knowledge Graphs to provide more accurate, contextual, and relationship-aware information retrieval for Large Language Models.
How GraphRAG Works
Traditional RAG uses vector similarity search to find relevant documents. GraphRAG extends this by:
- Building a Knowledge Graph from source documents
- Extracting entities and their relationships
- Combining vector search with graph traversal
- Following relationship chains to gather comprehensive context
- Providing structured context to the LLM
// GraphRAG query flow
const context = await trustgraph.retrieve({
query: "Impact of AI on healthcare industry",
strategy: "graph-rag",
// Vector component
vectorTopK: 10,
vectorThreshold: 0.7,
// Graph component
graphDepth: 3,
relationshipTypes: ["impacts", "enables", "requires"],
// Fusion
fusion: "weighted",
weights: { vector: 0.4, graph: 0.6 }
});
// Returns rich, structured context
{
entities: [
{ id: "ai", type: "Technology", properties: {...} },
{ id: "healthcare", type: "Industry", properties: {...} }
],
relationships: [
{ from: "ai", type: "transforms", to: "healthcare", confidence: 0.92 }
],
paths: [
["ai", "enables", "diagnosis_tools", "improve", "patient_outcomes"]
],
vectorMatches: [...]
}
Traditional RAG vs GraphRAG
Traditional RAG (Vector-Only)
// Vector similarity search only
const chunks = await vectorSearch(query, topK: 5);
// Returns: ["chunk1 text", "chunk2 text", ...]
const response = await llm.generate({
context: chunks.join("\n"),
query: query
});
// Limitations:
// - No understanding of relationships
// - May miss connected information
// - Cannot reason over multiple hops
// - Limited context assembly
GraphRAG (Graph + Vector)
// Hybrid retrieval
const context = await graphrag.retrieve(query);
// Returns structured knowledge
{
entities: [...], // Identified entities
relationships: [...], // How they connect
paths: [...], // Reasoning chains
vectorChunks: [...], // Similar text
fusedContext: "..." // Optimally combined
}
const response = await llm.generate({
context: context.fusedContext,
query: query
});
// Advantages:
// ✅ Understands relationships
// ✅ Multi-hop reasoning
// ✅ Comprehensive context
// ✅ Reduced hallucinations
Key Benefits
1. Relationship-Aware Context
GraphRAG understands how entities relate:
// Query: "How does company X's acquisition affect market Y?"
// Vector RAG might return:
// - Document mentioning company X
// - Document mentioning market Y
// - Document about acquisitions
// (No connection between them)
// GraphRAG returns:
{
entities: ["Company X", "Startup Z", "Market Y"],
relationships: [
"Company X" → "acquired" → "Startup Z",
"Startup Z" → "operates_in" → "Market Y",
"Company X" → "expands_to" → "Market Y" (inferred)
],
reasoning: "Company X acquired Startup Z, which operates in Market Y,
therefore Company X now has presence in Market Y"
}
2. Multi-Hop Reasoning
Follow relationship chains:
// Question: "What technologies might impact Company A's competitor B?"
// GraphRAG traverses:
// Technology → adopted_by → Company A
// Company A → competes_with → Company B
// Therefore: Technology → (indirect impact) → Company B
const analysis = await graphrag.reason({
startEntity: "Technology X",
targetEntity: "Company B",
maxHops: 3,
includeInferences: true
});
3. Reduced Hallucinations
Graph structure grounds LLM responses:
// GraphRAG validation
const response = await graphrag.generate({
query: "Describe relationship between X and Y",
groundingMode: "strict" // Only reference graph entities
});
// Automatic validation
if (response.containsHallucination) {
console.warn("Entity not in graph:", response.hallucinated);
// Can reject or flag response
}
4. Better Context Assembly
Intelligently combine graph and vector results:
// Fusion strategies
const context = await graphrag.retrieve({
query: "...",
fusion: "rrf" // Reciprocal Rank Fusion
});
// Or weighted combination
const context = await graphrag.retrieve({
query: "...",
fusion: "weighted",
weights: {
vector: 0.3, // 30% from vector similarity
graph: 0.5, // 50% from graph relationships
pagerank: 0.2 // 20% from entity importance
}
});
Implementation Approaches
TrustGraph Native GraphRAG
import { TrustGraph } from "@trustgraph/sdk";
const tg = new TrustGraph({ endpoint: "..." });
// Ingest documents - automatically builds graph
await tg.ingest({
sources: ["documents/"],
graphConfig: {
extractEntities: true,
buildRelationships: true,
linkingStrategy: "semantic"
}
});
// Query with GraphRAG
const result = await tg.query({
query: "Your question here",
strategy: "graph-rag", // Enables GraphRAG
vectorTopK: 20,
graphDepth: 3
});
DIY GraphRAG Stack
# Build your own GraphRAG
from langchain import VectorStore, GraphStore, LLM
# 1. Extract entities and relationships
entities = extract_entities(documents)
relationships = extract_relationships(documents)
# 2. Build Knowledge Graph
graph = GraphStore()
graph.add_entities(entities)
graph.add_relationships(relationships)
# 3. Create embeddings
vector_store = VectorStore()
vector_store.add_documents(documents)
# 4. Retrieve with both
def graphrag_retrieve(query):
# Vector search
vector_results = vector_store.similarity_search(query, k=10)
# Graph traversal
relevant_entities = graph.find_entities(query)
subgraph = graph.expand(relevant_entities, depth=3)
# Combine
context = combine_results(vector_results, subgraph)
return context
# 5. Generate response
context = graphrag_retrieve(query)
response = llm.generate(context=context, query=query)
Use Cases
1. Complex Q&A Systems
// Financial analysis
const analysis = await graphrag.query({
query: "How do rising interest rates affect Company X's suppliers?",
strategy: "graph-rag"
});
// Returns path:
// Interest Rates → affects → Banking Sector
// Banking Sector → impacts → Company X (loans)
// Company X → supplied_by → Supplier Y, Z
// Therefore: Interest Rates → (indirect) → Suppliers
2. Research Literature Review
// Academic research
const literature = await graphrag.query({
query: "Papers connecting quantum computing to drug discovery",
strategy: "graph-rag",
graphDepth: 4 // Follow longer chains
});
// Finds connections:
// Quantum Computing → enables → Molecular Simulation
// Molecular Simulation → used_in → Drug Discovery
3. Customer Intelligence
// Business intelligence
const insights = await graphrag.query({
query: "Which customers might be interested in Product X?",
strategy: "graph-rag"
});
// Traverses:
// Product X → similar_to → Product Y
// Product Y → purchased_by → Customer A, B, C
// Customer A → has_need → Feature Z
// Product X → provides → Feature Z
4. Compliance and Risk
// Regulatory analysis
const compliance = await graphrag.query({
query: "How does new regulation R affect our operations?",
strategy: "graph-rag"
});
// Maps:
// Regulation R → applies_to → Industry Sector
// Industry Sector → includes → Company Operations
// Operations → uses → Process P
// Process P → requires_compliance → Action A
Performance Considerations
Latency
Traditional RAG:
- Vector search: 10-50ms
- Total: 10-50ms
GraphRAG:
- Vector search: 10-50ms
- Graph traversal: 20-100ms
- Fusion: 5-10ms
- Total: 35-160ms
Trade-off: Slightly higher latency for significantly better accuracy
Accuracy Improvements
Benchmarks show GraphRAG improvements:
- Multi-hop questions: +40-60% accuracy
- Relationship queries: +50-70% accuracy
- Complex reasoning: +30-50% accuracy
- Hallucination reduction: -30-50%
Best Practices
1. Graph Quality Matters
// Invest in good entity extraction and linking
await trustgraph.ingest({
sources: ["docs/"],
graphConfig: {
extractEntities: true,
linkingStrategy: "semantic",
deduplication: "strict",
validation: "enabled",
ontology: "domain-schema.ttl" // Use domain ontology
}
});
2. Tune Fusion Parameters
// Experiment with fusion weights
const results_a = await graphrag.retrieve({
fusion: "weighted",
weights: { vector: 0.4, graph: 0.6 } // Graph-heavy
});
const results_b = await graphrag.retrieve({
fusion: "weighted",
weights: { vector: 0.7, graph: 0.3 } // Vector-heavy
});
// A/B test to find optimal balance
3. Control Graph Depth
// Balance depth vs. performance
const shallow = await graphrag.retrieve({
graphDepth: 1 // Fast, local context
});
const deep = await graphrag.retrieve({
graphDepth: 4 // Comprehensive, slower
});
// Typical: depth 2-3 for most queries
4. Use Graph Constraints
// Constrain traversal for focused results
const result = await graphrag.retrieve({
query: "...",
graphConstraints: {
relationshipTypes: ["employs", "manages", "reports_to"],
entityTypes: ["Person", "Department"],
excludeRelations: ["located_in"] // Ignore irrelevant
}
});
Comparison with Other Approaches
| Approach | Relationships | Multi-hop | Accuracy | Latency |
|---|---|---|---|---|
| Vector RAG | No | No | Good | Fast (10-50ms) |
| GraphRAG | Yes | Yes | Excellent | Medium (50-200ms) |
| Pure Graph | Yes | Yes | Good | Fast (20-100ms) |
| Hybrid (Best) | Yes | Yes | Best | Acceptable |
Limitations
- Graph Construction Overhead: Initial graph building takes time
- Graph Quality Dependency: Poor entity extraction = poor results
- Higher Latency: Graph traversal adds latency
- Storage Requirements: Graph + vectors requires more storage
- Complexity: More complex to implement and tune
Future Directions
- Dynamic Graphs: Real-time graph updates
- Multi-modal GraphRAG: Images, audio, video in graphs
- Federated GraphRAG: Distributed graph query
- Learning Graphs: Graphs that improve from feedback
- Probabilistic Edges: Uncertainty in relationships
See Also
Examples
- •Using GraphRAG to answer 'How does Company A's partnership with B affect market C?' by traversing relationship chains
- •Combining vector similarity search with graph traversal for comprehensive context retrieval
- •Building GraphRAG systems that understand multi-hop relationships between entities