This model is a fine-tuned version of
Qwen/Qwen1.5-1.8B-Chat
adapted for
Text-to-SQL generation using the Spider dataset.
Convert natural language questions into SQL queries.
The model retains general knowledge after SQL fine-tuning.
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3from peft import PeftModel
4
5base_model_name = "Qwen/Qwen1.5-1.8B-Chat"
6adapter_name = "faltooz123/qwen1.5-sql-qlora-spider"
7
8tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
9
10bnb_config = BitsAndBytesConfig(
11 load_in_4bit = True,
12 bnb_4bit_quant_type = "nf4",
13 bnb_4bit_compute_dtype = torch.float16,
14)
15base_model = AutoModelForCausalLM.from_pretrained(
16 base_model_name,
17 quantization_config = bnb_config,
18 device_map = {"": 0},
19 trust_remote_code = True,
20)
21
22model = PeftModel.from_pretrained(base_model, adapter_name)
23model.eval()
24
25def generate_sql(question, db_id):
26 prompt = (
27 "<|im_start|>system\n"
28 "You are an expert SQL assistant.<|im_end|>\n"
29 "<|im_start|>user\n"
30 f"Database: {db_id}\n"
31 f"Question: {question}\n"
32 "Write only the SQL query.<|im_end|>\n"
33 "<|im_start|>assistant\n"
34 )
35 inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
36 with torch.no_grad():
37 outputs = model.generate(**inputs, max_new_tokens=128, do_sample=False)
38 generated = outputs[0][inputs["input_ids"].shape[1]:]
39 return tokenizer.decode(generated, skip_special_tokens=True).strip()
40
41sql = generate_sql("How many singers do we have?", "concert_singer")
42print(sql)
1@misc{qwen1.5-sql-qlora,
2 title = {Qwen1.5-1.8B SQL Fine-Tuned with QLoRA on Spider},
3 year = {2025},
4}