Qwen2.5-Coder-1.5B-Text2SQL-LoRA
a compact model for generating SQLite queries
2 min readupdated
This model turns plain-English questions and table schemas into ready-to-run SQLite queries. Built on the compact Qwen2.5-Coder-1.5B-Instruct language model (1.54B parameters), it was fine-tuned using QLoRA (quantized low-rank adaptation, which cuts VRAM usage during training). Because the adapter weights are already merged into the base model in fp16, you can load it just like a standard LLM with no extra setup. Compared to the original base model, this release delivers higher execution accuracy on real databases, making it a practical fit for developers who want a fast, lightweight local assistant for SQLite.
Key features
- Higher execution accuracy: scores jumped from 61.0% to 71.3% (+10.3 percentage points) on the Gretel test set, and from 46.1% to 55.2% (+9.1 percentage points) on unseen Spider dev schemas.
- Ready-to-use merged weights in fp16 in the repository root, alongside a standalone adapter in the
adapter/folder. - Trained on 12,000 validated examples from the
gretelai/synthetic_text_to_sqldataset with loss calculated strictly on response tokens. - Open Apache-2.0 license, fully permissive for commercial use.
What you can do
- Embed a local natural-language interface into a lightweight app or Telegram bot to query SQLite databases without sending your schemas to cloud APIs.
- Speed up database prototyping by generating draft SELECT queries for quick manual review.
- Grab the standalone adapter from the
adapter/folder to experiment with merging or further fine-tuning on top ofQwen2.5-Coder-1.5B-Instruct.
Example
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
repo = "2003Rohansinha/Qwen2.5-Coder-1.5B-Text2SQL-LoRA"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo, torch_dtype=torch.float16, device_map="auto")
msgs = [{"role": "system", "content": "You are an expert SQL assistant. Given a database schema and a question, write one SQLite query that answers the question. Return only the SQL query in a ```sql code block."}, {"role": "user", "content": "CREATE TABLE employees (id INTEGER, name TEXT, dept TEXT, salary REAL); What is the average salary in each department?"}]
ids = tok.apply_chat_template(msgs, add_generation_prompt=True, return_tensors="pt").to(model.device)
print(tok.decode(model.generate(ids, max_new_tokens=256, do_sample=False)[0][ids.shape[1]:], skip_special_tokens=True))
Read more
DetailsBenchmarks (execution accuracy, greedy decoding): Gretel test (300 questions) — 71.3% vs. 61.0% for the base model; Spider dev (1,034 questions) — 55.2% vs. 46.1% for the base model.
Training: QLoRA (4-bit NF4, r=16, alpha=32, dropout 0.05) on 2× T4 GPUs in Kaggle, lr 2e-4.
Limitations: strictly tailored to SQLite syntax (requires adjustments for Postgres or MySQL), may struggle with complex JOINs and nested subqueries, and generated SQL should only be executed over a read-only connection.










Comments