An end-to-end experimental pipeline for constructing high-quality Knowledge Graphs (KGs) using lightweight and efficient LLMs with dynamic memory handling, ontology discovery, and Neo4j-based entity resolution.
Building Knowledge Graphs from large corpora using LLMs introduces several critical challenges:
- ❗ Context window limitations
- ❗ Duplicate entity creation
- ❗ Weak ontology consistency
- ❗ Lack of dynamic memory across chunks
This project explores a modular, research-oriented pipeline designed to address these issues through:
- ✅ Ontology Engineering with LLMs
- ✅ Structured Knowledge Extraction
- ✅ Vector-based Entity Resolution
- ✅ Neo4j Graph Storage
- ✅ Dynamic Memory Strategies
flowchart LR
A[Raw Corpus / PDF] --> B[Chunking]
B --> C[Ontology Discovery]
C --> D[Ontology Refinement]
D --> E[Knowledge Extraction]
E --> F[Entity Resolution]
F --> G[Neo4j Graph]
G --> H[Post-processing & Analysis]
.
├── ontology_extractor.py # Ontology discovery & refinement
├── kg_llm_extractor.py # LLM-based knowledge extraction
├── neo4j_entity_resolution.py # Vector-based entity deduplication
├── .env # Neo4j credentials
└── README.mdFile: ontology_extractor.py
Automatically discover and refine the schema before Knowledge Graph construction.
The LLM analyzes corpus chunks to identify:
- Candidate entity types
- Relationship types
- Attribute patterns
- Domain concepts
The discovered ontology is:
- Deduplicated
- Normalized
- Structured into a consistent schema
- Validated for conflicts
Without ontology grounding:
- KG becomes noisy
- Relations become inconsistent
- Downstream reasoning fails
This stage provides schema stability before extraction.
File: kg_llm_extractor.py
Transform unstructured text into structured triples.
For each chunk:
- Pass chunk to a small LLM
- Extract structured triples
- Attach metadata (
chunk_id, confidence, etc.) - Store intermediate results
{
"entities": [...],
"relationships": [...],
"source_chunk": "...",
"confidence": 0.xx
}Each triple retains provenance.
Extraction is constrained by the discovered ontology.
Designed to run efficiently with lightweight models such as Mistral-7B.
File: neo4j_entity_resolution.py
This is one of the most critical components of the pipeline.
When processing chunks independently:
- The same entity appears multiple times
- The graph becomes fragmented
- Query quality degrades
The pipeline implements vector-based entity resolution using:
- Sentence embeddings
- Similarity search
- Neo4j vector index
- Threshold-based merging
flowchart TD
A[New Entity] --> B[Generate Embedding]
B --> C[Search Similar Entities]
C --> D{Similarity > Threshold?}
D -->|Yes| E[Merge Entities]
D -->|No| F[Create New Node]
Comparison is performed using:
- Name similarity
- Semantic embedding similarity
- Optional attribute matching
This significantly reduces duplicate nodes.
One major challenge during development was:
❓ How do we prevent the LLM from "forgetting" previous chunks?
Processing chunks independently leads to:
- Duplicate entities
- Inconsistent relations
- Ontology drift
The pipeline combines:
- Ontology grounding
- Schema constraints
- Entity resolution
- Graph merging
- Community clustering
Instead of forcing the LLM to remember everything — which is expensive and unreliable — the pipeline:
- ✔ Lets the model work locally
- ✔ Fixes globally through graph algorithms
- ✔ Maintains scalability
The graph layer provides:
- Persistent memory
- Efficient traversal
- Vector similarity search
- Graph analytics
Create a .env file:
NEO4J_URI=bolt://localhost:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=your_passwordpip install -r requirements.txtEnsure Neo4j is running locally.
python ontology_extractor.pypython kg_llm_extractor.pypython neo4j_entity_resolution.pyThis project follows several key principles.
Instead of overloading context windows, the pipeline relies on:
- Graph memory
- Vector search
- Post-processing
The system is optimized for:
- Mistral-7B class models
- Kaggle and consumer GPUs
- Efficient inference
High-quality Knowledge Graphs require:
- Deduplication
- Clustering
- Schema enforcement
—not just extraction.
- Online ontology adaptation
- Streaming KG construction
- Community detection integration
- Temporal knowledge graphs
- Multi-agent extraction
- GraphRAG integration
Contributions, ideas, and research discussions are welcome.
If you are working on:
- KG construction
- GraphRAG
- Ontology learning
- LLM pipelines
feel free to open an issue or submit a PR.
This project is an experimental research effort exploring the intersection of:
- Knowledge Graphs
- Small Language Models
- Graph Databases
- Representation Learning
Building robust Knowledge Graphs with LLMs is not just an extraction problem — it is a systems engineering problem involving memory, ontology, and graph intelligence.