crimson3327 commited on
Commit
28c684d
·
verified ·
1 Parent(s): 5f53540

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +114 -0
README.md CHANGED
@@ -1,3 +1,117 @@
1
  ---
2
  license: mit
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
+ base_model: unsloth/Llama-3.2-3B-Instruct
4
+ tags:
5
+ - text-to-sql
6
+ - sql
7
+ - lora
8
+ - unsloth
9
+ - llama
10
+ language:
11
+ - en
12
  ---
13
+
14
+ # Llama-3.2-3B Text-to-SQL
15
+
16
+ A LoRA fine-tune of [Llama-3.2-3B-Instruct](https://huggingface.co/unsloth/Llama-3.2-3B-Instruct)
17
+ for generating SQL queries from a natural language question plus a table schema.
18
+
19
+ Trained locally on an AMD Radeon RX 9070 XT using [Unsloth](https://github.com/unslothai/unsloth).
20
+
21
+ ## What this model does
22
+
23
+ Given a table schema and a question in plain English, it returns a SQL query — no
24
+ explanation, no preamble, no alternative approaches. The base instruct model tends to
25
+ either refuse ("I don't have access to your database") or respond with an explanatory
26
+ Python example instead of raw SQL. Fine-tuning fixed that: this model reliably answers
27
+ with bare SQL by default.
28
+
29
+ ## Prompt format
30
+
31
+ **This model expects the schema to be included in the prompt.** Without one, it will
32
+ guess plausible-sounding table/column names rather than asking for clarification — same
33
+ as any model would.
34
+
35
+ ```
36
+ Given this schema: CREATE TABLE employees (name VARCHAR, salary INTEGER)
37
+
38
+ Answer this question in SQL: List the names of employees who earn more than 50000
39
+ ```
40
+
41
+ Expected output:
42
+ ```sql
43
+ SELECT name FROM employees WHERE salary > 50000
44
+ ```
45
+
46
+ Use the tokenizer's chat template (`apply_chat_template`) with this as a single user
47
+ turn — see the usage example below.
48
+
49
+ ## Usage
50
+
51
+ ```python
52
+ from unsloth import FastLanguageModel
53
+
54
+ model, tokenizer = FastLanguageModel.from_pretrained(
55
+ model_name = "crimson3327/text_to_sql",
56
+ max_seq_length = 1024,
57
+ )
58
+ FastLanguageModel.for_inference(model)
59
+
60
+ schema = "CREATE TABLE employees (name VARCHAR, salary INTEGER)"
61
+ question = "List the names of employees who earn more than 50000"
62
+
63
+ inputs = tokenizer.apply_chat_template(
64
+ [{"role": "user", "content": f"Given this schema: {schema}\n\nAnswer this question in SQL: {question}"}],
65
+ tokenize=True, add_generation_prompt=True, return_tensors="pt"
66
+ ).to("cuda")
67
+
68
+ outputs = model.generate(input_ids=inputs, max_new_tokens=150)
69
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
70
+ ```
71
+
72
+ ## Training details
73
+
74
+ - **Base model:** unsloth/Llama-3.2-3B-Instruct
75
+ - **Method:** LoRA, rank 16, alpha 16, all attention + MLP projections targeted
76
+ - **Dataset:** [b-mc2/sql-create-context](https://huggingface.co/datasets/b-mc2/sql-create-context)
77
+ (78,577 question/schema/SQL triples), trained on the first 74,000 rows
78
+ - **Held-out eval set:** the remaining ~4,500 rows, never seen during training
79
+ - **Steps:** 1,200 (batch size 2, gradient accumulation 4 — effective batch 8)
80
+ - **Optimizer:** AdamW, linear LR schedule, 2e-4 peak learning rate
81
+ - **Checkpoint selection:** best checkpoint chosen automatically by **eval loss**, not
82
+ training loss, to avoid shipping an overfit checkpoint
83
+ - **Final eval loss:** 0.559
84
+
85
+ ## Example: base model vs. this fine-tune
86
+
87
+ Same prompt, no schema given, asked to filter employee data:
88
+
89
+ **Base Llama-3.2-3B-Instruct** — rewrote the task as a pandas exercise with invented
90
+ sample data, offered a second alternative approach using bitwise operators, no SQL
91
+ produced.
92
+
93
+ **This model:**
94
+ ```sql
95
+ SELECT * FROM employee WHERE salary > 10000 AND Department = 'IT' AND Name = 'John Doe'
96
+ ```
97
+
98
+ ## Known limitations
99
+
100
+ - **Multi-table joins (3+ tables) with aggregation are inconsistent.** Earlier training
101
+ checkpoints produced invalid SQL (joins placed after `GROUP BY`/`HAVING`) and
102
+ hallucinated columns on this pattern specifically. The checkpoint published here
103
+ (trained with the held-out eval split) resolved the specific cases tested, but this
104
+ remains the weakest area — verify output on complex joins before trusting it blindly.
105
+ - **Ambiguous negation phrasing** (e.g. "not yet shipped") may be interpreted as a
106
+ literal status string rather than a negated condition (`!=`). Prefer explicit phrasing
107
+ in questions where this matters.
108
+ - **No schema validation.** The model doesn't verify that referenced columns/tables
109
+ exist — always pass an accurate schema and review output before executing against a
110
+ real database.
111
+ - Trained on SQLite-flavored syntax (matching the source dataset); some queries may need
112
+ adjustment for strict-mode PostgreSQL or other dialects.
113
+
114
+ ## Acknowledgements
115
+
116
+ Fine-tuned with [Unsloth](https://github.com/unslothai/unsloth). Dataset:
117
+ [b-mc2/sql-create-context](https://huggingface.co/datasets/b-mc2/sql-create-context).