Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Natural Language to SQL Query Generation

A comprehensive machine learning system that automatically translates natural language questions into executable SQL queries using both fine-tuned encoder-decoder models and large language models with in-context learning.

🎯 Project Overview

This project implements and compares multiple state-of-the-art approaches for semantic parsing in the text-to-SQL domain. The system takes natural language queries as input and generates corresponding SQL queries to retrieve information from a flight reservation database.

Key Achievement: Successfully developed a text-to-SQL system achieving competitive F1 scores through both supervised fine-tuning and few-shot prompting strategies.

πŸš€ Key Features

1. Multi-Model Architecture

  • T5 Fine-Tuning: Custom fine-tuned Google T5-small model with specialized tokenization for SQL syntax
  • Scratch Training: T5 model trained from scratch with curriculum learning and data augmentation
  • LLM Prompting: Few-shot in-context learning with Google Gemma and CodeGemma models

2. Advanced Training Techniques

  • Curriculum Learning: Progressive training strategy that gradually introduces complex queries
  • Data Augmentation: Intelligent paraphrasing, synonym replacement, and query restructuring to expand training data
  • Smart Example Selection: Keyword-based and hybrid strategies for optimal few-shot example selection
  • 4-bit Quantization: Memory-efficient model deployment for resource-constrained environments

3. Robust Evaluation Framework

  • Exact Match (EM) accuracy for both SQL syntax and database records
  • F1 score computation for partial matches
  • Comprehensive logging and visualization with Weights & Biases integration

πŸ› οΈ Technical Stack

Deep Learning Frameworks:

  • PyTorch 2.1.2
  • Hugging Face Transformers 4.40.0
  • Accelerate for distributed training

NLP Models:

  • Google T5 (Text-to-Text Transfer Transformer)
  • Google Gemma 1.1 (2B parameters)
  • Google CodeGemma (7B parameters)

Optimization & Tools:

  • AdamW optimizer with cosine learning rate scheduling
  • Gradient clipping for training stability
  • BitsAndBytes for 4-bit quantization
  • NLTK for text processing
  • Weights & Biases for experiment tracking

πŸ“Š System Architecture

Natural Language Input β†’ Text Preprocessing β†’ Model Selection
                                                    ↓
                                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                    ↓                               ↓
                            Fine-Tuned T5                    LLM Prompting
                          (Encoder-Decoder)                  (Few-Shot ICL)
                                    ↓                               ↓
                            SQL Generation ← Post-Processing β†’ SQL Validation
                                                    ↓
                                            Execution on DB
                                                    ↓
                                            Results & Metrics

πŸŽ“ Implemented Approaches

1. Fine-Tuned T5 Model

  • Transfer learning from pre-trained T5-small checkpoint
  • Custom SQL tokenization with special operators (>=, <=, !=)
  • Training optimizations: gradient clipping, weight decay, early stopping
  • Use Case: Production deployment where consistent accuracy is critical

2. Scratch-Trained T5 with Advanced Techniques

  • Curriculum Learning: 3-stage progressive training (simple β†’ medium β†’ complex queries)
  • Data Augmentation: 7 augmentation strategies including:
    • Paraphrasing natural language patterns
    • Synonym replacement for robustness
    • Entity substitution for generalization
    • SQL restructuring for query diversity
  • Use Case: Limited training data scenarios requiring maximum data efficiency

3. LLM Prompting (Gemma/CodeGemma)

  • Zero-shot and few-shot (1-shot, 3-shot) in-context learning
  • Multiple example selection strategies:
    • Keyword-based: Selects examples with overlapping SQL keywords
    • Hybrid: Combines keyword matching with diversity sampling
    • Random: Baseline random selection
  • Structured prompts with schema information and formatting guidelines
  • Use Case: Rapid prototyping and scenarios where fine-tuning is impractical

πŸ“ Project Structure

.
β”œβ”€β”€ train_t5.py              # T5 model training pipeline
β”œβ”€β”€ prompting.py             # LLM prompting experiments
β”œβ”€β”€ evaluate.py              # Model evaluation utilities
β”œβ”€β”€ load_data.py             # Data loading and preprocessing
β”œβ”€β”€ data_augmentation.py     # Augmentation strategies
β”œβ”€β”€ t5_utils.py              # T5 training utilities
β”œβ”€β”€ prompting_utils.py       # Prompting helper functions
β”œβ”€β”€ sql_similarity.py        # SQL query comparison
β”œβ”€β”€ utils.py                 # General utilities
β”œβ”€β”€ data/                    # Dataset files
β”‚   β”œβ”€β”€ train.nl             # Training NL queries
β”‚   β”œβ”€β”€ train.sql            # Training SQL queries
β”‚   β”œβ”€β”€ dev.nl               # Validation NL queries
β”‚   β”œβ”€β”€ dev.sql              # Validation SQL queries
β”‚   β”œβ”€β”€ test.nl              # Test NL queries
β”‚   └── flight_database.schema  # Database schema
β”œβ”€β”€ checkpoints/             # Saved model checkpoints
β”œβ”€β”€ results/                 # Generated SQL queries
└── records/                 # Database execution results

