# wbbeyourself/mac-sql architecture

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

- Interactive diagram: https://gitdiagram.com/wbbeyourself/mac-sql
- Repository: https://github.com/wbbeyourself/mac-sql

## Overview

##### Project Type and Purpose
- This is a research‐oriented Python framework (“MAC‐SQL”) for multi‐agent collaborative Text‐to‐SQL generation. It provides:
- An **inference pipeline** in `core/` that orchestrates three agents (Selector, Decomposer, Refiner) via a ChatManager.
- **Evaluation** tools under `evaluation/` for execution accuracy and validity metrics (BIRD and Spider datasets).
- **Demo scripts** in `scripts/` to run a lightweight Flask/SQLite app for query execution.
- Optional deployment of a local SQL‐Llama model (`SQL‐Llama‐deployment.md`).

##### File Structure Analysis
- Top‐level directories:
- `data/`: raw BIRD & Spider datasets (to be populated by unzipping `data.zip`).
- `core/`: inference modules:
– `agents.py`: definitions of Selector, Decomposer, Refiner.
– `chat_manager.py`: orchestrates inter‐agent communication.
– `llm.py` & `api_config.py`: wraps OpenAI or local LLM calls.
– `utils.py`, `const.py`: prompts, templates, helper functions.
- `training_scripts/`: end‐to‐end data binarization, config, fine‐tuning, inference for model training.
- `evaluation/`: scripts for measuring Execution Accuracy (EX) and Valid Efficiency Score (VES).
- `scripts/`: small Flask demo apps (`app_bird.py`, `app_spider.py`) and HTML template.
- Config & entry points: `requirements.txt`, `run.py`/`run.sh`/`run.bat`.
- Architectural patterns:
- **Modular pipeline**: training vs. inference vs. evaluation vs. demo.
- **Multi‐agent orchestration**: pipeline stages as independent agents.
- **API abstraction**: `core/llm.py` hides details of OpenAI vs. local model.

