# priyanshsaxena01/pubmed-multimodal-rag architecture

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

- Interactive diagram: https://gitdiagram.com/priyanshsaxena01/pubmed-multimodal-rag
- Repository: https://github.com/priyanshsaxena01/pubmed-multimodal-rag

## Overview

1. **Repository purpose**
   - This repo is a multimodal medical RAG system for PubMed-based Q&A over text and images.
   - It has two distinct concerns: **inference serving** and **model training/preparation**.

2. **Top-level runtime split**
   - The live system is split across:
     - a **cloud-hosted UI/API container** for the user-facing app, and
     - a **DGX GPU backend** for embeddings, reranking, and generation.
   - The cloud side is intentionally lightweight; the heavy models stay on the DGX.

3. **User-facing frontend**
   - `frontend.py` is the **Streamlit** interface.
   - It handles chat UI, file upload, image upload/preview, and streaming responses.
   - It does not run model inference locally; it forwards requests to the backend APIs.

4. **Central orchestrator**
   - `orchestrator.py` is the main application logic and RAG coordinator, exposed as a **FastAPI** service.
   - It owns the request lifecycle: document ingestion, indexing, retrieval, reranking, prompt construction, and forwarding to the generator.
   - It is the key boundary between UI and model services.

5. **Knowledge ingestion path**
   - Uploaded PDFs and text files are parsed and chunked.
   - PDFs are also mined for embedded images, which are treated as retrievable items alongside text chunks.
   - Each chunk/image is tagged with source metadata such as filename and page number for later citation.

6. **Retrieval index**
   - The orchestrator keeps a session-scoped **FAISS** vector index in memory.
   - Text and image items are embedded into a shared multimodal space, then stored with parallel metadata records.
   - This is the fast first-stage retrieval layer.

7. **Retrieval models service**
   - `support_apis.py` exposes the retrieval models behind separate endpoints:
     - `/embed` for multimodal embeddings
     - `/rerank` for cross-encoder reranking
   - These run on a separate GPU from generation to avoid memory contention and allow independent scaling.

8. **RAG query flow**
   - A user query goes through:
     - embedding of the query,
     - FAISS top-K search,
     - reranking of the candidates,
     - selection of the top few results,
     - prompt assembly with retrieved context and strict system instructions.
   - The final answer is streamed back to the frontend token-by-token.

9. **Generation backend**
   - The answer generator is a fine-tuned **Qwen3-VL-4B-Instruct** model served through **LMDeploy**.
   - It runs on a separate DGX GPU from the retrieval models.
   - The model is treated as an OpenAI-compatible API endpoint from the orchestrator’s perspective.

10. **Training pipeline**
    - `train.py` is the offline fine-tuning entrypoint.
    - It trains on `PubMedVision-enhanced`, using **LoRA** on top of a quantized base model.
    - The resulting adapter weights are merged by `merge_lora.py` into a standalone inference model for serving.

11. **Model wrapper submodule**
    - `qwen3_vl_wrapper/` is a vendored dependency bundle for Qwen3-VL embedding/reranker tooling.
    - It contains model wrappers, notebooks, and evaluation scripts.
    - In the production path, it mainly supports `support_apis.py`.

12. **Prompt and compatibility patches**
    - `patch_system_prompt.py` enforces the clinical-answering constraints used by the orchestrator.
    - `patch_wrapper.py` fixes wrapper path issues so the reranker can load correctly outside its original working directory.

13. **Deployment shape**
    - The repo includes a containerized deployment path with **Streamlit + FastAPI** in one image.
    - The frontend container exposes the UI while calling external DGX endpoints via **Cloudflare tunnels**.
    - A Kubernetes/GKE deployment is also documented for scaling and public exposure.

14. **Non-runtime assets**
    - The evaluation notebooks, scripts, and generated result artifacts under `qwen3_vl_wrapper/examples` are supporting material, not part of the request path.
    - They help validate the retrieval models but do not sit on the critical inference chain.

