Skip to content

Commit 003c0a1

Browse files
committed
feature #1689 [Platform][ModelsDev] Add well-known base URLs for providers with dedicated npm packages (fabpot)
This PR was squashed before being merged into the main branch. Discussion ---------- [Platform][ModelsDev] Add well-known base URLs for providers with dedicated npm packages | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | n/a | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- c110084 [Platform][ModelsDev] Add well-known base URLs for providers with dedicated npm packages
2 parents 3b9c7e1 + c110084 commit 003c0a1

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

src/platform/src/Bridge/ModelsDev/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
---------
33

4+
0.5
5+
---
6+
7+
* Add well-known base URLs for providers with dedicated npm packages
8+
49
0.4
510
---
611

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)