-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory.php
More file actions
164 lines (148 loc) · 6.93 KB
/
Factory.php
File metadata and controls
164 lines (148 loc) · 6.93 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
<?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\Generic\CompletionsModel;
use Symfony\AI\Platform\Bridge\Generic\EmbeddingsModel;
use Symfony\AI\Platform\Bridge\Generic\Factory as GenericFactory;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\ModelRouter\CatalogBasedModelRouter;
use Symfony\AI\Platform\ModelRouterInterface;
use Symfony\AI\Platform\Platform;
use Symfony\AI\Platform\ProviderInterface;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Creates a Platform instance for any models.dev provider.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
final class Factory
{
/**
* Well-known API base URLs for providers whose models.dev entry
* omits the "api" field because the Vercel AI SDK hardcodes the
* URL inside their dedicated npm packages.
*
* @var array<string, string> npm package => base URL
*/
private const NPM_PACKAGE_BASE_URLS = [
'@ai-sdk/cerebras' => 'https://api.cerebras.ai',
'@ai-sdk/cohere' => 'https://api.cohere.com/compatibility',
'@ai-sdk/deepinfra' => 'https://api.deepinfra.com/v1/openai',
'@ai-sdk/groq' => 'https://api.groq.com/openai',
'@ai-sdk/mistral' => 'https://api.mistral.ai',
'@ai-sdk/openai' => 'https://api.openai.com',
'@ai-sdk/perplexity' => 'https://api.perplexity.ai',
'@ai-sdk/togetherai' => 'https://api.together.xyz',
'@ai-sdk/xai' => 'https://api.x.ai',
];
public static function createProvider(
string $provider,
#[\SensitiveParameter] ?string $apiKey = null,
?string $baseUrl = null,
?string $dataPath = null,
?Contract $contract = null,
?HttpClientInterface $httpClient = null,
?EventDispatcherInterface $eventDispatcher = null,
): ProviderInterface {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
$data = DataLoader::load($dataPath);
if (!isset($data[$provider])) {
throw new InvalidArgumentException(\sprintf('Provider "%s" not found in models.dev data.', $provider));
}
$providerData = $data[$provider];
$npmPackage = $providerData['npm'] ?? null;
// Check if this provider requires a specialized bridge (e.g., Anthropic, Google).
// When a custom baseUrl is given, skip this check: the user is pointing at an
// OpenAI-compatible proxy, so the generic bridge is always the right choice.
if (null === $baseUrl && null !== $npmPackage && BridgeResolver::requiresSpecializedBridge($npmPackage)) {
$package = BridgeResolver::getComposerPackage($npmPackage);
$factoryClass = BridgeResolver::getBridgeFactory($npmPackage);
if (!BridgeResolver::isBridgeAvailable($npmPackage)) {
throw new InvalidArgumentException(\sprintf('Provider "%s" requires a specialized bridge (%s); install it with composer require "%s".', $provider, $npmPackage, $package));
}
if (!BridgeResolver::isRoutable($npmPackage)) {
throw new InvalidArgumentException(\sprintf('Provider "%s" requires "%s" which has a different factory signature; use "%s::createProvider()" directly.', $provider, $package, $factoryClass));
}
$modelCatalog = new ModelCatalog(
$provider,
$dataPath,
completionsModelClass: BridgeResolver::getCompletionsModelClass($npmPackage),
embeddingsModelClass: BridgeResolver::getEmbeddingsModelClass($npmPackage),
);
return $factoryClass::createProvider(
apiKey: $apiKey,
modelCatalog: $modelCatalog,
contract: $contract,
httpClient: $httpClient,
eventDispatcher: $eventDispatcher,
name: $provider,
);
}
// Use the generic OpenAI-compatible bridge
if (null === $baseUrl) {
$baseUrl = (new ProviderRegistry($dataPath))->getApiBaseUrl($provider);
}
if (null === $baseUrl && null !== $npmPackage) {
$baseUrl = self::NPM_PACKAGE_BASE_URLS[$npmPackage] ?? null;
}
if (null === $baseUrl) {
throw new InvalidArgumentException(\sprintf('Provider "%s" does not have a known API base URL; please provide one via the $baseUrl argument.', $provider));
}
// Base URL should NOT include /v1 suffix (e.g., https://api.groq.com/openai not https://api.groq.com/openai/v1)
// The paths /v1/chat/completions and /v1/embeddings will be appended by the Generic bridge
$baseUrl = rtrim($baseUrl, '/');
// Automatically detect what the provider supports based on its model catalog
$supportsCompletions = false;
$supportsEmbeddings = false;
$modelCatalog = new ModelCatalog($provider, $dataPath);
foreach ($modelCatalog->getModels() as $modelData) {
if (CompletionsModel::class === $modelData['class'] || is_subclass_of($modelData['class'], CompletionsModel::class)) {
$supportsCompletions = true;
}
if (EmbeddingsModel::class === $modelData['class'] || is_subclass_of($modelData['class'], EmbeddingsModel::class)) {
$supportsEmbeddings = true;
}
// Early exit if we found both types
if ($supportsCompletions && $supportsEmbeddings) {
break;
}
}
return GenericFactory::createProvider(
baseUrl: $baseUrl,
apiKey: $apiKey,
modelCatalog: $modelCatalog,
contract: $contract,
httpClient: $httpClient,
eventDispatcher: $eventDispatcher,
supportsCompletions: $supportsCompletions,
supportsEmbeddings: $supportsEmbeddings,
name: $provider,
);
}
public static function createPlatform(
string $provider,
#[\SensitiveParameter] ?string $apiKey = null,
?string $baseUrl = null,
?string $dataPath = null,
?Contract $contract = null,
?HttpClientInterface $httpClient = null,
?EventDispatcherInterface $eventDispatcher = null,
?ModelRouterInterface $modelRouter = null,
): Platform {
return new Platform(
[self::createProvider($provider, $apiKey, $baseUrl, $dataPath, $contract, $httpClient, $eventDispatcher)],
$modelRouter ?? new CatalogBasedModelRouter(),
$eventDispatcher,
);
}
}