- Lay out four horizontal layers:
[A] Data Layer
– BIRD dataset
– Spider dataset
[B] Training Pipeline
– `binarized_data.py` → `config.json` → `finetuning.py` → Trained Model artifact
[C] Inference Pipeline
– Front­end Script (optional Flask UI in `scripts/`)
– Orchestrator (`core/chat_manager.py`)
- Agent 1: Selector (`core/agents.py`)
- Agent 2: Decomposer
- Agent 3: Refiner
– LLM Interface (`core/llm.py`, `core/api_config.py`) → External OpenAI API or Local SQL‐Llama
– SQL Processor (`evaluation/process_sql.py` or demo's SQLite backend)
[D] Evaluation Pipeline
– Execution Evaluator (`evaluation/evaluation_*ex.py`)
– Validity Evaluator (`evaluation/evaluation_*ves.py`)
– Metrics & Reports

- Components & Interactions:

##### Architectural Patterns & Design Principles
- **Separation of Concerns**: training, inference, evaluation, and demo are separate modules.
- **Pipeline & Orchestration**: each agent is a pipeline stage; ChatManager routes messages.
- **Abstraction**: `llm.py` abstracts underlying LLM choice (OpenAI vs. local).

## Diagram (Mermaid)

```mermaid
flowchart TB
  subgraph "Data Layer"
    BIRD["BIRD Dataset"]:::data
    Spider["Spider Dataset"]:::data
    BadCases["Bad Case Examples"]:::data
  end

  subgraph "Training Pipeline"
    Bin["Data Binarization"]:::code
    Config["Training Config"]:::code
    FT["Fine-tuning Script"]:::code
    FTsh["Fine-tuning Launcher"]:::code
    Gen["Synthetic Data Generation"]:::code
    ValInf["Training Inference"]:::code
    TUtils["Training Utilities"]:::code
    Model["Trained Model Artifact"]:::data
  end

  subgraph "Inference Pipeline"
    AppBird["BIRD App (Flask UI)"]:::code
    AppSpider["Spider App (Flask UI)"]:::code
    FastChat["FastChat Demo"]:::code
    HTMLT["HTML Template"]:::code
    ChatMgr["ChatManager Orchestrator"]:::orchestrator
    Sel["Selector Agent"]:::code
    Dec["Decomposer Agent"]:::code
    Ref["Refiner Agent"]:::code
    LLMif["LLM Interface"]:::code
    APIcfg["API Configuration"]:::code
    OpenAI["OpenAI ChatCompletion API"]:::external
    LocalLLM["Local SQL-Llama Server"]:::external
    SQLProc["SQL Processor"]:::data
    SQLiteDB["SQLite Database"]:::data
  end

  subgraph "Evaluation Pipeline"
    ExecAcc["Execution Accuracy Evaluator"]:::code
    ValEff["Valid Efficiency Evaluator"]:::code
    Parse["Parsing & Metric Helpers"]:::code
    Reports["Metrics & Reports"]:::data
  end

  BIRD --> Bin
  Spider --> Bin
  BadCases --> Bin
  Bin --> Config
  Config --> FT
  FT --> Model
  Gen --> Model
  ValInf --> Model
  TUtils --> Model

  Model --> ChatMgr
  BIRD --> ChatMgr
  Spider --> ChatMgr

  AppBird --> ChatMgr
  AppSpider --> ChatMgr
  FastChat --> ChatMgr

  ChatMgr --> Sel
  Sel --> Dec
  Dec --> Ref
  Ref --> LLMif
  LLMif --> APIcfg
  APIcfg --> OpenAI
  APIcfg --> LocalLLM
  OpenAI --> SQLProc
  LocalLLM --> SQLProc
  SQLProc --> SQLiteDB

  SQLProc --> ExecAcc
  SQLProc --> ValEff
  Parse --> ExecAcc
  Parse --> ValEff
  ExecAcc --> Reports
  ValEff --> Reports

  classDef code fill:#D3E5FF,stroke:#3B70A6,color:#000;
  classDef data fill:#DFFFE0,stroke:#2E8B57,color:#000;
  classDef external fill:#FFE5CC,stroke:#E55D18,color:#000;
  classDef orchestrator fill:#FFF4C1,stroke:#D4A017,color:#000;

  click BIRD "https://github.com/wbbeyourself/mac-sql/tree/main/data/"
  click BadCases "https://github.com/wbbeyourself/mac-sql/tree/main/bad_cases/"
  click Bin "https://github.com/wbbeyourself/mac-sql/blob/main/training_scripts/binarized_data.py"
  click Config "https://github.com/wbbeyourself/mac-sql/blob/main/training_scripts/configs/default_offload_opt_param.json"
  click FT "https://github.com/wbbeyourself/mac-sql/blob/main/training_scripts/finetuning.py"
  click FTsh "https://github.com/wbbeyourself/mac-sql/blob/main/training_scripts/finetuning.sh"
  click Gen "https://github.com/wbbeyourself/mac-sql/blob/main/training_scripts/generate.py"
  click ValInf "https://github.com/wbbeyourself/mac-sql/blob/main/training_scripts/inference.py"
  click TUtils "https://github.com/wbbeyourself/mac-sql/blob/main/training_scripts/utils.py"
  click ChatMgr "https://github.com/wbbeyourself/mac-sql/blob/main/core/chat_manager.py"
  click Sel "https://github.com/wbbeyourself/mac-sql/blob/main/core/agents.py"
  click Dec "https://github.com/wbbeyourself/mac-sql/blob/main/core/agents.py"
  click Ref "https://github.com/wbbeyourself/mac-sql/blob/main/core/agents.py"
  click LLMif "https://github.com/wbbeyourself/mac-sql/blob/main/core/llm.py"
  click APIcfg "https://github.com/wbbeyourself/mac-sql/blob/main/core/api_config.py"
  click SQLProc "https://github.com/wbbeyourself/mac-sql/blob/main/evaluation/process_sql.py"
  click ExecAcc "https://github.com/wbbeyourself/mac-sql/blob/main/evaluation/exec_eval.py"
  click ValEff "https://github.com/wbbeyourself/mac-sql/blob/main/evaluation/evaluation_bird_ves.py"
  click Parse "https://github.com/wbbeyourself/mac-sql/blob/main/evaluation/parse.py"
  click AppBird "https://github.com/wbbeyourself/mac-sql/blob/main/scripts/app_bird.py"
  click AppSpider "https://github.com/wbbeyourself/mac-sql/blob/main/scripts/app_spider.py"
  click FastChat "https://github.com/wbbeyourself/mac-sql/blob/main/scripts/fastchat_demo.py"
  click HTMLT "https://github.com/wbbeyourself/mac-sql/blob/main/scripts/templates/index.html"
```
