# aelaraby6/docqa architecture

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

- Interactive diagram: https://gitdiagram.com/aelaraby6/docqa
- Repository: https://github.com/aelaraby6/docqa

## Overview

#### Purpose and Entry Points
DocQA is a PDF-backed character chat service: it indexes a book/story, retrieves relevant passages for a question, and asks an LLM to answer in a selected character’s voice without exceeding document context. The runtime API entry point is `src/main.py`, launched as `uvicorn src.main:app`. Its public boundaries are `POST /upload` for replacing the active document and `POST /chat` for inference.

#### API Orchestration
`src/main.py` owns the FastAPI application, request handling, and lifecycle coordination between ingestion, retrieval, and generation. It also auto-indexes the bundled sample document, `data/the_lighthouse_keepers_secret.pdf`, at startup when applicable. The indexed document is application-local state rather than a durable multi-document service.

#### PDF Ingestion Pipeline
`src/utils/pdf_utils.py` extracts text page-by-page from uploaded or default PDFs and splits it into overlapping chunks (800 characters, 100-character overlap). `/upload` accepts multipart PDF data, writes it to the runtime upload area, then triggers rebuilding of the active index. This is a replacement workflow: a new upload supersedes prior retrieval content.

#### Embedding and Vector Retrieval
`src/vector_store.py` encapsulates the shared Sentence-Transformers encoder (`all-MiniLM-L6-v2`) and in-memory FAISS `IndexFlatL2` index. During ingestion, each chunk is embedded and inserted alongside its source text. During chat, the same encoder embeds the question and FAISS retrieves the nearest four chunks. Key boundary: FAISS provides similarity search only; chunk text must remain associated in application memory for prompt context.

#### Persona-Grounded Generation
`src/persona.py` builds the system prompt and configures the Groq client. It combines the requested character name, retrieved passages, and constraints to preserve character voice while requiring factual grounding in retrieved text. The resulting prompt plus user question is sent to Groq’s `Llama-3.3-70b-versatile`; the model output becomes the `/chat` answer.

#### Primary Control Flow
1. Startup or `/upload`: PDF → extraction/chunking → embeddings → rebuilt FAISS index.
2. `/chat`: character name and question → query embedding → top-4 FAISS chunks → persona/context prompt → Groq completion → JSON response.

The API layer should reject or clearly handle chat requests before a successful index exists, invalid/non-PDF uploads, and external LLM failures.

#### Runtime and External Dependencies
The service runs on Python/FastAPI with PyPDF-based parsing, Sentence-Transformers local embedding inference, and FAISS in-process vector search. Groq is the only required remote inference dependency and is authenticated through `GROQ_API_KEY` from `.env`/`.env.example`. `requirements.txt` defines the deployable dependency set. Persistent storage is not represented in the repository: uploads and indexes are runtime-local, so restarts require re-indexing.

