# vijayendradwari/medqa architecture

> Architecture diagram of the GitHub repository vijayendradwari/medqa, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vijayendradwari/medqa
- Repository: https://github.com/vijayendradwari/medqa

## Overview

##### Main Components to Be Represented
- Model Layer:
- Custom sampling controls or stop sequences if necessary.
- Retrieval Layer:
- FAISS vector store component.
- Sentence-transformer embeddings that generate vectors for the documents.
- Top-k document retrieval process.
- Integration Layer:
- LangChain RetrievalQA chain that combines the outputs of the retrieval stage with the generation capabilities.
- Custom implementation (termed “LoRAMedicalLLM” in the README) connecting model and retrieval layers.
- Web Interface:
- Gradio-based front end to demonstrate the system interaction.
- External Data Sources:
- Mention the PubMedQA dataset as the primary data source for medical queries.

##### Relationships and Data Flow
- The query is forwarded to the Integration Layer where the LangChain chain processes it.
- The Integration Layer then interacts with the Retrieval Layer to fetch highly relevant documents using FAISS and sentence embeddings.
- Retrieved documents are sent along with the original query to the Model Layer.
- The Llama model, enhanced with LoRA fine-tuning, generates an evidence-based answer that is finally delivered back to the user via the Gradio web UI.

##### Architectural Patterns and Design Principles
- Modular Design: Each layer (Model, Retrieval, Integration, Frontend) is separated, making the system scalable and easy to update.
- RAG Pattern: The combination of retrieval (to fetch relevant evidence) and generation (to produce a final answer) is clearly identifiable.
- Clear Separation of Concerns: The design shows that data handling (retrieval) and language processing (generation) are distinct, yet integrated through an orchestration mechanism provided by LangChain.

##### Technologies, Frameworks, and Libraries
- Python (with version 3.8+ as specified).
- LangChain library for managing the RetrievalQA chain.
- FAISS for vectorized document retrieval.
- Sentence-transformers for text embeddings.
- Llama 3.1-8B model, along with LoRA fine-tuning.
- Gradio for building the interactive web application interface.
- (Any custom scripts detailed in the notebooks, such as MedQA_Llama3_1_8B_RAG and MedQA_Llama3_1_8B_RAG_gradioDemo.)

##### Detail
- That every essential component described in the README is represented.
- Avoid oversimplification; rather, break down the system into as many distinct components as possible to capture the entire architecture.
- This detailed breakdown will be critical for maintenance, enhancements, and onboarding new team members.

## Diagram (Mermaid)

```mermaid
flowchart TB
    User["User/Query Input"]:::user

    subgraph "Web Interface"
        Gradio["Gradio Web Interface"]:::frontend
    end

    subgraph "Integration Layer"
        Integration["LangChain RetrievalQA chain + LoRAMedicalLLM"]:::integration
    end

    subgraph "Retrieval Layer"
        Retrieval["FAISS + Sentence-Transformers\n(top-k Retrieval)"]:::retrieval
    end

    subgraph "Model Layer"
        Model["Llama 3.1-8B with LoRA Fine-Tuning"]:::model
    end

    subgraph "External Data Sources"
        PubMed["PubMedQA Dataset"]:::external
        Docs["Project Documentation"]:::doc
    end

    User -->|"providesQuery"| Gradio
    Gradio -->|"sendsQuery"| Integration
    Integration -->|"triggersRetrieval"| Retrieval
    PubMed -->|"suppliesData"| Retrieval
    Retrieval -->|"returnsDocs"| Integration
    Integration -->|"queriesWithEvidence"| Model
    Model -->|"generatesAnswer"| Integration
    Integration -->|"sendsAnswer"| Gradio
    Gradio -->|"displaysAnswer"| User
    Docs ---|"reference"| Integration

    click Model "https://github.com/vijayendradwari/medqa/blob/main/MedQA_Llama3_1_8B.ipynb"
    click Integration "https://github.com/vijayendradwari/medqa/blob/main/MedQA_Llama3_1_8B_RAG.ipynb"
    click Retrieval "https://github.com/vijayendradwari/medqa/blob/main/MedQA_Llama3_1_8B_RAG.ipynb"
    click Gradio "https://github.com/vijayendradwari/medqa/blob/main/MedQA_Llama3_1_8B_RAG_gradioDemo.ipynb"
    click Docs "https://github.com/vijayendradwari/medqa/blob/main/README.md"

    classDef user fill:#E6E6FA,stroke:#333,stroke-width:2px;
    classDef frontend fill:#FFCCCC,stroke:#333,stroke-width:2px;
    classDef integration fill:#CCFFCC,stroke:#333,stroke-width:2px;
    classDef retrieval fill:#CCCCFF,stroke:#333,stroke-width:2px;
    classDef model fill:#FFE4B5,stroke:#333,stroke-width:2px;
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:2px;
    classDef doc fill:#F0E68C,stroke:#333,stroke-width:2px;
```
