-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm-optimization.php
More file actions
59 lines (49 loc) · 1.75 KB
/
llm-optimization.php
File metadata and controls
59 lines (49 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* Example: LLM Token Optimization
*
* Demonstrates token savings when using TOON vs JSON for LLM payloads.
*/
// Sample dataset that might be sent to an LLM
$customers = array_map(function($i) {
return [
'id' => $i,
'name' => "Customer $i",
'email' => "customer$i@example.com",
'status' => 'active',
'balance' => round(rand(1000, 10000) / 100, 2),
'last_order' => '2024-01-15'
];
}, range(1, 50));
$data = ['customers' => $customers];
echo "=== LLM Token Optimization ===" . PHP_EOL . PHP_EOL;
// JSON encoding
$json = json_encode($data, JSON_PRETTY_PRINT);
$jsonSize = strlen($json);
$jsonLines = substr_count($json, "\n") + 1;
echo "JSON Format:" . PHP_EOL;
echo "- Size: $jsonSize bytes" . PHP_EOL;
echo "- Lines: $jsonLines" . PHP_EOL;
echo "- Estimated tokens: ~" . ceil($jsonSize / 4) . PHP_EOL;
// TOON encoding
$toon = toon_encode($data);
$toonSize = strlen($toon);
$toonLines = substr_count($toon, "\n") + 1;
echo PHP_EOL . "TOON Format:" . PHP_EOL;
echo "- Size: $toonSize bytes" . PHP_EOL;
echo "- Lines: $toonLines" . PHP_EOL;
echo "- Estimated tokens: ~" . ceil($toonSize / 4) . PHP_EOL;
// Savings
$savings = round((1 - $toonSize / $jsonSize) * 100, 1);
$tokenSavings = ceil($jsonSize / 4) - ceil($toonSize / 4);
echo PHP_EOL . "Savings:" . PHP_EOL;
echo "- Size reduction: {$savings}%" . PHP_EOL;
echo "- Token savings: ~{$tokenSavings} tokens" . PHP_EOL;
echo "- Cost impact: Reduces API costs by {$savings}%" . PHP_EOL;
echo PHP_EOL . "Sample TOON output (first 20 lines):" . PHP_EOL;
echo "---" . PHP_EOL;
$lines = explode("\n", $toon);
echo implode("\n", array_slice($lines, 0, 20)) . PHP_EOL;
if (count($lines) > 20) {
echo "... (" . (count($lines) - 20) . " more lines)" . PHP_EOL;
}