Views
No views yet
CREATE TABLE statement (context) and a question, and the model generates the corresponding SQL query.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.transformers, peft, bitsandbytes, trl.q_proj, k_proj, v_proj, o_proj| 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. |
CREATE TABLE employees (name VARCHAR, dept VARCHAR, salary INT)SELECT name FROM employees WHERE dept = 'Sales' AND salary > 50000peft and transformers libraries. Since this is an adapter, you need to load the base model first.1import torch
2from peft import PeftModel
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5# 1. Load the Base Model
6base_model_id = "Qwen/Qwen2.5-1.5B-Instruct"
7base_model = AutoModelForCausalLM.from_pretrained(
8 base_model_id,
9 device_map="auto",
10 torch_dtype=torch.float16
11)
12
13# 2. Load the Fine-Tuned Adapters
14adapter_model_id = "manuelaschrittwieser/Qwen2.5-1.5B-SQL-Assistant"
15model = PeftModel.from_pretrained(base_model, adapter_model_id)
16tokenizer = AutoTokenizer.from_pretrained(base_model_id)
17
18# 3. Define Context and Question
19context = "CREATE TABLE students (id INT, name VARCHAR, grade INT, subject VARCHAR)"
20question = "List the names of students in grade 10 who study Math."
21
22# 4. Format Prompt
23messages = [
24 {"role": "system", "content": "You are a SQL expert."},
25 {"role": "user", "content": f"{context}\nQuestion: {question}"}
26]
27text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
28
29# 5. Generate SQL
30inputs = tokenizer(text, return_tensors="pt").to(model.device)
31outputs = model.generate(**inputs, max_new_tokens=100)
32response = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
34print("Generated SQL:")
35print(response.split("assistant")[-1].strip())
36