Chapter 43
๐ Knowledge Graph RAG with Verifiable Citations
๐ Knowledge Graph RAG with Verifiable Citations
A Streamlit application demonstrating how Knowledge Graph-based Retrieval-Augmented Generation (RAG) provides multi-hop reasoning with fully verifiable source attribution.
๐ฏ What Makes This Different?
Traditional vector-based RAG finds similar text chunks, but struggles with:
- Questions requiring information from multiple documents
- Complex reasoning chains
- Providing verifiable sources for each claim
Knowledge Graph RAG solves these by:
- Building a structured graph of entities and relationships from documents
- Traversing connections to find related information (multi-hop reasoning)
- Tracking provenance so every claim links back to its source
โจ Features
| Feature | Description |
|---|---|
| ๐ Multi-hop Reasoning | Traverse entity relationships to answer complex questions |
| ๐ Verifiable Citations | Every claim includes source document and text |
| ๐ง Reasoning Trace | See exactly how the answer was derived |
| ๐ Fully Local | Uses Ollama for LLM, Neo4j for graph storage |
๐ Quick Start
Prerequisites
-
Ollama - Local LLM inference
bash# Install from https://ollama.ai ollama pull llama3.2 -
Neo4j - Knowledge graph database
bash# Using Docker docker run -d \ --name neo4j \ -p 7474:7474 -p 7687:7687 \ -e NEO4J_AUTH=neo4j/password \ neo4j:latest
Installation
# Clone and navigate
cd knowledge_graph_rag_citations
# Install dependencies
pip install -r requirements.txt
# Run the app
streamlit run knowledge_graph_rag.py๐ How It Works
Step 1: Document โ Knowledge Graph
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Document โ โโโบ โ LLM Extraction โ โโโบ โ Knowledge Graph โ
โ (Text/PDF) โ โ (Entities+Rels) โ โ (Neo4j) โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโThe LLM extracts:
- Entities: People, organizations, concepts, technologies
- Relationships: How entities connect (e.g., "works_for", "created", "uses")
- Provenance: Source document and chunk for each extraction
Step 2: Query โ Multi-hop Traversal
โโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโ
โ Query โ โโโบ โ Find Start โ โโโบ โ Traverse โ โโโบ โ Context โ
โ โ โ Entities โ โ Relations โ โ + Sourcesโ
โโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโStep 3: Answer โ Verified Citations
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ Context โ โโโบ โ Generate โ โโโบ โ Answer with โ
โ + Sources โ โ Answer โ โ [1][2] Citationsโ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโ
โ Citation Details โ
โ โข Source Doc โ
โ โข Source Text โ
โ โข Reasoning Path โ
โโโโโโโโโโโโโโโโโโโโ๐ฅ๏ธ Usage Example
1. Add a Document
Paste or select a sample document. The system extracts entities and relationships:
Document: "GraphRAG was developed by Microsoft Research.
Darren Edge led the project..."
Extracted:
โโโ Entity: GraphRAG (TECHNOLOGY)
โโโ Entity: Microsoft Research (ORGANIZATION)
โโโ Entity: Darren Edge (PERSON)
โโโ Relationship: Darren Edge --[WORKS_FOR]--> Microsoft Research2. Ask a Question
Question: "Who developed GraphRAG and what organization are they from?"3. Get Verified Answer
Answer: GraphRAG was developed by researchers at Microsoft Research [1],
with Darren Edge leading the project [2].
Citations:
[1] Source: AI Research Paper
Text: "GraphRAG is a technique developed by Microsoft Research..."
[2] Source: AI Research Paper
Text: "...introduced by researchers including Darren Edge..."๐ง Configuration
| Setting | Default | Description |
|---|---|---|
| Neo4j URI | bolt://localhost:7687 | Neo4j connection string |
| Neo4j User | neo4j | Database username |
| Neo4j Password | - | Database password |
| LLM Model | llama3.2 | Ollama model for extraction/generation |
๐๏ธ Architecture
knowledge_graph_rag_citations/
โโโ knowledge_graph_rag.py # Main Streamlit application
โโโ requirements.txt # Python dependencies
โโโ README.md # This fileKey Components
KnowledgeGraphManager: Neo4j interface for graph operationsextract_entities_with_llm(): LLM-based entity/relationship extractiongenerate_answer_with_citations(): Multi-hop RAG with provenance tracking
๐ Learn More
This example is inspired by VeritasGraph, an enterprise-grade framework for:
- On-premise knowledge graph RAG
- Visual reasoning traces (Veritas-Scope)
- LoRA-tuned LLM integration
๐ License
MIT License
