-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelCatalog.php
More file actions
102 lines (90 loc) · 4.29 KB
/
Copy pathModelCatalog.php
File metadata and controls
102 lines (90 loc) · 4.29 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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\AI\Platform\Bridge\ModelsDev;
use Symfony\AI\Platform\Bridge\Anthropic\Claude;
use Symfony\AI\Platform\Bridge\Gemini\Embeddings;
use Symfony\AI\Platform\Bridge\Gemini\Gemini;
use Symfony\AI\Platform\Bridge\Generic\CompletionsModel;
use Symfony\AI\Platform\Bridge\Generic\EmbeddingsModel;
use Symfony\AI\Platform\Bridge\VertexAi\Embeddings\Model as VertexAiEmbeddings;
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model as VertexAiGemini;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\ModelCatalog\AbstractModelCatalog;
/**
* Model catalog powered by models.dev data.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
final class ModelCatalog extends AbstractModelCatalog
{
/**
* Maps a provider's models.dev npm package to the model classes its specialized bridge
* requires. Providers without an entry use the generic model classes.
*
* @var array<string, array{completions?: class-string, embeddings?: class-string}>
*/
private const MODEL_CLASS_OVERRIDES = [
'@ai-sdk/anthropic' => [
'completions' => Claude::class,
],
'@ai-sdk/google' => [
'completions' => Gemini::class,
'embeddings' => Embeddings::class,
],
'@ai-sdk/google-vertex' => [
'completions' => VertexAiGemini::class,
'embeddings' => VertexAiEmbeddings::class,
],
];
/**
* @var array<string, string> npm package => human-readable provider name
*/
private const UNSUPPORTED_NPM_PACKAGES = [
'@ai-sdk/amazon-bedrock' => 'Amazon Bedrock',
];
/**
* @param string $providerId The models.dev provider ID (e.g. "openai", "groq", "deepseek")
* @param string|null $dataPath Path to the models.dev JSON file (defaults to the bundled file)
* @param array<string, array{class: class-string, capabilities: list<Capability>}> $additionalModels Additional models to merge into the catalog
* @param class-string|null $completionsModelClass Override the completions model class (defaults to the bridge-specific or generic class)
* @param class-string|null $embeddingsModelClass Override the embeddings model class (defaults to the bridge-specific or generic class)
*/
public function __construct(
string $providerId,
?string $dataPath = null,
array $additionalModels = [],
?string $completionsModelClass = null,
?string $embeddingsModelClass = null,
) {
$data = DataLoader::load($dataPath);
if (!isset($data[$providerId])) {
throw new InvalidArgumentException(\sprintf('Provider "%s" not found in models.dev data.', $providerId));
}
$npm = $data[$providerId]['npm'] ?? '';
if (isset(self::UNSUPPORTED_NPM_PACKAGES[$npm])) {
throw new InvalidArgumentException(\sprintf('Provider "%s" (%s) is not supported by the models.dev bridge because it cannot be driven by the generic client.', $providerId, self::UNSUPPORTED_NPM_PACKAGES[$npm]));
}
$override = self::MODEL_CLASS_OVERRIDES[$npm] ?? [];
$completionsModelClass ??= $override['completions'] ?? CompletionsModel::class;
$embeddingsModelClass ??= $override['embeddings'] ?? EmbeddingsModel::class;
$models = [];
foreach ($data[$providerId]['models'] as $modelData) {
if ('deprecated' === ($modelData['status'] ?? 'active')) {
continue;
}
$models[$modelData['id']] = [
'class' => CapabilityMapper::isEmbeddingModel($modelData) ? $embeddingsModelClass : $completionsModelClass,
'capabilities' => CapabilityMapper::map($modelData),
];
}
$this->models = array_merge($models, $additionalModels);
}
}