Text Generation
PEFT
Safetensors
English
text-to-sql
sql
grpo
reinforcement-learning
lora
trl
unsloth
conversational
Instructions to use bimal2004/sql-grpo-1.5b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use bimal2004/sql-grpo-1.5b with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("unsloth/Qwen2.5-1.5B-Instruct") model = PeftModel.from_pretrained(base_model, "bimal2004/sql-grpo-1.5b") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Unsloth Desktop
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,22 +1,87 @@
|
|
| 1 |
---
|
| 2 |
-
base_model:
|
| 3 |
-
|
| 4 |
-
- text-generation-inference
|
| 5 |
-
- transformers
|
| 6 |
-
- unsloth
|
| 7 |
-
- qwen2
|
| 8 |
-
- trl
|
| 9 |
license: apache-2.0
|
| 10 |
language:
|
| 11 |
- en
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
- **
|
| 17 |
-
- **
|
| 18 |
-
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
-
[
|
|
|
|
| 1 |
---
|
| 2 |
+
base_model: Qwen/Qwen2.5-1.5B-Instruct
|
| 3 |
+
library_name: peft
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
license: apache-2.0
|
| 5 |
language:
|
| 6 |
- en
|
| 7 |
+
pipeline_tag: text-generation
|
| 8 |
+
tags:
|
| 9 |
+
- text-to-sql
|
| 10 |
+
- sql
|
| 11 |
+
- grpo
|
| 12 |
+
- reinforcement-learning
|
| 13 |
+
- lora
|
| 14 |
+
- trl
|
| 15 |
+
- unsloth
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# SQL-GRPO 1.5B
|
| 19 |
+
|
| 20 |
+
A LoRA adapter for **Qwen2.5-1.5B-Instruct**, trained with **GRPO** (Group Relative Policy Optimization) to turn natural-language questions into SQLite queries. The reward comes from **executing the generated SQL** against the real database: no human-labeled feedback, the database is the judge.
|
| 21 |
+
|
| 22 |
+
🚀 **[Live demo](https://huggingface.co/spaces/bimal2004/sql-grpo-demo)** · 💻 **[Code](https://github.com/bimal1023/sql-grpo)**
|
| 23 |
+
|
| 24 |
+
## Results
|
| 25 |
+
|
| 26 |
+
Spider dev set (1,034 questions on databases not seen during training), execution accuracy:
|
| 27 |
+
|
| 28 |
+
| Model | Accuracy | Valid SQL | Correct format |
|
| 29 |
+
| --- | --- | --- | --- |
|
| 30 |
+
| Qwen2.5-1.5B-Instruct (untrained) | 40.72% | 58.41% | 76.89% |
|
| 31 |
+
| **This model (GRPO)** | **57.74%** | **82.40%** | **100.00%** |
|
| 32 |
+
| gpt-4.1-mini (prompted, for reference) | 74.37% | 98.74% | 100.00% |
|
| 33 |
+
|
| 34 |
+
GRPO closed about half the gap to gpt-4.1-mini, with gains on every question type: aggregation +18.3 points, GROUP BY +15.5, nested queries +11.9, JOINs +10.0.
|
| 35 |
+
|
| 36 |
+
## How to use
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
import re
|
| 40 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 41 |
+
from peft import PeftModel
|
| 42 |
+
|
| 43 |
+
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct", torch_dtype="auto")
|
| 44 |
+
model = PeftModel.from_pretrained(base, "bimal2004/sql-grpo-1.5b")
|
| 45 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct")
|
| 46 |
+
|
| 47 |
+
schema = """CREATE TABLE singer (
|
| 48 |
+
Singer_ID int, Name text, Country text, Age int,
|
| 49 |
+
PRIMARY KEY (Singer_ID)
|
| 50 |
+
)"""
|
| 51 |
+
question = "How many singers are from France?"
|
| 52 |
+
|
| 53 |
+
messages = [
|
| 54 |
+
{"role": "system", "content": (
|
| 55 |
+
"You are a SQLite expert. Given a database schema and a question, "
|
| 56 |
+
"write one SQLite query that answers it. "
|
| 57 |
+
"Put only the query inside <sql></sql> tags.")},
|
| 58 |
+
{"role": "user", "content": f"Schema:\n{schema}\n\nQuestion: {question}"},
|
| 59 |
+
]
|
| 60 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 61 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 62 |
+
out = model.generate(**inputs, max_new_tokens=256, do_sample=False)
|
| 63 |
+
answer = tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 64 |
+
|
| 65 |
+
sql = re.search(r"<sql>(.*?)</sql>", answer, re.DOTALL).group(1).strip()
|
| 66 |
+
print(sql)
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
**Prompt format matters:** the model was trained with exactly this system prompt, with the schema given as `CREATE TABLE` statements, and answers inside `<sql></sql>` tags.
|
| 70 |
+
|
| 71 |
+
## Training details
|
| 72 |
+
|
| 73 |
+
- **Method:** GRPO via Hugging Face TRL `GRPOTrainer` + Unsloth, LoRA rank 32 (about 3% of parameters trained)
|
| 74 |
+
- **Data:** Spider train split (6,289 questions after filtering very long schemas)
|
| 75 |
+
- **Reward:** +1.0 if the query returns the same rows as the gold SQL, +0.2 if it runs without error, +0.1 for correct `<sql>` formatting, −0.1 for overly long output. Queries run on a read-only connection with a 2-second timeout.
|
| 76 |
+
- **Setup:** 400 steps, 8 sampled answers per question, learning rate 5e-6, on a single free Kaggle T4 GPU
|
| 77 |
+
|
| 78 |
+
## Limitations
|
| 79 |
|
| 80 |
+
- Trained and evaluated only on **Spider** (SQLite); performance on other SQL dialects or very large real-world schemas is untested.
|
| 81 |
+
- The most common remaining error is **schema linking**: referencing columns that don't exist (about a third of failures). JOINs and nested queries remain the hardest cases (~40% accuracy).
|
| 82 |
+
- Some correct answers on borderline questions can flip under different numerical precision or batching. Evaluate on many questions, not single examples.
|
| 83 |
+
- Always review generated SQL before running it on important data.
|
| 84 |
|
| 85 |
+
## Credits
|
| 86 |
|
| 87 |
+
Built by [Bimal Kumal](https://bimalkumal7.com.np). Base model: [Qwen2.5-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct). Data: [Spider](https://yale-lily.github.io/spider) (Yu et al., 2018).
|