A Retrieval-Augmented Generation (RAG) system powered by Neo4J Knowledge Graphs and LangChain — combining structured graph reasoning with LLM-based question answering.
Traditional RAG systems retrieve flat text chunks from vector databases. This project explores a graph-enhanced approach: entities and their relationships are stored in a Neo4J Knowledge Graph, enabling the LLM to reason over structured connections — not just similar text.
This is particularly powerful for domains where relationships matter: education, healthcare, e-commerce, and enterprise knowledge management.
User Query
│
▼
┌─────────────────────┐
│ Query Processor │ → Entity extraction (spaCy / LLM)
└─────────┬───────────┘
│
┌─────┴──────┐
│ │
▼ ▼
┌────────┐ ┌──────────────┐
│ Neo4J │ │ Vector Store │ (Chroma / Qdrant)
│ KG │ │ Embeddings │
└────┬───┘ └──────┬───────┘
│ │
└──────┬───────┘
│ Context Fusion
▼
┌───────────────┐
│ LLM (Claude │
│ / GPT-4) │
└───────┬───────┘
│
▼
Final Answer
- 🔗 Graph-based retrieval via Neo4J Cypher queries
- 🧩 Hybrid search — combines graph traversal + semantic vector search
- 🤖 LangChain integration with
GraphCypherQAChain - 📊 Knowledge Graph builder — ingest raw text and extract entities/relations automatically
- 🖥️ FastAPI endpoint for easy integration
- 📓 Jupyter notebooks for exploration and demos
- Python 3.11+
- Neo4J Desktop or Docker
- OpenAI or Anthropic API key
git clone https://github.com/YOUR_USERNAME/knowledge-graph-rag.git
cd knowledge-graph-rag
pip install -r requirements.txtdocker-compose up -dNeo4J Browser: http://localhost:7474 (user: neo4j, password: password)
cp .env.example .env
# Edit .env with your API keys and Neo4J credentialspython src/graph/kg_builder.py --input data/raw/sample_docs.txtpython src/main.py --query "What are the prerequisites for machine learning?"uvicorn src.api.app:app --reload
# Web-UI: http://localhost:8000uvicorn src.api.app:app --reload
# API docs: http://localhost:8000/docsknowledge-graph-rag/
├── data/
│ ├── raw/ # Input documents (txt, pdf, json)
│ └── processed/ # Extracted entities & relations
├── src/
│ ├── graph/
│ │ ├── kg_builder.py # Entity/relation extraction → Neo4J
│ │ ├── neo4j_client.py # Neo4J connection & Cypher queries
│ │ └── schema.py # Graph schema definition
│ ├── rag/
│ │ ├── retriever.py # Hybrid graph + vector retriever
│ │ ├── embeddings.py # Embedding model wrapper
│ │ └── chain.py # LangChain RAG chain
│ ├── api/
│ │ └── app.py # FastAPI application
│ └── main.py # CLI entrypoint
├── notebooks/
│ ├── 01_kg_exploration.ipynb # Explore the knowledge graph
│ ├── 02_rag_pipeline.ipynb # RAG pipeline walkthrough
│ └── 03_evaluation.ipynb # Evaluate retrieval quality
├── tests/
│ ├── test_kg_builder.py
│ ├── test_retriever.py
│ └── test_api.py
├── docs/
│ └── architecture.md
├── docker-compose.yml
├── requirements.txt
├── .env.example
└── README.md
from src.rag.chain import GraphRAGChain
chain = GraphRAGChain()
response = chain.query("Which topics are related to neural networks?")
print(response)
# → "Neural networks are related to: deep learning, backpropagation,
# activation functions, and gradient descent. In the knowledge graph,
# they connect directly to CNNs, RNNs, and Transformers..."| Notebook | Description |
|---|---|
01_kg_exploration.ipynb |
Visualize the graph, run Cypher queries |
02_rag_pipeline.ipynb |
End-to-end RAG demo step by step |
03_evaluation.ipynb |
Compare graph RAG vs. standard RAG |
- Support PDF ingestion
- Add GraphRAG (Microsoft) comparison
- Streamlit demo UI
- Multi-hop reasoning over graph paths
- Evaluation with RAGAS framework
- DeepLearning.AI – Knowledge Graphs for RAG
- LangChain GraphCypherQAChain
- LightRAG – Knowledge Graph + RAG
Paul Meteng — Software Engineer & AI Engineer
LinkedIn · GitHub
MIT License — see LICENSE