# nelsonlin0321/webdev-rag-backend-api architecture

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

- Interactive diagram: https://gitdiagram.com/nelsonlin0321/webdev-rag-backend-api
- Repository: https://github.com/nelsonlin0321/webdev-rag-backend-api

## Overview

This FastAPI service ingests PDFs and answers document-grounded questions. Ingestion checks for an existing document, saves and uploads the PDF, extracts page text and embeddings, then stores document and embedding records. Retrieval embeds the supplied context (or question), combines vector and keyword search results, and sends retrieved passages to OpenAI for an answer with a page number. The graph separates API orchestration, document processing, retrieval/generation, and persistence. Source excerpts show the main calls; unsampled behavior is not inferred.

Main entry points: [`app/server.py`](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/server.py) (Answer endpoint), [`app/mongodb_engine.py`](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py) (Hybrid search), [`app/processor.py`](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/processor.py) (PDF processing), [`app/payload.py`](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/payload.py) (Request payload)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API orchestration"]
  node_ingest_api["Ingest endpoint<br/>[server.py]"]
  node_rag_api["Answer endpoint<br/>[server.py]"]
  node_health["Health endpoint<br/>[server.py]"]
  node_payload["Request payload<br/>[payload.py]"]
end

subgraph group_ingest["Document ingestion"]
  node_file_utils["File handling<br/>[utils.py]"]
  node_pdf_processor["PDF processing<br/>[processor.py]"]
  node_pdf_reader["PDF reader"]
  node_embedding_api["Embedding service"]
  node_s3[("S3 file storage")]
end

subgraph group_rag["Retrieval and generation"]
  node_search["Hybrid search<br/>[mongodb_engine.py]"]
  node_vector_search["Vector search<br/>[mongodb_engine.py]"]
  node_keyword_search["Keyword search<br/>[mongodb_engine.py]"]
  node_result_fusion["Result fusion<br/>[mongodb_engine.py]"]
  node_prompt_utils["Prompt builder<br/>[utils.py]"]
  node_page_parser["Page parser<br/>[utils.py]"]
  node_openai["OpenAI chat"]
end

subgraph group_data["Persistence"]
  node_mongo_engine["MongoDB client<br/>[mongodb_engine.py]"]
  node_mongo[("MongoDB")]
end

node_user(("API client"))

node_user -->|"uploads PDF"| node_ingest_api
node_user -->|"asks question"| node_rag_api
node_user -->|"checks status"| node_health
node_ingest_api -->|"saves file"| node_file_utils
node_ingest_api -->|"uploads file"| node_s3
node_ingest_api -->|"creates embeddings"| node_pdf_processor
node_pdf_processor -->|"loads PDF"| node_pdf_reader
node_pdf_processor -->|"requests embeddings"| node_embedding_api
node_ingest_api -->|"checks and stores"| node_mongo_engine
node_rag_api -->|"accepts request"| node_payload
node_rag_api -->|"embeds query"| node_pdf_processor
node_rag_api -->|"retrieves passages"| node_search
node_search -->|"runs vector search"| node_vector_search
node_search -->|"runs keyword search"| node_keyword_search
node_search -->|"combines results"| node_result_fusion
node_rag_api -->|"builds prompt"| node_prompt_utils
node_rag_api -->|"requests answer"| node_openai
node_rag_api -->|"parses page number"| node_page_parser
node_mongo_engine -->|"reads and writes"| node_mongo
node_vector_search -->|"queries vectors"| node_mongo
node_keyword_search -->|"queries keywords"| node_mongo

click node_ingest_api "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/server.py"
click node_rag_api "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/server.py"
click node_health "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/server.py"
click node_payload "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/payload.py"
click node_file_utils "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/utils.py"
click node_pdf_processor "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/processor.py"
click node_search "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py"
click node_vector_search "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py"
click node_keyword_search "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py"
click node_result_fusion "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py"
click node_prompt_utils "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/utils.py"
click node_page_parser "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/utils.py"
click node_mongo_engine "https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.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_ingest_api,node_rag_api,node_health,node_payload,node_user toneBlue
class node_file_utils,node_pdf_processor,node_pdf_reader,node_embedding_api,node_s3 toneAmber
class node_search,node_vector_search,node_keyword_search,node_result_fusion,node_prompt_utils,node_page_parser,node_openai toneMint
class node_mongo_engine,node_mongo toneRose
```

## Components

### API orchestration

- [Ingest endpoint](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/server.py): `app/server.py`
- [Answer endpoint](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/server.py): `app/server.py`
- [Health endpoint](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/server.py): `app/server.py`
- [Request payload](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/payload.py): `app/payload.py`

### Document ingestion

- [File handling](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/utils.py): `app/utils.py`
- [PDF processing](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/processor.py): `app/processor.py`
- **PDF reader**
- **Embedding service**
- **S3 file storage**

### Retrieval and generation

- [Hybrid search](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py): `app/mongodb_engine.py`
- [Vector search](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py): `app/mongodb_engine.py`
- [Keyword search](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py): `app/mongodb_engine.py`
- [Result fusion](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py): `app/mongodb_engine.py`
- [Prompt builder](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/utils.py): `app/utils.py`
- [Page parser](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/utils.py): `app/utils.py`
- **OpenAI chat**

### Persistence

- [MongoDB client](https://github.com/nelsonlin0321/webdev-rag-backend-api/blob/main/app/mongodb_engine.py): `app/mongodb_engine.py`
- **MongoDB**

### Other components

- **API client**

## Connections

- API client → Ingest endpoint: uploads PDF (evidence: `README.md`)
- API client → Answer endpoint: asks question (evidence: `README.md`)
- API client → Health endpoint: checks status (evidence: `app/server.py`)
- Ingest endpoint → File handling: saves file (evidence: `app/server.py`)
- Ingest endpoint → S3 file storage: uploads file (evidence: `app/server.py`)
- Ingest endpoint → PDF processing: creates embeddings (evidence: `app/server.py`)
- PDF processing → PDF reader: loads PDF (evidence: `app/processor.py`)
- PDF processing → Embedding service: requests embeddings (evidence: `app/processor.py`)
- Ingest endpoint → MongoDB client: checks and stores (evidence: `app/server.py`)
- Answer endpoint → Request payload: accepts request (evidence: `app/server.py`)
- Answer endpoint → PDF processing: embeds query (evidence: `app/server.py`)
- Answer endpoint → Hybrid search: retrieves passages (evidence: `app/server.py`)
- Hybrid search → Vector search: runs vector search (evidence: `app/mongodb_engine.py`)
- Hybrid search → Keyword search: runs keyword search (evidence: `app/mongodb_engine.py`)
- Hybrid search → Result fusion: combines results (evidence: `app/mongodb_engine.py`)
- Answer endpoint → Prompt builder: builds prompt (evidence: `app/server.py`)
- Answer endpoint → OpenAI chat: requests answer (evidence: `app/server.py`)
- Answer endpoint → Page parser: parses page number (evidence: `app/server.py`)
- MongoDB client → MongoDB: reads and writes (evidence: `app/mongodb_engine.py`)
- Vector search → MongoDB: queries vectors (evidence: `app/mongodb_engine.py`)
- Keyword search → MongoDB: queries keywords (evidence: `app/mongodb_engine.py`)
