text2sql-qwen2.5-7b

A fine-tune of Qwen/Qwen2.5-7B-Instruct that turns a plain-English question plus a database schema into one SQLite query. It is meant to run offline on a laptop GPU through Ollama: the Q4_K_M file is 4.7 GB and fits in 8 GB of VRAM.

Warning: do not use this model on real databases. On 160 hand-written questions over four realistic multi-table databases it scores 16.3%, while the untuned base model scores 81.9%. It only does well on questions shaped like its training data (tiny, mostly single-table schemas). For real use, run the base model instead, for example ollama run qwen2.5:7b with the prompt format below. A second training run with better data reached 65.6% and is not published either. This repository is kept as a record of the experiment.

Files

File What it is
text2sql-qwen2.5-7b-Q4_K_M.gguf Merged model, quantized to Q4_K_M (4.7 GB). This is the one to run.
Modelfile Ollama Modelfile with the Qwen chat template and the system prompt.
lora-adapter/ The LoRA adapter on its own (161 MB), for use with the full-precision base model.

Results

Real-world questions (the number that matters)

Execution accuracy on 160 hand-written questions over four multi-table SQLite databases (shop, HR, school, football), none of which appear in the training data.

Qwen2.5-7B-Instruct, untuned This model (run 1) Run 2, epoch 1 (not published) Run 2, epoch 2 (not published)
All 160 81.9% 16.3% 65.6% 62.5%
Easy (48) 93.8% 25.0% 81.2% 83.3%
Medium (64) 87.5% 14.1% 67.2% 64.1%
Hard (48) 62.5% 10.4% 47.9% 39.6%
Query runs without error 96.3% 54.4% 93.1% 91.2%

All four were tested as Q4_K_M files in Ollama.

Fine-tuning made this model much worse than its base. It joins tables the question never mentions and invents column names.

Run 2 was a second attempt to fix that: 6,598 Spider examples with the full schema of each database plus 3,000 multi-table examples from gretelai/synthetic_text_to_sql, a lower learning rate (5e-5), rank 16 and 2 epochs. It removed most of the invented columns and needless joins, but the result is still below the untuned base, so its weights are not published. Compared with the base it is less careful with details: it drops tie-break ordering and rounding that the question asks for, and writes = 'null' where IS NULL is needed. Epoch 2 scored lower than epoch 1.

Conclusion: neither fine-tune beats the untuned base model on realistic databases. The public training answers are terser and less careful than what Qwen2.5-7B-Instruct already writes.

In-distribution test set

Execution accuracy on 625 held-out questions from the same dataset as the training data. A query counts as correct only if it runs and returns the same rows as the reference query on three differently seeded SQLite databases.

Model All 625 Hard subset (500) Valid SQL
Qwen2.5-Coder-1.5B-Instruct, untuned (4-bit) 57.6% 54.0% 100%
Qwen2.5-7B-Instruct, untuned (Q4_K_M) 68.0% 61.6% 100%
This model (Q4_K_M) 83.4% 81.6% 100%

"Hard" means the reference query uses a join, grouping, ordering, an aggregate, a set operation, or more than one table.

Use with Ollama

Download text2sql-qwen2.5-7b-Q4_K_M.gguf and Modelfile into the same folder, then:

ollama create text2sql -f Modelfile
ollama run text2sql

Send the schema as CREATE TABLE statements, a blank line, then the question. This is the exact format the model was trained on:

Schema:
CREATE TABLE instructor (salary INTEGER)

Question: Find the salaries of all distinct instructors that are less than the largest salary.

The reply is a single query in a fenced block:

```sql
SELECT DISTINCT salary FROM instructor WHERE salary < (SELECT MAX(salary) FROM instructor)
```

The system prompt, already set in the Modelfile, is:

You are a SQLite expert. Given a database schema and a question, write one SQLite query that answers it. Reply with only the query inside a ```sql code block.

Training

  • Method: supervised LoRA fine-tuning on Nebius Token Factory.
  • Data: 5,698 training and 300 validation examples drawn from b-mc2/sql-create-context, keeping only examples whose reference query runs and returns rows on generated SQLite databases. The 625 test questions are a separate slice of the same dataset.
  • Settings: rank 32, alpha 64, dropout 0.05, learning rate 1e-4, batch size 16, 3 epochs (1,071 steps, about 2.0M tokens), all attention and MLP projection layers.
  • This is the epoch-3 checkpoint, merged into the base weights and quantized with llama.cpp.

Limitations

  • It does not generalise beyond its training distribution. On realistic multi-table schemas it is far worse than the base model it was trained from (16.3% against 81.9%), and almost half of its queries fail to run. The in-distribution score should not be read as a measure of usefulness.
  • About one answer in six is wrong even on the in-distribution test set. Read the query before trusting its result. A typical slip is picking the wrong column to return or the wrong comparison direction.
  • Columns the question does not need can distract it. With the schema instructor (name, salary, dept) the example above comes back with a stray GROUP BY dept inside the subquery, which is wrong.
  • It writes one read-only SELECT statement in SQLite dialect. It was not trained on writes, multi-statement scripts, or other dialects.
  • Training schemas are small, mostly one or two tables, and nearly every column in them is used by the answer. That is the likely cause of the failure on real schemas.
  • Many questions in the source data do not say which aggregate is wanted, and the reference picks one arbitrarily (MIN versus MAX, for example). That caps the score any model can reach on this test set.
  • The published checkpoint was chosen between epoch 2 (82.6%) and epoch 3 (83.4%) using the test set itself. The gap is five questions, well within noise, so treat 83.4% as slightly optimistic.
  • The 625 test questions come from the same dataset as the training data, which is why that score is high while the real-world score is low.

Credits and licenses

Downloads last month
102
GGUF
Model size
8B params
Architecture
qwen2
Hardware compatibility
Log In to add your hardware

4-bit

Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Model tree for Hex990/text2sql-qwen2.5-7b

Base model

Qwen/Qwen2.5-7B
Adapter
(2,985)
this model

Dataset used to train Hex990/text2sql-qwen2.5-7b