Views
No views yet
| Metric | Base Model | Fine-Tuned |
|---|---|---|
| Exact Match | 0.0% | 82.0% |
| Execution Accuracy | 84.0% | 96.0% |
| BLEU Score | 55.79 | 96.42 |
| Property | Value |
|---|---|
| Base model | microsoft/Phi-3-mini-4k-instruct (3.8B params) |
| Fine-tuning method | QLoRA (4-bit NF4 + LoRA) |
| LoRA rank | r=16, alpha=32 |
| Trainable parameters | 8,912,896 (0.23%) |
| Training examples | 76,577 |
| Training hardware | NVIDIA RTX 5060 Ti 8GB |
| Training time | 3 hours 2 minutes |
| Final train loss | 0.5677 |
| Max sequence length | 256 tokens |
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from peft import PeftModel
4
5base_model = "microsoft/Phi-3-mini-4k-instruct"
6adapter = "Sid9797/querycraft-phi3-sql"
7
8bnb_config = BitsAndBytesConfig(
9 load_in_4bit=True,
10 bnb_4bit_quant_type="nf4",
11 bnb_4bit_compute_dtype=torch.bfloat16,
12 bnb_4bit_use_double_quant=True,
13)
14
15tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
16model = AutoModelForCausalLM.from_pretrained(
17 base_model,
18 quantization_config=bnb_config,
19 device_map="cuda:0",
20 trust_remote_code=True,
21 torch_dtype=torch.bfloat16,
22)
23model = PeftModel.from_pretrained(model, adapter)
24model.eval()
25
26prompt = '''### System:
27You are a SQL expert. Given a database schema and a natural language question, generate a valid SQL query that answers the question. Output only the SQL query with no explanation.
28
29### Schema:
30CREATE TABLE employees (id INTEGER, name VARCHAR, department VARCHAR, salary FLOAT)
31
32### Question:
33What is the average salary by department?
34
35### SQL:
36'''
37
38inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
39with torch.no_grad():
40 outputs = model.generate(**inputs, max_new_tokens=128, do_sample=False)
41
42sql = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
43print(sql.strip().split("\n")[0])
44# SELECT AVG(salary) FROM employees GROUP BY department```sql ... ```) and appended semicolons.
This formatting breaks exact match evaluation even when the SQL logic is correct.
Fine-tuning on consistently formatted examples eliminated this entirely.