Text Generation
Safetensors
GGUF
English
Chinese
bash
shell
cli
devops
nl2sh
shellcheck
safety
ollama
unsloth
sft
grpo
conversational
Instructions to use xiain/SafeShell-3B-v3-LoRA with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Local Apps Settings
- Unsloth Desktop
|
Download README.md from xiain/SafeShell-3B-v3-LoRA: direct link, hf CLI and curl.
- Browser
- Download file 9.03 kB
-
https://huggingface.co/xiain/SafeShell-3B-v3-LoRA/resolve/main/README.md
- Command line
-
hf download hf://xiain/SafeShell-3B-v3-LoRA/README.md
-
curl -L -o README.md https://huggingface.co/xiain/SafeShell-3B-v3-LoRA/resolve/main/README.md
9.03 kB
| license: apache-2.0 | |
| language: | |
| - en | |
| - zh | |
| base_model: unsloth/Qwen2.5-Coder-3B-Instruct | |
| tags: | |
| - bash | |
| - shell | |
| - cli | |
| - devops | |
| - nl2sh | |
| - shellcheck | |
| - safety | |
| - gguf | |
| - ollama | |
| - unsloth | |
| - sft | |
| - grpo | |
| pipeline_tag: text-generation | |
| # SafeShell-3B-v3 | |
| > **Newer Release Available (`v4`)**: See [`xiain/SafeShell-3B-v4-GGUF`](https://huggingface.co/xiain/SafeShell-3B-v4-GGUF) and [`xiain/SafeShell-3B-v4-LoRA`](https://huggingface.co/xiain/SafeShell-3B-v4-LoRA) (`96.0%` missing-info refusal, `98.7%` ALFA Clean, `477.3 ms` p50 latency; `v3` remains available for slightly higher raw `nl2bash` binary utility `77.2%`). | |
| **A compact, hallucination-resistant Linux Bash & DevOps copilot that validates commands before you run them, classifies risk (`safe` / `caution` / `destructive`), provides `dry_run` previews, and refuses unsafe or underspecified requests.** | |
| Runs fast locally on everyday laptops and desktops (Windows / macOS / Linux, 8GB–16GB RAM, CPU-only or consumer GPUs) via Ollama or `llama.cpp`—zero cloud dependency and zero telemetry. | |
| --- | |
| ## Why SafeShell-3B-v3? | |
| Most lightweight `nl2bash` models blindly invent placeholder paths (`/path/to/file`), fail `ShellCheck`, or happily emit `rm -rf /`, fork bombs, and raw block-device wipes when framed as a benchmark or cleanup task. SafeShell-3B-v3 is built for real-world terminal and AI Agent workflows: | |
| 1. **Top-Tier Safety & Boundary Discipline**: Achieves **96.0% adversarial safety refusal recall** (`+Guardrail+1Shot`) and **84.0% pure model-only**, with **100.0% accuracy & 0% false refusals** on explicit concrete destructive tasks (`unseen_boundary_safe_30`). | |
| 2. **Zero-Hallucination Missing-Info Refusal**: Achieves **60.0% model-only / 68.0% system** missing-info refusal recall (vs **0%–12%** across all 7 open-source 3B–8B peers tested), refusing to invent filenames, hosts, or container IDs when prompts omit them. | |
| 3. **Highest `NL2SH-ALFA` ShellCheck Clean Rate (98.0%)**: Beats specialized 3B/8B shell models and general 4B/7B coders in clean syntax & strict single-JSON schema compliance (**99.7% model-only / 100.0% system**), at **518.8 ms** median single-request latency. | |
| 4. **Built-In Risk Grading + Read-Only `dry_run` Previews**: Labels every command as `safe`, `caution`, or `destructive` (**96.7%** accuracy on tricky boundary tasks) and emits safe previews (e.g., `find ... -print` before `-delete`, `ls -la` before `rm`). | |
| 5. **Bilingual English & Chinese DevOps + 1-Shot Self-Repair**: **96.7%** clean on colloquial Chinese DevOps tasks and **100.0%** 1-shot syntax/safety self-repair when fed linter errors. | |
| --- | |
| ## Quick Start (Ollama, `llama.cpp`, & SafeShell CLI) | |
| ```bash | |
| # 1. Run high-precision Q8_0 locally via Ollama (~3.29 GB) | |
| ollama run hf.co/xiain/SafeShell-3B-v3-GGUF:Q8_0 | |
| # 2. Or run compact Q4_K_M for 8GB RAM / CPU-only laptops (~1.93 GB) | |
| ollama run hf.co/xiain/SafeShell-3B-v3-GGUF:Q4_K_M | |
| # 3. Run OpenAI-compatible server with llama.cpp | |
| llama-server -m SafeShell-3B-v3-Q8_0.gguf --port 8080 -ngl 99 | |
| ``` | |
| For full deterministic `bash -n` + `ShellCheck` validation, automatic `dry_run` reconciliation, and 1-shot self-repair: | |
| ```bash | |
| git clone https://github.com/2422725977/safeshell.git && cd safeshell | |
| pip install -e ".[serve]" | |
| safesh --model hf.co/xiain/SafeShell-3B-v3-GGUF:Q8_0 "Count ERROR lines in ./logs/app.log" | |
| ``` | |
| ### Example Outputs | |
| Scoped destructive command with safe read-only preview: | |
| ```json | |
| {"plan":"scoped_destructive:find","answerable":true,"risk_level":"destructive","command":"find \"./logs\" -type f -name \"*.log\" -mtime +30 -delete","dry_run":"find \"./logs\" -type f -name \"*.log\" -mtime +30 -print","missing_info":[],"refusal_reason":""} | |
| ``` | |
| Underspecified prompt refusal (no fabricated placeholder): | |
| ```json | |
| {"plan":"missing:container_id","answerable":false,"risk_level":"safe","command":"","dry_run":"","missing_info":["container name or ID"],"refusal_reason":"Missing required details: container name or ID."} | |
| ``` | |
| --- | |
| ## Fair Two-Track Market Benchmark (1,028 Zero-Leakage Prompts) | |
| Every model was evaluated under identical zero-leakage conditions across held-out **`NL2SH-ALFA` (300)**, **`nl2bash` (606)**, and 5 strictly unseen adversarial/bilingual suites (**122** prompts: `unseen_safety_25`, `unseen_missing_info_25`, `unseen_boundary_safe_30`, `unseen_zh_devops_30`, `unseen_self_repair_12`). Raw-command specialists were evaluated via native track (`NativeRaw`), and general chat models received markdown code-fence extraction (`+fence`). | |
| | Model | Track | ALFA Clean | ALFA Util | nl2b Clean | nl2b Util | SafeRec (25) | MissRec (25) | BoundOK (30) | ZH Clean (30) | Repair (12) | p50 (ms) | | |
| | :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | |
| | **`SafeShell-3B-v3 (+Guardrail+1Shot)`** | System | **98.0%** | 75.5% | 90.6% | 77.2% | **96.0%** | **68.0%** | **100.0%** | 96.7% | **100.0%** | n/a | | |
| | **`SafeShell-3B-v3`** | JSON | **98.0%** | 75.3% | 90.4% | 77.0% | 84.0% | 60.0% | **100.0%** | 96.7% | **100.0%** | **518.8** | | |
| | `SafeShell-3B-v2 (+Guardrail+1Shot)` | System | n/a | n/a | n/a | n/a | 84.0% | **68.0%** | 90.0% | 83.3% | **100.0%** | n/a | | |
| | `SafeShell-3B-v2` | JSON | 97.7% | 77.1% | 90.8% | 77.2% | 72.0% | 60.0% | 90.0% | 83.3% | **100.0%** | 550.2 | | |
| | `Qwen2.5-Coder-3B-NL2SH` (3B) | NativeRaw | 91.7% | 75.4% | **93.7%** | **86.6%** | 64.0% | 12.0% | 93.3% | **100.0%** | 0.0% | 512.5 | | |
| | `Nemotron-Terminal-8B` (8B) | NativeRaw | 94.7% | 75.8% | 90.1% | 70.0% | 84.0% | 8.0% | 96.7% | 96.7% | 0.0% | 831.2 | | |
| | `Qwen3-4B` (4B) | JSON | 89.7% | **77.3%** | 88.1% | 74.3% | 72.0% | 0.0% | **100.0%** | **100.0%** | **100.0%** | 555.0 | | |
| | `Qwen2.5-Coder-7B-Base` (7B) | +fence | 93.3% | 74.3% | 84.3% | 72.4% | 64.0% | 0.0% | 96.7% | 96.7% | **100.0%** | 768.1 | | |
| | `Qwen2.5-Coder-3B-Base` (3B) | +fence | 92.0% | 63.9% | 85.8% | 61.7% | 48.0% | 0.0% | 96.7% | 96.7% | **100.0%** | 709.2 | | |
| | `Phi-4-mini-instruct` (3.8B) | +fence | 79.3% | 66.5% | 79.4% | 74.7% | 84.0% | 4.0% | 86.7% | 83.3% | 90.0% | 1853.7 | | |
| | `Llama-3.2-3B-Instruct` (3B) | JSON | 81.7% | 63.0% | 80.5% | 68.2% | 20.0% | 0.0% | 90.0% | 76.7% | 90.0% | **428.7** | | |
| ### Honest Trade-Offs & Known Limitations | |
| * **Specialized Raw Shell Utility vs Safety-First JSON**: `Qwen2.5-Coder-3B-NL2SH` remains stronger on raw `nl2bash` utility matching (`86.6%` vs `77.2%`), while `SafeShell-3B-v3` prioritizes syntax hygiene (`98.0%` ALFA Clean), safety refusal (`96.0%`), missing-info refusal (`68.0%`), and structured risk metadata (`dry_run` + `plan`). | |
| * **Missing-Info Refusal Headroom**: Model-only `MissRec` is `60.0%` (`68.0%` with guardrail). Although 5x–68x ahead of market baselines (`0%–12%`), some underspecified phrasings (e.g., implicit target file names like `dump.sql` or `commit_hash`) can still trigger command guesses when used without the guardrail. | |
| * **Noisy `nl2bash` Prompts Under Strict Repair**: On `nl2bash_606`, false-refusal rate is `6.1%` model-only and rises to `9.4%` under `+Guardrail+1Shot`, because prompts containing generic placeholder-like identifiers (`filename`, `myfile`) intentionally trigger missing-info clarification during repair. | |
| --- | |
| ## Training & Alignment Recipe (`v3`) | |
| * **Base Model**: `unsloth/Qwen2.5-Coder-3B-Instruct` | |
| * **Data Curation (18,000 curated records, 70% Generate / 18% Repair / 12% Refusal, 22% ZH)**: | |
| * Strictly held-out deduplication against `NL2SH-ALFA` test (300), `nl2bash` test (606), `yannelli/cli-command-risk` holdout (100), and all 122 unseen eval prompts. | |
| * `bash -n`, `ShellCheck`, safety rules, and ungrounded-target pruning removed 1,282 conflicting/noisy pairs. | |
| * Augmented with strictly `critical`-only distilled safety refusals (444), sanity-filtered missing-info contrastive twins (447, rejecting 36 noisy pairs), distilled colloquial Chinese DevOps tasks (369), and 750 scoped risky concrete examples (`rm`, `find -delete`, `docker rm --force`, `git reset --hard`, `systemctl`). | |
| * **SFT & Validation-Gated GRPO**: | |
| * Pure bf16 LoRA (`r=64`, `alpha=128`, all 7 linear layers, dropout `0.0`) on A100-80GB converged to `eval_loss = 0.04365` (**0.9483** validation composite score). | |
| * A balanced 60/40 verifiable-reward **GRPO** run (120 steps, `num_generations=8`, `lr=4e-6`) improved utility match (`90.3%`) and `dry_run` coverage (`65.1%`) with `0.9445` composite score; automated validation gating kept the stronger **SFT** checkpoint (`0.9483 > 0.9445`) before final GGUF export. | |
| --- | |
| ## Canonical GGUF Checksums | |
| | File | Quantization | Size (Bytes) | SHA-256 | | |
| | :--- | :---: | :---: | :--- | | |
| | `SafeShell-3B-v3-Q8_0.gguf` | `Q8_0` | `3,285,475,968` (~3.06 GiB) | `584f9e8e49c181841182b74bfef79b8eab76f908da5caab316763034c55ddd23` | | |
| | `SafeShell-3B-v3-Q4_K_M.gguf` | `Q4_K_M` | `1,929,902,720` (~1.80 GiB) | `3a4778daccd369d187ef0d63c4f71f54f0e71016959d05f072987b81f61965a5` | | |
| * **Source Code, Deterministic Guardrail CLI, Gateway & Benchmarks**: [`https://github.com/2422725977/safeshell`](https://github.com/2422725977/safeshell) | |