-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLMStudioModelInfoProvider.php
More file actions
144 lines (122 loc) · 4.08 KB
/
LMStudioModelInfoProvider.php
File metadata and controls
144 lines (122 loc) · 4.08 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
<?php
declare(strict_types=1);
namespace Cortex\ModelInfo\Providers;
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\Providers\Concerns\ChecksSupport;
use Cortex\ModelInfo\Providers\Concerns\MakesRequests;
/**
* @phpstan-type LMStudioModelInfoResponse array{id: string, object: string, type: string, max_context_length: int, type: ?string}
*/
class LMStudioModelInfoProvider implements ModelInfoProvider
{
use ChecksSupport;
use MakesRequests;
public function __construct(
protected string $host = 'http://localhost:1234',
?ClientInterface $httpClient = null,
) {
$this->httpClient = $httpClient ?? self::discoverHttpClientOrFail();
}
public function supportedModelProviders(): array
{
return [
ModelProvider::LMStudio,
];
}
/**
* @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
*
* @return array<array-key, \Cortex\ModelInfo\Data\ModelInfo>
*/
public function getModels(ModelProvider $modelProvider): array
{
$this->checkSupportOrFail($modelProvider);
$body = $this->getModelsResponse();
return array_values(array_map(
fn(array $model): ModelInfo => self::mapModelInfo($model),
$body['data'],
));
}
public function getModelInfo(ModelProvider $modelProvider, string $model): ModelInfo
{
$this->checkSupportOrFail($modelProvider);
return self::mapModelInfo(
$this->getModelInfoResponse($model),
);
}
/**
* @param LMStudioModelInfoResponse $body
*/
protected static function mapModelInfo(array $body): ModelInfo
{
return new ModelInfo(
name: $body['id'],
provider: ModelProvider::LMStudio,
type: self::getModelType($body['type'] ?? ''),
maxInputTokens: self::getMaxInputTokens($body['max_context_length']),
maxOutputTokens: null,
inputCostPerToken: 0.0,
outputCostPerToken: 0.0,
features: self::getFeatures($body),
);
}
/**
* @param LMStudioModelInfoResponse $body
*
* @return array<array-key, \Cortex\ModelInfo\Enums\ModelFeature>
*/
protected static function getFeatures(array $body): array
{
$features = [];
if ($body['type'] === 'llm') {
$features[] = ModelFeature::JsonOutput;
$features[] = ModelFeature::StructuredOutput;
}
return $features;
}
protected static function getModelType(string $type): ModelType
{
return match ($type) {
'llm' => ModelType::Chat,
'embeddings' => ModelType::Embedding,
default => ModelType::Unknown,
};
}
protected static function getMaxInputTokens(int $maxContextLength): ?int
{
return (int) ($maxContextLength * 0.9765625);
}
/**
* @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
*
* @return LMStudioModelInfoResponse
*/
protected function getModelInfoResponse(string $model): array
{
$body = self::discoverHttpStreamFactoryOrFail()->createStream(json_encode([
'model' => $model,
], JSON_THROW_ON_ERROR));
$request = self::discoverHttpRequestFactoryOrFail()
->createRequest('POST', $this->host . '/api/show')
->withBody($body);
// @phpstan-ignore return.type
return $this->getJsonResponse($request);
}
/**
* @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
*
* @return array{data: array<array-key, LMStudioModelInfoResponse>}
*/
protected function getModelsResponse(): array
{
$request = self::discoverHttpRequestFactoryOrFail()
->createRequest('GET', $this->host . '/api/v0/models');
// @phpstan-ignore return.type
return $this->getJsonResponse($request);
}
}