--- license: apache-2.0 base_model: Qwen/Qwen2.5-Coder-1.5B-Instruct base_model_relation: finetune datasets: - gretelai/synthetic_text_to_sql language: - en library_name: transformers pipeline_tag: text-generation tags: - text-to-sql - sql - sqlite - qlora - lora --- # Qwen2.5-Coder-1.5B Text-to-SQL (QLoRA fine-tune) A QLoRA fine-tune of [Qwen2.5-Coder-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct) for turning a question plus a table schema into one SQLite query. The weights here are the LoRA adapter merged into the fp16 base, so it loads like any normal model. The adapter alone is in the `adapter/` folder. ## Results (execution accuracy) A query counts as correct when it runs on the real database and returns the same rows as the reference query (row order only checked when the reference uses ORDER BY). | Benchmark | Questions | Base | This model | Change | |---|---|---|---|---| | Gretel synthetic_text_to_sql, held-out test subset | 300 | 61.0% | 71.3% | +10.3 pts (paired sign test p = 0.0001) | | Spider dev (databases never seen in training) | 1,034 | 46.1% | 55.2% | +9.1 pts (181 fixed, 87 broken, p < 0.0001) | Same prompt, greedy decoding for both models. Standard error on Spider is about 1.5 points per model. The fine-tune also breaks some questions the base got right (87 on Spider), so it is better on average, not better on every question. These use my own execution-comparison script, not the official Spider evaluator, so do not compare the numbers directly with leaderboard figures. ## Usage ```python 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") system = "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." schema = "CREATE TABLE employees (id INTEGER, name TEXT, dept TEXT, salary REAL);" question = "What is the average salary in each department?" user = f"{schema} {question}" msgs = [{"role": "system", "content": system}, {"role": "user", "content": user}] ids = tok.apply_chat_template(msgs, add_generation_prompt=True, return_tensors="pt").to(model.device) out = model.generate(ids, max_new_tokens=256, do_sample=False) print(tok.decode(out[0][ids.shape[1]:], skip_special_tokens=True)) ``` Prompt format used in training and evaluation (keep it for best results): ``` SYSTEM: 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. USER: Database schema:\n\n\nQuestion: ``` The model answers with a single query inside a ```sql block. ## Training - Data: usable examples from [gretelai/synthetic_text_to_sql](https://huggingface.co/datasets/gretelai/synthetic_text_to_sql) (Apache-2.0); 12,000 training examples whose reference query runs on a database built from the example's schema. - Method: QLoRA (4-bit NF4), LoRA r=16, alpha=32, dropout 0.05 on all attention and MLP projections, learning rate 2e-4, effective batch size 16, loss on the answer tokens only, 2x T4 GPUs on Kaggle. - Checkpoints were backed up to the Hub during training. ## Limitations - Targets SQLite syntax. Other dialects (Postgres, MySQL, BigQuery) may need edits. - Trained mostly on synthetic data; real, messy schemas are harder (see the lower Spider score). - A 1.5B model makes mistakes on joins, nested queries and unusual schemas. Always review generated SQL. - Never run generated SQL on a database you cannot afford to change; use a read-only connection. ## License Apache-2.0, same as the base model. The training data is Apache-2.0.