Main entry points: [`orchestrator.py`](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/orchestrator.py) (Orchestrator), [`support_apis.py`](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/support_apis.py) (Support APIs), [`qwen3_vl_wrapper/src/evaluation/mmeb_v2`](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/tree/main/qwen3_vl_wrapper/src/evaluation/mmeb_v2) (Evaluation), [`qwen3_vl_wrapper/src/models/qwen3_vl_embedding.py`](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/qwen3_vl_wrapper/src/models/qwen3_vl_embedding.py) (Embed model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cloud["Cloud app"]
  node_frontend["Frontend<br/>Streamlit UI<br/>[frontend.py]"]
  node_orchestrator["Orchestrator<br/>FastAPI RAG<br/>[orchestrator.py]"]
  node_patch_prompt["Prompt patch"]
  node_patch_wrapper["Wrapper patch<br/>path fix patch<br/>[patch_wrapper.py]"]
  node_ingest_pipe["Ingestion<br/>parse and chunk"]
  node_query_flow["Query flow<br/>RAG pipeline"]
end

subgraph group_dgx["DGX inference"]
  node_support_apis["Support APIs<br/>GPU model APIs<br/>[support_apis.py]"]
  node_embed_api(("Embed API<br/>/embed endpoint<br/>[support_apis.py]"))
  node_rerank_api(("Rerank API<br/>/rerank endpoint<br/>[support_apis.py]"))
  node_gen_backend["Generator<br/>LMDeploy service"]
  node_index_store[("Vector index<br/>FAISS session index")]
end

subgraph group_training["Training pipeline"]
  node_train["Training<br/>LoRA fine-tune<br/>[train.py]"]
  node_merge_lora["Merge LoRA<br/>adapter merge<br/>[merge_lora.py]"]
end

subgraph group_wrapper["Qwen3 wrapper"]
  node_wrapper_main["Wrapper entry<br/>package entrypoint<br/>[main.py]"]
  node_embed_model["Embed model<br/>Qwen3-VL embedding"]
  node_rerank_model["Rerank model<br/>Qwen3-VL reranker"]
  node_evaluation["Evaluation<br/>MMEB v2"]
end

node_frontend -->|"chat + uploads"| node_orchestrator
node_orchestrator -->|"ingest docs"| node_ingest_pipe
node_ingest_pipe -->|"store items"| node_index_store
node_orchestrator -->|"retrieve"| node_index_store
node_orchestrator -->|"model calls"| node_support_apis
node_support_apis -->|"embed"| node_embed_api
node_support_apis -->|"rerank"| node_rerank_api
node_embed_api -->|"uses"| node_embed_model
node_rerank_api -->|"uses"| node_rerank_model
node_orchestrator -->|"route query"| node_query_flow
node_query_flow -->|"embed/rerank"| node_support_apis
node_query_flow -->|"generate"| node_gen_backend
node_orchestrator -->|"answer stream"| node_gen_backend
node_train -->|"export model"| node_merge_lora
node_merge_lora -->|"serve weights"| node_gen_backend
node_patch_prompt -->|"policy"| node_orchestrator
node_patch_wrapper -->|"load fix"| node_support_apis
node_wrapper_main -->|"package"| node_embed_model
node_wrapper_main -->|"package"| node_rerank_model
node_evaluation -.->|"benchmarks"| node_embed_model
node_evaluation -.->|"benchmarks"| node_rerank_model
node_support_apis -.->|"reference"| node_evaluation

click node_frontend "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/frontend.py"
click node_orchestrator "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/orchestrator.py"
click node_patch_prompt "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/patch_system_prompt.py"
click node_patch_wrapper "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/patch_wrapper.py"
click node_support_apis "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/support_apis.py"
click node_embed_api "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/support_apis.py"
click node_rerank_api "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/support_apis.py"
click node_train "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/train.py"
click node_merge_lora "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/merge_lora.py"
click node_wrapper_main "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/qwen3_vl_wrapper/main.py"
click node_embed_model "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/qwen3_vl_wrapper/src/models/qwen3_vl_embedding.py"
click node_rerank_model "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/qwen3_vl_wrapper/src/models/qwen3_vl_reranker.py"
click node_evaluation "https://github.com/priyanshsaxena01/pubmed-multimodal-rag/tree/main/qwen3_vl_wrapper/src/evaluation/mmeb_v2"

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,node_orchestrator,node_patch_prompt,node_patch_wrapper,node_ingest_pipe,node_query_flow toneBlue
class node_support_apis,node_embed_api,node_rerank_api,node_gen_backend,node_index_store toneAmber
class node_train,node_merge_lora toneMint
class node_wrapper_main,node_embed_model,node_rerank_model,node_evaluation toneRose
```

## Components

### Cloud app

Lightweight user-facing container that serves the UI and forwards work to the DGX services.

- [Frontend](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/frontend.py): Streamlit UI · Chat interface, file upload, image preview, and streamed answers. · `frontend.py`
- [Orchestrator](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/orchestrator.py): FastAPI RAG · Owns ingestion, indexing, retrieval, reranking, prompt assembly, and answer streaming. · `orchestrator.py`
- [Prompt patch](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/patch_system_prompt.py): system prompt patch · Enforces the clinical-answering system constraints used by the orchestrator. · `patch_system_prompt.py`
- [Wrapper patch](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/patch_wrapper.py): path fix patch · Adjusts wrapper loading so reranker code resolves correctly outside its original working directory. · `patch_wrapper.py`
- **Ingestion**: parse and chunk · Parses PDFs and text, chunks content, and extracts embedded images with source metadata.
- **Query flow**: RAG pipeline · Embeds the query, searches FAISS, reranks candidates, and selects final context.

### DGX inference

GPU-backed runtime for retrieval models, reranking, and generation.

- [Support APIs](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/support_apis.py): GPU model APIs · Exposes multimodal embedding and reranking endpoints used by the orchestrator. · `support_apis.py`
- [Embed API](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/support_apis.py): /embed endpoint · Produces shared-space multimodal embeddings for text and images. · `support_apis.py`
- [Rerank API](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/support_apis.py): /rerank endpoint · Cross-encoder reranking over retrieved candidates. · `support_apis.py`
- **Generator**: LMDeploy service · Fine-tuned Qwen3-VL inference endpoint used for final answer generation.
- **Vector index**: FAISS session index · In-memory session-scoped multimodal retrieval index with parallel metadata records.

### Training pipeline

Offline fine-tuning and model packaging path.

- [Training](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/train.py): LoRA fine-tune · Offline fine-tuning entrypoint on PubMedVision-enhanced using a quantized base model. · `train.py`
- [Merge LoRA](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/merge_lora.py): adapter merge · Merges trained adapters into a standalone inference model for serving. · `merge_lora.py`

### Qwen3 wrapper

Vendored Qwen3-VL tooling used by the retrieval services and evaluation scripts.

- [Wrapper entry](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/qwen3_vl_wrapper/main.py): package entrypoint · Vendored Qwen3-VL package entrypoint for tooling and service integration. · `qwen3_vl_wrapper/main.py`
- [Embed model](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/qwen3_vl_wrapper/src/models/qwen3_vl_embedding.py): Qwen3-VL embedding · Model wrapper backing multimodal embedding generation. · `qwen3_vl_wrapper/src/models/qwen3_vl_embedding.py`
- [Rerank model](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/blob/main/qwen3_vl_wrapper/src/models/qwen3_vl_reranker.py): Qwen3-VL reranker · Model wrapper backing cross-encoder reranking. · `qwen3_vl_wrapper/src/models/qwen3_vl_reranker.py`
- [Evaluation](https://github.com/priyanshsaxena01/pubmed-multimodal-rag/tree/main/qwen3_vl_wrapper/src/evaluation/mmeb_v2): MMEB v2 · Benchmarks, notebooks, and scripts used to validate the wrapper models. · `qwen3_vl_wrapper/src/evaluation/mmeb_v2`

## Connections

- Frontend → Orchestrator: chat + uploads · The UI forwards user messages, files, and image selections to the RAG service.
- Orchestrator → Ingestion: ingest docs · Uploaded PDFs and text are parsed, chunked, and indexed with image extraction.
- Ingestion → Vector index: store items · Text chunks and image items are embedded and stored with metadata.
- Orchestrator → Vector index: retrieve · The orchestrator performs session-scoped similarity search against FAISS.
- Orchestrator → Support APIs: model calls · The orchestrator calls external retrieval services for embeddings and reranking.
- Support APIs → Embed API: embed · Serves multimodal embeddings for query and document items.
- Support APIs → Rerank API: rerank · Serves candidate reranking for retrieved passages and images.
- Embed API → Embed model: uses · Embedding endpoint is backed by the Qwen3-VL embedding wrapper.
- Rerank API → Rerank model: uses · Rerank endpoint is backed by the Qwen3-VL reranker wrapper.
- Orchestrator → Query flow: route query · User questions move through embedding, FAISS search, reranking, and context selection.
- Query flow → Support APIs: embed/rerank · Retrieval stages depend on the GPU model APIs.
- Query flow → Generator: generate · The final prompt is sent to the OpenAI-compatible generation service.
- Orchestrator → Generator: answer stream · Streaming tokens are proxied back to the UI.
- Training → Merge LoRA: export model · Training produces adapters that are merged for serving.
- Merge LoRA → Generator: serve weights · Merged weights are used by the generation backend.
- Prompt patch → Orchestrator: policy · Applies the clinical response constraints used during prompt construction.
- Wrapper patch → Support APIs: load fix · Fixes wrapper path resolution for retrieval service startup.
- Wrapper entry → Embed model: package · Wrapper package exposes the embedding model implementation.
- Wrapper entry → Rerank model: package · Wrapper package exposes the reranker model implementation.
- Evaluation → Embed model: benchmarks · Evaluation scripts validate the embedding model.
- Evaluation → Rerank model: benchmarks · Evaluation scripts validate the reranker model.
- Support APIs → Evaluation: reference · The vendored wrapper also contains non-runtime validation material.
