# tyang816/medchatzh architecture

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

- Interactive diagram: https://gitdiagram.com/tyang816/medchatzh
- Repository: https://github.com/tyang816/medchatzh

## Overview

##### Project Type and Purpose
- This is a specialized full-stack application that fine-tunes a large language model (Baichuan-7B and variants) for Chinese medical dialogue.
- Its purpose is to provide an AI assistant that understands and responds to Traditional Chinese medical queries. The README emphasizes both dialogue and inference/deployment capabilities.

##### File Structure
- The top-level files such as .gitignore, LICENSE, README.md, and run_sf.py indicate standard project management.
- A “scripts” directory contains shell and Python scripts for various operations:
- Data conversion (convert_to_conv_data.py),
- Decryption and interfacing (decrypt.py/.sh, dialog.sh, interface.sh),
- Running inference (inference.sh) and reward scoring.
- The “src” folder holds the core logic:
- “configs” contains JSON configuration files for DeepSpeed and LoRA settings, indicating both distributed/deepspeed training/inference and parameter-efficient fine-tuning capabilities.
- “data” includes scripts for generating data from models.
- “eval” has modules for inference, evaluation metrics, and reward scoring.
- “dialog.py” and “interface.py” are key entry points for handling dialogue and deploying the web interface.
- The “models” directory is further split into subfolders for different underlying base models (baichuan and chatglm), each with configuration, tokenization, and modeling code. This segregation suggests that model-specific logic is cleanly separated from the rest of the system.

a. Main Components:
- User Interface/Frontend:
- Scripts like “dialog.sh” serve for simple user Q&A.
- “interface.sh” and “interface.py” provide the web deployment layer.
- Application Logic:
- Core dialogue handling in “src/dialog.py” (and possibly run_sf.py).
- Evaluation modules in “src/eval” (for inference, CEval, metrics, and reward scoring).
- Model Handling:
- “src/models” houses implementations for different model architectures (Baichuan and ChatGLM). Each submodule (configuration, tokenization, modeling) is a component.
- Configuration and Tuning:
- “src/configs” contains both DeepSpeed and LoRA configuration files, indicating system scaling and tuning parameters.
- Data Processing:
- Data generation scripts (“src/data/gen_data_from_model.py”) and additional processing scripts in the “scripts” folder.
b. Relationships and Data Flow:
- The user (or external system) interacts with the UI layer.
- The UI then calls the dialogue interface (src/dialog.py and src/interface.py) which routes requests to the appropriate model component.
- The model component uses its own configuration files (from src/configs) and tokenization modules (within each model’s folder) to process input and compute responses.
- Evaluation components (src/eval) take outputs from the model to compute metrics and apply reward scoring, possibly feeding back into training or further tuning.
- Scripts are provided as wrappers for operations – for instance, initiating inference, data conversion, and deploying the interface.
c. Architectural Patterns and Design Principles:
- Separation of Concerns: Different layers/modules (UI, business logic, model handling, evaluation, data processing) are clearly separated.
- Modularity: Model-specific code is grouped under the “models” directory, facilitating easy extension (e.g., adding new base model integrations).
- Configurability: Use of external JSON config files to handle runtime parameters (DeepSpeed, LoRA) allows flexible scaling and tuning.
d. Relevant Technologies/Frameworks/Libraries:
- DeepSpeed and LoRA for scalable fine-tuning and efficient training/inference.
- Underlying model-specific libraries for Baichuan-7B and ChatGLM.
- Shell scripts wrapping Python code for deployment and inference.
- The deployment layer likely utilizes standard Python web frameworks (though not explicitly named in this file tree, the presence of “interface.py” indicates a possible lightweight or custom web server).

