Text Generation
PEFT
Safetensors
English
Chinese
text-to-sql
nl2sql
sql
lora
small-language-model
qwen2.5-coder
dpo
postgresql
mysql
sqlite
ollama
llama-cpp
local-llm
conversational
Eval Results (legacy)
Run this model with HF Jobs
Run xiain/SwiftSQL-3B-LoRA on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/xiain/SwiftSQL-3B-LoRA. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/xiain/SwiftSQL-3B-LoRA on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/xiain/SwiftSQL-3B-LoRA. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/xiain/SwiftSQL-3B-LoRA. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use xiain/SwiftSQL-3B-LoRA with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use xiain/SwiftSQL-3B-LoRA with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("unsloth/Qwen2.5-Coder-3B-Instruct-bnb-4bit") model = PeftModel.from_pretrained(base_model, "xiain/SwiftSQL-3B-LoRA") - Notebooks
- Google Colab
- Kaggle
docs: add SwiftSQL model card
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137---license: apache-2.0base_model: Qwen/Qwen2.5-Coder-3B-Instructlanguage: - en - zhpipeline_tag: text-generationtags: - text-to-sql - sql - gguf - qwen2.5-coder - dpo - rpo - postgresql - mysql - sqlite - ollama - llama-cpp - apple-silicondatasets: - gretelai/synthetic_text_to_sql - trl-lab/SQaLe-text-to-SQL-dataset - jtjt520j/CSpider_and_DUSQL_sql_create_context---
# SwiftSQL-3B (GGUF)
**SwiftSQL-3B** is a fast, accurate, bilingual (**English & Chinese**), multi-dialect (**PostgreSQL / MySQL / SQLite**) lightweight Text-to-SQL model fine-tuned from [`Qwen/Qwen2.5-Coder-3B-Instruct`](https://huggingface.co/Qwen/Qwen2.5-Coder-3B-Instruct) via **SQLite-sandbox-verified SFT + RPO-stabilized Iterative Execution-Guided DPO**.
Designed specifically to run comfortably on **16GB Apple Silicon (Mac mini / MacBook)** and consumer GPUs, while serving production-grade APIs with built-in **anti-hallucination refusal** and **1-shot SQL self-repair**.
- **Code, Training Pipeline & FastAPI Guardrail Gateway**: [github.com/2422725977/swift-sql](https://github.com/2422725977/swift-sql)
---
## Key Advantages / 核心优势
1. **+10.7% Execution Accuracy over Base 3B**: Trained on sandbox-verified executable SQL + iterative execution-guided RPO-DPO (`rpo_alpha=0.5`), pushing 3B execution accuracy from **59.42% → 70.15%** (with 1-shot self-repair).2. **Anti-Hallucination Refusal (`0% → 90.41%` F1)**: Unlike standard open Text-to-SQL models that blindly fabricate missing columns, SwiftSQL explicitly returns `"answerable": false` and lists missing schema columns.3. **Fast Auto-Think (Low Latency)**: Skips verbose Chain-of-Thought (`"plan": ""`) on simple and single-JOIN queries; emits an ultra-compact `<= 60`-char telegraphic plan only for complex multi-JOIN / CTE / subquery / window queries.4. **Native Multi-Dialect & Bilingual (EN/ZH)**: Accepts standard `CREATE TABLE` DDL directly (no custom M-Schema conversion required) and explicitly targets `postgresql`, `mysql`, or `sqlite`.5. **Single-Model Generate + 1-Shot SQL Self-Repair + Strict JSON**: Always outputs a single parseable JSON object (`100%` JSON compliance in eval) and natively supports `Task: repair_sql` when paired with a lightweight `sqlglot` or DB error guardrail.
---
## Verified Benchmark Results (3B `opt2`, 300-sample Held-Out Multi-Dialect + Refusal Eval)
| Metric | Base `3B` | V1 SFT (Sandbox-Cleaned) | V2 R1 (RPO-DPO) | **V2 R2 (Exported)** | **Best + 1-Shot Repair** || :--- | :---: | :---: | :---: | :---: | :---: || **Execution Accuracy (EX)** | 59.42% | 67.63% | 71.28% | **69.15%** | **70.15%** (+10.73%) || **Refusal F1 (Anti-Hallucination)** | 0.00% | 80.00% | 67.35% | **90.41%** | **90.41%** (+90.41%) || **Strict JSON Compliance** | 100.0% | 100.0% | 100.0% | **100.0%** | **100.0%** || **Multi-Dialect Syntax Valid** | 99.63% | 100.0% | 88.01% | **97.38%** | **97.38%** || **Avg Output Length (chars)** | 177.9 | 222.3 | 210.5 | **216.6** | **217.3** |
> *Evaluation runs generated queries transpiled back to an isolated in-memory SQLite sandbox against held-out schemas & seed data, plus bilingual and missing-column refusal tests.*
---
## Available GGUF Files
| File | Size | Recommendation || :--- | :---: | :--- || `SwiftSQL-3B-Q4_K_M.gguf` | 1.80 GB | Ultra-fast, minimal memory footprint & higher concurrency || `SwiftSQL-3B-Q8_0.gguf` | 3.06 GB | Near-lossless quality, ideal for 16GB Apple Silicon / Mac mini |
---
## Quick Start
### 1. Run Immediately with Ollama (1 Command)```bashollama run hf.co/xiain/SwiftSQL-3B-GGUF:Q8_0```
### 2. Serve High-Concurrency OpenAI-Compatible API via `llama.cpp````bashllama-server -hf xiain/SwiftSQL-3B-GGUF:Q8_0 --port 11434 --ctx-size 4096 --parallel 4 --flash-attn```
### 3. Production API Gateway with Read-Only Guardrail & Auto-RepairClone [github.com/2422725977/swift-sql](https://github.com/2422725977/swift-sql) and start `serve/api_gateway.py` in front of Ollama / `llama-server` to get Bearer API key auth, rate limiting, `sqlglot` read-only safety blocking, and automatic 1-shot SQL self-repair.
---
## Prompt Format
**System Prompt**```textYou are SwiftSQL, a fast and accurate multi-dialect Text-to-SQL engine.Rules:1. Respond ONLY with a single valid JSON object. No markdown fences or extra text.2. Follow the requested SQL dialect strictly (postgresql, mysql, or sqlite) and output read-only SELECT queries only.3. Auto-Think: leave "plan" empty ("") for simple and single-JOIN queries; only for complex multi-JOIN, subquery, CTE, HAVING, or window queries, write an ultra-brief plan (<= 60 chars) in "plan" before "sql".4. Never invent tables or columns. If the schema lacks required information to answer the question, set "answerable": false, "sql": "", and list missing fields in "missing".```
**User Prompt (Generation)**```textTask: generate_sqlDialect: postgresqlMode: autoSchema:CREATE TABLE orders (id INT, user_id INT, amount NUMERIC, status TEXT);
Question:统计每个用户的已完成订单总金额,按总金额降序取前 10 名```
**Expected Output**```json{"answerable": true, "plan": "", "dialect": "postgresql", "sql": "SELECT user_id, SUM(amount) AS total_amount FROM orders WHERE status = 'completed' GROUP BY user_id ORDER BY total_amount DESC LIMIT 10", "tables": ["orders"]}```
**User Prompt (1-Shot SQL Self-Repair)**```textTask: repair_sqlDialect: postgresqlSchema:CREATE TABLE orders (id INT, user_id INT, amount NUMERIC, status TEXT);
Question:统计每个用户的订单数
Failed SQL:SELECT user_id, COUNT(*) FORM orders GROUP BY user_id
Error:syntax error at or near "FORM"```
---
## Limitations & Responsible Use- Always enforce database-level read-only permissions and static SQL validation (e.g., `sqlglot`) before executing model-generated SQL in production.- Built on `Qwen/Qwen2.5-Coder-3B-Instruct` (Apache 2.0) and open public datasets; released under **Apache 2.0**.README.mdAdded+137