https://github.com/sbsaga/toon
TOON — Laravel AI package for compact, human-readable, token-efficient data format with JSON ⇄ TOON conversion for ChatGPT, OpenAI, and other LLM prompts.
https://github.com/sbsaga/toon
ai ai-library api-optimization chatgpt compact-format data-compression developer-tool human-readable json laravel laravel-package large-language-models llm openai php php-library prompt-engineering serialization token-optimization toon
Last synced: 12 days ago
JSON representation
TOON — Laravel AI package for compact, human-readable, token-efficient data format with JSON ⇄ TOON conversion for ChatGPT, OpenAI, and other LLM prompts.
- Host: GitHub
- URL: https://github.com/sbsaga/toon
- Owner: sbsaga
- License: mit
- Created: 2025-11-02T06:18:26.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-12-20T18:06:42.000Z (3 months ago)
- Last Synced: 2025-12-22T16:06:43.558Z (3 months ago)
- Topics: ai, ai-library, api-optimization, chatgpt, compact-format, data-compression, developer-tool, human-readable, json, laravel, laravel-package, large-language-models, llm, openai, php, php-library, prompt-engineering, serialization, token-optimization, toon
- Language: PHP
- Homepage: https://packagist.org/packages/sbsaga/toon
- Size: 2.35 MB
- Stars: 39
- Watchers: 0
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🧠 TOON for Laravel
### Compact · Token-Efficient · Human-Readable Data Format for AI Prompts & LLM Contexts
---
## 📚 Table of Contents
1. [Overview](#-overview)
2. [Key Features](#-key-features)
3. [Benchmark & Analytics](#-real-world-benchmark)
4. [Installation](#-installation)
5. [Configuration](#-configuration)
6. [Usage](#-usage)
7. [Quick Benchmark Route](#-quick-benchmark-route)
8. [Analytics & Visualization](#-analytics--visualization)
9. [CLI Commands](#-artisan-commands)
10. [Integration Use Cases](#-integration-use-cases)
11. [Compatibility](#-compatibility)
12. [Compression Visualization](#-example-compression-visualization)
13. [SEO & AI Integration Keywords](#-seo--ai-integration-keywords)
14. [License](#-license)
---
## ✨ Overview
**TOON for Laravel** — also known as **Token-Optimized Object Notation** — is a **Laravel-native AI data optimization library** that transforms large JSON or PHP arrays into a **compact, readable, and token-efficient format**.
It’s crafted for developers working with **ChatGPT, Gemini, Claude, Mistral, or OpenAI APIs**, helping you:
✅ Save tokens and reduce API costs
✅ Simplify complex prompt structures
✅ Improve AI response quality and context understanding
✅ Maintain human readability and reversibility
> 💬 *“Compress your prompts, not your ideas.”*
---
## 🚀 Key Features
| Feature | Description |
|----------|-------------|
| 🔁 **Bidirectional Conversion** | Convert JSON ⇄ TOON with ease |
| 🧩 **Readable & Compact** | YAML-like structure, human-friendly format |
| 💰 **Token-Efficient** | Save up to 70% tokens on every AI prompt |
| ⚙️ **Seamless Laravel Integration** | Built with Facades, Service Providers, and Artisan commands |
| 🔒 **Preserves Key Order** | Ensures deterministic data output |
| 📊 **Built-in Analytics** | Measure token, byte, and compression performance |
| 🌍 **AI & LLM Ready** | Optimized for ChatGPT, Gemini, Claude, and Mistral models |
| 🆕 **Complex Nested Array Support** | Fully supports deeply nested associative and indexed arrays |
---
## 🆕 Complex & Nested Array Support (v1.1.8+)
TOON now supports **deeply nested and mixed data structures**, including:
- Multi-level associative arrays
- Indexed collections inside objects
- Complex real-world structures like users, profiles, orders, metadata, and logs
This enhancement ensures that **no structural information is lost**, while still benefiting from TOON’s compact, token-efficient format.
### Example: Nested Data Conversion
```php
$data = [
'user' => [
'id' => 101,
'active' => true,
'roles' => ['admin', 'editor'],
'profile' => [
'age' => 32,
'location' => [
'city' => 'Delhi',
'country' => 'India',
],
],
],
'orders' => [
[
'order_id' => 'ORD-1001',
'amount' => 1998,
'status' => 'paid',
],
],
];
echo Toon::convert($data);
```
This structure remains **human-readable, reversible, and compact**, even with deep nesting.
---
## 🧪 Real-World Benchmark
| Metric | JSON | TOON | Reduction |
|---------|------|------|-----------|
| Size (bytes) | 7,718 | 2,538 | **67.12% smaller** |
| Tokens (est.) | 1,930 | 640 | **~66.8% fewer tokens** |
### 📈 Visual Comparison
```
JSON (7.7 KB)
██████████████████████████████████████████████████████████████████████████
TOON (2.5 KB)
█████████████████
```
💡 **TOON** reduces token load by *60–75%*, improving **AI efficiency**, **cost**, and **response quality**.
---
## ⚙️ Installation
```bash
composer require sbsaga/toon
```
> Laravel automatically discovers the Service Provider and Facade.
---
## ⚙️ Configuration
```bash
php artisan vendor:publish --provider="Sbsaga\Toon\ToonServiceProvider" --tag=config
```
Creates a configuration file at `config/toon.php`:
```php
return [
'enabled' => true,
'escape_style' => 'backslash',
'min_rows_to_tabular' => 2,
'max_preview_items' => 200,
];
```
---
## 🧠 Usage
### ➤ Convert JSON → TOON
```php
use Sbsaga\Toon\Facades\Toon;
$data = [
'user' => 'Sagar',
'message' => 'Hello, how are you?',
'tasks' => [
['id' => 1, 'done' => false],
['id' => 2, 'done' => true],
],
];
$converted = Toon::convert($data);
echo $converted;
```
**Output:**
```
user: Sagar
message: Hello\, how are you?
tasks:
items[2]{done,id}:
false,1
true,2
```
---
### ➤ Convert TOON → JSON
```php
$toon = <<json([
'json_size_bytes' => strlen($jsonEncoded),
'toon_size_bytes' => strlen($toonEncoded),
'saving_percent' => round(100 - (strlen($toonEncoded) / strlen($jsonEncoded) * 100), 2),
'json_content' => $jsonEncoded,
'toon_content' => $toonEncoded,
]);
});
```
---
## 📊 Analytics & Visualization
| Metric | Description | Example |
|--------|--------------|---------|
| `json_size_bytes` | Original JSON size | 7,718 |
| `toon_size_bytes` | Optimized TOON size | 2,538 |
| `saving_percent` | Space saved | 67.12% |
| `tokens_estimate` | Estimated token count | 640 |
| `compression_ratio` | Ratio (TOON/JSON) | 0.33 |
🧠 **Visual Graph (Efficiency Comparison)**
```
| JSON: ██████████████████████████████████████████████████████████ 100%
| TOON: ████████████ 33%
```
---
## 🧰 Artisan Commands
```bash
php artisan toon:convert storage/test.json
php artisan toon:convert storage/test.toon --decode --pretty
php artisan toon:convert storage/test.json --output=storage/result.toon
```
---
## 🧩 Integration Use Cases
| Use Case | Benefit |
|-----------|----------|
| 🤖 **AI Prompt Engineering** | Compress structured data for ChatGPT / LLMs |
| 📉 **Token Optimization** | Reduce token usage and API costs |
| 🧠 **Data Preprocessing** | Streamline complex structured inputs |
| 🧾 **Logging & Debugging** | Store compact, readable structured logs |
| 🗄️ **Database Storage Optimization** | Reduce JSON storage size while preserving structure |
| 🔍 **Developer Tools** | Perfect for previews and compact dashboards |
---
## 🧰 Compatibility
| Laravel | PHP | Package Version |
|----------|-----|----------------|
| 9.x – 12.x | ≥ 8.1 | v1.1.0+ |
---
## 📉 Example Compression Visualization
```
JSON (7.7 KB)
██████████████████████████████████████████████████████████████████████████
TOON (2.5 KB)
█████████████████
```
🧠 **~67% size reduction** while retaining complete data accuracy.
---
## 🌐 SEO & AI Integration Keywords
**Keywords:**
`laravel ai`, `ai for laravel`, `chatgpt laravel`, `laravel chatgpt`, `gemini laravel`, `laravel gemini`, `laravel anthropic`, `laravel mistral`, `laravel openai`, `openai laravel`, `laravel llm`, `llm laravel`, `ai laravel package`, `prompt compression`, `token optimizer`, `laravel json formatter`, `compact notation`, `laravel data compressor`, `token saver`, `laravel ai integration`, `sbsaga toon`, `laravel-toon`, `toon php`, `laravel ai toolkit`, `openai cost optimizer`, `laravel ai efficiency`, `chatgpt laravel toolkit`, `ai-ready laravel package`.
---
## 💡 Contribution
Contributions are highly encouraged!
- Fork the repository
- Create a new feature branch
- Commit & push improvements
- Submit a Pull Request 🎉
---
## 📜 License
Licensed under the **MIT License** — free for both commercial and personal use.
---
🧠 “Compress your prompts, not your ideas.” — TOON helps you talk to AI efficiently.