Downloads · 30 days
4
9% of all-time downloads
BY-ALF/llama-3.2-3b-sql-lora
llama-3.2-3b-sql-lora is a machine learning model from BY-ALF. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for peft. The card lists the license as llama3.2.
LoRA adapter fine-tuning Llama-3.2-3B-Instruct to convert natural language questions + database schema into clean, directly-executable SQL queries.
Downloads · 30 days
4
9% of all-time downloads
All-time downloads
47
Public
Repo size
35.6 MB
Likes
0
Public
Click a slice to open those files.
.safetensors18.4 MB · 52%
From the Hugging Face model README
LoRA adapter fine-tuning Llama-3.2-3B-Instruct to convert natural language questions + database schema into clean, directly-executable SQL queries.
This model fine-tunes Llama-3.2-3B-Instruct using LoRA to specialize in text-to-SQL generation. Given a table schema and a natural language question, it outputs the corresponding SQL query with no extra formatting, markdown, or explanation — making it directly usable in automated pipelines.
Given a schema (as a CREATE TABLE statement) and a natural language question, generates the corresponding SQL query. Intended for prototyping text-to-SQL tools, automating simple database queries from plain English, or as a base for further fine-tuning on domain-specific schemas.
Not intended for production use on critical systems without human review of generated queries. Not evaluated on SQL dialects other than standard/SQLite-style syntax. Not evaluated for adversarial/injection-style inputs.
Always review generated SQL before running it against a production database. Consider adding execution-based validation (e.g., running against a sandboxed copy of the schema) before trusting outputs in an automated pipeline.
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
base_model_name = "meta-llama/Llama-3.2-3B-Instruct"
adapter_name = "BY-ALF/llama-3.2-3b-sql-lora"
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
base_model = AutoModelForCausalLM.from_pretrained(base_model_name, torch_dtype=torch.bfloat16, device_map="auto")
model = PeftModel.from_pretrained(base_model, adapter_name)
prompt = """### Context:
CREATE TABLE employees (name VARCHAR, department VARCHAR, salary INTEGER)
### Question:
What is the average salary in the Sales department?
### SQL:
"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=100, do_sample=False)
print(tokenizer.decode(output[0], skip_special_tokens=True).split("### SQL:")[-1].strip())
b-mc2/sql-create-context — approximately 75,000 examples, each containing a database schema (as a CREATE TABLE statement), a natural language question, and the corresponding correct SQL query.
Each example was formatted into a single prompt combining schema, question, and target SQL, separated by section headers (### Context:, ### Question:, ### SQL:).
100 held-out examples from the same dataset (b-mc2/sql-create-context), not seen during training.
Exact-match accuracy: generated SQL normalized (whitespace, casing) and compared verbatim to ground truth.
| Model | Exact Match Accuracy |
|---|---|
| Base Llama-3.2-3B-Instruct | 30% |
| Fine-tuned (this model) | 76% |
The fine-tuned model substantially outperforms the base model on exact-match accuracy. Manual review showed the base model's underlying SQL logic was frequently correct, but it consistently wrapped output in markdown code fences and added explanatory text, which fails strict exact-match scoring. The fine-tuned model learned to produce clean, directly-executable SQL matching the expected format — a meaningful practical improvement for automated use, though part of the accuracy gap reflects formatting compliance rather than pure reasoning improvement. The most common remaining failure mode is incorrect join conditions in queries involving 3+ tables.
Causal language model (Llama 3.2 architecture, 3B parameters) fine-tuned via LoRA for the text-to-SQL generation task, using standard supervised fine-tuning (next-token prediction on schema+question+SQL sequences).
AMD Radeon RX 7900 XTX (24GB VRAM), AMD Ryzen 7 processor
Questions or feedback: open an issue on the linked GitHub repository.