|
28 | 28 | */ |
29 | 29 | final class PlatformFactory |
30 | 30 | { |
| 31 | + /** |
| 32 | + * Well-known API base URLs for providers whose models.dev entry |
| 33 | + * omits the "api" field because the Vercel AI SDK hardcodes the |
| 34 | + * URL inside their dedicated npm packages. |
| 35 | + * |
| 36 | + * @var array<string, string> npm package => base URL |
| 37 | + */ |
| 38 | + private const NPM_PACKAGE_BASE_URLS = [ |
| 39 | + '@ai-sdk/cerebras' => 'https://api.cerebras.ai', |
| 40 | + '@ai-sdk/cohere' => 'https://api.cohere.com/compatibility', |
| 41 | + '@ai-sdk/deepinfra' => 'https://api.deepinfra.com/v1/openai', |
| 42 | + '@ai-sdk/groq' => 'https://api.groq.com/openai', |
| 43 | + '@ai-sdk/mistral' => 'https://api.mistral.ai', |
| 44 | + '@ai-sdk/openai' => 'https://api.openai.com', |
| 45 | + '@ai-sdk/perplexity' => 'https://api.perplexity.ai', |
| 46 | + '@ai-sdk/togetherai' => 'https://api.together.xyz', |
| 47 | + '@ai-sdk/xai' => 'https://api.x.ai', |
| 48 | + ]; |
| 49 | + |
31 | 50 | public static function create( |
32 | 51 | string $provider, |
33 | 52 | #[\SensitiveParameter] ?string $apiKey = null, |
@@ -75,7 +94,13 @@ public static function create( |
75 | 94 | } |
76 | 95 |
|
77 | 96 | // Use the generic OpenAI-compatible bridge |
78 | | - if ((null === $baseUrl) && null === $baseUrl = (new ProviderRegistry($dataPath))->getApiBaseUrl($provider)) { |
| 97 | + if (null === $baseUrl) { |
| 98 | + $baseUrl = (new ProviderRegistry($dataPath))->getApiBaseUrl($provider); |
| 99 | + } |
| 100 | + if (null === $baseUrl && null !== $npmPackage) { |
| 101 | + $baseUrl = self::NPM_PACKAGE_BASE_URLS[$npmPackage] ?? null; |
| 102 | + } |
| 103 | + if (null === $baseUrl) { |
79 | 104 | throw new InvalidArgumentException(\sprintf('Provider "%s" does not have a known API base URL; please provide one via the $baseUrl argument.', $provider)); |
80 | 105 | } |
81 | 106 |
|
|
0 commit comments