b-mc2/sql-create-context
Viewer β’ Updated β’ 78.6k β’ 9.22k β’ 518
How to use ipz235bvv/qwen3-4b-sql-qlora-adapter with PEFT:
from peft import PeftModel
from transformers import AutoModelForCausalLM
base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-4B")
model = PeftModel.from_pretrained(base_model, "ipz235bvv/qwen3-4b-sql-qlora-adapter")This repository contains a QLoRA (4-bit NF4) adapter fine-tuned from Qwen/Qwen3-4B for Text-to-SQL generation. Given a relational database schema (CREATE TABLE ...) and a natural language question in English, the model outputs a single clean, executable SQL query without markdown fences, explanations, or extra commentary.
ipz235bvv, Zhytomyr Polytechnic State University, group ΠΠΠ-23-5)Qwen/Qwen3-4BAutomated translation of natural language analytical questions into SQL queries based on a provided CREATE TABLE DDL schema for read-only database engines and BI pipelines.
INSERT, UPDATE, DELETE, DROP) without validation.import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
base_model_id = "Qwen/Qwen3-4B"
adapter_id = "ipz235bvv/qwen3-4b-sql-qlora-adapter"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
)
tokenizer = AutoTokenizer.from_pretrained(adapter_id)
base_model = AutoModelForCausalLM.from_pretrained(
base_model_id,
quantization_config=bnb_config,
device_map="auto",
)
model = PeftModel.from_pretrained(base_model, adapter_id)
model.eval()
messages = [
{
"role": "system",
"content": "Translate the user question into a valid SQL query using the provided schema. Output SQL only.",
},
{
"role": "user",
"content": "Schema:\nCREATE TABLE head (age INTEGER)\n\nQuestion:\nHow many heads of the departments are older than 56?",
},
]
prompt = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True, enable_thinking=False
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=128, do_sample=False)
sql = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
print(sql)
b-mc2/sql-create-context (built from WikiSQL and Spider).seed=42): 2,000 training examples, 200 validation examples, and 300 isolated test examples formatted in ChatML with enable_thinking=False.TRL SFTTrainer) with QLoRA (4-bit NF4, double quantization, fp16 compute).r=16, lora_alpha=32, lora_dropout=0.05, target_modules="all-linear" (33,030,144 trainable parameters β 0.8145% of total).learning_rate=1e-4, cosine scheduler, warmup_steps=10, effective batch size 16 (4 Γ 4), max_steps=125 (1 epoch), max_length=512, assistant_only_loss=True.14.90 min, peak GPU memory 3.91 GB, adapter size 132.2 MB.Evaluated on 300 held-out test examples (comparing the base model with a 96-token detailed system prompt vs. the fine-tuned QLoRA adapter with an 18-token short system prompt):
| Metric | Base Qwen3-4B (96-tok prompt) |
Fine-tuned QLoRA (18-tok prompt) | Change (Ξ) |
|---|---|---|---|
| Clean SQL (no fences/extra text), % | 96.67% | 100.00% | +3.33% |
Valid SQLite (EXPLAIN), % |
97.67% | 98.67% | +1.00% |
| Normalized Exact Match, % | 5.00% | 69.67% | +64.67% |
| Exact Match (quote-insensitive), % | 53.00% | 69.67% | +16.67% |