# reni-renisha/study_guide architecture

> Architecture diagram of the GitHub repository reni-renisha/study_guide, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/reni-renisha/study_guide
- Repository: https://github.com/reni-renisha/study_guide

## Overview

1. This repo is a small, self-contained local RAG application. The architecture is centered on a single Python backend (`main.py`) and a single-page frontend (`index.html`), with no separate services besides local Ollama and the local vector store.

2. The user-facing boundary is the browser UI. The HTML/vanilla JS page lets a user upload PDFs, view indexed documents, delete documents, and submit questions. It talks only to the FastAPI backend over HTTP.

3. The backend boundary is `main.py`, which exposes the API described in the README:
   - `POST /upload` for PDF ingestion
   - `GET /documents` for listing indexed files
   - `DELETE /documents/{doc_id}` for removal
   - `POST /ask` for retrieval-augmented Q&A

4. Document ingestion flow: uploaded PDFs are parsed locally with PyMuPDF. The backend extracts text from each PDF page, associates content with filename/page metadata, and prepares it for indexing. This is the first major processing pipeline.

5. Indexing and persistence are handled by ChromaDB running locally in persistent mode. Parsed text is converted into embeddings using `sentence-transformers` with the `MiniLM-L6-v2` model, then stored in the vector index along with document/page metadata so later answers can cite sources.

6. Query flow: when a user asks a question, the backend embeds the query with the same sentence-transformers model, performs cosine-similarity retrieval against ChromaDB, and selects the most relevant chunks/pages. This retrieval layer is the key bridge between the documents and the generated answer.

7. Answer generation is delegated to a local LLM through Ollama. The README specifies `mistral` running locally, so the backend sends the retrieved context plus the question to Ollama, then formats the result into a grounded response.

8. Grounding is an explicit architectural constraint. The assistant is designed to answer strictly from retrieved document content and to return “Answer not found in documents.” when the retrieved context is insufficient. That means the retrieval step is not optional; it is the gatekeeper for response generation.

9. Source attribution is part of the API contract and response shape. Answers include filename, page number, similarity score, and a snippet, which means the retrieval layer must preserve page-level provenance all the way through to the frontend.

10. Document management is a second data lifecycle path besides Q&A. The `/documents` and `/documents/{doc_id}` endpoints imply there is document identity and cleanup logic in the backend so users can inspect and remove indexed content without rebuilding the entire store.

11. Deployment/runtime is intentionally local-first:
   - Python backend via FastAPI and Uvicorn
   - local embedding model from `sentence-transformers`
   - local vector persistence via ChromaDB
   - local generation via Ollama
   This keeps the full system offline and avoids external cloud dependencies.

12. At the diagram level, the main flows to show are:
   - Browser UI → FastAPI API
   - Upload PDF → parse/extract text → embed → store in ChromaDB
   - Ask question → embed query → retrieve relevant chunks → send to Ollama → return grounded answer with citations
   - List/delete document → backend metadata/store update