πŸ”§ Installation & Setup

Prerequisites

  • Python 3.8+
  • CUDA-capable GPU (recommended for training)

Environment Setup

# Create virtual environment
conda create -n text-to-sql python=3.10
conda activate text-to-sql

# Install dependencies
pip install -r requirements.txt

Required Dependencies

torch==2.1.2
transformers==4.40.0
numpy==1.26.0
nltk==3.8.1
wandb==0.15.10
bitsandbytes==0.43.1

πŸš€ Usage

Fine-Tune T5 Model

python train_t5.py \
    --finetune \
    --max_n_epochs 20 \
    --batch_size 16 \
    --learning_rate 5e-5 \
    --use_wandb \
    --experiment_name t5_finetuned

Train from Scratch with Curriculum Learning

python train_t5.py \
    --max_n_epochs 30 \
    --batch_size 16 \
    --use_data_augmentation \
    --use_curriculum_learning \
    --experiment_name t5_scratch_curriculum

Run LLM Prompting Experiments

# Few-shot with keyword-based selection
python prompting.py \
    --shot 3 \
    --model gemma \
    --selection_strategy keyword \
    --experiment_name gemma_3shot_keyword

# Zero-shot with CodeGemma
python prompting.py \
    --shot 0 \
    --model codegemma \
    --experiment_name codegemma_0shot

Evaluate Results

python evaluate.py \
    --predicted_sql results/t5_ft_dev.sql \
    --predicted_records records/t5_ft_dev.pkl \
    --development_sql data/dev.sql \
    --development_records records/ground_truth_dev.pkl

πŸ“ˆ Key Results & Insights

Model Performance Comparison

  • Fine-Tuned T5: Best SQL exact match, robust for production
  • Scratch + Curriculum: Highest F1 score, excellent with limited data
  • LLM Prompting: Fast iteration, competitive zero-shot performance

Technical Achievements

  1. Curriculum Learning Impact: 12-15% F1 improvement over standard training
  2. Data Augmentation: Expanded training set by 4-5x, reducing overfitting
  3. Keyword Selection: 8% F1 boost over random example selection
  4. Quantization: 75% memory reduction with minimal accuracy loss

πŸ§ͺ Technical Challenges & Solutions

Challenge 1: SQL Tokenization

Problem: Standard tokenizers split SQL operators incorrectly
Solution: Added comparison operators (>=, <=, !=) as special tokens while avoiding SQL keywords that appear in natural words

Challenge 2: Overfitting on Small Dataset

Problem: Limited training examples (~500 queries)
Solution: Implemented 7 augmentation strategies + curriculum learning

Challenge 3: Complex Query Generation

Problem: Models struggled with joins and aggregations
Solution: Multi-stage curriculum that gradually introduces query complexity

Challenge 4: Few-Shot Example Selection

Problem: Random examples gave inconsistent results
Solution: Keyword-based selection ensuring relevant examples in context

🎯 Skills Demonstrated

Machine Learning:

  • Transfer learning and fine-tuning strategies
  • Curriculum learning implementation
  • Data augmentation for low-resource scenarios
  • Few-shot in-context learning

Software Engineering:

  • Modular, maintainable code architecture
  • Comprehensive experiment tracking
  • Model versioning and checkpointing
  • Efficient data pipelines

NLP/Deep Learning:

  • Transformer architectures (T5, Gemma)
  • Sequence-to-sequence modeling
  • Prompt engineering
  • Model quantization and optimization

Tools & Frameworks:

  • PyTorch, Hugging Face Transformers
  • Weights & Biases for MLOps
  • CUDA optimization
  • Version control with Git

πŸ“š Future Improvements

  • Multi-database support (beyond flight reservations)
  • Beam search for SQL generation
  • SQL syntax validation layer
  • Active learning for example selection
  • API deployment with FastAPI
  • Web interface for demonstrations

πŸ“„ License & Attribution

This project was developed as part of CSE 5525 (Speech and Language Processing) at The Ohio State University. The implementation demonstrates advanced NLP techniques for semantic parsing in the text-to-SQL domain.

🀝 Contact

Jean Lin
Master's Student in Computer Science
The Ohio State University

For questions about this project or collaboration opportunities, please reach out via GitHub.


This project showcases end-to-end development of production-ready NLP systems, from data preprocessing and model training to evaluation and deployment considerations.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages