bonsai-slm 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. bonsai_slm-1.0.0/LICENSE +21 -0
  2. bonsai_slm-1.0.0/PKG-INFO +265 -0
  3. bonsai_slm-1.0.0/README.md +236 -0
  4. bonsai_slm-1.0.0/bonsai/__init__.py +6 -0
  5. bonsai_slm-1.0.0/bonsai/cli.py +340 -0
  6. bonsai_slm-1.0.0/bonsai/core/__init__.py +1 -0
  7. bonsai_slm-1.0.0/bonsai/core/constants.py +47 -0
  8. bonsai_slm-1.0.0/bonsai/core/holdout.py +102 -0
  9. bonsai_slm-1.0.0/bonsai/core/ingest.py +105 -0
  10. bonsai_slm-1.0.0/bonsai/core/manifest.py +60 -0
  11. bonsai_slm-1.0.0/bonsai/core/probe.py +175 -0
  12. bonsai_slm-1.0.0/bonsai/core/teacher.py +593 -0
  13. bonsai_slm-1.0.0/bonsai/evaluators/__init__.py +1 -0
  14. bonsai_slm-1.0.0/bonsai/evaluators/exam.py +300 -0
  15. bonsai_slm-1.0.0/bonsai/evaluators/reporter.py +324 -0
  16. bonsai_slm-1.0.0/bonsai/exporters/__init__.py +1 -0
  17. bonsai_slm-1.0.0/bonsai/exporters/ollama.py +95 -0
  18. bonsai_slm-1.0.0/bonsai/trainers/__init__.py +8 -0
  19. bonsai_slm-1.0.0/bonsai/trainers/merger.py +104 -0
  20. bonsai_slm-1.0.0/bonsai/trainers/trainer.py +233 -0
  21. bonsai_slm-1.0.0/bonsai/web_server.py +689 -0
  22. bonsai_slm-1.0.0/bonsai_pruned/serve.py +49 -0
  23. bonsai_slm-1.0.0/bonsai_slm.egg-info/PKG-INFO +265 -0
  24. bonsai_slm-1.0.0/bonsai_slm.egg-info/SOURCES.txt +37 -0
  25. bonsai_slm-1.0.0/bonsai_slm.egg-info/dependency_links.txt +1 -0
  26. bonsai_slm-1.0.0/bonsai_slm.egg-info/entry_points.txt +2 -0
  27. bonsai_slm-1.0.0/bonsai_slm.egg-info/requires.txt +13 -0
  28. bonsai_slm-1.0.0/bonsai_slm.egg-info/top_level.txt +2 -0
  29. bonsai_slm-1.0.0/pyproject.toml +47 -0
  30. bonsai_slm-1.0.0/setup.cfg +4 -0
  31. bonsai_slm-1.0.0/tests/test_cli.py +90 -0
  32. bonsai_slm-1.0.0/tests/test_evaluator_multimetric.py +52 -0
  33. bonsai_slm-1.0.0/tests/test_holdout_leakage.py +77 -0
  34. bonsai_slm-1.0.0/tests/test_ingest.py +34 -0
  35. bonsai_slm-1.0.0/tests/test_manifest.py +33 -0
  36. bonsai_slm-1.0.0/tests/test_merger.py +61 -0
  37. bonsai_slm-1.0.0/tests/test_multi_archetype_teacher.py +123 -0
  38. bonsai_slm-1.0.0/tests/test_probe.py +29 -0
  39. bonsai_slm-1.0.0/tests/test_web_server.py +107 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Bonsai AI Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,265 @@