Main entry points: [`src/main.py`](https://github.com/aelaraby6/docqa/blob/main/src/main.py) (FastAPI App), [`src/vector_store.py`](https://github.com/aelaraby6/docqa/blob/main/src/vector_store.py) (Sentence-Transformers Encoder), [`src/utils/pdf_utils.py`](https://github.com/aelaraby6/docqa/blob/main/src/utils/pdf_utils.py) (PDF Extractor & Chunker), [`src/persona.py`](https://github.com/aelaraby6/docqa/blob/main/src/persona.py) (Persona Prompt Builder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["FastAPI Service"]
  node_main["FastAPI App<br/>API orchestration<br/>[main.py]"]
  node_sample_pdf["Bundled Sample PDF<br/>default document"]
  node_upload_pdf["Uploaded PDF<br/>multipart input"]
end

subgraph group_ingestion["Indexing Pipeline"]
  node_pdf_utils["PDF Extractor &amp; Chunker<br/>ingestion<br/>[pdf_utils.py]"]
end

subgraph group_retrieval["Retrieval State"]
  node_encoder{{"Sentence-Transformers Encoder<br/>local embeddings<br/>[vector_store.py]"}}
  node_vector_store[("FAISS Vector Store<br/>in-memory similarity index<br/>[vector_store.py]")]
  node_chunk_text["Chunk Text Store<br/>in-memory context<br/>[vector_store.py]"]
end

subgraph group_generation["Grounded Generation"]
  node_persona["Persona Prompt Builder<br/>prompt orchestration<br/>[persona.py]"]
  node_groq{{"Groq Completion API<br/>remote LLM"}}
  node_groq_key["GROQ_API_KEY<br/>runtime secret<br/>[.env.example]"]
end

node_client(("API Client"))
node_requirements["Python Dependencies<br/>deployment manifest<br/>[requirements.txt]"]

node_client -->|"POST /upload or /chat"| node_main
node_sample_pdf -->|"startup default"| node_main
node_main -->|"stores multipart PDF"| node_upload_pdf
node_main -->|"indexes default or upload"| node_pdf_utils
node_pdf_utils -->|"text chunks"| node_encoder
node_encoder -->|"chunk embeddings"| node_vector_store
node_pdf_utils -->|"source chunks"| node_chunk_text
node_main -->|"chat question embedding"| node_encoder
node_encoder -->|"query vector"| node_vector_store
node_vector_store -->|"top-4 vector matches"| node_chunk_text
node_main -->|"character, question, passages"| node_persona
node_chunk_text -->|"retrieved context"| node_persona
node_persona -->|"grounded completion request"| node_groq
node_groq_key -.->|"authentication"| node_groq
node_groq -->|"answer or failure"| node_main
node_main -->|"JSON response"| node_client
node_requirements -.->|"runtime dependencies"| node_main

click node_main "https://github.com/aelaraby6/docqa/blob/main/src/main.py"
click node_sample_pdf "https://github.com/aelaraby6/docqa/blob/main/data/the_lighthouse_keepers_secret.pdf"
click node_pdf_utils "https://github.com/aelaraby6/docqa/blob/main/src/utils/pdf_utils.py"
click node_encoder "https://github.com/aelaraby6/docqa/blob/main/src/vector_store.py"
click node_vector_store "https://github.com/aelaraby6/docqa/blob/main/src/vector_store.py"
click node_chunk_text "https://github.com/aelaraby6/docqa/blob/main/src/vector_store.py"
click node_persona "https://github.com/aelaraby6/docqa/blob/main/src/persona.py"
click node_groq_key "https://github.com/aelaraby6/docqa/blob/main/.env.example"
click node_requirements "https://github.com/aelaraby6/docqa/blob/main/requirements.txt"

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_main,node_sample_pdf,node_upload_pdf toneBlue
class node_pdf_utils toneAmber
class node_encoder,node_vector_store,node_chunk_text toneMint
class node_persona,node_groq,node_groq_key toneRose
class node_client,node_requirements toneNeutral
```

## Components

### FastAPI Service

- [FastAPI App](https://github.com/aelaraby6/docqa/blob/main/src/main.py): API orchestration · Owns startup, upload replacement, chat validation, and JSON responses. · `src/main.py`
- [Bundled Sample PDF](https://github.com/aelaraby6/docqa/blob/main/data/the_lighthouse_keepers_secret.pdf): default document · Auto-indexed at startup when applicable. · `data/the_lighthouse_keepers_secret.pdf`
- **Uploaded PDF**: multipart input · Written to runtime-local upload storage before reindexing.

### Indexing Pipeline

- [PDF Extractor & Chunker](https://github.com/aelaraby6/docqa/blob/main/src/utils/pdf_utils.py): ingestion · Extracts pages and creates overlapping 800-character chunks. · `src/utils/pdf_utils.py`

### Retrieval State

Application-local state; rebuilt for each active document.

- [Sentence-Transformers Encoder](https://github.com/aelaraby6/docqa/blob/main/src/vector_store.py): local embeddings · Shared all-MiniLM-L6-v2 encoder for chunks and questions. · `src/vector_store.py`
- [FAISS Vector Store](https://github.com/aelaraby6/docqa/blob/main/src/vector_store.py): in-memory similarity index · IndexFlatL2 retrieves nearest chunk vectors; text stays in application memory. · `src/vector_store.py`
- [Chunk Text Store](https://github.com/aelaraby6/docqa/blob/main/src/vector_store.py): in-memory context · Maintains source chunks aligned with FAISS vectors. · `src/vector_store.py`

### Grounded Generation

- [Persona Prompt Builder](https://github.com/aelaraby6/docqa/blob/main/src/persona.py): prompt orchestration · Combines character, passages, grounding constraints, and question. · `src/persona.py`
- **Groq Completion API**: remote LLM · Runs Llama-3.3-70b-versatile.
- [GROQ_API_KEY](https://github.com/aelaraby6/docqa/blob/main/.env.example): runtime secret · Authenticates the Groq client from environment settings. · `.env.example`

### Other components

- **API Client**: client
- [Python Dependencies](https://github.com/aelaraby6/docqa/blob/main/requirements.txt): deployment manifest · Defines FastAPI, parsing, embedding, FAISS, and Groq dependencies. · `requirements.txt`

## Connections

- API Client → FastAPI App: POST /upload or /chat
- Bundled Sample PDF → FastAPI App: startup default
- FastAPI App → Uploaded PDF: stores multipart PDF
- FastAPI App → PDF Extractor & Chunker: indexes default or upload
- PDF Extractor & Chunker → Sentence-Transformers Encoder: text chunks
- Sentence-Transformers Encoder → FAISS Vector Store: chunk embeddings
- PDF Extractor & Chunker → Chunk Text Store: source chunks
- FastAPI App → Sentence-Transformers Encoder: chat question embedding
- Sentence-Transformers Encoder → FAISS Vector Store: query vector
- FAISS Vector Store → Chunk Text Store: top-4 vector matches
- FastAPI App → Persona Prompt Builder: character, question, passages
- Chunk Text Store → Persona Prompt Builder: retrieved context
- Persona Prompt Builder → Groq Completion API: grounded completion request
- GROQ_API_KEY → Groq Completion API: authentication
- Groq Completion API → FastAPI App: answer or failure
- FastAPI App → API Client: JSON response
- Python Dependencies → FastAPI App: runtime dependencies
