# basheerahmedshk/medical-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/basheerahmedshk/medical-chatbot
- Repository: https://github.com/basheerahmedshk/medical-chatbot

## Overview

##### Project Type and Purpose
- MediBot is a full-stack Retrieval-Augmented Generation (RAG) system for answering medical queries.
- It consists of three logical phases: Memory Setup (vector DB build), Retrieval + LLM integration, and Chatbot UI.
- Key technologies: Python, FAISS, LangChain, Streamlit, LLMs (Mistral-7B / LLaMA-4), SentenceTransformers, PyPDF2.

##### File Structure
- Top-level scripts:
- create_memory_for_llm.py – PDF → text chunks → embeddings → FAISS index
- connect_memory_with_llm.py – RetrievalQA pipeline connecting FAISS to an LLM
- medibot.py – Streamlit UI exposing the chat interface
- data/ – source PDFs
- vectorstore/ – persisted FAISS index files
- requirements.txt – dependency list
- README.md, Overview.md, presentation.pptx – documentation and slide deck

##### README Insights
- Phases described in README map directly to the three main components.
- Emphasis on cached FAISS index for speed, real-time streaming, and source citation.
- Mentions LangChain RetrievalQA, safe prompting, and the chosen embedding model.

##### Main Components and Relationships
a. Memory Setup Component
- Inputs: PDFs from data/
- Subcomponents:
- PDF Parser (PyPDF2)
- Text Chunker
- Embedding Generator (SentenceTransformers all-MiniLM-L6-v2)
- FAISS Vector Store (IndexFlatIP)
- Outputs: vectorstore/db_faiss/index.faiss + index.pkl

b. Retrieval + LLM Component
- Inputs: user query, FAISS index
- Subcomponents:
- FAISS Retriever (k=3)
- RetrievalQA Pipeline (LangChain)
- LLM Client (Mistral-7B or LLaMA-4 via Groq)
- Outputs: answer text + source chunk citations

c. Chatbot UI Component
- Built with Streamlit (medibot.py)
- Features: session history, real-time streaming, source display
- Connects to Retrieval + LLM component via local function calls or API

##### Architectural Patterns and Principles
- Layered/pipeline architecture: ingestion → indexing → retrieval → generation → presentation
- Separation of concerns: batch indexing separated from real-time query pipeline
- Stateless UI: Streamlit app calls pure functions for retrieval and generation
- Modular: each phase can be replaced or scaled independently (e.g., swap FAISS for another vector DB)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Indexing Zone
    subgraph "Indexing Zone"
        direction TB
        createMemoryScript["create_memory_for_llm.py"]:::indexing
        Data["data/"]:::storage
        Parser["PDF Parser\n(PyPDF2)"]:::indexing
        Chunker["Text Chunker"]:::indexing
        Embed["Embedding Generator\n(all-MiniLM-L6-v2)"]:::indexing
        FAISSBuild["FAISS VectorStore\n(IndexFlatIP)"]:::indexing
        FAISSStorage["FAISS Index\n(index.faiss & index.pkl)"]:::storage
    end

    %% RAG Zone
    subgraph "RAG Zone"
        direction TB
        connectScript["connect_memory_with_llm.py"]:::rag
        Retriever["Retriever (k=3)"]:::rag
        QA["RetrievalQA Pipeline\n(LangChain)"]:::rag
        LLM["LLM Client\n(Mistral-7B/LLaMA-4)"]:::external
    end

    %% UI Zone
    subgraph "UI Zone"
        direction TB
        uiScript["medibot.py\n(Streamlit UI)"]:::ui
        Browser["User Browser"]:::ui
    end

    %% Documentation
    subgraph "Documentation & Config"
        direction TB
        README["README.md"]:::docs
        Overview["Overview.md"]:::docs
        Requirements["requirements.txt"]:::docs
        PPT["presentation.pptx"]:::docs
    end

    %% Connections - Indexing
    createMemoryScript -->|runs| Parser
    Parser -->|extract text| Chunker
    Chunker -->|chunk text| Embed
    Embed -->|384-dim embeddings| FAISSBuild
    FAISSBuild -->|store index| FAISSStorage

    %% Connections - RAG
    FAISSStorage -->|load index| connectScript
    connectScript -->|setup retriever| Retriever
    Retriever -->|k=3 retrieval| QA
    QA -->|queries| LLM
    LLM -->|response| QA

    %% Connections - UI/User Flow
    Browser -->|user query| uiScript
    uiScript -->|call pipeline| connectScript
    connectScript --> QA
    QA -->|answer+citations| uiScript
    uiScript -->|stream responses| Browser

    %% Click Events
    click createMemoryScript "https://github.com/basheerahmedshk/medical-chatbot/blob/main/create_memory_for_llm.py"
    click Data "https://github.com/basheerahmedshk/medical-chatbot/tree/main/data/"
    click FAISSStorage "https://github.com/basheerahmedshk/medical-chatbot/blob/main/vectorstore/db_faiss/index.faiss"
    click FAISSStorage "https://github.com/basheerahmedshk/medical-chatbot/blob/main/vectorstore/db_faiss/index.pkl"
    click connectScript "https://github.com/basheerahmedshk/medical-chatbot/blob/main/connect_memory_with_llm.py"
    click uiScript "https://github.com/basheerahmedshk/medical-chatbot/blob/main/medibot.py"
    click README "https://github.com/basheerahmedshk/medical-chatbot/blob/main/README.md"
    click Overview "https://github.com/basheerahmedshk/medical-chatbot/blob/main/Overview.md"
    click Requirements "https://github.com/basheerahmedshk/medical-chatbot/blob/main/requirements.txt"
    click PPT "https://github.com/basheerahmedshk/medical-chatbot/blob/main/presentation.pptx"

    %% Styles
    classDef indexing fill:#D0E8FF,stroke:#0288D1,stroke-width:1px
    classDef rag fill:#D0F2D8,stroke:#2E7D32,stroke-width:1px
    classDef ui fill:#FFEFD5,stroke:#E65100,stroke-width:1px
    classDef storage fill:#F3E5F5,stroke:#6A1B9A,stroke-width:1px
    classDef external fill:#FFEBEE,stroke:#C62828,stroke-width:1px
    classDef docs fill:#ECEFF1,stroke:#607D8B,stroke-width:1px
```
