Instructions to use Shreyas-11/llama-3.2-1b-sql-q4_k_m with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use Shreyas-11/llama-3.2-1b-sql-q4_k_m with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M # Run inference directly in the terminal: llama cli -hf Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M # Run inference directly in the terminal: llama cli -hf Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
Build from source code
git clone https://github.com/ggml-org/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-app # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama serve -hf Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M # Run inference directly in the terminal: ./build/bin/llama cli -hf Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
Use Docker
docker model run hf.co/Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
- Llama new
- LM Studio
- Jan
- Ollama
How to use Shreyas-11/llama-3.2-1b-sql-q4_k_m with Ollama:
ollama run hf.co/Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
- Unsloth Desktop
- Pi
How to use Shreyas-11/llama-3.2-1b-sql-q4_k_m with Pi:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama serve -hf Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
Configure the model in Pi
# Install Pi: npm install -g @earendil-works/pi-coding-agent # Add to ~/.pi/agent/models.json: { "providers": { "llama-cpp": { "baseUrl": "http://localhost:9931/v1", "api": "openai-completions", "apiKey": "none", "models": [ { "id": "Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M" } ] } } }Run Pi
# Start Pi in your project directory: pi
- Docker Model Runner
How to use Shreyas-11/llama-3.2-1b-sql-q4_k_m with Docker Model Runner:
docker model run hf.co/Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
- Lemonade
How to use Shreyas-11/llama-3.2-1b-sql-q4_k_m with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
Run and chat with the model
lemonade run user.llama-3.2-1b-sql-q4_k_m-Q4_K_M
List all available models
lemonade list
- Hermes Agent
How to use Shreyas-11/llama-3.2-1b-sql-q4_k_m with Hermes Agent:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama serve -hf Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
Configure Hermes
# Install Hermes: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash hermes setup # Point Hermes at the local server: hermes config set model.provider custom hermes config set model.base_url http://127.0.0.1:9931/v1 hermes config set model.default Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
Run Hermes
hermes
- Atomic Chat
- OpenClaw
How to use Shreyas-11/llama-3.2-1b-sql-q4_k_m with OpenClaw:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama serve -hf Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M
Configure OpenClaw
# Install OpenClaw: npm install -g openclaw@latest # Register the local server and set it as the default model: openclaw onboard --non-interactive --mode local \ --auth-choice custom-api-key \ --custom-base-url http://127.0.0.1:9931/v1 \ --custom-model-id "Shreyas-11/llama-3.2-1b-sql-q4_k_m:Q4_K_M" \ --custom-provider-id llama-cpp \ --custom-compatibility openai \ --custom-text-input \ --accept-risk \ --skip-health
Run OpenClaw
openclaw agent --local --agent main --message "Hello from Hugging Face"
- Enterprise SLM Text-to-SQL Model
- Model Training Deep Dive & Engineering Learnings
- Empirical Benchmarks
- Benchmark 1: Untrained Base Model vs. Fine-Tuned QLoRA 1B
- Benchmark 2: 20-Query Production Stress Test on 75,858 Rows (7 Tables)
- Q1: "Why use an SLM (1B) instead of GPT-4, Claude, or a 70B open-weights model?"
- Q2: "How does QLoRA work, and why was it chosen here?"
- Q3: "What is GGUF and why use Q4_K_M quantization?"
- Q4: "Why did the raw 1B model struggle when given all 7 tables at once?"
- Q5: "How does the 5-Stage Pipeline achieve 100% accuracy?"
- Q6: "How do you guarantee database security against prompt injection or data corruption?"
Enterprise SLM Text-to-SQL Model
An end-to-end, zero-fallback Text-to-SQL model that translates natural language business questions into precise, executable SQL queries over an enterprise relational database.
Powered by a 4-bit QLoRA fine-tuned Llama-3.2-1B-Instruct model quantized to Q4_K_M GGUF, can be run locally on any device with basic configuration.
Model Training Deep Dive & Engineering Learnings
1. Base Model Selection: Why a 1B Small Language Model (SLM)?
- Footprint & Edge Viability:
Llama-3.2-1B-Instructquantizes to a ~750 MB GGUF binary (Q4_K_M), enabling real-time execution on ordinary CPU hardware without requiring dedicated multi-GPU clusters. - Inference Latency: Sub-second response times (
450 ms – 1,100 ms) compared to 5–15+ seconds for remote 70B models or cloud APIs. - Data Privacy & Security: Zero internal database schema definitions or proprietary company records ever leave the local network boundary.
- Cost Efficiency: Zero ongoing token API costs or cloud compute infrastructure bills.
2. Fine-Tuning Setup: 4-Bit QLoRA via Unsloth
The model was fine-tuned in Google Colab using notebooks/train_text_to_sql.ipynb using Unsloth and Hugging Face TRL (SFTTrainer):
- Base Precision: 4-bit NormalFloat (
NF4) with double quantization to minimize memory footprint. - LoRA Target Matrices: Applied across all linear layers:
- Attention projections:
q_proj,k_proj,v_proj,o_proj - SwiGLU MLP projections:
gate_proj,up_proj,down_proj
- Attention projections:
- LoRA Hyperparameters: Rank $r=16$, $\alpha=32$ ($\text{scaling factor} = 2.0$),
lora_dropout=0(enabling Unsloth fused Triton forward/backward kernels). - Trainable Parameter Ratio: ~11.27M parameters trained out of 1.25B (only 0.90%), preserving base language understanding while mastering schema-to-SQL translation.
- Optimizer & Schedule: 8-bit AdamW optimizer (
optim="adamw_8bit"),learning_rate=2e-4, linear schedule with warmup, effective batch size of 8 (per_device_batch_size=2,gradient_accumulation_steps=4). - Telemetry & Hardware Monitoring: Custom
ResourceMonitorCallbacktracking GPU VRAM, CPU utilization, and system RAM to Weights & Biases (wandb) and TensorBoard.
3. Prompt Template & Formatting
Trained using the single-turn Alpaca prompt structure matching backend/services/sql_generator.py:
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
Write a SQL query to answer this question: {question}
### Input:
{context}
### Response:
{answer}<|eot_id|>
4. Critical Learning: Why Raw Small Models Over-Join on Database Dumps
During initial stress testing, when all 7 tables were passed into the raw 1B model, it attempted 4-to-5 table joins and crashed on simple questions. Why?
- Dataset Artifact (
b-mc2/sql-create-context): In standard Text-to-SQL benchmarks, every example contains only the exact tables needed for that query. There are zero "distractor" tables in the dataset. - The Model's Learned Prior: The model learned the conditional distribution: "If $N$
CREATE TABLEstatements are in### Input:, the output SQL must join all $N$ tables." - The Engineering Fix: Rather than replacing the 1B model with a bloated 70B model, we built Stage 1 (Schema Linker) and Stage 2 (FK Graph BFS) in front of the model. By pruning the prompt down to the relevant tables, the prompt returned to the model's training distribution:
- Pure 1B exact match jumped from
15.0%to55.0%without any validator repairs. - Latency dropped by 60% due to shorter prompt context.
- Adding Stage 4 (AST Validator) boosted final accuracy to
100.0%.
- Pure 1B exact match jumped from
Empirical Benchmarks
Benchmark 1: Untrained Base Model vs. Fine-Tuned QLoRA 1B
Evaluated directly in the training notebook on an unseen multi-table relational schema:
| Metric | Untrained Base 1B | Fine-Tuned QLoRA 1B | Difference |
|---|---|---|---|
| Inference Latency | 4.05s |
1.16s |
3.49x Faster |
| Tokens Generated | 128 (hit token limit) |
41 (stopped at EOS) |
68.0% Token Reduction |
| Output Format | Markdown fences + conversational prose | Pure, executable SQL | Zero parsing fragility |
| Syntactic Validity | Invalid (conversational filler) | 100% Valid SQL | Production-ready |
Benchmark 2: 20-Query Production Stress Test on 75,858 Rows (7 Tables)
Evaluated across 5 complexity tiers using backend/stress_test.py on the 7-table enterprise database:
| Pipeline Stage Configuration | Valid SQL Execution Rate | Exact Result Match (EX) | Avg SLM Latency | Avg 75K-Row DB Latency |
|---|---|---|---|---|
| 1. Direct Raw 1B (Full 7-table dump, no pipeline) | 13/20 (65.0%) |
3/20 – 9/20 (15.0% – 45.0%) |
2,137 ms – 3,225 ms |
N/A (crashes) |
2. 1B + Schema Linker Only (Repairs: []) |
20/20 (100.0%) |
11/20 (55.0%) |
1,004 ms – 1,260 ms |
6.41 ms |
| 3. Full 5-Stage Production Pipeline | 20/20 (100.0%) |
20/20 (100.0%) |
1,004 ms (2.56x faster) |
6.41 ms (Indexed B-Tree) |
Q1: "Why use an SLM (1B) instead of GPT-4, Claude, or a 70B open-weights model?"
"For Text-to-SQL in an enterprise environment, a 1B SLM offers three critical advantages: zero data leakage, deterministic latency, and cost-free scalability. A 750 MB GGUF model runs locally on existing CPU servers in under 1 second without sending proprietary database schemas or customer records over the public internet. By combining fine-tuning with a 5-stage deterministic pipeline (schema linking, FK expansion, AST validation), we match or exceed the accuracy of large models at a fraction of the cost."
Q2: "How does QLoRA work, and why was it chosen here?"
"QLoRA (Quantized Low-Rank Adaptation) freezes the base model weights quantized in 4-bit NormalFloat (NF4) and attaches small trainable low-rank decomposition matrices ($\Delta W = \frac{\alpha}{r} B A$) to the attention and MLP layers. In our case, with $r=16$ and $\alpha=32$, we only trained ~11.27M parameters (0.90% of the model). This allowed us to fine-tune on a single free Google Colab T4 GPU in ~5 minutes without GPU out-of-memory errors, while fully preserving the model's base language capabilities."
Q3: "What is GGUF and why use Q4_K_M quantization?"
"GGUF is the binary model format designed by the
llama.cppecosystem for fast CPU inference with optimized SIMD/AVX instructions.Q4_K_Muses medium 4-bit quantization with higher precision allocated to critical attention layers. It compresses the model from ~2.5 GB to ~750 MB with virtually zero loss in SQL generation accuracy, allowing standard application servers to serve inference without GPUs."
Q4: "Why did the raw 1B model struggle when given all 7 tables at once?"
"This was one of our most interesting discoveries. The
b-mc2/sql-create-contextdataset contains zero distractor tables; every table in the prompt is used in the target query. As a result, the model learned a strong prior: 'if 7 tables are in the prompt, join all 7 tables.' When handed a full database dump, it over-joined. Instead of increasing parameter size, we solved this with an upstream Schema Linker that uses an inverted index over categorical values and table attributes to prune the prompt. That single change boosted raw model accuracy from 15% to 55%."
Q5: "How does the 5-Stage Pipeline achieve 100% accuracy?"
*"The pipeline separates responsibilities:
- Schema Linker: Selects the required tables using catalog attribute and categorical value matching.
- FK Graph BFS: Discovers intermediate bridge tables (e.g.,
Users$\leftrightarrow$Orders$\leftrightarrow$Products).- 1B SLM: Generates the relational SQL logic in 450–1,100 ms.
- AST Validator: Deterministically fixes common small-model edge cases (verifying catalog foreign keys in
ONclauses, cross-table alias alignment, stripping unmentioned filters, and aligningGROUP BYprojections).- Read-Only SQLite: Executes the query in sub-15ms using pre-indexed B-trees with strict security controls."*
Q6: "How do you guarantee database security against prompt injection or data corruption?"
*"Security is enforced at multiple layers:
- Driver-Level Read-Only URI: The backend connects to SQLite using
file:analytics.db?mode=rowithuri=True. Any attempt to executeDROP,DELETE,UPDATE, orINSERTfails at the C-level SQLite engine boundary regardless of what the LLM produces.- AST Whitelisting: Only single
SELECTorWITHqueries are accepted; semicolons and multiple statements are rejected before execution.- Progress Handler & Row Caps: SQLite progress handler aborts queries exceeding 5 seconds to eliminate Cartesian DoS attacks, and result sets are capped at 5,000 rows."*
Fine tuned by SHREYAS PATHAK
- Downloads last month
- 458
4-bit
Model tree for Shreyas-11/llama-3.2-1b-sql-q4_k_m
Base model
meta-llama/Llama-3.2-1B-Instruct