# sergiocipher/pdf_peter architecture

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

- Interactive diagram: https://gitdiagram.com/sergiocipher/pdf_peter
- Repository: https://github.com/sergiocipher/pdf_peter

## Overview

1. **High-level shape**
   - This is a two-tier RAG application: a **FastAPI backend** does document ingestion and question answering, while a **React/Vite frontend** provides the upload-and-chat UI.
   - The core architectural boundary is between **document processing + retrieval in Python** and **user interaction in the browser**.

2. **Backend entrypoint and API surface**
   - `app/main.py` is the FastAPI application assembly point.
   - The HTTP surface is split into two route modules:
     - `app/api/upload.py` for PDF ingestion and indexing
     - `app/api/chat.py` for user questions and grounded answers
   - `run.py` is the local startup wrapper.

3. **Ingestion pipeline**
   - The upload path is a staged pipeline implemented in services:
     - `app/services/pdf_loader.py` extracts text from PDFs
     - `app/services/chunking.py` splits text into overlapping chunks
     - `app/services/embeddings.py` turns chunks into vectors
     - `app/services/vector_store.py` persists vectors and metadata in Qdrant
   - This is the write side of the system: PDF in, searchable document index out.

4. **Query / RAG pipeline**
   - The chat path is the read side:
     - `app/services/retriever.py` embeds the user query and fetches relevant chunks from Qdrant
     - `app/services/llm.py` sends retrieved context plus the question to the model
   - The response is grounded in retrieved document context, with citations/page references coming from stored chunk metadata.

5. **Model and external service boundaries**
   - The architecture relies on three external AI services:
     - **Gemini embeddings** for vectorization
     - **Qdrant** for vector search and storage
     - **Groq** for LLM inference
   - These are important boundaries because the backend orchestrates them rather than hosting models locally.

6. **Prompt and configuration layer**
   - `app/core/prompts.py` defines the system prompting used to constrain answers to document context.
   - `app/core/config.py` centralizes runtime configuration such as API keys, Qdrant connection details, and other environment-driven settings.
   - This layer separates policy/configuration from the pipeline logic.

7. **Frontend architecture**
   - The frontend lives under `frontend/src` and is organized as a small component-based UI:
     - `components/UploadBox.jsx` for PDF upload
     - `components/ChatBox.jsx` and `components/MessageBubble.jsx` for conversation display
     - `services/api.js` for HTTP calls to the backend
   - `App.jsx` stitches the upload and chat experiences together.

8. **Client-server interaction**
   - The browser does not talk directly to Qdrant or the model providers.
   - It only calls the backend API, which acts as an orchestration layer and hides the retrieval/model details.
   - This keeps secrets, vector search, and prompt logic server-side.

9. **Operational and debug utilities**
   - `debug_pipeline.py` is likely for testing or tracing the ingestion/retrieval flow end-to-end.
   - `reset_qdrant.py` is an administrative utility to clear the vector store state.
   - These scripts indicate the repo has a separate operational boundary for pipeline maintenance.

10. **Deployment and environment**
   - The README describes deployment split across **Render** for the backend, **Vercel** for the frontend, and **Qdrant Cloud** for the vector database.
   - `requirements.txt` and `frontend/package.json` define the Python and Node runtimes respectively.
   - The system is environment-driven rather than monolithic: the app depends on configured external services at runtime.

11. **Data flow summary**
   - Upload path: **PDF → text extraction → chunking → embeddings → Qdrant**
   - Query path: **question → query embedding → retrieval from Qdrant → prompt assembly → Groq response**
   - The architecture is a classic RAG loop with a clear separation between indexing and answering.

12. **Diagramming takeaway**
   - When drawing this repo, show three major boxes:
     - **React UI**
     - **FastAPI orchestration backend**
     - **External AI/data services (Qdrant, Gemini embeddings, Groq)**
   - Then represent two internal backend pipelines: **ingestion/indexing** and **question answering/retrieval**.

