Text Generation
Transformers
Safetensors
English
phi3
sql
text-to-sql
code-generation
phi-3
fine-tuned
conversational
custom_code
text-generation-inference
Instructions to use Shizu0n/phi3-mini-sql-generator-merged with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Shizu0n/phi3-mini-sql-generator-merged with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Shizu0n/phi3-mini-sql-generator-merged", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("Shizu0n/phi3-mini-sql-generator-merged", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("Shizu0n/phi3-mini-sql-generator-merged", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use Shizu0n/phi3-mini-sql-generator-merged with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Shizu0n/phi3-mini-sql-generator-merged" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Shizu0n/phi3-mini-sql-generator-merged", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/Shizu0n/phi3-mini-sql-generator-merged
- SGLang
How to use Shizu0n/phi3-mini-sql-generator-merged with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Shizu0n/phi3-mini-sql-generator-merged" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Shizu0n/phi3-mini-sql-generator-merged", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Shizu0n/phi3-mini-sql-generator-merged" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Shizu0n/phi3-mini-sql-generator-merged", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use Shizu0n/phi3-mini-sql-generator-merged with Docker Model Runner:
docker model run hf.co/Shizu0n/phi3-mini-sql-generator-merged
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
---
|
|
|
|
|
|
|
|
|
|
| 2 |
language:
|
| 3 |
- en
|
| 4 |
-
|
|
|
|
| 5 |
tags:
|
| 6 |
- sql
|
| 7 |
- text-to-sql
|
|
@@ -9,21 +13,15 @@ tags:
|
|
| 9 |
- phi-3
|
| 10 |
- fine-tuned
|
| 11 |
- text-generation
|
| 12 |
-
|
|
|
|
| 13 |
---
|
| 14 |
|
| 15 |
# Phi-3 Mini SQL Generator — Merged Model
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
## Why two versions?
|
| 22 |
-
|
| 23 |
-
| Repo | Purpose |
|
| 24 |
-
|---|---|
|
| 25 |
-
| `Shizu0n/phi3-mini-sql-generator` | Original QLoRA adapter — documents the training pipeline |
|
| 26 |
-
| `Shizu0n/phi3-mini-sql-generator-merged` | Merged standalone model — used for deployment and inference |
|
| 27 |
|
| 28 |
## Evaluation — Base vs Fine-tuned
|
| 29 |
|
|
@@ -34,29 +32,25 @@ Evaluated on 200 held-out examples from [b-mc2/sql-create-context](https://huggi
|
|
| 34 |
| Phi-3-mini-4k-instruct (base) | 2.0% |
|
| 35 |
| **This model (fine-tuned)** | **73.5%** |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
## Training Details
|
| 38 |
|
| 39 |
- **Dataset:** b-mc2/sql-create-context — 1,000 train / 200 validation examples
|
| 40 |
-
- **Method:** QLoRA (4-bit NF4, LoRA rank 16, alpha 32)
|
| 41 |
- **Hardware:** NVIDIA T4 (Google Colab free tier)
|
| 42 |
- **Training time:** ~21 min
|
| 43 |
- **Final train loss:** 0.6526
|
|
|
|
| 44 |
|
| 45 |
-
##
|
| 46 |
-
|
| 47 |
-
The merge was accepted only after all three smoke tests returned a concrete SQL query:
|
| 48 |
-
|
| 49 |
-
1. PEFT adapter loaded on the base model.
|
| 50 |
-
2. Local merged directory after `merge_and_unload()` and `save_pretrained()`.
|
| 51 |
-
3. Downloaded model from this Hugging Face repo with `force_download=True`.
|
| 52 |
-
|
| 53 |
-
Reference smoke output:
|
| 54 |
-
|
| 55 |
-
```sql
|
| 56 |
-
SELECT AVG(salary), department FROM employees GROUP BY department
|
| 57 |
-
```
|
| 58 |
-
|
| 59 |
-
## Inference example
|
| 60 |
|
| 61 |
```python
|
| 62 |
import torch
|
|
@@ -64,26 +58,20 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 64 |
|
| 65 |
model_id = "Shizu0n/phi3-mini-sql-generator-merged"
|
| 66 |
|
| 67 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=
|
| 68 |
model = AutoModelForCausalLM.from_pretrained(
|
| 69 |
model_id,
|
| 70 |
-
|
| 71 |
device_map="auto",
|
| 72 |
trust_remote_code=False,
|
| 73 |
attn_implementation="eager",
|
| 74 |
)
|
| 75 |
-
model.
|
| 76 |
|
| 77 |
prompt = (
|
| 78 |
-
"Given the following SQL table, write a SQL query.
|
| 79 |
-
|
| 80 |
-
"
|
| 81 |
-
"Table: employees (id, name, department, salary)
|
| 82 |
-
|
| 83 |
-
"
|
| 84 |
-
"Question: What is the average salary per department?
|
| 85 |
-
|
| 86 |
-
SQL:"
|
| 87 |
)
|
| 88 |
|
| 89 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
@@ -96,13 +84,24 @@ with torch.inference_mode():
|
|
| 96 |
repetition_penalty=1.1,
|
| 97 |
pad_token_id=tokenizer.eos_token_id,
|
| 98 |
)
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
```
|
| 103 |
|
| 104 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
-
|
| 107 |
-
-
|
| 108 |
-
-
|
|
|
|
| 1 |
---
|
| 2 |
+
base_model: microsoft/Phi-3-mini-4k-instruct
|
| 3 |
+
library_name: transformers
|
| 4 |
+
license: mit
|
| 5 |
language:
|
| 6 |
- en
|
| 7 |
+
datasets:
|
| 8 |
+
- b-mc2/sql-create-context
|
| 9 |
tags:
|
| 10 |
- sql
|
| 11 |
- text-to-sql
|
|
|
|
| 13 |
- phi-3
|
| 14 |
- fine-tuned
|
| 15 |
- text-generation
|
| 16 |
+
- phi3
|
| 17 |
+
pipeline_tag: text-generation
|
| 18 |
---
|
| 19 |
|
| 20 |
# Phi-3 Mini SQL Generator — Merged Model
|
| 21 |
|
| 22 |
+
Merged standalone version of [Shizu0n/phi3-mini-sql-generator](https://huggingface.co/Shizu0n/phi3-mini-sql-generator)
|
| 23 |
+
— LoRA adapter weights fused into [Phi-3-mini-4k-instruct](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct).
|
| 24 |
+
No PEFT dependency required for inference.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
## Evaluation — Base vs Fine-tuned
|
| 27 |
|
|
|
|
| 32 |
| Phi-3-mini-4k-instruct (base) | 2.0% |
|
| 33 |
| **This model (fine-tuned)** | **73.5%** |
|
| 34 |
|
| 35 |
+
> Exact match: normalized SQL comparison (lowercase, strip whitespace/semicolons).
|
| 36 |
+
|
| 37 |
+
## Why two versions?
|
| 38 |
+
|
| 39 |
+
| Repo | Purpose |
|
| 40 |
+
|---|---|
|
| 41 |
+
| [`Shizu0n/phi3-mini-sql-generator`](https://huggingface.co/Shizu0n/phi3-mini-sql-generator) | QLoRA adapter — documents the training pipeline |
|
| 42 |
+
| `Shizu0n/phi3-mini-sql-generator-merged` | Merged standalone — used for deployment and inference |
|
| 43 |
+
|
| 44 |
## Training Details
|
| 45 |
|
| 46 |
- **Dataset:** b-mc2/sql-create-context — 1,000 train / 200 validation examples
|
| 47 |
+
- **Method:** QLoRA (4-bit NF4, LoRA rank 16, alpha 32, target modules: qkv_proj/o_proj/gate_up_proj/down_proj)
|
| 48 |
- **Hardware:** NVIDIA T4 (Google Colab free tier)
|
| 49 |
- **Training time:** ~21 min
|
| 50 |
- **Final train loss:** 0.6526
|
| 51 |
+
- **Best checkpoint:** step 250 (by eval loss)
|
| 52 |
|
| 53 |
+
## Inference Example
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
```python
|
| 56 |
import torch
|
|
|
|
| 58 |
|
| 59 |
model_id = "Shizu0n/phi3-mini-sql-generator-merged"
|
| 60 |
|
| 61 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 62 |
model = AutoModelForCausalLM.from_pretrained(
|
| 63 |
model_id,
|
| 64 |
+
torch_dtype=torch.float16,
|
| 65 |
device_map="auto",
|
| 66 |
trust_remote_code=False,
|
| 67 |
attn_implementation="eager",
|
| 68 |
)
|
| 69 |
+
model.eval()
|
| 70 |
|
| 71 |
prompt = (
|
| 72 |
+
"Given the following SQL table, write a SQL query.\n\n"
|
| 73 |
+
"Table: employees (id, name, department, salary)\n\n"
|
| 74 |
+
"Question: What is the average salary per department?\n\nSQL:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
| 84 |
repetition_penalty=1.1,
|
| 85 |
pad_token_id=tokenizer.eos_token_id,
|
| 86 |
)
|
| 87 |
+
prompt_len = inputs["input_ids"].shape[-1]
|
| 88 |
+
print(tokenizer.decode(outputs[0][prompt_len:], skip_special_tokens=True))
|
| 89 |
+
```
|
| 90 |
|
| 91 |
+
Expected output:
|
| 92 |
+
```sql
|
| 93 |
+
SELECT AVG(salary), department FROM employees GROUP BY department
|
| 94 |
```
|
| 95 |
|
| 96 |
+
## Validation
|
| 97 |
+
|
| 98 |
+
Merge accepted after three smoke tests:
|
| 99 |
+
1. PEFT adapter loaded on base model
|
| 100 |
+
2. Local merged directory after `merge_and_unload()` + `save_pretrained()`
|
| 101 |
+
3. Downloaded from this repo with `force_download=True`
|
| 102 |
+
|
| 103 |
+
## Limitations
|
| 104 |
|
| 105 |
+
- Fine-tuned on 1,000 examples — best suited for simple to medium complexity SELECT queries
|
| 106 |
+
- Not tested on dialect-specific SQL (PostgreSQL/MySQL-specific functions)
|
| 107 |
+
- May struggle with multi-table JOINs and nested subqueries
|