# entbappy/end-to-end-medical-chatbot-using-llama2 architecture

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

- Interactive diagram: https://gitdiagram.com/entbappy/end-to-end-medical-chatbot-using-llama2
- Repository: https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2

## Overview

##### Project type and purpose
- This is a full-stack, retrieval-augmented medical chatbot built with Python, Flask and LangChain.
- It ingests a medical PDF, indexes it in Pinecone, and serves chat queries to a local Llama 2 model via a Flask web UI.

##### File structure
Top-level files
- app.py – Flask application exposing REST endpoints for chat.
- store_index.py – ingestion pipeline that reads data/Medical_book.pdf, creates embeddings, and writes them to Pinecone.
- template.py – prompt‐template definitions.
- requirements.txt / setup.py – dependency and install scripts.
- .gitignore, LICENSE, README.md – project metadata.

Data and model
- data/Medical_book.pdf – the core knowledge source.
- model/instruction.txt – custom system prompt instructions for Llama 2.

Source code
- src/helper.py – utility functions (e.g., PDF loader, embedding helper).
- src/prompt.py – constructs LangChain prompt patterns.
- static/style.css & templates/chat.html – front-end UI assets.

Research
- research/trials.ipynb – exploratory notebooks.

##### Key README insights
- Uses LangChain for RAG.
- Flask for web server.
- Pinecone for vector store.
- Meta Llama2 (quantized .bin) as the local LLM.

##### Main components and interactions
a. Frontend (Browser)
- chat.html + style.css – user interface to send queries and show responses.
– communicates via HTTP(S) to Flask backend.

b. Backend (Flask app.py)
- REST endpoint (/chat or root)
- Loads environment variables (.env) for Pinecone credentials.
- Uses helper functions to:
– Submit user query
– Retrieve context from Pinecone
– Construct prompt via template.py / src/prompt.py
– Call the local Llama 2 model (llama_cpp or similar) with model/instruction.txt

c. Vector Store (Pinecone)
- Stores document embeddings for Medical_book.pdf (created by store_index.py).
- Backend queries nearest neighbors given a new query embedding.

d. Ingestion Pipeline (store_index.py)
- Loads PDF from data/
- Splits into chunks, computes embeddings (via same embedding model), pushes to Pinecone.

e. Local LLM (Llama 2 .bin)
- Quantized model loaded by LangChain’s LLM wrapper.
- Receives system + user + context prompt and returns a response.

##### Architectural patterns and design principles
- Separation of concerns: ingestion vs. serving pipeline.
- Retrieval-augmented generation (RAG) pattern.
- Modular prompt engineering (template.py + src/prompt.py).
- Single-process Flask server suitable for prototyping, can evolve into microservices.

##### Technologies & frameworks to annotate
- Flask
- LangChain
- Pinecone
- Llama2 (ggml quantized)
- Conda environment for reproducibility
- HTML/CSS for frontend

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend
    Browser["Browser UI<br/>chat.html + style.css"]:::frontend
    click Browser "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/templates/chat.html"
    click Browser "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/static/style.css"

    %% Backend Service
    subgraph "Flask Backend Service"
        direction TB
        FlaskApp["Flask App<br/>(app.py)"]:::backend
        click FlaskApp "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/app.py"
        Env[".env<br/>(Pinecone credentials)"]:::config
        subgraph "Business Logic"
            direction TB
            Helper["Helper Utilities<br/>(src/helper.py)"]:::backend
            click Helper "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/src/helper.py"
            Template["Prompt Templates<br/>(template.py)"]:::backend
            click Template "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/template.py"
            PromptMod["Prompt Builder<br/>(src/prompt.py)"]:::backend
            click PromptMod "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/src/prompt.py"
        end
    end

    %% Vector Store
    Pinecone["Pinecone Vector DB"]:::storage

    %% Local LLM
    LLM["Llama 2 Model<br/>(model/instruction.txt)"]:::model
    click LLM "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/model/instruction.txt"

    %% Ingestion Pipeline
    subgraph "Ingestion Pipeline" 
        direction TB
        Ingest["store_index.py"]:::pipeline
        click Ingest "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/store_index.py"
        PDF["Medical_book.pdf"]:::document
        click PDF "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/data/Medical_book.pdf"
    end

    %% Dependencies & Research
    Req["requirements.txt"]:::config
    click Req "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/requirements.txt"
    Setup["setup.py"]:::config
    click Setup "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/setup.py"
    Research["research/trials.ipynb"]:::research
    click Research "https://github.com/entbappy/end-to-end-medical-chatbot-using-llama2/blob/main/research/trials.ipynb"

    %% Data Flows
    Browser -->|"HTTP POST Query"| FlaskApp
    FlaskApp -->|"Load Env"| Env
    FlaskApp -->|"Call Helper"| Helper
    FlaskApp -->|"Build Prompt"| Template
    Template --> PromptMod
    FlaskApp -->|"Query Embeddings"| Pinecone
    FlaskApp -->|"Call LLM"| LLM

    Ingest --> PDF
    Ingest -->|"Compute & Upsert Embeddings"| Pinecone

    %% Styles
    classDef frontend fill:#D2E5FF,stroke:#0366d6,color:#0366d6;
    classDef backend fill:#E8F0FE,stroke:#1A73E8,color:#1A73E8;
    classDef storage fill:#FFF4E5,stroke:#FFA726,color:#E65100;
    classDef model fill:#F3E5F5,stroke:#AB47BC,color:#6A1B9A;
    classDef pipeline fill:#E0F2F1,stroke:#26A69A,color:#004D40;
    classDef config fill:#F5F5F5,stroke:#9E9E9E,color:#424242;
    classDef document fill:#FFFDE7,stroke:#FDD835,color:#F9A825;
    classDef research fill:#EFEFEF,stroke:#607D8B,color:#455A64;
```
