Downloads · 30 days
27
16% of all-time downloads
manuelaschrittwieser/Qwen2.5-1.5B-SQL-Assistant
Qwen2.5-1.5B-SQL-Assistant is a machine learning model from manuelaschrittwieser. 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 apache-2.0.
Qwen2.5-1.5B-SQL-Assistant is a fine-tuned version of Qwen/Qwen2.5-1.5B-Instruct, specialized in translating natural language questions into syntactically correct SQL queries based on a provided database schema.
Downloads · 30 days
27
16% of all-time downloads
All-time downloads
166
Public
Repo size
28.9 MB
Likes
1
Public
Click a slice to open those files.
.safetensors17.5 MB · 52%
From the Hugging Face model README
Qwen2.5-1.5B-SQL-Assistant is a fine-tuned version of Qwen/Qwen2.5-1.5B-Instruct, specialized in translating natural language questions into syntactically correct SQL queries based on a provided database schema.
This model was trained using PEFT (Parameter-Efficient Fine-Tuning) and LoRA (Low-Rank Adaptation) techniques to ensure efficient training on consumer hardware while retaining the reasoning capabilities of the base model.
CREATE TABLE statement (context) and a question, and the model generates the corresponding SQL query.The model was fine-tuned on the b-mc2/sql-create-context dataset.
context: The SQL schema definition (e.g., CREATE TABLE ...).question: A natural language query (e.g., "How many users are active?").answer: The correct SQL query corresponding to the question.system, user, assistant roles) to leverage the instruction-following capabilities of the base model.The model was trained using QLoRA (Quantized LoRA) to minimize memory usage.
transformers, peft, bitsandbytes, trl.q_proj, k_proj, v_proj, o_projThe model was evaluated qualitatively on a hold-out test set.
| Feature | Base Model (Qwen 2.5-1.5B-Instruct) | Fine-Tuned Model (SQL-Assistant) |
|---|---|---|
| Response Format | Often chatty; explains the code before/after. | Concise; outputs strictly the SQL query. |
| Schema Adherence | Sometimes hallucinates column names not in the schema. | Strongly adheres to the provided CREATE TABLE context. |
| Syntax Accuracy | Good, but prone to minor syntax errors in complex joins. | Improved syntax specific to standard SQL queries. |
Sample Test Case:
CREATE TABLE employees (name VARCHAR, dept VARCHAR, salary INT)SELECT name FROM employees WHERE dept = 'Sales' AND salary > 50000You can load this model using the peft and transformers libraries. Since this is an adapter, you need to load the base model first.
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
# 1. Load the Base Model
base_model_id = "Qwen/Qwen2.5-1.5B-Instruct"
base_model = AutoModelForCausalLM.from_pretrained(
base_model_id,
device_map="auto",
torch_dtype=torch.float16
)
# 2. Load the Fine-Tuned Adapters
adapter_model_id = "manuelaschrittwieser/Qwen2.5-1.5B-SQL-Assistant"
model = PeftModel.from_pretrained(base_model, adapter_model_id)
tokenizer = AutoTokenizer.from_pretrained(base_model_id)
# 3. Define Context and Question
context = "CREATE TABLE students (id INT, name VARCHAR, grade INT, subject VARCHAR)"
question = "List the names of students in grade 10 who study Math."
# 4. Format Prompt
messages = [
{"role": "system", "content": "You are a SQL expert."},
{"role": "user", "content": f"{context}\nQuestion: {question}"}
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
# 5. Generate SQL
inputs = tokenizer(text, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=100)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("Generated SQL:")
print(response.split("assistant")[-1].strip())