A production-ready Knowledge Graph RAG (Retrieval-Augmented Generation) solution using Neo4j and Google Gemini.
- Knowledge Graph Construction: Automatically extract entities and relationships from text documents
- Entity Resolution: Merge duplicate entities using exact, fuzzy, or semantic matching
- Vector Search: Semantic similarity search using embeddings
- Graph-Enhanced RAG: Combine vector search with graph traversal for better context
- Production Ready: Proper constraints, indexes, and error handling
This solution follows the official Neo4j GraphRAG patterns:
┌─────────────────────────────────────────────────────────────┐
│ Document Ingestion │
├─────────────────────────────────────────────────────────────┤
│ Text → Chunks → Entity Extraction → Graph Building │
│ ↓ │
│ Entity Resolution (Deduplication) │
│ ↓ │
│ Vector Index + Graph Storage │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ Query │
├─────────────────────────────────────────────────────────────┤
│ Question → Vector Search + Graph Traversal → LLM Answer │
└─────────────────────────────────────────────────────────────┘
- Python 3.12+
- Neo4j AuraDB or local Neo4j instance (5.18+)
- Google Gemini API key
- Clone the repository:
git clone https://github.com/behoss/neo4j-graphrag.git
cd neo4j-graphrag- Install dependencies using uv:
uv syncOr using pip:
pip install -e .- Create a
.env.localfile:
cp .env.example .env.local- Edit
.env.localwith your credentials:
# Neo4j AuraDB credentials
NEO4J_URI=neo4j+s://your-instance.databases.neo4j.io
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=your-password
# Google Gemini API key
GEMINI_API_KEY=your-api-key- Initialize the database:
python -m src.cli initThe CLI provides several commands for managing your knowledge graph:
# Initialize database with constraints and indexes
python -m src.cli init
# Ingest a document
python -m src.cli ingest files/document.txt --title "My Document"
# List all documents
python -m src.cli list
# Query the knowledge graph
python -m src.cli query
# Show document info
python -m src.cli info my-document
# Delete a document
python -m src.cli delete my-document
# Show database statistics
python -m src.cli stats
# Purge all data (use with caution!)
python -m src.cli purgeimport asyncio
from src.config import PipelineConfig
from src.pipeline import GraphRAGPipeline
# Load configuration from environment
config = PipelineConfig.from_env()
# Create pipeline
with GraphRAGPipeline(config) as pipeline:
# Initialize database
pipeline.db.initialize()
# Ingest a document
result = asyncio.run(pipeline.ingest_text(
text="Your document text here...",
document_id="my-doc",
document_title="My Document",
))
print(f"Ingested: {result['entities']} entities, {result['relationships']} relationships")
# Query the knowledge graph
answer = asyncio.run(pipeline.query("What is this document about?"))
print(f"Answer: {answer}")Run the demo script to see the pipeline in action:
python demo.pyThe default schema includes common entity types and relationships. You can customize it in src/config.py:
from src.config import SchemaConfig
schema = SchemaConfig(
node_types=[
{"label": "Person", "properties": [{"name": "name", "type": "STRING", "required": True}]},
{"label": "Organization", "properties": [{"name": "name", "type": "STRING", "required": True}]},
# Add more entity types...
],
relationship_types=[
{"label": "WORKS_FOR", "description": "Person works for organization"},
# Add more relationship types...
],
patterns=[
("Person", "WORKS_FOR", "Organization"),
# Add more patterns...
],
)Three resolution strategies are available:
- Exact Match (default): Merges entities with identical names
- Fuzzy Match: Uses Levenshtein distance for similar names (requires
rapidfuzz) - Semantic Match: Uses embeddings for semantic similarity (requires
spacy)
Configure in your pipeline:
config = PipelineConfig.from_env()
config.resolution_type = "fuzzy" # or "exact" or "semantic"neo4j-graphrag/
├── src/
│ ├── __init__.py
│ ├── config.py # Configuration management
│ ├── database.py # Neo4j database operations
│ ├── pipeline.py # Main GraphRAG pipeline
│ ├── logging_config.py # Logging utilities
│ └── cli.py # Command-line interface
├── files/ # Document storage
├── logs/ # Execution logs
├── cache/ # Extraction cache
├── demo.py # Demo script
├── pyproject.toml # Project dependencies
└── README.md
This production version includes several improvements:
- Entity Deduplication: MERGE queries now match only on entity name, not document_id
- Proper Constraints: Uniqueness constraints prevent duplicate entities
- Entity Resolution: Post-processing step to merge similar entities
- Modular Architecture: Clean separation of concerns
- Error Handling: Graceful fallbacks and detailed logging
- Vector Search: Proper vector index setup and querying
- CLI Tools: Rich command-line interface for all operations
If you see duplicate entities in your graph:
- Run entity resolution manually:
from neo4j_graphrag.experimental.components.resolver import SinglePropertyExactMatchResolver
resolver = SinglePropertyExactMatchResolver(driver)
await resolver.run()- Or use the fuzzy matcher for similar names:
from neo4j_graphrag.experimental.components.resolver import FuzzyMatchResolver
resolver = FuzzyMatchResolver(driver)
await resolver.run()Ensure your Neo4j credentials are correct and the database is accessible:
python -c "from src.config import PipelineConfig; c = PipelineConfig.from_env(); print('OK')"MIT License
Contributions are welcome! Please read the contributing guidelines before submitting a pull request.