-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiteLLMModelInfoProvider.php
More file actions
219 lines (182 loc) · 6.75 KB
/
LiteLLMModelInfoProvider.php
File metadata and controls
219 lines (182 loc) · 6.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
declare(strict_types=1);
namespace Cortex\ModelInfo\Providers;
use JsonException;
use SensitiveParameter;
use Psr\SimpleCache\CacheInterface;
use Cortex\ModelInfo\Data\ModelInfo;
use Psr\Http\Client\ClientInterface;
use Cortex\ModelInfo\Enums\ModelType;
use Cortex\ModelInfo\Enums\ModelFeature;
use Cortex\ModelInfo\Enums\ModelProvider;
use Cortex\ModelInfo\Contracts\ModelInfoProvider;
use Cortex\ModelInfo\Exceptions\ModelInfoException;
use Cortex\ModelInfo\Providers\Concerns\ChecksSupport;
use Cortex\ModelInfo\Providers\Concerns\MakesRequests;
class LiteLLMModelInfoProvider implements ModelInfoProvider
{
use ChecksSupport;
use MakesRequests;
protected const string LITELLM_STATIC_URL = 'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json';
protected ClientInterface $httpClient;
public function __construct(
protected ?string $host = null,
#[SensitiveParameter]
protected ?string $apiKey = null,
?ClientInterface $httpClient = null,
protected ?CacheInterface $cache = null,
) {
$this->httpClient = $httpClient ?? self::discoverHttpClientOrFail();
$this->cache = $cache ?? self::discoverCache();
}
public function supportedModelProviders(): array
{
return array_filter(
ModelProvider::cases(),
fn(ModelProvider $provider): bool => $provider !== ModelProvider::Custom,
);
}
/**
* @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
*
* @return array<array-key, \Cortex\ModelInfo\Data\ModelInfo>
*/
public function getModels(ModelProvider $modelProvider): array
{
$this->checkSupportOrFail($modelProvider);
$body = $this->getStaticResponse();
$models = array_filter(
$body,
fn(array $model): bool => $model['litellm_provider'] === $modelProvider->value,
);
return array_values(array_map(
fn(array $modelInfo, string $model): ModelInfo => self::mapModelInfo($modelProvider, $model, $modelInfo),
$models,
array_keys($models),
));
}
/**
* @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
*/
public function getModelInfo(ModelProvider $modelProvider, string $model): ModelInfo
{
$this->checkSupportOrFail($modelProvider);
$body = $this->getStaticResponse();
$models = array_filter(
$body,
fn(array $model): bool => $model['litellm_provider'] === $modelProvider->value,
);
$modelInfo = array_values(
array_filter(
$models,
fn(string $key): bool => $key === $model,
ARRAY_FILTER_USE_KEY,
),
)[0] ?? null;
if ($modelInfo === null) {
throw new ModelInfoException('Model not found');
}
return self::mapModelInfo($modelProvider, $model, $modelInfo);
}
protected static function mapModelInfo(
ModelProvider $modelProvider,
string $model,
array $modelInfo,
): ModelInfo {
return new ModelInfo(
name: $model,
provider: $modelProvider,
type: self::mapModelType($modelInfo['mode'] ?? ''),
maxInputTokens: $modelInfo['max_input_tokens'] ?? null,
maxOutputTokens: $modelInfo['max_output_tokens'] ?? null,
inputCostPerToken: $modelInfo['input_cost_per_token'] ?? 0.0,
outputCostPerToken: $modelInfo['output_cost_per_token'] ?? 0.0,
features: self::getFeatures($modelInfo),
isDeprecated: isset($modelInfo['deprecation_date']) && $modelInfo['deprecation_date'] !== null,
);
}
/**
* @param array<string, mixed> $info
*
* @return array<int, \Cortex\Enums\ModelFeature>
*/
protected static function getFeatures(array $info): array
{
$features = [];
if ($info['supports_response_schema'] ?? false) {
$features[] = ModelFeature::StructuredOutput;
$features[] = ModelFeature::JsonOutput;
}
if ($info['supports_function_calling'] ?? false) {
$features[] = ModelFeature::ToolCalling;
}
if ($info['supports_vision'] ?? false) {
$features[] = ModelFeature::Vision;
}
if ($info['supports_tool_choice'] ?? false) {
$features[] = ModelFeature::ToolChoice;
}
if ($info['supports_reasoning'] ?? false) {
$features[] = ModelFeature::Reasoning;
}
if ($info['supports_web_search'] ?? false) {
$features[] = ModelFeature::WebSearch;
}
if ($info['supports_prompt_caching'] ?? false) {
$features[] = ModelFeature::PromptCaching;
}
if ($info['supports_audio_input'] ?? false) {
$features[] = ModelFeature::AudioInput;
}
if ($info['supports_audio_output'] ?? false) {
$features[] = ModelFeature::AudioOutput;
}
return $features;
}
protected static function mapModelType(string $type): ModelType
{
return match ($type) {
'chat' => ModelType::Chat,
'completion' => ModelType::Completion,
'embedding' => ModelType::Embedding,
'image_generation' => ModelType::ImageGeneration,
'audio_speech' => ModelType::TextToSpeech,
'audio_transcription' => ModelType::SpeechToText,
'moderation' => ModelType::Moderation,
default => ModelType::Unknown,
};
}
/**
* @throws \Cortex\Exceptions\ModelInfoException
*
* @return array<string, mixed>
*/
protected function getStaticResponse(): array
{
$request = self::discoverHttpRequestFactory()->createRequest('GET', self::LITELLM_STATIC_URL);
$response = $this->httpClient->sendRequest($request);
if ($response->getStatusCode() !== 200) {
throw new ModelInfoException('Failed to get model info');
}
try {
return json_decode($response->getBody()->getContents(), true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $jsonException) {
throw new ModelInfoException('Failed to decode model info', previous: $jsonException);
}
}
/**
* @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
*
* @return array<string, mixed>
*/
protected function getApiModelsResponse(): array
{
$request = self::discoverHttpRequestFactory()
->createRequest('GET', $this->host . '/v1/models');
return $this->getJsonResponse($request);
}
protected function shouldUseStaticResponse(): bool
{
return $this->host === null || $this->apiKey === null;
}
}