Skip to content

Commit e734e53

Browse files
committed
[Platform][ModelsDev] Add well-known base URLs for providers with dedicated npm packages
1 parent 070c478 commit e734e53

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/platform/src/Bridge/ModelsDev/PlatformFactory.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@
2828
*/
2929
final class PlatformFactory
3030
{
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+
3150
public static function create(
3251
string $provider,
3352
#[\SensitiveParameter] ?string $apiKey = null,
@@ -75,7 +94,13 @@ public static function create(
7594
}
7695

7796
// 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) {
79104
throw new InvalidArgumentException(\sprintf('Provider "%s" does not have a known API base URL; please provide one via the $baseUrl argument.', $provider));
80105
}
81106

src/platform/src/Bridge/ModelsDev/Tests/PlatformFactoryTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,25 @@ public function testCreateWithExplicitBaseUrl()
4545
$this->assertInstanceOf(Platform::class, $platform);
4646
}
4747

48-
public function testCreateThrowsWhenNoBaseUrlAvailable()
48+
public function testCreateWithWellKnownNpmPackageProvider()
4949
{
50-
$this->expectException(InvalidArgumentException::class);
51-
$this->expectExceptionMessage('does not have a known API base URL');
52-
53-
PlatformFactory::create(
50+
$platform = PlatformFactory::create(
5451
provider: 'openai',
5552
apiKey: 'test-key',
5653
);
54+
55+
$this->assertInstanceOf(Platform::class, $platform);
5756
}
5857

59-
public function testCreateWithGroq()
58+
public function testCreateThrowsWhenNoBaseUrlAvailable()
6059
{
61-
$platform = PlatformFactory::create(
62-
provider: 'groq',
60+
$this->expectException(InvalidArgumentException::class);
61+
$this->expectExceptionMessage('does not have a known API base URL');
62+
63+
PlatformFactory::create(
64+
provider: 'azure',
6365
apiKey: 'test-key',
64-
baseUrl: 'https://api.groq.com/openai/v1',
6566
);
66-
67-
$this->assertInstanceOf(Platform::class, $platform);
6867
}
6968

7069
public function testCreateWithProviderHavingApiUrl()

0 commit comments

Comments
 (0)