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.
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.
- 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
- 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
- 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
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
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
- 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
- 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
- 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
.
βββ 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
- Python 3.8+
- CUDA-capable GPU (recommended for training)
# Create virtual environment
conda create -n text-to-sql python=3.10
conda activate text-to-sql
# Install dependencies
pip install -r requirements.txttorch==2.1.2
transformers==4.40.0
numpy==1.26.0
nltk==3.8.1
wandb==0.15.10
bitsandbytes==0.43.1
python train_t5.py \
--finetune \
--max_n_epochs 20 \
--batch_size 16 \
--learning_rate 5e-5 \
--use_wandb \
--experiment_name t5_finetunedpython train_t5.py \
--max_n_epochs 30 \
--batch_size 16 \
--use_data_augmentation \
--use_curriculum_learning \
--experiment_name t5_scratch_curriculum# 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_0shotpython 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- 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
- Curriculum Learning Impact: 12-15% F1 improvement over standard training
- Data Augmentation: Expanded training set by 4-5x, reducing overfitting
- Keyword Selection: 8% F1 boost over random example selection
- Quantization: 75% memory reduction with minimal accuracy loss
Problem: Standard tokenizers split SQL operators incorrectly
Solution: Added comparison operators (>=, <=, !=) as special tokens while avoiding SQL keywords that appear in natural words
Problem: Limited training examples (~500 queries)
Solution: Implemented 7 augmentation strategies + curriculum learning
Problem: Models struggled with joins and aggregations
Solution: Multi-stage curriculum that gradually introduces query complexity
Problem: Random examples gave inconsistent results
Solution: Keyword-based selection ensuring relevant examples in context
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
- 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
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.
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.