A production-grade NLP-to-SQL pipeline that enables users to query relational databases using plain English. This project features a fine-tuned Llama-3-8B model protected by a deterministic Python security validation layer ("The Gatekeeper").
This project implements a robust, two-pillar software architecture designed to bridge the gap between non-technical users and complex data structures:
-
The Brain (Fine-Tuned LLM):
A Llama-3-8B model fine-tuned using Parameter-Efficient Fine-Tuning (PEFT/LoRA). It is trained to map natural language intent to specific database schemas. -
The Gatekeeper (Validation Layer):
A deterministic security interceptor that inspects AI-generated SQL to prevent destructive actions (DROP, DELETE, etc.) before they reach the database.
- Model: Meta Llama-3-8B (via Unsloth)
- Fine-Tuning: LoRA / QLoRA (Parameter-Efficient Fine-Tuning)
- Training Framework: Unsloth (2x faster, 70% less memory)
- Dataset: Spider (Cross-domain complex SQL)
- Environment: Kaggle t4 x 2 GPU
- Safety Layer: Custom Python Validation Logic
The fine-tuned adapters for this project are hosted on the Hugging Face Hub.
- Model Name:
Ilyankhan69/schema-aware-sql-agent - Base Model:
unsloth/llama-3-8b-bnb-4bit - Training Data: Spider Text-to-SQL Dataset
- Model Link: https://huggingface.co/Ilyankhan69/schema-aware-sql-agent
The model was fine-tuned for 60 steps as a proof-of-concept. Using LoRA, only 0.17% of the model's parameters (13.6M out of 8.04B) were trained, allowing the training to complete in under 4 minutes on a single T4 GPU while preserving the general reasoning capabilities of Llama-3.
To make the AI output production-ready, a deterministic validation layer is incorporated. Even if the LLM "hallucinates" or receives a malicious prompt, the Gatekeeper ensures system safety.
Key features of the Gatekeeper:
- Keyword Filtering: Blocks dangerous commands like
DROP,DELETE,TRUNCATE,ALTER,INSERT, andUPDATE. - Read-Only Enforcement: Ensures all queries start with the
SELECTstatement. - Syntax Scrubbing: Cleans and formats the SQL string before it enters the execution queue.
The system was evaluated by running a 1,000-question stress test against the Spider dataset.
| Metric | Result |
|---|---|
| Total Queries Processed | 1000 |
| ✅ Approved by Gatekeeper | 1000 |
| 🚫 Blocked by Gatekeeper | 0 |
| Overall Safety Accuracy | 100% |
Note: The 100% safety rate confirms that the model reliably adheres to the training format, consistently generating safe read-only queries.
To validate against overfitting, the model was tested on 50 unseen, synthetic schemas (e.g., SpaceStation_Inventory, Hospital_Management) that were not part of the Spider training dataset.
Overall Generalization Accuracy: 96.00%
| Metric | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Success | 0.96 | 1.00 | 0.98 | 48 |
| Minor Syntax Slip | 0.00 | 0.00 | 0.00 | 2 |
Finding: The model demonstrated zero hallucination of training-set metadata and correctly applied structural SQL aggregation logic to novel schemas, proving it has learned underlying SQL reasoning rather than memorizing dataset samples.
├── sql_agent_notebook.ipynb # Full training and testing pipeline
├── requirements.txt # Environment dependencies
└── README.md # Project documentation
- Open the
.ipynbfile in Kaggle or Google Colab. - Enable a GPU accelerator (T4 recommended).
- Install dependencies:
pip install -r requirements.txt - Run all notebook cells to see training, inference, and the Gatekeeper in action.
Created by Ilyankhan69