Advanced RAG
Graph RAG â Structured Knowledge for Better Reasoning
Knowledge graphs provide structured, relational knowledge that complements unstructured text retrieval. Graph RAG combines graph-based reasoning with LLMs for multi-hop, complex question answering.
- Entity Extraction â Identify entities and relationships from text
- Graph Construction â Build knowledge graphs from documents
- Community Detection â Find thematic clusters for global queries
The best retrieval understands not just what was said, but how things are connected.
Graph RAG and Knowledge Graphs
Traditional RAG retrieves text chunks based on semantic similarity. Graph RAG retrieves based on relationships between entities, enabling multi-hop reasoning and global understanding that flat text retrieval cannot achieve.
Knowledge Graph Construction
Entity and Relationship Extraction
from openai import OpenAI
def extract_entities_and_relations(text, model="gpt-4"):
prompt = f"""Extract all entities and relationships from this text.
Text: {text}
Return JSON format:
{{
"entities": [
{{"name": "Entity Name", "type": "PERSON|ORG|LOCATION|CONCEPT", "description": "Brief description"}}
],
"relations": [
{{"source": "Entity1", "target": "Entity2", "relation": "relationship type", "weight": 0.0-1.0}}
]
}}"""
response = OpenAI().chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
Graph Construction
import networkx as nx
class KnowledgeGraph:
def __init__(self):
self.graph = nx.DiGraph()
def add_document(self, doc_id, text):
"""Extract and add entities from a document."""
extraction = extract_entities_and_relations(text)
for entity in extraction["entities"]:
self.graph.add_node(
entity["name"],
type=entity["type"],
description=entity["description"],
documents=[doc_id]
)
for relation in extraction["relations"]:
if self.graph.has_edge(relation["source"], relation["target"]):
# Update weight
self.graph[relation["source"]][relation["target"]]["weight"] += relation["weight"]
else:
self.graph.add_edge(
relation["source"],
relation["target"],
relation=relation["relation"],
weight=relation["weight"]
)
def find_path(self, entity1, entity2, max_hops=3):
"""Find shortest path between two entities."""
try:
path = nx.shortest_path(self.graph, entity1, entity2)
return path[:max_hops + 1]
except nx.NetworkXNoPath:
return None
def get_entity_context(self, entity_name, max_hops=2):
"""Get all related entities and their descriptions."""
if entity_name not in self.graph:
return ""
context = []
for neighbor in nx.neighbors(self.graph, entity_name):
edge_data = self.graph[entity_name][neighbor]
node_data = self.graph.nodes[neighbor]
context.append(
f"{entity_name} --[{edge_data['relation']}]--> {neighbor} "
f"({node_data.get('description', '')})"
)
return "\n".join(context)
Community Detection for Global Queries
import community as community_louvain
def detect_communities(knowledge_graph):
"""Detect communities in the knowledge graph."""
partition = community_louvain.best_partition(knowledge_graph.graph.to_undirected())
communities = {}
for node, comm_id in partition.items():
if comm_id not in communities:
communities[comm_id] = []
communities[comm_id].append(node)
return communities
def summarize_community(community_nodes, knowledge_graph, llm):
"""Generate a summary for a community."""
node_descriptions = []
for node in community_nodes:
desc = knowledge_graph.graph.nodes[node].get("description", "")
node_descriptions.append(f"{node}: {desc}")
prompt = f"""Summarize the following group of related entities and their relationships:
{chr(10).join(node_descriptions)}
Provide a concise summary of the key themes and relationships:"""
response = llm.generate(prompt)
return response
Graph-Based Retrieval Strategies
Local Retrieval
def local_retrieval(query, knowledge_graph, llm):
# Extract entities from query
query_entities = extract_entities_from_query(query, llm)
context_parts = []
for entity in query_entities:
# Get entity neighborhood
context = knowledge_graph.get_entity_context(entity, max_hops=2)
if context:
context_parts.append(context)
return "\n\n".join(context_parts)
Global Retrieval
def global_retrieval(query, communities, community_summaries, llm):
# Identify relevant communities
relevant_comms = []
for comm_id, summary in community_summaries.items():
relevance = compute_relevance(query, summary, llm)
if relevance > 0.5:
relevant_comms.append((comm_id, summary))
# Combine summaries
context = "\n\n".join([s for _, s in relevant_comms])
return context
Hybrid Retrieval
def hybrid_graph_retrieval(query, knowledge_graph, text_index, llm):
"""Combine graph and text retrieval."""
# Graph retrieval for entities
graph_context = local_retrieval(query, knowledge_graph, llm)
# Text retrieval for supporting details
text_context = text_index.search(query, top_k=5)
# Combine and rank
combined_context = f"Graph Knowledge:\n{graph_context}\n\nSupporting Text:\n{text_context}"
return combined_context
Graph RAG vs Standard RAG
| Feature | Standard RAG | Graph RAG |
|---|---|---|
| Retrieval basis | Semantic similarity | Entity relationships |
| Multi-hop reasoning | Limited | Excellent |
| Global queries | Poor | Excellent |
| Entity relationships | Implicit | Explicit |
| Indexing complexity | Low | High |
| Query latency | Low | Medium |
| Best for | Specific facts | Complex reasoning |
Practice Exercises
-
Graph Construction: Build a knowledge graph from 10 Wikipedia articles about a specific topic. Extract entities, relationships, and identify communities.
-
Multi-Hop Query: Using your knowledge graph, answer a 3-hop question: "Entity A is related to Entity B, which is located in Entity C. What companies operate in C?"
-
Community Summarization: Detect communities in your graph and generate summaries. How well do community summaries capture the main themes?
-
Hybrid Retrieval: Compare standard RAG vs Graph RAG on a set of queries that require multi-hop reasoning. What is the performance difference?
Key Takeaways
What to Learn Next
-> RAG System Design Advanced RAG architecture and design patterns.
-> Retrieval-Augmented Generation RAG fundamentals and basic implementation.
-> Multi-Modal RAG RAG with images, audio, and other modalities.
-> Self-RAG and Adaptive Retrieval When to retrieve and when to rely on parametric knowledge.
-> Graph Neural Networks Learning on graph-structured data.
-> Agentic RAG Systems Agent-based approaches to retrieval and generation.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions