Qwen3-4B SQL QLoRA Adapter
Accurate Text-to-SQL Generator
2 min readupdated
This compact adapter is trained to turn English questions and database schemas into clean, ready-to-use SQL queries without extra explanations or markup. The model is built on top of Qwen/Qwen3-4B using QLoRA (4-bit quantization to save memory) and the sql-create-context dataset. The output is strictly executable code for relational databases. The tool is a great fit for developers and analysts who need to automate reporting and database workflows through text prompts.
Key features
- SQL specialization: Precise understanding of table schemas and generation of correct queries.
- Efficient architecture: Based on the lightweight Qwen3-4B model using QLoRA (a small add-on over the base model for targeted fine-tuning).
- Clean output: Outputs only SQL code without markdown wrappers or extra text.
- Low system requirements: Training and inference are possible on accessible GPUs thanks to 4-bit compression.
What you can do
- Chatbot integration: Set up a bot that fetches data from a database based on user text prompts.
- Analytical assistants: Embed the model into BI dashboards to generate queries on the fly.
- Quick prototype: Use it to automate the creation of simple reports using a DDL schema (
CREATE TABLE).
Example
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
base_model_id = "Qwen/Qwen3-4B"
adapter_id = "ipz235bvv/qwen3-4b-sql-qlora-adapter"
tokenizer = AutoTokenizer.from_pretrained(adapter_id)
base_model = AutoModelForCausalLM.from_pretrained(base_model_id, device_map="auto")
model = PeftModel.from_pretrained(base_model, adapter_id)
messages = [
{"role": "user", "content": "Schema:\nCREATE TABLE head (age INTEGER)\n\nQuestion:\nHow many heads?"}
]
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
print(prompt)
Read more
DetailsBenchmarks on 300 test cases: Clean SQL — 100.00% (vs. 96.67% for base), Normalized Exact Match — 69.67% (vs. 5.00%).
Hardware requirements: Trained on NVIDIA Tesla T4 (15.6 GB VRAM), peak memory usage 3.91 GB.
Training details: Dataset of 2,000 examples, 1 epoch, lora_r=16, adapter size 132.2 MB.
License: Apache 2.0 / CC-BY-SA-4.0.










Comments