Views
No views yet
| Metric | Value |
|---|---|
| Initial Loss | 2.50 |
| Final Loss | 0.37 |
| Loss Reduction | 85% |
| Trainable Parameters | 9.17M (0.51% of 1.8B total) |
| Training Time | 47 minutes |
| GPU | NVIDIA A10G (24GB VRAM) |
pip install transformers torch accelerate1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# Load model and tokenizer
5model_name = "Abhisek987/llama-3.2-sql-merged"
6model = AutoModelForCausalLM.from_pretrained(
7 model_name,
8 device_map="auto",
9 torch_dtype=torch.float16
10)
11tokenizer = AutoTokenizer.from_pretrained(model_name)
12
13# Prepare prompt
14database = "employees"
15question = "What are the names of all employees who earn more than 50000?"
16
17prompt = f"""### Instruction:
18You are a SQL expert. Generate a SQL query to answer the given question for the specified database.
19
20### Input:
21Database: {database}
22Question: {question}
23
24### Response:
25"""
26
27# Generate SQL
28inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
29outputs = model.generate(
30 **inputs,
31 max_new_tokens=256,
32 temperature=0.1,
33 do_sample=True,
34 pad_token_id=tokenizer.eos_token_id
35)
36
37result = tokenizer.decode(outputs[0], skip_special_tokens=True)
38sql_query = result.split("### Response:")[-1].strip()
39print(sql_query)SELECT name FROM employees WHERE salary > 50000;1def generate_sql_batch(questions_with_db):
2 """Generate SQL for multiple questions"""
3 results = []
4
5 for database, question in questions_with_db:
6 prompt = f"""### Instruction:
7You are a SQL expert. Generate a SQL query.
8
9### Input:
10Database: {database}
11Question: {question}
12
13### Response:
14"""
15 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
16 outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.1)
17 result = tokenizer.decode(outputs[0], skip_special_tokens=True)
18 sql = result.split("### Response:")[-1].strip()
19 results.append(sql)
20
21 return results
22
23# Example usage
24queries = [
25 ("employees", "Show all employees"),
26 ("sales", "Top 5 products by revenue"),
27 ("customers", "Count by country")
28]
29
30sql_queries = generate_sql_batch(queries)| Database | Question | Generated SQL |
|---|---|---|
| employees | "Show all employees with salary above 60000" | SELECT name FROM employees WHERE salary > 60000; |
| sales | "Show me the top 5 products by total sales" | SELECT product_id, sum(sales) FROM sales GROUP BY product_id ORDER BY sum(sales) DESC LIMIT 5; |
| customers | "How many customers are from each country?" | SELECT count(*), country FROM customers GROUP BY country; |
| orders | "Find all orders placed in the last 30 days" | SELECT order_id FROM orders WHERE date_order_placed BETWEEN DATE('now') - INTERVAL 30 DAY AND DATE('now') - INTERVAL 1 DAY; |
| concert_singer | "What are the names of all singers ordered by net worth?" | SELECT Name FROM singer ORDER BY Net_Worth; |
1@misc{llama32-sql-merged,
2 author = {Abhisek Behera},
3 title = {Llama 3.2 3B SQL Query Generator - LoRA Fine-tuned},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/Abhisek987/llama-3.2-sql-merged}},
7 note = {Fine-tuned on Spider dataset using LoRA}
8}1@article{llama3,
2 title={Llama 3 Model Card},
3 author={Meta AI},
4 year={2024},
5 url={https://github.com/meta-llama/llama3}
6}1@inproceedings{yu2018spider,
2 title={Spider: A Large-Scale Human-Labeled Dataset for Complex and Cross-Domain Semantic Parsing and Text-to-SQL Task},
3 author={Yu, Tao and Zhang, Rui and Yang, Kai and others},
4 booktitle={EMNLP},
5 year={2018}
6}