Advanced NL2SQL: Production-Ready Open-Source Stacks with DB-GPT-Hub, SQLCoder, and Text-to-SQL Fine-Tuning

  1. MindSQL – One-Line RAG for Database Chat

MindSQL is a lightweight Python library that turns any relational database into a conversational agent. It plugs into PostgreSQL, MySQL, SQLite, Snowflake, BigQuery, and more, while letting you pick any LLM (GPT-4, Llama-2, Gemini) and vector store (Chroma, FAISS).

pip install mindsql

from mindsql.core import MindSQLCore
from mindsql.databases import Sqlite
from mindsql.llms import GoogleGenAi
from mindsql.vectorstores import ChromaDB

cfg = {"api_key": "YOUR-KEY"}
engine = MindSQLCore(
    llm=GoogleGenAi(config=cfg),
    vectorstore=ChromaDB(),
    database=Sqlite()
)

conn = engine.database.create_connection("sqlite:///hr.db")
engine.index_all_ddls(conn, db_name="hr")
engine.index(bulk=True, path="qa_pairs.json")

answer = engine.ask_db(
    question="Which department has the highest average salary?",
    connection=conn,
    visualize=True
)
answer["chart"].show()
conn.close()

  1. DB-GPT-Hub – End-to-End Text-to-SQL Fine-Tuning

DB-GPT-Hub is a reproducible pipeline that turns open-source LLMs into SQL specialists. It covers data curation, preprocessing, parameter-efficient fine-tuning (LoRA/QLoRA), evaluation, and inference.

2.1 Datasets

  • Spider – 10 k questions, 200 DBs, 138 domains.
  • BIRD – 12 k pairs, 95 DBs, 33 GB, emphasizes external knowledge & execution efficiency.
  • WikiSQL, CHASE, CoSQL, SPaRK – single-table, multi-turn, and dialogue varients.

2.2 Hardware Baseline (QLoRA-4 bit)

Model Size GPU RAM CPU RAM Disk
7 B 6 GB 3.6 GB 36 GB
13 B 13 GB 6 GB 60 GB

2.3 Quick-Start

git clone https://github.com/eosphoros-ai/DB-GPT-Hub.git
cd DB-GPT-Hub
conda create -n dbgpt python=3.10 && conda activate dbgpt
pip install poetry
poetry install

2.3.1 Pre-processing

# downloads Spider and builds train/dev splits
poetry run sh dbgpt_hub/scripts/gen_train_eval_data.sh

Produces example_text2sql_train.json (8 659 rows) and example_text2sql_dev.json (1 034 rows). Each record contains db_id, schema description, question, and gold SQL.

2.3.2 Training Script

poetry run sh dbgpt_hub/scripts/train_sft.sh   # QLoRA by default

Key flags:

  • --model_name_or_path codellama/CodeLlama-13b-Instruct-hf
  • --quantization_bit 4
  • --max_source_length 2048
  • --lora_rank 64
  • --num_train_epochs 8

2.3.3 Multi-GPU / DeepSpeed

deepspeed --num_gpus 2 dbgpt_hub/train/sft_train.py \
    --deepspeed dbgpt_hub/configs/ds_config.json \
    --quantization_bit 4 ...

2.3.4 Inference

poetry run sh dbgpt_hub/scripts/predict_sft.sh

Outputs predicted SQL under dbgpt_hub/output/pred/.

2.3.5 Evaluation

poetry run python dbgpt_hub/eval/evaluation.py \
    --plug_value \
    --input dbgpt_hub/output/pred/pred_sql_dev_skeleton.sql

Spider execution accuracy for the released adapter: 0.789.

2.3.6 Export Merged Weights

poetry run sh dbgpt_hub/scripts/export_merge.sh

  1. SQLCoder – State-of-the-Art Small Model

Released by Defog, SQLCoder-7B/15B outperforms GPT-3.5 and StarCoder on the Spider benchmark, approaching GPT-4 accuracy while remaining fully open.

GitHub: defog-ai/sqlcoder

  1. Modal-Finetune-SQL – Serverless Fine-Tuning

A minimal repo that fine-tunes Llama-2-7B on Modal’s serverless GPUs. Includes data prep, training, and evaluation scripts.

GitHub: run-llama/modal_finetune_sql

  1. LLaMA-Factory – Universal Tuning Toolkit

Supports 20+ models (Llama, Mistral, Qwen, Baichuan, ChatGLM, etc.) and every major tuning recipe: full, freeze, LoRA, QLoRA, PPO, DPO, ORPO, plus Flash-Attn-2 and vLLM inference.

GitHub: hiyouga/LLaMA-Factory

Method Full Partial LoRA QLoRA
Pre-train
SFT
Reward Model
PPO / DPO / ORPO

Web UI and TensorBoard/WandB dashboards are included for experiment tracking.

Tags: Text2SQL DB-GPT-Hub SQLCoder MindSQL LoRA

Posted on Sat, 08 Aug 2026 16:56:46 +0000 by The Saint