# 0011ashwin/self-healing-rag-with-local-ollama-models architecture

> Architecture diagram of the GitHub repository 0011ashwin/self-healing-rag-with-local-ollama-models, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0011ashwin/self-healing-rag-with-local-ollama-models
- Repository: https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models

## Overview

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: [`modules/healing.py`](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/healing.py) (Healer), [`main.py`](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/main.py) (main.py), [`modules/retriever.py`](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/retriever.py) (Retriever), [`modules/embeddings.py`](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/embeddings.py) (Embeddings)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g1["Runtime"]
  node_n1["main.py<br/>orchestrator<br/>[main.py]"]
end

subgraph group_g2["Ingestion"]
  node_n2["PDF loader<br/>etl<br/>[loader.py]"]
  node_n3["Chunker<br/>etl<br/>[chunker.py]"]
end

subgraph group_g3["Indexing"]
  node_n4(("Embeddings<br/>embedder<br/>[embeddings.py]"))
  node_n5[("Vector DB<br/>index writer<br/>[vector_db.py]")]
end

subgraph group_g4["Query"]
  node_n6["Retriever<br/>query retrieval<br/>[retriever.py]"]
  node_n7{{"Healer<br/>self-heal control<br/>[healing.py]"}}
  node_n8["Generator<br/>llm response<br/>[generator.py]"]
end

subgraph group_g5["Support"]
  node_n9["Config<br/>[config.py]"]
  node_n10["Logger<br/>observability<br/>[logger.py]"]
  node_n11[("Memory<br/>session state<br/>[memory.py]")]
  node_n12(("Ollama<br/>local model backend"))
  node_n16["Helper scripts<br/>utility scripts"]
end

subgraph group_g6["Persistence"]
  node_n13["Data PDFs<br/>source corpus"]
  node_n14[("Vectorstore<br/>durable index")]
  node_n15["Logs<br/>runtime traces"]
end

node_n13 -->|"source PDFs"| node_n2
node_n2 -->|"raw text"| node_n3
node_n3 -->|"chunks"| node_n4
node_n4 -->|"vectors"| node_n5
node_n5 -->|"persist"| node_n14
node_n1 -->|"query path"| node_n6
node_n6 -->|"search"| node_n14
node_n6 -->|"empty or broken"| node_n7
node_n7 -.->|"re-ingest"| node_n2
node_n7 -.->|"re-chunk"| node_n3
node_n7 -.->|"re-embed"| node_n4
node_n7 -.->|"reindex"| node_n5
node_n6 -->|"context"| node_n8
node_n8 -->|"llm call"| node_n12
node_n4 -->|"embed model"| node_n12
node_n1 -->|"session state"| node_n11
node_n1 -->|"trace"| node_n10
node_n10 -->|"writes"| node_n15
node_n1 -->|"settings"| node_n9
node_n16 -.->|"inspect"| node_n13
node_n16 -.->|"maintain"| node_n14

click node_n1 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/main.py"
click node_n2 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/etl/loader.py"
click node_n3 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/etl/chunker.py"
click node_n4 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/embeddings.py"
click node_n5 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/vector_db.py"
click node_n6 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/retriever.py"
click node_n7 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/healing.py"
click node_n8 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/generator.py"
click node_n9 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/config.py"
click node_n10 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/logger.py"
click node_n11 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/memory.py"
click node_n13 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/tree/main/data"
click node_n14 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/tree/main/vectorstore"
click node_n15 "https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/tree/main/logs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n1 toneBlue
class node_n2,node_n3 toneAmber
class node_n4,node_n5 toneMint
class node_n6,node_n7,node_n8 toneRose
class node_n9,node_n10,node_n11,node_n12,node_n16 toneIndigo
class node_n13,node_n14,node_n15 toneTeal
```

## Components

### Runtime

Interactive query loop and application control.

- [main.py](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/main.py): orchestrator · CLI-style entrypoint that coordinates ingest, retrieve, heal, and answer generation. · `main.py`

### Ingestion

PDF loading and chunk preparation.

- [PDF loader](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/etl/loader.py): etl · Extracts text from PDFs in the local data corpus. · `etl/loader.py`
- [Chunker](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/etl/chunker.py): etl · Splits extracted text into overlapping retrieval chunks. · `etl/chunker.py`

### Indexing

Embedding and durable vector storage.

- [Embeddings](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/embeddings.py): embedder · Turns chunks into vectors using the local embedding model. · `modules/embeddings.py`
- [Vector DB](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/vector_db.py): index writer · Persists embeddings and chunk metadata into ChromaDB. · `modules/vector_db.py`

### Query

Retrieval, healing, and response synthesis.

- [Retriever](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/retriever.py): query retrieval · Runs similarity search over the stored vector index. · `modules/retriever.py`
- [Healer](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/healing.py): self-heal control · Detects missing, stale, or empty retrieval states and triggers rebuilds. · `modules/healing.py`
- [Generator](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/generator.py): llm response · Synthesizes grounded answers from retrieved context using the local LLM. · `modules/generator.py`

### Support

Configuration, logging, memory, and utility scripts.

- [Config](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/config.py): runtime config · Centralizes paths, model names, and runtime settings. · `modules/config.py`
- [Logger](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/logger.py): observability · Writes operational traces for query flow and healing events. · `modules/logger.py`
- [Memory](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/blob/main/modules/memory.py): session state · Holds lightweight conversation or session state separate from the vector store. · `modules/memory.py`
- **Ollama**: local model backend · Serves both embedding and generation models locally.
- **Helper scripts**: utility scripts · Maintenance and inspection entrypoints outside the main RAG loop.

### Persistence

Local source corpus, logs, and vector index.

- [Data PDFs](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/tree/main/data): source corpus · Local PDF documents used as the knowledge base. · `data`
- [Vectorstore](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/tree/main/vectorstore): durable index · Persistent ChromaDB files backing retrieval across runs. · `vectorstore`
- [Logs](https://github.com/0011ashwin/self-healing-rag-with-local-ollama-models/tree/main/logs): runtime traces · Stored execution logs used to inspect failures and healing behavior. · `logs`

## Connections

- 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.
