How 0011ashwin/self-healing-rag-with-local-ollama-models works
1. What this repo is
- A local, privacy-first RAG application that chats over PDFs.
- The runtime is centered around main.py, with a CLI-style interaction loop and a persistent local vector store under vectorstore/.
2. Top-level architecture
- The system has four main stages: document ingestion, embedding/indexing, retrieval, and answer generation.
- A separate self-healing layer watches for broken or empty retrieval states and re-triggers ingestion/indexing when needed.
3. Document ingestion boundary
- The ETL layer lives in etl/.
- loader.py extracts text from PDFs in data/, and chunker.py splits that text into smaller overlapping chunks suitable for retrieval.
- This is the boundary between raw files and searchable knowledge.
4. Embedding and index creation
- modules/embeddings.py converts chunks into vectors using a local Ollama embedding model (nomic-embed-text per the README).
- modules/vector_db.py persists those vectors into ChromaDB on disk.
- The persisted state is not ephemeral: vectorstore/chroma.sqlite3 and the HNSW index files are the durable retrieval backbone.
5. Retrieval path at query time
- modules/retriever.py is the query-side access point into the vector database.
- It performs similarity search over the stored chunks and returns the most relevant context for the user question.
- If retrieval is empty or the index is missing, control does not stop there; it feeds into healing logic.
6. Answer generation
- modules/generator.py calls a local Ollama LLM (phi3:mini in the README) to synthesize the final response.
- The generator is intended to work only from retrieved context, so the architecture is classic grounded RAG rather than free-form chat.
7. Self-healing loop
- modules/healing.py is the distinctive control-plane component.
- It detects failure modes like missing vector store artifacts, stale state, or zero-result retrievals, then triggers re-ingestion/re-indexing before retrying the query path.
- This creates a closed loop: query -> retrieve -> detect failure -> heal -> rebuild -> retry.
8. Configuration and runtime concerns
- modules/config.py centralizes paths, model names, and runtime settings.
- modules/logger.py writes operational traces into logs/, which is important because healing behavior depends on observing failures and recovery steps.
9. State and persistence
- vectorstore/ is the durable knowledge index; data/ is the source corpus.
- modules/memory.py likely holds lightweight session or conversation state, separate from the persistent vector database.
- This separation keeps user conversation state distinct from indexed document knowledge.
10. Operational scripts and utilities
- read_pdf.py, list_models.py, and vc.py appear to be helper scripts for inspection or maintenance rather than the core runtime path.
- They support the system but are not the main request/response architecture.
11. External dependency model
- The only material external runtime dependency is Ollama, which serves both embeddings and generation locally.
- ChromaDB provides local persistence; there are no cloud APIs in the main architecture, which matches the project’s privacy-first design.
12. How to draw the diagram
- Draw data/ PDFs feeding the ETL pipeline, then embeddings into vectorstore/.
- Draw main.py as the orchestrator that sends user queries to retriever.py, then to generator.py.
- Place healing.py beside the query/index path with arrows back to ETL/vector DB rebuild.
- Show Ollama as the local model backend behind both embeddings and generation.
Main entry points: healing.py Healer · main.py main.py · retriever.py Retriever · embeddings.py Embeddings
Components 16
Runtime
Interactive query loop and application control.
- main.py · CLI-style entrypoint that coordinates ingest, retrieve, heal, and answer generation.
main.py
Ingestion
PDF loading and chunk preparation.
- PDF loader · Extracts text from PDFs in the local data corpus.
etl/loader.py - Chunker · Splits extracted text into overlapping retrieval chunks.
etl/chunker.py
Indexing
Embedding and durable vector storage.
- Embeddings · Turns chunks into vectors using the local embedding model.
modules/embeddings.py - Vector DB · Persists embeddings and chunk metadata into ChromaDB.
modules/vector_db.py
Query
Retrieval, healing, and response synthesis.
Support
Configuration, logging, memory, and utility scripts.
- Config · Centralizes paths, model names, and runtime settings.
modules/config.py - Logger · Writes operational traces for query flow and healing events.
modules/logger.py - Memory · Holds lightweight conversation or session state separate from the vector store.
modules/memory.py - Ollama · Serves both embedding and generation models locally.
- Helper scripts · Maintenance and inspection entrypoints outside the main RAG loop.
Persistence
Local source corpus, logs, and vector index.
- Data PDFs · Local PDF documents used as the knowledge base.
data - Vectorstore · Persistent ChromaDB files backing retrieval across runs.
vectorstore - Logs · Stored execution logs used to inspect failures and healing behavior.
logs
Connections 21
- Data PDFs → PDF loader: source PDFs · PDFs are read from the local corpus.
- PDF loader → Chunker: raw text · Loaded document text is chunked for retrieval.
- Chunker → Embeddings: chunks · Chunks are converted into embeddings.
- Embeddings → Vector DB: vectors · Embeddings are written into the vector database.
- Vector DB → Vectorstore: persist · ChromaDB state is stored on disk.
- main.py → Retriever: query path · The main loop sends user questions to retrieval.
- Retriever → Vectorstore: search · Retriever queries the durable vector index.
- Retriever → Healer: empty or broken · Missing or zero-result retrieval states trigger healing.
- Healer → PDF loader: re-ingest · Healing can re-run document loading.
- Healer → Chunker: re-chunk · Healing can rebuild chunked text.
- Healer → Embeddings: re-embed · Healing can regenerate embeddings.
- Healer → Vector DB: reindex · Healing can repopulate the vector database.
- Retriever → Generator: context · Retrieved context is passed to generation.
- Generator → Ollama: llm call · Generation runs on local Ollama models.
- Embeddings → Ollama: embed model · Embeddings are also produced by Ollama.
- main.py → Memory: session state · The runtime may keep lightweight conversational state.
- main.py → Logger: trace · Application events are logged.
- Logger → Logs: writes · Logs are persisted to the logs directory.
- main.py → Config: settings · The app reads central runtime configuration.
- Helper scripts → Data PDFs: inspect · Utility scripts operate on the local corpus.
- Helper scripts → Vectorstore: maintain · Utility scripts can inspect or support the local index.