PHP code example of frolaxhq / laravel-llm-tokenkit
1. Go to this page and download the library: Download frolaxhq/laravel-llm-tokenkit library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
frolaxhq / laravel-llm-tokenkit example snippets
use Frolax\LlmTokenKit\Facades\LlmTokenKit;
use Frolax\LlmTokenKit\Data\ModelRef;
$model = new ModelRef(provider: 'openai', model: 'gpt-4o');
// Or use a simple array:
$model = ['provider' => 'openai', 'model' => 'gpt-4o'];
use Frolax\LlmTokenKit\Data\Pricing;
$cost = LlmTokenKit::cost(
usage: new TokenUsage(promptTokens: 1000, completionTokens: 200),
pricing: new Pricing(inputPer1m: 15.00, outputPer1m: 60.00, reasoningPer1m: 60.00),
);
use Frolax\LlmTokenKit\Data\ContextBuildRequest;
use Frolax\LlmTokenKit\Enums\ContextStrategy;
$result = LlmTokenKit::buildContext(new ContextBuildRequest(
system: 'You are a helpful assistant.',
memorySummary: 'User prefers dark mode.',
historyMessages: $chatHistory,
newUserMessage: 'How do I use Eloquent?',
modelRef: $model,
tokenBudget: 4000,
reservedOutputTokens: 1024,
strategy: ContextStrategy::TruncateByTokens,
));
$result->messages; // Ready-to-send message array
$result->tokenEstimate; // TokenEstimate DTO
$result->wasTruncated; // true if messages were dropped
$result->droppedMessageCount; // Number of dropped messages
$result->warnings; // Any warnings