How aelaraby6/docqa works
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: main.py FastAPI App · vector_store.py Sentence-Transformers Encoder · pdf_utils.py PDF Extractor & Chunker · persona.py Persona Prompt Builder
Components 12
FastAPI Service
- FastAPI App · Owns startup, upload replacement, chat validation, and JSON responses.
src/main.py - Bundled Sample PDF · Auto-indexed at startup when applicable.
data/the_lighthouse_keepers_secret.pdf - Uploaded PDF · Written to runtime-local upload storage before reindexing.
Indexing Pipeline
- PDF Extractor & Chunker · 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 · Shared all-MiniLM-L6-v2 encoder for chunks and questions.
src/vector_store.py - FAISS Vector Store · IndexFlatL2 retrieves nearest chunk vectors; text stays in application memory.
src/vector_store.py - Chunk Text Store · Maintains source chunks aligned with FAISS vectors.
src/vector_store.py
Grounded Generation
- Persona Prompt Builder · Combines character, passages, grounding constraints, and question.
src/persona.py - Groq Completion API · Runs Llama-3.3-70b-versatile.
- GROQ_API_KEY · Authenticates the Groq client from environment settings.
.env.example
Other components
- API Client
- Python Dependencies · Defines FastAPI, parsing, embedding, FAISS, and Groq dependencies.
requirements.txt
Connections 17
- 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