Main entry points: [`app/api/upload.py`](https://github.com/sergiocipher/pdf_peter/blob/main/app/api/upload.py) (Upload API), [`app/main.py`](https://github.com/sergiocipher/pdf_peter/blob/main/app/main.py) (FastAPI app), [`frontend/src/services/api.js`](https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/services/api.js) (API client), [`app/api/chat.py`](https://github.com/sergiocipher/pdf_peter/blob/main/app/api/chat.py) (Chat API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["React app<br/>UI shell<br/>[App.jsx]"]
  node_upload_box["Upload box<br/>upload UI<br/>[UploadBox.jsx]"]
  node_chat_box["Chat box<br/>chat UI<br/>[ChatBox.jsx]"]
  node_message_bubble["Message bubble<br/>ui component<br/>[MessageBubble.jsx]"]
  node_api_js["API client<br/>http client<br/>[api.js]"]
end

subgraph group_backend["Backend"]
  node_run_py["run.py<br/>startup<br/>[run.py]"]
  node_main_py["FastAPI app<br/>api assembly<br/>[main.py]"]
  node_upload_api["Upload API<br/>route<br/>[upload.py]"]
  node_chat_api["Chat API<br/>route<br/>[chat.py]"]
  node_config_py["Config<br/>settings<br/>[config.py]"]
  node_prompts_py["Prompts<br/>prompt policy<br/>[prompts.py]"]
  node_pdf_loader["PDF loader<br/>extractor<br/>[pdf_loader.py]"]
  node_chunking["Chunking<br/>splitter<br/>[chunking.py]"]
  node_embeddings["Embeddings<br/>vectorizer<br/>[embeddings.py]"]
  node_vector_store[("Vector store<br/>index writer<br/>[vector_store.py]")]
  node_retriever["Retriever<br/>search<br/>[retriever.py]"]
  node_llm["LLM client<br/>generation<br/>[llm.py]"]
end

subgraph group_external["External services"]
  node_gemini{{"Gemini embeddings<br/>external model"}}
  node_qdrant[("Qdrant<br/>vector db")]
  node_groq{{"Groq<br/>external LLM"}}
end

subgraph group_ops["Ops"]
  node_debug_pipeline["Debug pipeline<br/>[debug_pipeline.py]"]
  node_reset_qdrant["Reset Qdrant<br/>admin tool<br/>[reset_qdrant.py]"]
end

node_run_py -->|"starts"| node_main_py
node_main_py -->|"mounts"| node_upload_api
node_main_py -->|"mounts"| node_chat_api
node_main_py -->|"loads"| node_config_py
node_upload_api -->|"extracts"| node_pdf_loader
node_upload_api -->|"splits"| node_chunking
node_upload_api -->|"vectorizes"| node_embeddings
node_upload_api -->|"indexes"| node_vector_store
node_pdf_loader -->|"feeds"| node_chunking
node_chunking -->|"feeds"| node_embeddings
node_embeddings -->|"uses"| node_gemini
node_vector_store -->|"stores"| node_qdrant
node_chat_api -->|"retrieves"| node_retriever
node_chat_api -->|"answers"| node_llm
node_retriever -->|"embeds"| node_embeddings
node_retriever -->|"queries"| node_qdrant
node_llm -->|"applies"| node_prompts_py
node_llm -->|"calls"| node_groq
node_frontend_app -->|"contains"| node_upload_box
node_frontend_app -->|"contains"| node_chat_box
node_chat_box -->|"renders"| node_message_bubble
node_upload_box -->|"uploads"| node_api_js
node_chat_box -->|"asks"| node_api_js
node_api_js -->|"calls"| node_upload_api
node_api_js -->|"calls"| node_chat_api
node_debug_pipeline -.->|"traces"| node_pdf_loader
node_debug_pipeline -.->|"traces"| node_retriever
node_reset_qdrant -.->|"clears"| node_qdrant

click node_run_py "https://github.com/sergiocipher/pdf_peter/blob/main/run.py"
click node_main_py "https://github.com/sergiocipher/pdf_peter/blob/main/app/main.py"
click node_upload_api "https://github.com/sergiocipher/pdf_peter/blob/main/app/api/upload.py"
click node_chat_api "https://github.com/sergiocipher/pdf_peter/blob/main/app/api/chat.py"
click node_config_py "https://github.com/sergiocipher/pdf_peter/blob/main/app/core/config.py"
click node_prompts_py "https://github.com/sergiocipher/pdf_peter/blob/main/app/core/prompts.py"
click node_pdf_loader "https://github.com/sergiocipher/pdf_peter/blob/main/app/services/pdf_loader.py"
click node_chunking "https://github.com/sergiocipher/pdf_peter/blob/main/app/services/chunking.py"
click node_embeddings "https://github.com/sergiocipher/pdf_peter/blob/main/app/services/embeddings.py"
click node_vector_store "https://github.com/sergiocipher/pdf_peter/blob/main/app/services/vector_store.py"
click node_retriever "https://github.com/sergiocipher/pdf_peter/blob/main/app/services/retriever.py"
click node_llm "https://github.com/sergiocipher/pdf_peter/blob/main/app/services/llm.py"
click node_frontend_app "https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/App.jsx"
click node_upload_box "https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/components/UploadBox.jsx"
click node_chat_box "https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/components/ChatBox.jsx"
click node_message_bubble "https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/components/MessageBubble.jsx"
click node_api_js "https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/services/api.js"
click node_debug_pipeline "https://github.com/sergiocipher/pdf_peter/blob/main/debug_pipeline.py"
click node_reset_qdrant "https://github.com/sergiocipher/pdf_peter/blob/main/reset_qdrant.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_frontend_app,node_upload_box,node_chat_box,node_message_bubble,node_api_js toneBlue
class node_run_py,node_main_py,node_upload_api,node_chat_api,node_config_py,node_prompts_py,node_pdf_loader,node_chunking,node_embeddings,node_vector_store,node_retriever,node_llm toneAmber
class node_gemini,node_qdrant,node_groq toneMint
class node_debug_pipeline,node_reset_qdrant toneRose
```

## Components

### Frontend

Browser UI and client-side API calls.

- [React app](https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/App.jsx): UI shell · Top-level browser application composition. · `frontend/src/App.jsx`
- [Upload box](https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/components/UploadBox.jsx): upload UI · User interface for selecting and sending PDFs. · `frontend/src/components/UploadBox.jsx`
- [Chat box](https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/components/ChatBox.jsx): chat UI · Conversation panel for asking questions and viewing replies. · `frontend/src/components/ChatBox.jsx`
- [Message bubble](https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/components/MessageBubble.jsx): ui component · Renders individual chat messages. · `frontend/src/components/MessageBubble.jsx`
- [API client](https://github.com/sergiocipher/pdf_peter/blob/main/frontend/src/services/api.js): http client · Browser-side wrapper for backend API requests. · `frontend/src/services/api.js`

### Backend

FastAPI orchestration, ingestion, retrieval, and answer generation.

- [run.py](https://github.com/sergiocipher/pdf_peter/blob/main/run.py): startup · Local backend startup wrapper. · `run.py`
- [FastAPI app](https://github.com/sergiocipher/pdf_peter/blob/main/app/main.py): api assembly · Assembles the FastAPI application and mounts routes. · `app/main.py`
- [Upload API](https://github.com/sergiocipher/pdf_peter/blob/main/app/api/upload.py): route · HTTP endpoint for PDF ingestion and indexing. · `app/api/upload.py`
- [Chat API](https://github.com/sergiocipher/pdf_peter/blob/main/app/api/chat.py): route · HTTP endpoint for questions and grounded answers. · `app/api/chat.py`
- [Config](https://github.com/sergiocipher/pdf_peter/blob/main/app/core/config.py): settings · Central runtime configuration and environment settings. · `app/core/config.py`
- [Prompts](https://github.com/sergiocipher/pdf_peter/blob/main/app/core/prompts.py): prompt policy · System prompt templates and answer constraints. · `app/core/prompts.py`
- [PDF loader](https://github.com/sergiocipher/pdf_peter/blob/main/app/services/pdf_loader.py): extractor · Extracts text from uploaded PDFs. · `app/services/pdf_loader.py`
- [Chunking](https://github.com/sergiocipher/pdf_peter/blob/main/app/services/chunking.py): splitter · Splits extracted text into overlapping chunks. · `app/services/chunking.py`
- [Embeddings](https://github.com/sergiocipher/pdf_peter/blob/main/app/services/embeddings.py): vectorizer · Turns text chunks and queries into vectors. · `app/services/embeddings.py`
- [Vector store](https://github.com/sergiocipher/pdf_peter/blob/main/app/services/vector_store.py): index writer · Persists vectors and metadata into Qdrant. · `app/services/vector_store.py`
- [Retriever](https://github.com/sergiocipher/pdf_peter/blob/main/app/services/retriever.py): search · Finds relevant chunks from Qdrant for a question. · `app/services/retriever.py`
- [LLM client](https://github.com/sergiocipher/pdf_peter/blob/main/app/services/llm.py): generation · Calls the model with retrieved context to produce an answer. · `app/services/llm.py`

### External services

Managed AI and storage dependencies used at runtime.

- **Gemini embeddings**: external model · Hosted embedding service used for vectorization.
- **Qdrant**: vector db · External vector database for storage and retrieval.
- **Groq**: external LLM · Hosted inference service for answer generation.

### Ops

Local utilities for debugging and maintenance.

- [Debug pipeline](https://github.com/sergiocipher/pdf_peter/blob/main/debug_pipeline.py): utility · End-to-end ingestion and retrieval tracing helper. · `debug_pipeline.py`
- [Reset Qdrant](https://github.com/sergiocipher/pdf_peter/blob/main/reset_qdrant.py): admin tool · Utility for clearing vector store state. · `reset_qdrant.py`

## Connections

- run.py → FastAPI app: starts · Launches the FastAPI application.
- FastAPI app → Upload API: mounts · Registers the upload route module.
- FastAPI app → Chat API: mounts · Registers the chat route module.
- FastAPI app → Config: loads · Reads runtime configuration.
- Upload API → PDF loader: extracts · Converts uploaded PDFs into text.
- Upload API → Chunking: splits · Breaks extracted text into chunks.
- Upload API → Embeddings: vectorizes · Creates embeddings for chunks.
- Upload API → Vector store: indexes · Writes vectors and metadata to the index.
- PDF loader → Chunking: feeds · Text extraction feeds chunking.
- Chunking → Embeddings: feeds · Chunks are embedded for retrieval.
- Embeddings → Gemini embeddings: uses · Delegates vector generation to Gemini.
- Vector store → Qdrant: stores · Persists indexed content in Qdrant.
- Chat API → Retriever: retrieves · Finds relevant context for the user question.
- Chat API → LLM client: answers · Generates a grounded answer from context.
- Retriever → Embeddings: embeds · Embeds the user query before search.
- Retriever → Qdrant: queries · Searches the vector database for matches.
- LLM client → Prompts: applies · Uses prompt templates to constrain answers.
- LLM client → Groq: calls · Sends the prompt to the hosted LLM.
- React app → Upload box: contains · Composes upload UI.
- React app → Chat box: contains · Composes chat UI.
- Chat box → Message bubble: renders · Displays individual messages.
- Upload box → API client: uploads · Sends PDFs to the backend.
- Chat box → API client: asks · Sends questions to the backend.
- API client → Upload API: calls · Browser requests the upload endpoint.
- API client → Chat API: calls · Browser requests the chat endpoint.
- Debug pipeline → PDF loader: traces · Exercises the extraction stage.
- Debug pipeline → Retriever: traces · Exercises the retrieval stage.
- Reset Qdrant → Qdrant: clears · Administrative reset of vector storage.
