Instructions to use AaronTekle/SQLQwen with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use AaronTekle/SQLQwen with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Coder-0.5B-Instruct") model = PeftModel.from_pretrained(base_model, "AaronTekle/SQLQwen") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -154,11 +154,93 @@ completed fine-tuning run shows:
|
|
| 154 |
|
| 155 |
## Imports (PEFT library):
|
| 156 |
```python
|
|
|
|
| 157 |
from peft import PeftModel
|
| 158 |
-
from transformers import AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Coder-0.5B-Instruct")
|
| 161 |
-
model = PeftModel.from_pretrained(base_model, "AaronTekle/SQLQwen")
|
| 162 |
```
|
| 163 |
|
| 164 |
## References:
|
|
|
|
| 154 |
|
| 155 |
## Imports (PEFT library):
|
| 156 |
```python
|
| 157 |
+
|
| 158 |
from peft import PeftModel
|
| 159 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 160 |
+
import torch
|
| 161 |
+
|
| 162 |
+
# Load base model
|
| 163 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 164 |
+
"Qwen/Qwen2.5-Coder-0.5B-Instruct"
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# Load LoRA adapter
|
| 168 |
+
model = PeftModel.from_pretrained(
|
| 169 |
+
base_model,
|
| 170 |
+
"AaronTekle/SQLQwen"
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# Load tokenizer
|
| 174 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 175 |
+
"Qwen/Qwen2.5-Coder-0.5B-Instruct"
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
# Database table context
|
| 179 |
+
schema = """
|
| 180 |
+
Database: company_db
|
| 181 |
+
|
| 182 |
+
Table: employees
|
| 183 |
+
Columns:
|
| 184 |
+
- employee_id (INT, Primary Key)
|
| 185 |
+
- first_name (VARCHAR)
|
| 186 |
+
- last_name (VARCHAR)
|
| 187 |
+
- department_id (INT)
|
| 188 |
+
- salary (DECIMAL)
|
| 189 |
+
- hire_date (DATE)
|
| 190 |
+
|
| 191 |
+
Table: departments
|
| 192 |
+
Columns:
|
| 193 |
+
- department_id (INT, Primary Key)
|
| 194 |
+
- department_name (VARCHAR)
|
| 195 |
+
- location (VARCHAR)
|
| 196 |
+
|
| 197 |
+
Table: projects
|
| 198 |
+
Columns:
|
| 199 |
+
- project_id (INT, Primary Key)
|
| 200 |
+
- project_name (VARCHAR)
|
| 201 |
+
- employee_id (INT, Foreign Key -> employees.employee_id)
|
| 202 |
+
- budget (DECIMAL)
|
| 203 |
+
"""
|
| 204 |
+
|
| 205 |
+
# User question
|
| 206 |
+
question = "Find the names of employees earning more than 50000."
|
| 207 |
+
|
| 208 |
+
# Build prompt
|
| 209 |
+
prompt = f"""
|
| 210 |
+
You are an expert SQL query generator.
|
| 211 |
+
|
| 212 |
+
Generate a SQL query based on the database schema and user question.
|
| 213 |
+
|
| 214 |
+
Database Schema:
|
| 215 |
+
{schema}
|
| 216 |
+
|
| 217 |
+
Question:
|
| 218 |
+
{question}
|
| 219 |
+
|
| 220 |
+
Return only the SQL query without explanation.
|
| 221 |
+
"""
|
| 222 |
+
|
| 223 |
+
# Tokenize
|
| 224 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 225 |
+
|
| 226 |
+
# Generate SQL
|
| 227 |
+
with torch.no_grad():
|
| 228 |
+
outputs = model.generate(
|
| 229 |
+
**inputs,
|
| 230 |
+
max_new_tokens=200,
|
| 231 |
+
do_sample=False,
|
| 232 |
+
pad_token_id=tokenizer.eos_token_id
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
# Display result
|
| 236 |
+
generated_text = tokenizer.decode(
|
| 237 |
+
outputs[0][inputs["input_ids"].shape[1]:],
|
| 238 |
+
skip_special_tokens=True
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
print("\nGenerated SQL:")
|
| 242 |
+
print(generated_text)
|
| 243 |
|
|
|
|
|
|
|
| 244 |
```
|
| 245 |
|
| 246 |
## References:
|