##### Final Recommendations
- That every component is detailed and sufficiently labeled, so it’s immediately clear what role each component plays.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface/Frontend
    subgraph "User Interface/Frontend"
        DS("Dialog Script"):::frontend
        IS("Interface Script"):::frontend
        WI("Web Interface"):::frontend
    end

    %% Application Logic
    subgraph "Application Logic"
        DC("Dialogue Controller"):::app_logic
        RM("Run Manager"):::app_logic
    end

    %% Model Handling - Baichuan
    subgraph "Model Handling - Baichuan"
        BC_Config("Baichuan Config"):::model
        BC_Token("Baichuan Tokenization"):::model
        BC_Model("Baichuan Modeling"):::model
        BC_Handler("Baichuan Handler"):::model
    end

    %% Model Handling - ChatGLM
    subgraph "Model Handling - ChatGLM"
        CG_Config("ChatGLM Config"):::model
        CG_Token("ChatGLM Tokenization"):::model
        CG_Model("ChatGLM Modeling"):::model
        CG_Quant("ChatGLM Quantization"):::model
        CG_Test("ChatGLM Test"):::model
    end

    %% Configuration and Tuning
    subgraph "Configuration and Tuning"
        DS_Config("DeepSpeed Config"):::config
        DS_Config_Stage3("DeepSpeed Config Stage3"):::config
        Lora_Bloom("LoRA Config Bloom"):::config
        Lora_Llama("LoRA Config Llama"):::config
    end

    %% Evaluation Modules
    subgraph "Evaluation Modules"
        INF("Inference"):::evaluation
        INF_CE("Inference CEval"):::evaluation
        METRICS("Metrics Score"):::evaluation
        REWARD("Reward Score"):::evaluation
    end

    %% Data Processing
    subgraph "Data Processing"
        DATA_GEN("Data Generation"):::data
        DATA_CONV("Data Conversion"):::data
    end

    %% Data Flow Connections
    %% User Interface to Application Logic
    DS -->|"triggers"| DC
    IS -->|"triggers"| DC
    WI -->|"triggers"| DC
    RM -->|"manages"| DC

    %% Application Logic to Model Handling
    DC -->|"routesTo"| BC_Handler
    DC -->|"routesTo"| CG_Model

    %% Configuration to Model Handling
    DS_Config --> BC_Handler
    DS_Config_Stage3 --> BC_Handler
    Lora_Bloom --> BC_Handler
    Lora_Llama --> BC_Handler

    DS_Config --> CG_Config
    DS_Config_Stage3 --> CG_Config
    Lora_Bloom --> CG_Config
    Lora_Llama --> CG_Config

    %% Model Handling to Evaluation Modules
    BC_Handler -->|"outputs"| INF
    CG_Model -->|"outputs"| INF
    BC_Handler -->|"evaluatedBy"| METRICS
    BC_Handler -->|"evaluatedBy"| REWARD
    CG_Model -->|"evaluatedBy"| METRICS
    CG_Model -->|"evaluatedBy"| REWARD

    %% Data Processing Interactions
    DATA_CONV -->|"providesData"| DC
    DATA_GEN -->|"supports"| INF_CE

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef app_logic fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef model fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef config fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef evaluation fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef data fill:#fdfd96,stroke:#999900,stroke-width:2px;

    %% Click Events
    click DS "https://github.com/tyang816/medchatzh/blob/main/scripts/dialog.sh"
    click IS "https://github.com/tyang816/medchatzh/blob/main/scripts/interface.sh"
    click WI "https://github.com/tyang816/medchatzh/blob/main/src/interface.py"
    click DC "https://github.com/tyang816/medchatzh/blob/main/src/dialog.py"
    click RM "https://github.com/tyang816/medchatzh/blob/main/run_sf.py"
    click INF "https://github.com/tyang816/medchatzh/blob/main/src/eval/inference.py"
    click INF_CE "https://github.com/tyang816/medchatzh/blob/main/src/eval/inference_ceval.py"
    click METRICS "https://github.com/tyang816/medchatzh/blob/main/src/eval/metrics_score.py"
    click REWARD "https://github.com/tyang816/medchatzh/blob/main/src/eval/reward_score.py"
    click BC_Config "https://github.com/tyang816/medchatzh/blob/main/src/models/baichuan/configuration_baichuan.py"
    click BC_Token "https://github.com/tyang816/medchatzh/blob/main/src/models/baichuan/tokenization_baichuan.py"
    click BC_Model "https://github.com/tyang816/medchatzh/blob/main/src/models/baichuan/modeling_baichuan.py"
    click BC_Handler "https://github.com/tyang816/medchatzh/blob/main/src/models/baichuan/handler.py"
    click CG_Config "https://github.com/tyang816/medchatzh/blob/main/src/models/chatglm/configuration_chatglm.py"
    click CG_Token "https://github.com/tyang816/medchatzh/blob/main/src/models/chatglm/tokenization_chatglm.py"
    click CG_Model "https://github.com/tyang816/medchatzh/blob/main/src/models/chatglm/modeling_chatglm.py"
    click CG_Quant "https://github.com/tyang816/medchatzh/blob/main/src/models/chatglm/quantization.py"
    click CG_Test "https://github.com/tyang816/medchatzh/blob/main/src/models/chatglm/test_modeling_chatglm.py"
    click DS_Config "https://github.com/tyang816/medchatzh/blob/main/src/configs/deepspeed_config.json"
    click DS_Config_Stage3 "https://github.com/tyang816/medchatzh/blob/main/src/configs/deepspeed_config_stage3.json"
    click Lora_Bloom "https://github.com/tyang816/medchatzh/blob/main/src/configs/lora_config_bloom.json"
    click Lora_Llama "https://github.com/tyang816/medchatzh/blob/main/src/configs/lora_config_llama.json"
    click DATA_GEN "https://github.com/tyang816/medchatzh/blob/main/src/data/gen_data_from_model.py"
    click DATA_CONV "https://github.com/tyang816/medchatzh/blob/main/scripts/convert_to_conv_data.py"
```
