Files
s41r4j-aish/docs/QWEN_UNSLOTH_FINETUNE_PLAN.md
T

6.7 KiB

AiSH Qwen + Unsloth Fine-Tuning Plan

This document defines the first practical model-training track for AiSH.

Decision

AiSH will start by fine-tuning a small Qwen-family model with Unsloth.

Preferred order:

1. Qwen3.5-0.8B, if the exact model ID is available
2. Closest official sub-1B Qwen-family model, if Qwen3.5-0.8B is not available
3. Around-2B Qwen-family model, if the sub-1B model is underwhelming

The first goal is not to create a general chatbot. The model should be specialized for terminal command assistance.

AiSH Model Role

The model should help with:

- command generation from short user intent
- command completion from partial input
- command explanation
- command alternative suggestions
- shell-aware command translation
- OS-aware command translation

The model should not own execution.

Execution flow:

User input
  ↓
AiSH UI
  ↓
History/project completion layer
  ↓
Fine-tuned Qwen model, only when needed
  ↓
Command candidate
  ↓
AiSH runtime safety detector
  ↓
User confirmation if risky
  ↓
Shell execution

Guardrail Position

AiSH should not train the model to behave like a restricted assistant that refuses normal terminal tasks.

Instead:

Model layer:      command-focused, direct, minimal refusal behavior
Runtime layer:    detects risky/destructive commands
UX layer:         asks for confirmation before risky execution

The model can output commands, explanations, and alternatives. AiSH decides whether the command needs confirmation before execution.

Why Start Small

The sub-1B model is the correct first experiment because AiSH needs low latency and on-device execution.

Target behavior:

- fast enough for interactive use
- small enough for laptops
- good at common shell workflows
- useful for command generation when manually triggered

If the sub-1B model cannot produce reliable commands after fine-tuning, move to an around-2B Qwen-family model.

Training Method

Use Unsloth with LoRA or QLoRA.

Recommended first setup:

Method:           QLoRA
Base precision:   4-bit
LoRA rank:        16 or 32
Sequence length:  512 to 2048
Batching:         gradient accumulation
Training style:   supervised fine-tuning

The first training run should optimize output format and command correctness, not broad reasoning.

Dataset Format

Use instruction-style JSONL.

Example:

{"instruction":"Suggest a command for the user's terminal task.","input":"OS: windows\nShell: powershell\nCWD type: node_vite_project\nUser input: start this app","output":"npm run dev"}

Example with explanation:

{"instruction":"Return a command and a short explanation.","input":"OS: macos\nShell: zsh\nCWD type: git_repo\nUser input: show changed files","output":"Command: git status --short\nExplanation: Shows modified, staged, and untracked files in a compact format."}

Example with alternatives:

{"instruction":"Suggest command alternatives.","input":"OS: linux\nShell: bash\nUser input: list files with details","output":"1. ls -la\n2. find . -maxdepth 1 -type f\n3. tree -a -L 1"}

Dataset Sources

Use a mixture of:

- generated command-intent pairs
- package manager examples
- git examples
- docker examples
- python tooling examples
- node/npm/pnpm/yarn examples
- cargo examples
- make examples
- powershell examples
- cmd examples
- bash/zsh/fish examples
- accepted AiSH suggestions later
- rejected AiSH suggestions later

Dataset Buckets

Create separate buckets so evaluation is easier.

1. command_generation
2. command_completion
3. command_explanation
4. command_alternatives
5. shell_translation
6. os_translation
7. project_aware_commands
8. risky_command_labeling

The risky-command bucket is for labeling and explanation quality. Execution safety remains inside AiSH runtime, not inside the model.

Output Formats

AiSH should train predictable output formats.

Command only

npm run dev

Command with metadata

{
  "command": "npm run dev",
  "confidence": 0.86,
  "requires_confirmation": false,
  "reason": "Detected a Node project with a dev script."
}

The structured JSON format is better for app integration. The command-only format is better for raw latency experiments.

First Fine-Tuning Target

First experiment:

Base model:       Qwen3.5-0.8B if available
Fallback model:   closest Qwen sub-1B model
Trainer:          Unsloth
Method:           QLoRA
Dataset size:     5k to 20k high-quality examples
Eval set:         500 to 1k examples
Goal:             command-focused behavior

Second Fine-Tuning Target

If the first model is underwhelming:

Base model:       around-2B Qwen-family model
Trainer:          Unsloth
Method:           QLoRA
Dataset size:     20k to 50k examples
Eval set:         1k to 2k examples
Goal:             better command generation and shell reasoning

Evaluation

Measure:

- exact command match
- semantic command match
- top-3 useful suggestion rate
- invalid command rate
- wrong shell syntax rate
- wrong OS command rate
- structured JSON validity
- latency after quantization
- risky command flagging accuracy

Minimum acceptance targets for the sub-1B model:

Command validity:        85%+
Correct shell syntax:    85%+
JSON validity:           95%+
Useful top-3 rate:       75%+
Risky flag recall:       95%+

If it misses these after dataset cleanup and one retune, move to the around-2B model.

Runtime Safety

Runtime safety is outside the model.

AiSH should parse a generated command before execution and classify it as:

safe
needs_confirmation
blocked_by_policy

For normal development commands, the command can be accepted quickly.

For risky commands, AiSH should show:

- command
- reason it is risky
- affected path/service if detectable
- explicit confirmation prompt

The model should not be relied on as the final safety authority.

Export Plan

After fine-tuning:

1. Save LoRA adapter
2. Merge adapter into base model for release builds if needed
3. Quantize for local inference
4. Export GGUF for llama.cpp-style runtimes if generation is used locally
5. Keep ONNX or a smaller ranker path for ultra-fast ranking

AiSH can use two model paths:

Fast path:  deterministic suggestions + ranker
AI path:    fine-tuned Qwen generator on manual trigger

Final Recommendation

Start with the Qwen sub-1B experiment through Unsloth, but keep the product architecture model-agnostic.

The correct first model milestone is:

A small local command generator that is useful when triggered manually,
while AiSH runtime handles destructive-command confirmation before execution.