PHP code example of codechap / yii3-context-trimmer
1. Go to this page and download the library: Download codechap/yii3-context-trimmer 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/ */
codechap / yii3-context-trimmer example snippets
use Codechap\Yii3ContextTrimmer\ContextTrimmerInterface;
final class MyService
{
public function __construct(
private readonly ContextTrimmerInterface $trimmer,
) {}
public function process(string $text): array
{
return $this->trimmer
->withMaxTokens(4096)
->withRemoveDuplicateLines(true)
->trim($text);
}
}
use Codechap\Yii3ContextTrimmer\ContextTrimmer;
$trimmer = new ContextTrimmer();
$segments = $trimmer
->withMaxTokens(4096)
->withRemoveDuplicateLines(true)
->trim($longText);
// Example: tiktoken-based tokenizer for OpenAI models
$trimmer = new ContextTrimmer(
tokenizer: function (string $text): array {
return your_tiktoken_encode($text);
},
);
return [
'codechap/yii3-context-trimmer' => [
'maxTokens' => 4096, // Max tokens per segment (default: 8192)
'removeDuplicateLines' => true, // Remove duplicate lines (default: false)
'removeShortWords' => false, // Remove short words (default: false)
'minWordLength' => 2, // Min word length to keep (default: 2)
'removeExtraneous' => false, // Remove brackets/parens/etc (default: false)
'compressWhitespace' => true, // Compress whitespace (default: true)
],
];
bash
composer analyse
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.