b-mc2/sql-create-context
Viewer • Updated • 78.6k • 4.56k • 514
How to use Varshitha2603/qwen2.5-coder-3b-text2sql-qlora with PEFT:
from peft import PeftModel
from transformers import AutoModelForCausalLM
base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Coder-3B-Instruct")
model = PeftModel.from_pretrained(base_model, "Varshitha2603/qwen2.5-coder-3b-text2sql-qlora")QLoRA adapter for Qwen/Qwen2.5-Coder-3B-Instruct, fine-tuned to turn a table schema and a question into a SQL query.
Code and full evaluation: https://github.com/VarshithaChappidi/text2sql-qlora
| Model | Strict exact match | Normalized exact match |
|---|---|---|
| Base, prompted | 0.023 | 0.570 |
| This adapter | 0.767 | 0.773 |
Normalized match unifies quote style, drops wrapping quotes, treats INNER JOIN as JOIN, and ignores AND order. It still rejects some valid SQL written differently, so it is a lower bound.
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
base_id = "Qwen/Qwen2.5-Coder-3B-Instruct"
tok = AutoTokenizer.from_pretrained(base_id)
base = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype=torch.float16, device_map="auto")
model = PeftModel.from_pretrained(base, "Varshitha2603/qwen2.5-coder-3b-text2sql-qlora")
schema = "CREATE TABLE singer (name VARCHAR, age INT)"
question = "How many singers are older than 30?"
msgs = [
{"role": "system", "content": "You are a SQL expert. Given a table schema and a question, reply with only the SQL query."},
{"role": "user", "content": f"Schema:\n{schema}\n\nQuestion: {question}"},
]
prompt = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
inputs = tok(prompt, return_tensors="pt").to(model.device)
out = model.generate(**inputs, max_new_tokens=128, do_sample=False)
print(tok.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True))
b-mc2/sql-create-context (300 other examples held out for testing)