# mchatzakis/chatmgl architecture

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

- Interactive diagram: https://gitdiagram.com/mchatzakis/chatmgl
- Repository: https://github.com/mchatzakis/chatmgl

## Overview

##### Project type and purpose
- This is an ML research/engineering pipeline: a large-language model (ChatMGL) fine-tuned on data-science Q&A.
- It includes data preparation, supervised & RLHF training, multiple reward models, evaluation, and a simple serving interface.

##### File structure
Top-level directories/files:
- data/  – raw and split JSON datasets (MATH, MathQA, Reward, StackExchange, interactions)
- dataset/ – combined supervised and RLHF dataset JSONs used during training
- models/ – pre-trained generative base (BART) and reward model checkpoints (custom BERT, DistilBERT, GPT-2)
- src/
– src/dataset/ (dataset classes: RLHF_dataset.py, generative_dataset.py)
– src/utils/ (utility functions)
– src/generative_model.py (GenerativeModel class: load and generate)
– src/reward_model.py (wrapper for reward models)
– src/gen_script_chatMGL.py (CLI for batch generation)
– src/RLHF_pipeline.ipynb & notebooks/ (pipeline and eval experiments)
- scripts: get_chatMGL.sh (download model via gdown), run.sh (invoke generation)
- config/deps: requirements.txt, python.txt

##### README
- Describes installation, model download via Google Drive (gdown), generation workflow (run.sh → gen_script_chatMGL.py), usage of GenerativeModel API, seed control for reproducibility.
- Mentions datasets in repo and links to full model.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data Sources
    RawData["Raw Data JSONs<br/>(MATH, MathQA, Reward, StackExchange, interactions)"]:::data
    click RawData "https://github.com/mchatzakis/chatmgl/tree/main/data/"

    %% Preprocessing
    subgraph "Preprocessing & Dataset Module"
        direction TB
        Utils["Utility Functions"]:::processing
        click Utils "https://github.com/mchatzakis/chatmgl/blob/main/src/utils/utils.py"
        GenDataset["Generative Dataset Class"]:::processing
        click GenDataset "https://github.com/mchatzakis/chatmgl/blob/main/src/dataset/generative_dataset.py"
        RLHFDataset["RLHF Dataset Class"]:::processing
        click RLHFDataset "https://github.com/mchatzakis/chatmgl/blob/main/src/dataset/RLHF_dataset.py"
    end

    RawData -->|"preprocess"| Utils
    Utils --> GenDataset
    Utils --> RLHFDataset

    %% Preprocessed Outputs
    subgraph "Preprocessed Datasets" 
        direction TB
        SupervData["Supervised JSONs"]:::data
        click SupervData "https://github.com/mchatzakis/chatmgl/tree/main/dataset/supervised/"
        RLHFData["RLHF JSONs"]:::data
        click RLHFData "https://github.com/mchatzakis/chatmgl/tree/main/dataset/RLHF/"
        TestData["Test JSONs"]:::data
        click TestData "https://github.com/mchatzakis/chatmgl/tree/main/dataset/Test/"
    end

    GenDataset --> SupervData
    RLHFDataset --> RLHFData
    RLHFDataset --> TestData

    %% Training Components
    subgraph "Training Components"
        direction TB
        subgraph "Supervised Fine-tuning"
            direction TB
            SupervisedFT["Fine-tune BART"]:::model
            click SupervisedFT "https://github.com/mchatzakis/chatmgl/tree/main/models/BART/"
        end

        subgraph "Reward Model Training"
            direction TB
            RewardWrapper["Reward Model Wrapper"]:::processing
            click RewardWrapper "https://github.com/mchatzakis/chatmgl/blob/main/src/reward_model.py"
            subgraph "Reward Model Checkpoints"
                direction TB
                BERT["Custom BERT"]:::model
                click BERT "https://github.com/mchatzakis/chatmgl/tree/main/models/reward_model/custom_bert/"
                DistilBERT["DistilBERT"]:::model
                click DistilBERT "https://github.com/mchatzakis/chatmgl/tree/main/models/reward_model/distilbert/lr5e-05-warmup0.2/"
                GPT2["GPT-2"]:::model
                click GPT2 "https://github.com/mchatzakis/chatmgl/tree/main/models/reward_model/gpt2/lr5e-05-warmup0.2/"
            end
        end

        RLHFPipeline["RLHF Pipeline Notebook"]:::processing
        click RLHFPipeline "https://github.com/mchatzakis/chatmgl/blob/main/src/RLHF_pipeline.ipynb"
    end

    SupervData --> SupervisedFT
    SupervData --> RewardWrapper
    RewardWrapper --> BERT
    RewardWrapper --> DistilBERT
    RewardWrapper --> GPT2
    SupervisedFT --> RLHFPipeline
    BERT --> RLHFPipeline
    DistilBERT --> RLHFPipeline
    GPT2 --> RLHFPipeline

    %% Model Artifacts
    ChatMGL["Final ChatMGL Checkpoint"]:::model
    click ChatMGL "https://github.com/mchatzakis/chatmgl/tree/main/models/chatMGL/"
    GetModel["Download Script"]:::external
    click GetModel "https://github.com/mchatzakis/chatmgl/blob/main/get_chatMGL.sh"

    RLHFPipeline --> ChatMGL
    GetModel --> ChatMGL

    %% Serving & Evaluation
    subgraph "Serving & Evaluation"
        direction TB
        RunSh["Batch Generation Script"]:::serve
        click RunSh "https://github.com/mchatzakis/chatmgl/blob/main/run.sh"
        GenScript["gen_script_chatMGL.py"]:::serve
        click GenScript "https://github.com/mchatzakis/chatmgl/blob/main/src/gen_script_chatMGL.py"
        API["GenerativeModel API"]:::serve
        click API "https://github.com/mchatzakis/chatmgl/blob/main/src/generative_model.py"
        Prompts["prompts.json"]:::data
        Answers["answers_chatMGL.json"]:::data
        Notebooks["Evaluation Notebooks"]:::serve
        click Notebooks "https://github.com/mchatzakis/chatmgl/tree/main/src/notebooks/"
        EvalNB["Evaluation Notebook"]:::serve
        click EvalNB "https://github.com/mchatzakis/chatmgl/blob/main/src/evaluation.ipynb"
    end

    ChatMGL --> RunSh
    RunSh --> GenScript
    GenScript --> Prompts
    GenScript --> Answers
    API --> ChatMGL
    ChatMGL --> API
    ChatMGL --> Notebooks
    Notebooks --> EvalNB

    %% External Services
    GoogleDrive["Google Drive"]:::external
    GetModel --> GoogleDrive

    %% Styles
    classDef data fill:#D6E9FF,stroke:#4D90FE,color:#000
    classDef processing fill:#E8F6E8,stroke:#28A745,color:#000
    classDef model fill:#FFE5CC,stroke:#FF8C00,color:#000
    classDef serve fill:#F2E8FF,stroke:#9B59B6,color:#000
    classDef external fill:#ECECEC,stroke:#AAAAAA,color:#000
```
