BaseLLMAdapter::compressData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Squareetlabs\LaravelToon\Adapters;
6
7
use Squareetlabs\LaravelToon\Services\CostCalculator;
0 ignored issues
show
Bug introduced by
The type Squareetlabs\LaravelToon\Services\CostCalculator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Squareetlabs\LaravelToon\Services\TokenAnalyzer;
9
use Squareetlabs\LaravelToon\Services\ToonService;
10
11
abstract class BaseLLMAdapter
12
{
13
    protected ToonService $toon;
14
    protected TokenAnalyzer $tokenAnalyzer;
15
    protected CostCalculator $costCalculator;
16
17
    public function __construct()
18
    {
19
        $this->toon = app(ToonService::class);
20
        $this->tokenAnalyzer = app(TokenAnalyzer::class);
21
        $this->costCalculator = app(CostCalculator::class);
22
    }
23
24
    abstract public function getName(): string;
25
26
    abstract public function isEnabled(): bool;
27
28
    abstract public function sendMessage(
29
        string $message,
30
        ?string $model = null,
31
        ?array $options = null
32
    ): array;
33
34
    abstract public function chat(
35
        array $messages,
36
        ?string $model = null,
37
        ?array $options = null
38
    ): array;
39
40
    public function compressMessage(string $message, string $format = 'compact'): string
41
    {
42
        return $this->toon->convert($message, $format);
43
    }
44
45
    public function compressData(mixed $data, string $format = 'compact'): string
46
    {
47
        return $this->toon->convert($data, $format);
48
    }
49
50
    public function estimateMessageCost(string $message, ?string $model = null, string $role = 'input'): array
51
    {
52
        $model ??= $this->getDefaultModel();
53
54
        return $this->costCalculator->estimateCost($model, $message, $role);
55
    }
56
57
    public function analyzeMessage(string $message): array
58
    {
59
        return [
60
            'message' => $message,
61
            'original_tokens' => $this->tokenAnalyzer->estimateJson(['content' => $message])['tokens_estimate'],
62
            'compressed_tokens' => $this->tokenAnalyzer->estimate($message),
63
            'analysis' => $this->tokenAnalyzer->analyze($message),
64
        ];
65
    }
66
67
    abstract public function getDefaultModel(): string;
68
69
    abstract public function getAvailableModels(): array;
70
}
71
72