How priyanshsaxena01/pubmed-multimodal-rag works
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 Orchestrator · support_apis.py Support APIs · mmeb_v2 Evaluation · qwen3_vl_embedding.py Embed model
Components 17
Cloud app
Lightweight user-facing container that serves the UI and forwards work to the DGX services.
- Frontend · Chat interface, file upload, image preview, and streamed answers.
frontend.py - Orchestrator · Owns ingestion, indexing, retrieval, reranking, prompt assembly, and answer streaming.
orchestrator.py - Prompt patch · Enforces the clinical-answering system constraints used by the orchestrator.
patch_system_prompt.py - Wrapper patch · Adjusts wrapper loading so reranker code resolves correctly outside its original working directory.
patch_wrapper.py - Ingestion · Parses PDFs and text, chunks content, and extracts embedded images with source metadata.
- Query flow · Embeds the query, searches FAISS, reranks candidates, and selects final context.
DGX inference
GPU-backed runtime for retrieval models, reranking, and generation.
- Support APIs · Exposes multimodal embedding and reranking endpoints used by the orchestrator.
support_apis.py - Embed API · Produces shared-space multimodal embeddings for text and images.
support_apis.py - Rerank API · Cross-encoder reranking over retrieved candidates.
support_apis.py - Generator · Fine-tuned Qwen3-VL inference endpoint used for final answer generation.
- Vector index · In-memory session-scoped multimodal retrieval index with parallel metadata records.
Training pipeline
Offline fine-tuning and model packaging path.
- Training · Offline fine-tuning entrypoint on PubMedVision-enhanced using a quantized base model.
train.py - Merge LoRA · 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 · Vendored Qwen3-VL package entrypoint for tooling and service integration.
qwen3_vl_wrapper/main.py - Embed model · Model wrapper backing multimodal embedding generation.
qwen3_vl_wrapper/src/models/qwen3_vl_embedding.py - Rerank model · Model wrapper backing cross-encoder reranking.
qwen3_vl_wrapper/src/models/qwen3_vl_reranker.py - Evaluation · Benchmarks, notebooks, and scripts used to validate the wrapper models.
qwen3_vl_wrapper/src/evaluation/mmeb_v2
Connections 22
- 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.