-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelProvider.php
More file actions
94 lines (78 loc) · 2.65 KB
/
ModelProvider.php
File metadata and controls
94 lines (78 loc) · 2.65 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
<?php
declare(strict_types=1);
namespace Cortex\ModelInfo\Enums;
use Cortex\ModelInfo\Data\ModelInfo;
use Cortex\ModelInfo\ModelInfoFactory;
use Psr\Container\ContainerExceptionInterface;
use Cortex\ModelInfo\Providers\Concerns\DiscoversPsrImplementations;
enum ModelProvider: string
{
use DiscoversPsrImplementations;
case OpenAI = 'openai';
case Anthropic = 'anthropic';
case Groq = 'groq';
case Gemini = 'gemini';
case XAI = 'xai';
case Mistral = 'mistral';
case Ollama = 'ollama';
case LMStudio = 'lmstudio';
case Together = 'together';
case OpenRouter = 'openrouter';
case Bedrock = 'bedrock';
case DeepSeek = 'deepseek';
case Custom = 'custom';
/**
* @param array<array-key, \Cortex\ModelInfo\Contracts\ModelInfoProvider>|null $modelInfoProviders
*
* @return array<array-key, \Cortex\ModelInfo\Data\ModelInfo>
*/
public function models(?array $modelInfoProviders = null): array
{
return self::modelInfoFactory($modelInfoProviders)->getModels($this);
}
/**
* Get the info for a specific model.
*
* @param array<array-key, \Cortex\ModelInfo\Contracts\ModelInfoProvider>|null $modelInfoProviders
*/
public function info(string $model, ?array $modelInfoProviders = null): ?ModelInfo
{
return self::modelInfoFactory($modelInfoProviders)->getModelInfo($this, $model);
}
/**
* Get the input cost for tokens for a specific model.
*/
public function inputCostForTokens(string $model, int $tokens): ?float
{
$inputCostPerToken = $this->info($model)?->inputCostPerToken;
if ($inputCostPerToken === null) {
return null;
}
return $inputCostPerToken * $tokens;
}
/**
* Get the output cost for tokens for a specific model.
*/
public function outputCostForTokens(string $model, int $tokens): ?float
{
$outputCostPerToken = $this->info($model)?->outputCostPerToken;
if ($outputCostPerToken === null) {
return null;
}
return $outputCostPerToken * $tokens;
}
/**
* @param array<array-key, \Cortex\ModelInfo\Contracts\ModelInfoProvider>|null $modelInfoProviders
*/
public static function modelInfoFactory(?array $modelInfoProviders = null): ModelInfoFactory
{
$container = self::discoverContainer();
try {
/** @var \Cortex\ModelInfo\ModelInfoFactory $factory */
$factory = $container?->get(ModelInfoFactory::class);
} catch (ContainerExceptionInterface) {
//
}
return $factory ?? new ModelInfoFactory($modelInfoProviders);
}
}