1
+ Metadata-Version: 2.4
2
+ Name: bonsai-slm
3
+ Version: 1.0.0
4
+ Summary: The art of cultivating massive foundation models into pocket-sized intelligences on consumer silicon.
5
+ Author-email: Bonsai Core Team <cultivate@bonsai.ai>
6
+ License: MIT
7
+ Keywords: ai,distillation,lora,qlora,slm,local-ai,quantization,micro-models
8
+ Classifier: Development Status :: 4 - Beta
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
13
+ Requires-Python: >=3.10
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: typer[all]>=0.9.0
17
+ Requires-Dist: rich>=13.7.0
18
+ Requires-Dist: pydantic>=2.0.0
19
+ Requires-Dist: psutil>=5.9.0
20
+ Requires-Dist: requests>=2.31.0
21
+ Requires-Dist: transformers>=4.40.0
22
+ Requires-Dist: peft>=0.10.0
23
+ Requires-Dist: accelerate>=0.28.0
24
+ Requires-Dist: datasets>=2.18.0
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
27
+ Requires-Dist: black>=24.0.0; extra == "dev"
28
+ Dynamic: license-file
29
+
30
+ <p align="center">
31
+ <img src="assets/bonsai-hero-banner.png" alt="Bonsai Hero Banner" width="100%" />
32
+ </p>
33
+
34
+ <h1 align="center">๐ŸŒฑ BONSAI (Major v1.0.0)</h1>
35
+
36
+ <p align="center">
37
+ <strong>The art of cultivating massive foundation models into pocket-sized intelligences on consumer silicon.</strong>
38
+ </p>
39
+
40
+ <p align="center">
41
+ <a href="https://pypi.org/project/bonsai-slm/"><img src="https://img.shields.io/pypi/v/bonsai-slm.svg?color=emerald" alt="PyPI version" /></a>
42
+ <a href="https://github.com/shenald-dev/bonsai/actions"><img src="https://img.shields.io/badge/CI-Passing-2ea44f.svg" alt="CI Status" /></a>
43
+ <a href="https://github.com/shenald-dev/bonsai/releases/tag/v0.1.0"><img src="https://img.shields.io/github/v/release/shenald-dev/bonsai?color=emerald" alt="Release" /></a>
44
+ <a href="https://github.com/shenald-dev/bonsai/stargazers"><img src="https://img.shields.io/github/stars/shenald-dev/bonsai?style=social" alt="GitHub Stars" /></a>
45
+ <a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-0969da.svg" alt="License: MIT" /></a>
46
+ <a href="https://www.python.org/"><img src="https://img.shields.io/badge/Python-3.10%2B-blue.svg" alt="Python 3.10+" /></a>
47
+ <a href="https://pytorch.org/"><img src="https://img.shields.io/badge/PyTorch-2.6%2B%20CUDA-ee4c2c.svg" alt="PyTorch CUDA" /></a>
48
+ <a href="https://github.com/shenald-dev/bonsai"><img src="https://img.shields.io/badge/Zero--OOM-Guaranteed-brightgreen.svg" alt="Zero-OOM Protected" /></a>
49
+ </p>
50
+
51
+ ---
52
+
53
+ ## โšก The 10-Second Quickstart
54
+
55
+ ```bash
56
+ pip install bonsai-slm
57
+
58
+ # Or run instantly without installation:
59
+ uvx bonsai-slm prune ./my_raw_data
60
+ ```
61
+
62
+ No JSONL formatting. No CUDA out-of-memory crashes. No hyperparameter tuning. Drop raw logs, CSVs, code files, or a plain-English task goalโ€”Bonsai takes care of the rest.
63
+
64
+ ---
65
+
66
+ ## ๐ŸŒŠ Why Bonsai?
67
+
68
+ Running local models is solved (`ollama`, `LM Studio`). But **99% of developers and everyday power-users have zero idea how to distill or train their own micro-models (0.5B to 1.5B) on their own home PC.**
69
+
70
+ They get blocked by:
71
+ - โŒ Manually formatting instruction-tuning JSONL pairs
72
+ - โŒ Sudden `CUDA out of memory` crashes
73
+ - โŒ Confusing ML hyperparameter jargon (`lora_r`, `grad_accum`, `cosine_warmup`)
74
+ - โŒ Catastrophic forgetting and hallucination loops
75
+ - โŒ Zero quantitative proof that their fine-tuned model actually improved
76
+
77
+ **Bonsai eliminates the friction entirely.**
78
+
79
+ It acts as an autonomous master artisan: inspecting your exact GPU VRAM / Unified Memory, cultivating training pairs across diverse task archetypes with guaranteed zero data leakage, executing an ultra-fast LoRA distillation pass with prompt token loss masking, proving accuracy gains on an unseen blind holdout exam, fusing weights into standalone merged models, and hosting an instant local Web UI and OpenAI-compatible API.
80
+
81
+ ---
82
+
83
+ ## ๐Ÿ›๏ธ How It Works: The 5-Step Cultivation Loop
84
+
85
+ ```mermaid
86
+ flowchart LR
87
+ A["๐Ÿ’ป 1. PROBE<br/>Hardware Auto-Downshift"] --> B["๐Ÿ“ฅ 2. INGEST<br/>Raw Docs, Logs, or Goals"]
88
+ B --> C["๐Ÿงฌ 3. CULTIVATE<br/>Multi-Archetype Zero-Leakage Split"]
89
+ C --> D["โšก 4. DISTILL & FUSE<br/>Masked-Loss LoRA + Model Merge"]
90
+ D --> E["๐Ÿ”ฌ 5. PROOF & SERVE<br/>SVG Scorecard + OpenAI API"]
91
+ ```
92
+
93
+ 1. **Hardware Auto-Probe:** Probes GPU VRAM, Apple Unified Memory, or CPU RAM. If VRAM is tight (<4 GB), it automatically downshifts base models (e.g., `Qwen2.5-Coder-0.5B`) to guarantee **Zero-OOM**.
94
+ 2. **Universal Ingestion:** Drop raw `.txt`, `.csv`, `.md`, server logs, Python/JS scripts, or specify a plain-English task.
95
+ 3. **Multi-Archetype Teacher Engine:** Synthesizes high-fidelity instruction-response pairs across 4 distinct domains (JSON telemetry, Q&A extraction, code generation, support triage) and enforces strict scenario deduplication *before* augmentation, splitting into an 85% training set and a 15% unseen blind holdout exam.
96
+ 4. **Pocket LoRA Distillation & Weight Fusion:** Freezes 99.78% of the model and cultivates only the top 0.22% of synaptic adapter weights with **prompt loss masking** (`labels[:prompt_len] = -100`). Fuse weights directly into standalone weights via `bonsai merge` for zero-overhead inference or direct llama.cpp/GGUF conversion.
97
+ 5. **The Proof Machine & API Serving:** Evaluates the untrained base model side-by-side against your newly cultivated model on the unseen exam set, emitting a viral SVG scorecard, interactive HTML report, and instant OpenAI-compatible API endpoint.
98
+
99
+ ---
100
+
101
+ ## ๐Ÿ’ป CLI Commands
102
+
103
+ ### 1. Probe Hardware
104
+ Inspects silicon headroom and translates ML jargon into human concepts:
105
+ ```bash
106
+ bonsai probe
107
+ ```
108
+
109
+ ```
110
+ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
111
+ โ”‚ ๐ŸŒฑ BONSAI HARDWARE TELEMETRY โ”‚
112
+ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
113
+ Silicon: NVIDIA GeForce RTX 2050 (3300 MB Free VRAM)
114
+ Recommended Micro-Model: Qwen2.5-Coder-0.5B-Instruct (Zero-OOM Protected)
115
+
116
+ Translated Cultivation Recipe:
117
+ โ€ข Learning Agility (Learning Rate): 2e-4
118
+ โ€ข Synaptic Plasticity (LoRA Rank): r=8, alpha=16
119
+ โ€ข Study Passes (Epochs): 3 passes
120
+ โ€ข Attention Batching (Accumulation): 4 steps
121
+ ```
122
+
123
+ ### 2. Prune & Distill
124
+ Train a micro-AI on your local files or task description:
125
+ ```bash
126
+ # From a folder of raw logs/documents:
127
+ bonsai prune ./examples/messy_ops_to_json
128
+
129
+ # Or from a task prompt:
130
+ bonsai prune "Build a tiny AI that extracts strict JSON invoices from messy emails"
131
+ ```
132
+
133
+ ### 3. Exam & Proof Machine
134
+ Evaluate the newly trained model on the unseen blind test set:
135
+ ```bash
136
+ bonsai exam ./bonsai_pruned
137
+ ```
138
+
139
+ Generates:
140
+ * ๐Ÿ“Š **Viral SVG Scorecard:** `bonsai-report.svg`
141
+ * ๐ŸŒ **Interactive HTML Report:** `bonsai-report.html`
142
+ * ๐Ÿ“‹ **Canonical State Manifest:** `bonsai_manifest.json`
143
+
144
+ ### 4. Merge Synaptic Weights (New in v1.0)
145
+ Fuse LoRA adapter weights directly into the base model to create a standalone, zero-dependency model:
146
+ ```bash
147
+ bonsai merge ./bonsai_pruned -o ./bonsai_pruned/merged
148
+ ```
149
+
150
+ The merged output is a standard HuggingFace / safetensors directory that can be quantized directly to GGUF via `llama.cpp` or loaded with zero adapter overhead.
151
+
152
+ ### 5. Interactive Web Chat & OpenAI-Compatible API
153
+ Launch the local dark-mode UI and standards-compliant OpenAI API endpoint (`/v1/chat/completions`):
154
+ ```bash
155
+ bonsai ui --port 7860
156
+ ```
157
+
158
+ #### Querying via cURL:
159
+ ```bash
160
+ curl http://localhost:7860/v1/chat/completions \
161
+ -H "Content-Type: application/json" \
162
+ -d '{
163
+ "model": "bonsai-micro-ai",
164
+ "messages": [
165
+ {"role": "user", "content": "CRITICAL: db connection pool exhausted on worker-01"}
166
+ ]
167
+ }'
168
+ ```
169
+
170
+ #### Querying via Python `openai` SDK:
171
+ ```python
172
+ from openai import OpenAI
173
+
174
+ client = OpenAI(base_url="http://localhost:7860/v1", api_key="not-needed")
175
+ response = client.chat.completions.create(
176
+ model="bonsai-micro-ai",
177
+ messages=[{"role": "user", "content": "Extract invoice details from receipt"}]
178
+ )
179
+ print(response.choices[0].message.content)
180
+ ```
181
+
182
+ ---
183
+
184
+ ## ๐Ÿ“Š Live Verification Scorecard (Clean-Room NVIDIA RTX 2050)
185
+
186
+ <p align="center">
187
+ <img src="assets/bonsai-report.svg" alt="Bonsai Scorecard" width="100%" />
188
+ </p>
189
+
190
+ | Evaluation Metric | Raw Base Model | Cultivated Bonsai Model | Net Delta |
191
+ |:---|:---:|:---:|:---:|
192
+ | **Strict Schema Validity** | 100.0% | **100.0%** | Maintained |
193
+ | **Output Cleanliness** | Markdown code fences, float fields | **Strict Bare JSON, snake_case** | **Reflexive Precision** |
194
+ | **Training Duration** | โ€” | **34.05 seconds** | **Ultra-Fast** |
195
+ | **Loss Convergence** | 1.5905 | **0.7713** | **-51.5% Convergence** |
196
+ | **Peak VRAM Allocated** | โ€” | **2,045 MB** | **Zero-OOM Verified** |
197
+ | **Inference Latency** | 3,533 ms | **4,352 ms** | Consumer Silicon Ready |
198
+
199
+ ---
200
+
201
+ ## ๐ŸฅŠ Comparison Matrix
202
+
203
+ | Feature | Unsloth | Axolotl | Ollama | **๐ŸŒฑ Bonsai v1.0** |
204
+ |:---|:---:|:---:|:---:|:---:|
205
+ | **Target User** | ML Engineers | Senior ML Researchers | Everyone | **Everyone** |
206
+ | **Dataset Requirement** | Manual `.jsonl` | Complex YAML + `.jsonl` | None (Inference only) | **Zero-Knowledge (Raw files/prompts)** |
207
+ | **Hardware Safety** | Manual VRAM tuning | Manual tuning | Fixed GGUF | **Automated Zero-OOM Downshift** |
208
+ | **Synthetic Teacher** | โŒ Manual | โŒ Manual | โŒ None | **โœ… Universal Multi-Archetype Teacher** |
209
+ | **Adapter Fusion (`merge`)**| Manual Python | Manual Python | โŒ None | **โœ… 1-Command Weight Fusion (`bonsai merge`)** |
210
+ | **OpenAI API Server** | โŒ None | โŒ None | Third-party proxy | **โœ… Native `/v1/chat/completions` Endpoint** |
211
+ | **Automated Exam Suite** | โŒ None | โŒ None | โŒ None | **โœ… Unseen Blind Holdout Proof Machine** |
212
+ | **Visual Proof Reports** | โŒ W&B logs | โŒ W&B logs | โŒ None | **โœ… Standalone SVG & HTML Scorecards** |
213
+ | **1-Click Web UI & Desktop**| โŒ None | โŒ None | โŒ CLI only | **โœ… Dark-Mode UI + Windows .bat & Unix .sh** |
214
+
215
+ ---
216
+
217
+ ## ๐Ÿงช Automated Testing & Invariants
218
+
219
+ Bonsai is continuously verified with automated unit and integration tests:
220
+
221
+ ```bash
222
+ pytest tests/ -v
223
+ ```
224
+
225
+ ```
226
+ collected 28 items
227
+ tests/test_cli.py::test_cli_help PASSED [ 3%]
228
+ tests/test_cli.py::test_cli_version PASSED [ 7%]
229
+ tests/test_cli.py::test_cli_probe PASSED [ 10%]
230
+ tests/test_cli.py::test_cli_merge_help PASSED [ 14%]
231
+ tests/test_cli.py::test_cli_ui_help PASSED [ 17%]
232
+ tests/test_cli.py::test_cli_prune_mocked PASSED [ 21%]
233
+ tests/test_evaluator_multimetric.py::test_evaluate_json_payload PASSED [ 25%]
234
+ tests/test_evaluator_multimetric.py::test_evaluate_freeform_text PASSED [ 28%]
235
+ tests/test_holdout_leakage.py::test_zero_data_leakage PASSED [ 32%]
236
+ tests/test_holdout_leakage.py::test_deterministic_seed_reproducibility PASSED [ 35%]
237
+ tests/test_ingest.py::test_ingest_prompt_goal PASSED [ 39%]
238
+ tests/test_ingest.py::test_ingest_files PASSED [ 42%]
239
+ tests/test_ingest.py::test_ingest_empty_directory PASSED [ 46%]
240
+ tests/test_manifest.py::test_manifest_roundtrip PASSED [ 50%]
241
+ tests/test_merger.py::test_merger_initialization PASSED [ 53%]
242
+ tests/test_merger.py::test_merger_execution PASSED [ 57%]
243
+ tests/test_multi_archetype_teacher.py::test_teacher_goal_invoice_synthesis PASSED [ 60%]
244
+ tests/test_multi_archetype_teacher.py::test_teacher_goal_support_triage_synthesis PASSED [ 64%]
245
+ tests/test_multi_archetype_teacher.py::test_teacher_goal_sql_synthesis PASSED [ 67%]
246
+ tests/test_multi_archetype_teacher.py::test_teacher_csv_tabular_extraction PASSED [ 71%]
247
+ tests/test_multi_archetype_teacher.py::test_teacher_code_function_extraction PASSED [ 75%]
248
+ tests/test_multi_archetype_teacher.py::test_teacher_preformatted_alpaca_and_messages PASSED [ 78%]
249
+ tests/test_multi_archetype_teacher.py::test_teacher_domain_adaptive_augmentation PASSED [ 82%]
250
+ tests/test_probe.py::test_probe_hardware_structure PASSED [ 85%]
251
+ tests/test_probe.py::test_probe_jargon_translations PASSED [ 89%]
252
+ tests/test_web_server.py::test_server_init PASSED [ 92%]
253
+ tests/test_web_server.py::test_server_generate_mocked PASSED [ 96%]
254
+ tests/test_web_server.py::test_handler_endpoints PASSED [100%]
255
+
256
+ ============================= 28 passed in 41.73s =============================
257
+ ```
258
+
259
+ ---
260
+
261
+ ## ๐Ÿ“œ License & Community
262
+
263
+ Released under the **[MIT License](LICENSE)**. Cultivated with care for the global open-source AI community.
264
+
265
+ Contributions, issues, and PRs are warmly welcomed. See **[CONTRIBUTING.md](CONTRIBUTING.md)** for details.
@@ -0,0 +1,236 @@
1
+ <p align="center">
2
+ <img src="assets/bonsai-hero-banner.png" alt="Bonsai Hero Banner" width="100%" />
3
+ </p>
4
+
5
+ <h1 align="center">๐ŸŒฑ BONSAI (Major v1.0.0)</h1>
6
+
7
+ <p align="center">
8
+ <strong>The art of cultivating massive foundation models into pocket-sized intelligences on consumer silicon.</strong>
9
+ </p>
10
+
11
+ <p align="center">
12
+ <a href="https://pypi.org/project/bonsai-slm/"><img src="https://img.shields.io/pypi/v/bonsai-slm.svg?color=emerald" alt="PyPI version" /></a>
13
+ <a href="https://github.com/shenald-dev/bonsai/actions"><img src="https://img.shields.io/badge/CI-Passing-2ea44f.svg" alt="CI Status" /></a>
14
+ <a href="https://github.com/shenald-dev/bonsai/releases/tag/v0.1.0"><img src="https://img.shields.io/github/v/release/shenald-dev/bonsai?color=emerald" alt="Release" /></a>
15
+ <a href="https://github.com/shenald-dev/bonsai/stargazers"><img src="https://img.shields.io/github/stars/shenald-dev/bonsai?style=social" alt="GitHub Stars" /></a>
16
+ <a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-0969da.svg" alt="License: MIT" /></a>
17
+ <a href="https://www.python.org/"><img src="https://img.shields.io/badge/Python-3.10%2B-blue.svg" alt="Python 3.10+" /></a>
18
+ <a href="https://pytorch.org/"><img src="https://img.shields.io/badge/PyTorch-2.6%2B%20CUDA-ee4c2c.svg" alt="PyTorch CUDA" /></a>
19
+ <a href="https://github.com/shenald-dev/bonsai"><img src="https://img.shields.io/badge/Zero--OOM-Guaranteed-brightgreen.svg" alt="Zero-OOM Protected" /></a>
20
+ </p>
21
+
22
+ ---
23
+
24
+ ## โšก The 10-Second Quickstart
25
+
26
+ ```bash
27
+ pip install bonsai-slm
28
+
29
+ # Or run instantly without installation:
30
+ uvx bonsai-slm prune ./my_raw_data
31
+ ```
32
+
33
+ No JSONL formatting. No CUDA out-of-memory crashes. No hyperparameter tuning. Drop raw logs, CSVs, code files, or a plain-English task goalโ€”Bonsai takes care of the rest.
34
+
35
+ ---
36
+
37
+ ## ๐ŸŒŠ Why Bonsai?
38
+
39
+ Running local models is solved (`ollama`, `LM Studio`). But **99% of developers and everyday power-users have zero idea how to distill or train their own micro-models (0.5B to 1.5B) on their own home PC.**
40
+
41
+ They get blocked by:
42
+ - โŒ Manually formatting instruction-tuning JSONL pairs
43
+ - โŒ Sudden `CUDA out of memory` crashes
44
+ - โŒ Confusing ML hyperparameter jargon (`lora_r`, `grad_accum`, `cosine_warmup`)
45
+ - โŒ Catastrophic forgetting and hallucination loops
46
+ - โŒ Zero quantitative proof that their fine-tuned model actually improved
47
+
48
+ **Bonsai eliminates the friction entirely.**
49
+
50
+ It acts as an autonomous master artisan: inspecting your exact GPU VRAM / Unified Memory, cultivating training pairs across diverse task archetypes with guaranteed zero data leakage, executing an ultra-fast LoRA distillation pass with prompt token loss masking, proving accuracy gains on an unseen blind holdout exam, fusing weights into standalone merged models, and hosting an instant local Web UI and OpenAI-compatible API.
51
+
52
+ ---
53
+
54
+ ## ๐Ÿ›๏ธ How It Works: The 5-Step Cultivation Loop
55
+
56
+ ```mermaid
57
+ flowchart LR
58
+ A["๐Ÿ’ป 1. PROBE<br/>Hardware Auto-Downshift"] --> B["๐Ÿ“ฅ 2. INGEST<br/>Raw Docs, Logs, or Goals"]
59
+ B --> C["๐Ÿงฌ 3. CULTIVATE<br/>Multi-Archetype Zero-Leakage Split"]
60
+ C --> D["โšก 4. DISTILL & FUSE<br/>Masked-Loss LoRA + Model Merge"]
61
+ D --> E["๐Ÿ”ฌ 5. PROOF & SERVE<br/>SVG Scorecard + OpenAI API"]
62
+ ```
63
+
64
+ 1. **Hardware Auto-Probe:** Probes GPU VRAM, Apple Unified Memory, or CPU RAM. If VRAM is tight (<4 GB), it automatically downshifts base models (e.g., `Qwen2.5-Coder-0.5B`) to guarantee **Zero-OOM**.
65
+ 2. **Universal Ingestion:** Drop raw `.txt`, `.csv`, `.md`, server logs, Python/JS scripts, or specify a plain-English task.
66
+ 3. **Multi-Archetype Teacher Engine:** Synthesizes high-fidelity instruction-response pairs across 4 distinct domains (JSON telemetry, Q&A extraction, code generation, support triage) and enforces strict scenario deduplication *before* augmentation, splitting into an 85% training set and a 15% unseen blind holdout exam.
67
+ 4. **Pocket LoRA Distillation & Weight Fusion:** Freezes 99.78% of the model and cultivates only the top 0.22% of synaptic adapter weights with **prompt loss masking** (`labels[:prompt_len] = -100`). Fuse weights directly into standalone weights via `bonsai merge` for zero-overhead inference or direct llama.cpp/GGUF conversion.
68
+ 5. **The Proof Machine & API Serving:** Evaluates the untrained base model side-by-side against your newly cultivated model on the unseen exam set, emitting a viral SVG scorecard, interactive HTML report, and instant OpenAI-compatible API endpoint.
69
+
70
+ ---
71
+
72
+ ## ๐Ÿ’ป CLI Commands
73
+
74
+ ### 1. Probe Hardware
75
+ Inspects silicon headroom and translates ML jargon into human concepts:
76
+ ```bash
77
+ bonsai probe
78
+ ```
79
+
80
+ ```
81
+ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
82
+ โ”‚ ๐ŸŒฑ BONSAI HARDWARE TELEMETRY โ”‚
83
+ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
84
+ Silicon: NVIDIA GeForce RTX 2050 (3300 MB Free VRAM)
85
+ Recommended Micro-Model: Qwen2.5-Coder-0.5B-Instruct (Zero-OOM Protected)
86
+
87
+ Translated Cultivation Recipe:
88
+ โ€ข Learning Agility (Learning Rate): 2e-4
89
+ โ€ข Synaptic Plasticity (LoRA Rank): r=8, alpha=16
90
+ โ€ข Study Passes (Epochs): 3 passes
91
+ โ€ข Attention Batching (Accumulation): 4 steps
92
+ ```
93
+
94
+ ### 2. Prune & Distill
95
+ Train a micro-AI on your local files or task description:
96
+ ```bash
97
+ # From a folder of raw logs/documents:
98
+ bonsai prune ./examples/messy_ops_to_json
99
+
100
+ # Or from a task prompt:
101
+ bonsai prune "Build a tiny AI that extracts strict JSON invoices from messy emails"
102
+ ```
103
+
104
+ ### 3. Exam & Proof Machine
105
+ Evaluate the newly trained model on the unseen blind test set:
106
+ ```bash
107
+ bonsai exam ./bonsai_pruned
108
+ ```
109
+
110
+ Generates:
111
+ * ๐Ÿ“Š **Viral SVG Scorecard:** `bonsai-report.svg`
112
+ * ๐ŸŒ **Interactive HTML Report:** `bonsai-report.html`
113
+ * ๐Ÿ“‹ **Canonical State Manifest:** `bonsai_manifest.json`
114
+
115
+ ### 4. Merge Synaptic Weights (New in v1.0)
116
+ Fuse LoRA adapter weights directly into the base model to create a standalone, zero-dependency model:
117
+ ```bash
118
+ bonsai merge ./bonsai_pruned -o ./bonsai_pruned/merged
119
+ ```
120
+
121
+ The merged output is a standard HuggingFace / safetensors directory that can be quantized directly to GGUF via `llama.cpp` or loaded with zero adapter overhead.
122
+
123
+ ### 5. Interactive Web Chat & OpenAI-Compatible API
124
+ Launch the local dark-mode UI and standards-compliant OpenAI API endpoint (`/v1/chat/completions`):
125
+ ```bash
126
+ bonsai ui --port 7860
127
+ ```
128
+
129
+ #### Querying via cURL:
130
+ ```bash
131
+ curl http://localhost:7860/v1/chat/completions \
132
+ -H "Content-Type: application/json" \
133
+ -d '{
134
+ "model": "bonsai-micro-ai",
135
+ "messages": [
136
+ {"role": "user", "content": "CRITICAL: db connection pool exhausted on worker-01"}
137
+ ]
138
+ }'
139
+ ```
140
+
141
+ #### Querying via Python `openai` SDK:
142
+ ```python
143
+ from openai import OpenAI
144
+
145
+ client = OpenAI(base_url="http://localhost:7860/v1", api_key="not-needed")
146
+ response = client.chat.completions.create(
147
+ model="bonsai-micro-ai",
148
+ messages=[{"role": "user", "content": "Extract invoice details from receipt"}]
149
+ )
150
+ print(response.choices[0].message.content)
151
+ ```
152
+
153
+ ---
154
+
155
+ ## ๐Ÿ“Š Live Verification Scorecard (Clean-Room NVIDIA RTX 2050)
156
+
157
+ <p align="center">
158
+ <img src="assets/bonsai-report.svg" alt="Bonsai Scorecard" width="100%" />
159
+ </p>
160
+
161
+ | Evaluation Metric | Raw Base Model | Cultivated Bonsai Model | Net Delta |
162
+ |:---|:---:|:---:|:---:|
163
+ | **Strict Schema Validity** | 100.0% | **100.0%** | Maintained |
164
+ | **Output Cleanliness** | Markdown code fences, float fields | **Strict Bare JSON, snake_case** | **Reflexive Precision** |
165
+ | **Training Duration** | โ€” | **34.05 seconds** | **Ultra-Fast** |
166
+ | **Loss Convergence** | 1.5905 | **0.7713** | **-51.5% Convergence** |
167
+ | **Peak VRAM Allocated** | โ€” | **2,045 MB** | **Zero-OOM Verified** |
168
+ | **Inference Latency** | 3,533 ms | **4,352 ms** | Consumer Silicon Ready |
169
+
170
+ ---
171
+
172
+ ## ๐ŸฅŠ Comparison Matrix
173
+
174
+ | Feature | Unsloth | Axolotl | Ollama | **๐ŸŒฑ Bonsai v1.0** |
175
+ |:---|:---:|:---:|:---:|:---:|
176
+ | **Target User** | ML Engineers | Senior ML Researchers | Everyone | **Everyone** |
177
+ | **Dataset Requirement** | Manual `.jsonl` | Complex YAML + `.jsonl` | None (Inference only) | **Zero-Knowledge (Raw files/prompts)** |
178
+ | **Hardware Safety** | Manual VRAM tuning | Manual tuning | Fixed GGUF | **Automated Zero-OOM Downshift** |
179
+ | **Synthetic Teacher** | โŒ Manual | โŒ Manual | โŒ None | **โœ… Universal Multi-Archetype Teacher** |
180
+ | **Adapter Fusion (`merge`)**| Manual Python | Manual Python | โŒ None | **โœ… 1-Command Weight Fusion (`bonsai merge`)** |
181
+ | **OpenAI API Server** | โŒ None | โŒ None | Third-party proxy | **โœ… Native `/v1/chat/completions` Endpoint** |
182
+ | **Automated Exam Suite** | โŒ None | โŒ None | โŒ None | **โœ… Unseen Blind Holdout Proof Machine** |
183
+ | **Visual Proof Reports** | โŒ W&B logs | โŒ W&B logs | โŒ None | **โœ… Standalone SVG & HTML Scorecards** |
184
+ | **1-Click Web UI & Desktop**| โŒ None | โŒ None | โŒ CLI only | **โœ… Dark-Mode UI + Windows .bat & Unix .sh** |
185
+
186
+ ---
187
+
188
+ ## ๐Ÿงช Automated Testing & Invariants
189
+
190
+ Bonsai is continuously verified with automated unit and integration tests:
191
+
192
+ ```bash
193
+ pytest tests/ -v
194
+ ```
195
+
196
+ ```
197
+ collected 28 items
198
+ tests/test_cli.py::test_cli_help PASSED [ 3%]
199
+ tests/test_cli.py::test_cli_version PASSED [ 7%]
200
+ tests/test_cli.py::test_cli_probe PASSED [ 10%]
201
+ tests/test_cli.py::test_cli_merge_help PASSED [ 14%]
202
+ tests/test_cli.py::test_cli_ui_help PASSED [ 17%]
203
+ tests/test_cli.py::test_cli_prune_mocked PASSED [ 21%]
204
+ tests/test_evaluator_multimetric.py::test_evaluate_json_payload PASSED [ 25%]
205
+ tests/test_evaluator_multimetric.py::test_evaluate_freeform_text PASSED [ 28%]
206
+ tests/test_holdout_leakage.py::test_zero_data_leakage PASSED [ 32%]
207
+ tests/test_holdout_leakage.py::test_deterministic_seed_reproducibility PASSED [ 35%]
208
+ tests/test_ingest.py::test_ingest_prompt_goal PASSED [ 39%]
209
+ tests/test_ingest.py::test_ingest_files PASSED [ 42%]
210
+ tests/test_ingest.py::test_ingest_empty_directory PASSED [ 46%]
211
+ tests/test_manifest.py::test_manifest_roundtrip PASSED [ 50%]
212
+ tests/test_merger.py::test_merger_initialization PASSED [ 53%]
213
+ tests/test_merger.py::test_merger_execution PASSED [ 57%]
214
+ tests/test_multi_archetype_teacher.py::test_teacher_goal_invoice_synthesis PASSED [ 60%]
215
+ tests/test_multi_archetype_teacher.py::test_teacher_goal_support_triage_synthesis PASSED [ 64%]
216
+ tests/test_multi_archetype_teacher.py::test_teacher_goal_sql_synthesis PASSED [ 67%]
217
+ tests/test_multi_archetype_teacher.py::test_teacher_csv_tabular_extraction PASSED [ 71%]
218
+ tests/test_multi_archetype_teacher.py::test_teacher_code_function_extraction PASSED [ 75%]
219
+ tests/test_multi_archetype_teacher.py::test_teacher_preformatted_alpaca_and_messages PASSED [ 78%]
220
+ tests/test_multi_archetype_teacher.py::test_teacher_domain_adaptive_augmentation PASSED [ 82%]
221
+ tests/test_probe.py::test_probe_hardware_structure PASSED [ 85%]
222
+ tests/test_probe.py::test_probe_jargon_translations PASSED [ 89%]
223
+ tests/test_web_server.py::test_server_init PASSED [ 92%]
224
+ tests/test_web_server.py::test_server_generate_mocked PASSED [ 96%]
225
+ tests/test_web_server.py::test_handler_endpoints PASSED [100%]
226
+
227
+ ============================= 28 passed in 41.73s =============================
228
+ ```
229
+
230
+ ---
231
+
232
+ ## ๐Ÿ“œ License & Community
233
+
234
+ Released under the **[MIT License](LICENSE)**. Cultivated with care for the global open-source AI community.
235
+
236
+ Contributions, issues, and PRs are warmly welcomed. See **[CONTRIBUTING.md](CONTRIBUTING.md)** for details.
@@ -0,0 +1,6 @@
1
+ """
2
+ Bonsai - The art of cultivating massive foundation models into pocket-sized intelligences on consumer silicon.
3
+ """
4
+
5
+ __version__ = "0.1.0"
6
+ __app_name__ = "bonsai"