Main entry points: [`main.py`](https://github.com/reni-renisha/study_guide/blob/main/main.py) (FastAPI API), [`index.html`](https://github.com/reni-renisha/study_guide/blob/main/index.html) (Browser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Browser UI"]
  node_browser["Browser<br/>client<br/>[index.html]"]
  node_ui_upload["Upload PDFs<br/>ui flow<br/>[index.html]"]
  node_ui_docs["Documents View<br/>ui flow<br/>[index.html]"]
  node_ui_ask["Ask Question<br/>ui flow<br/>[index.html]"]
end

subgraph group_backend["FastAPI Backend"]
  node_api["FastAPI API<br/>http api<br/>[main.py]"]
  node_ingest["Ingest Pipeline<br/>pdf pipeline<br/>[main.py]"]
  node_metadata[("Doc Metadata<br/>document index<br/>[main.py]")]
  node_retrieve["Retrieval Layer<br/>search pipeline<br/>[main.py]"]
  node_response["Answer Formatter<br/>response builder<br/>[main.py]"]
end

subgraph group_local["Local Services"]
  node_pymupdf["PyMuPDF<br/>pdf parser"]
  node_embedder["MiniLM Embedder<br/>embedding model"]
  node_chroma[("ChromaDB<br/>vector store")]
  node_ollama["Ollama LLM<br/>local llm"]
end

node_browser -->|"HTTP requests"| node_api
node_ui_upload -->|"POST /upload"| node_api
node_ui_docs -->|"GET/DELETE docs"| node_api
node_ui_ask -->|"POST /ask"| node_api
node_api -->|"upload flow"| node_ingest
node_ingest -->|"extract text"| node_pymupdf
node_ingest -->|"embed pages"| node_embedder
node_embedder -->|"store vectors"| node_chroma
node_api -->|"manage docs"| node_metadata
node_api -->|"question flow"| node_retrieve
node_retrieve -->|"embed query"| node_embedder
node_retrieve -->|"similarity search"| node_chroma
node_retrieve -->|"ground context"| node_response
node_response -->|"generate answer"| node_ollama
node_response -->|"citations"| node_api
node_metadata -->|"source data"| node_response
node_chroma -->|"page provenance"| node_metadata
node_retrieve -->|"insufficient context"| node_response

click node_browser "https://github.com/reni-renisha/study_guide/blob/main/index.html"
click node_ui_upload "https://github.com/reni-renisha/study_guide/blob/main/index.html"
click node_ui_docs "https://github.com/reni-renisha/study_guide/blob/main/index.html"
click node_ui_ask "https://github.com/reni-renisha/study_guide/blob/main/index.html"
click node_api "https://github.com/reni-renisha/study_guide/blob/main/main.py"
click node_ingest "https://github.com/reni-renisha/study_guide/blob/main/main.py"
click node_metadata "https://github.com/reni-renisha/study_guide/blob/main/main.py"
click node_retrieve "https://github.com/reni-renisha/study_guide/blob/main/main.py"
click node_response "https://github.com/reni-renisha/study_guide/blob/main/main.py"

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_browser,node_ui_upload,node_ui_docs,node_ui_ask toneBlue
class node_api,node_ingest,node_metadata,node_retrieve,node_response toneAmber
class node_pymupdf,node_embedder,node_chroma,node_ollama toneMint
```

## Components

### Browser UI

Single-page upload, document, and Q&A interface

- [Browser](https://github.com/reni-renisha/study_guide/blob/main/index.html): client · User-facing interface in the browser · `index.html`
- [Upload PDFs](https://github.com/reni-renisha/study_guide/blob/main/index.html): ui flow · PDF upload action and document submission · `index.html`
- [Documents View](https://github.com/reni-renisha/study_guide/blob/main/index.html): ui flow · Lists indexed files and supports deletion · `index.html`
- [Ask Question](https://github.com/reni-renisha/study_guide/blob/main/index.html): ui flow · Question entry and answer display · `index.html`

### FastAPI Backend

Main application and data flow orchestration

- [FastAPI API](https://github.com/reni-renisha/study_guide/blob/main/main.py): http api · Backend HTTP boundary for upload, document management, and Q&A · `main.py`
- [Ingest Pipeline](https://github.com/reni-renisha/study_guide/blob/main/main.py): pdf pipeline · Parses uploaded PDFs and prepares page-level text for indexing · `main.py`
- [Doc Metadata](https://github.com/reni-renisha/study_guide/blob/main/main.py): document index · Tracks document identity, filename, page provenance, and cleanup state · `main.py`
- [Retrieval Layer](https://github.com/reni-renisha/study_guide/blob/main/main.py): search pipeline · Embeds queries and selects relevant document chunks from the vector store · `main.py`
- [Answer Formatter](https://github.com/reni-renisha/study_guide/blob/main/main.py): response builder · Packages grounded answers with citations and fallback messaging · `main.py`

### Local Services

Embedded model, vector store, and local generation runtime

- **PyMuPDF**: pdf parser · Local PDF text extraction from uploaded pages
- **MiniLM Embedder**: embedding model · Sentence-transformers MiniLM-L6-v2 embeddings for documents and queries
- **ChromaDB**: vector store · Persistent local vector index with similarity search and stored metadata
- **Ollama LLM**: local llm · Local mistral-backed generation using retrieved context

## Connections

- Browser → FastAPI API: HTTP requests · Browser actions call the backend API
- Upload PDFs → FastAPI API: POST /upload · Send uploaded PDFs to the backend
- Documents View → FastAPI API: GET/DELETE docs · Load and remove indexed documents
- Ask Question → FastAPI API: POST /ask · Submit a question for grounded answering
- FastAPI API → Ingest Pipeline: upload flow · Route PDF uploads into the ingestion pipeline
- Ingest Pipeline → PyMuPDF: extract text · Parse PDF pages locally into text
- Ingest Pipeline → MiniLM Embedder: embed pages · Convert page text into vector embeddings
- MiniLM Embedder → ChromaDB: store vectors · Persist embeddings and page metadata in ChromaDB
- FastAPI API → Doc Metadata: manage docs · Track document listing and deletion state
- FastAPI API → Retrieval Layer: question flow · Start retrieval for incoming user questions
- Retrieval Layer → MiniLM Embedder: embed query · Create query embedding with the same model
- Retrieval Layer → ChromaDB: similarity search · Find the most relevant stored chunks/pages
- Retrieval Layer → Answer Formatter: ground context · Pass retrieved evidence into answer assembly
- Answer Formatter → Ollama LLM: generate answer · Send context and question to the local LLM
- Answer Formatter → FastAPI API: citations · Return answer text with filename, page, score, and snippet
- Doc Metadata → Answer Formatter: source data · Provide provenance for citation formatting
- ChromaDB → Doc Metadata: page provenance · Keep document and page references attached to stored chunks
- Retrieval Layer → Answer Formatter: insufficient context · Trigger the fallback when retrieval is not strong enough
