Text Generation
PEFT
Safetensors
English
text-to-sql
sql
grpo
reinforcement-learning
lora
trl
unsloth
conversational
Instructions to use bimal2004/sql-grpo-0.5b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use bimal2004/sql-grpo-0.5b with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("unsloth/Qwen2.5-0.5B-Instruct") model = PeftModel.from_pretrained(base_model, "bimal2004/sql-grpo-0.5b") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Unsloth Desktop
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,22 +1,91 @@
|
|
| 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-0.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 0.5B
|
| 19 |
+
|
| 20 |
+
A LoRA adapter for **Qwen2.5-0.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)** (uses the 1.5B version) · 💻 **[Code](https://github.com/bimal1023/sql-grpo)** · 🔗 **[1.5B version](https://huggingface.co/bimal2004/sql-grpo-1.5b)**
|
| 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-0.5B-Instruct (untrained) | 18.18% | 35.40% | 66.73% |
|
| 31 |
+
| **This model (GRPO)** | **41.68%** | **73.21%** | **99.81%** |
|
| 32 |
+
| Qwen2.5-1.5B-Instruct (untrained, for reference) | 40.72% | 58.41% | 76.89% |
|
| 33 |
+
| gpt-4.1-mini (prompted, for reference) | 74.37% | 98.74% | 100.00% |
|
| 34 |
+
|
| 35 |
+
GRPO more than doubled accuracy (2.3×); the trained 0.5B model outperforms the untrained 1.5B model, which is 3× its size. The gain isn't only formatting: among well-formatted answers, correctness rose from about 27% to about 42%.
|
| 36 |
+
|
| 37 |
+
## How to use
|
| 38 |
+
|
| 39 |
+
```python
|
| 40 |
+
import re
|
| 41 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 42 |
+
from peft import PeftModel
|
| 43 |
+
|
| 44 |
+
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct", torch_dtype="auto")
|
| 45 |
+
model = PeftModel.from_pretrained(base, "bimal2004/sql-grpo-0.5b")
|
| 46 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
|
| 47 |
+
|
| 48 |
+
schema = """CREATE TABLE singer (
|
| 49 |
+
Singer_ID int, Name text, Country text, Age int,
|
| 50 |
+
PRIMARY KEY (Singer_ID)
|
| 51 |
+
)"""
|
| 52 |
+
question = "How many singers are from France?"
|
| 53 |
+
|
| 54 |
+
messages = [
|
| 55 |
+
{"role": "system", "content": (
|
| 56 |
+
"You are a SQLite expert. Given a database schema and a question, "
|
| 57 |
+
"write one SQLite query that answers it. "
|
| 58 |
+
"Put only the query inside <sql></sql> tags.")},
|
| 59 |
+
{"role": "user", "content": f"Schema:\n{schema}\n\nQuestion: {question}"},
|
| 60 |
+
]
|
| 61 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 62 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 63 |
+
out = model.generate(**inputs, max_new_tokens=256, do_sample=False)
|
| 64 |
+
answer = tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 65 |
+
|
| 66 |
+
sql = re.search(r"<sql>(.*?)</sql>", answer, re.DOTALL).group(1).strip()
|
| 67 |
+
print(sql)
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
**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.
|
| 71 |
+
|
| 72 |
+
At 0.5B parameters, this model is small enough to run on a laptop CPU.
|
| 73 |
+
|
| 74 |
+
## Training details
|
| 75 |
+
|
| 76 |
+
- **Method:** GRPO via Hugging Face TRL `GRPOTrainer` + Unsloth, LoRA rank 32 (about 3.4% of parameters trained)
|
| 77 |
+
- **Data:** Spider train split (6,289 questions after filtering very long schemas)
|
| 78 |
+
- **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.
|
| 79 |
+
- **Setup:** 400 steps (~2 hours), 8 sampled answers per question, learning rate 5e-6, on a single free Kaggle T4 GPU
|
| 80 |
+
|
| 81 |
+
## Limitations
|
| 82 |
|
| 83 |
+
- Trained and evaluated only on **Spider** (SQLite); performance on other SQL dialects or very large real-world schemas is untested.
|
| 84 |
+
- Accuracy is about 42%: most complex questions (JOINs, nested queries) are still answered incorrectly. For better accuracy, use the [1.5B version](https://huggingface.co/bimal2004/sql-grpo-1.5b) (57.74%).
|
| 85 |
+
- Common errors include referencing columns that don't exist and missing JOINs.
|
| 86 |
+
- Some correct answers on borderline questions can flip under different numerical precision or batching. Evaluate on many questions, not single examples.
|
| 87 |
+
- Always review generated SQL before running it on important data.
|
| 88 |
|
| 89 |
+
## Credits
|
| 90 |
|
| 91 |
+
Built by [Bimal Kumal](https://bimalkumal7.com.np). Base model: [Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct). Data: [Spider](https://github.com/taoyds/spider) (Yu et al., 2018).
|