Skip to content

Commit 78da996

Browse files
committed
bug #831 [Platform] Fix platform creation (franzwilding)
This PR was squashed before being merged into the main branch. Discussion ---------- [Platform] Fix platform creation | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | License | MIT At the moment the platform bundle does not test creating of all possible platforms. Because of this, some more exotic models like mistral could not be created (because of a PR of me some time ago). This PR fixes the creation of all platforms and also adds an test to really get the instances of the (lazy) services. Commits ------- 4f01e7f [Platform] Fix platform creation
2 parents fc32bc6 + 4f01e7f commit 78da996

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/ai-bundle/src/AiBundle.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
302302
$platform['host'],
303303
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
304304
new Reference('ai.platform.model_catalog.elevenlabs'),
305+
null,
305306
new Reference('event_dispatcher'),
306307
])
307308
->addTag('ai.platform', ['name' => 'eleven_labs']);
@@ -363,6 +364,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
363364
$httpClient,
364365
new Reference('ai.platform.model_catalog.vertexai.gemini'),
365366
new Reference('ai.platform.contract.vertexai.gemini'),
367+
null,
366368
new Reference('event_dispatcher'),
367369
])
368370
->addTag('ai.platform', ['name' => 'vertexai']);
@@ -403,6 +405,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
403405
$platform['api_key'],
404406
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
405407
new Reference('ai.platform.model_catalog.openrouter'),
408+
null,
406409
new Reference('event_dispatcher'),
407410
])
408411
->addTag('ai.platform', ['name' => 'openrouter']);
@@ -422,6 +425,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
422425
$platform['api_key'],
423426
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
424427
new Reference('ai.platform.model_catalog.mistral'),
428+
null,
425429
new Reference('event_dispatcher'),
426430
])
427431
->addTag('ai.platform', ['name' => 'mistral']);
@@ -441,6 +445,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
441445
$platform['host_url'],
442446
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
443447
new Reference('ai.platform.model_catalog.lmstudio'),
448+
null,
444449
new Reference('event_dispatcher'),
445450
])
446451
->addTag('ai.platform', ['name' => 'lmstudio']);
@@ -480,6 +485,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
480485
$platform['api_key'],
481486
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
482487
new Reference('ai.platform.model_catalog.cerebras'),
488+
null,
483489
new Reference('event_dispatcher'),
484490
])
485491
->addTag('ai.platform', ['name' => 'cerebras']);
@@ -499,6 +505,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
499505
$platform['api_key'],
500506
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
501507
new Reference('ai.platform.model_catalog.deepseek'),
508+
null,
502509
new Reference('event_dispatcher'),
503510
])
504511
->addTag('ai.platform', ['name' => 'deepseek']);
@@ -518,6 +525,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
518525
$platform['api_key'],
519526
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
520527
new Reference('ai.platform.model_catalog.voyage'),
528+
null,
521529
new Reference('event_dispatcher'),
522530
])
523531
->addTag('ai.platform', ['name' => 'voyage']);
@@ -557,6 +565,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
557565
$platform['host_url'],
558566
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
559567
new Reference('ai.platform.model_catalog.dockermodelrunner'),
568+
null,
560569
new Reference('event_dispatcher'),
561570
])
562571
->addTag('ai.platform');
@@ -576,6 +585,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
576585
$platform['api_key'],
577586
new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE),
578587
new Reference('ai.platform.model_catalog.scaleway'),
588+
null,
579589
new Reference('event_dispatcher'),
580590
])
581591
->addTag('ai.platform');

src/ai-bundle/tests/DependencyInjection/AiBundleTest.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,39 @@
3232
use Symfony\Component\DependencyInjection\ContainerInterface;
3333
use Symfony\Component\DependencyInjection\Definition;
3434
use Symfony\Component\DependencyInjection\Reference;
35+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
3536
use Symfony\Component\Translation\TranslatableMessage;
37+
use Symfony\Contracts\HttpClient\HttpClientInterface;
3638

3739
class AiBundleTest extends TestCase
3840
{
3941
#[DoesNotPerformAssertions]
4042
public function testExtensionLoadDoesNotThrow()
4143
{
42-
$this->buildContainer($this->getFullConfig());
44+
$container = $this->buildContainer($this->getFullConfig());
45+
46+
// Mock services that are used as platform create arguments, but should not be testet here or are not available.
47+
$container->set('event_dispatcher', $this->createMock(EventDispatcherInterface::class));
48+
$container->getDefinition('ai.platform.vertexai')->replaceArgument(2, $this->createMock(HttpClientInterface::class));
49+
50+
$platforms = $container->findTaggedServiceIds('ai.platform');
51+
52+
foreach (array_keys($platforms) as $platformId) {
53+
try {
54+
$platformService = $container->get($platformId);
55+
$platformService->getModelCatalog();
56+
} catch (\Throwable $e) {
57+
$failureMessage = \sprintf(
58+
'Failed to load platform service "%s" or call getModelCatalog(). '.
59+
'Original error: %s (in %s:%d)',
60+
$platformId,
61+
$e->getMessage(),
62+
$e->getFile(),
63+
$e->getLine()
64+
);
65+
$this->fail($failureMessage);
66+
}
67+
}
4368
}
4469

4570
public function testStoreCommandsArentDefinedWithoutStore()
@@ -2752,13 +2777,13 @@ private function getFullConfig(): array
27522777
'azure' => [
27532778
'my_azure_instance' => [
27542779
'api_key' => 'azure_key_full',
2755-
'base_url' => 'https://myazure.openai.azure.com/',
2780+
'base_url' => 'myazure.openai.azure.com/',
27562781
'deployment' => 'gpt-35-turbo',
27572782
'api_version' => '2024-02-15-preview',
27582783
],
27592784
'another_azure_instance' => [
27602785
'api_key' => 'azure_key_2',
2761-
'base_url' => 'https://myazure2.openai.azure.com/',
2786+
'base_url' => 'myazure2.openai.azure.com/',
27622787
'deployment' => 'gpt-4',
27632788
'api_version' => '2024-02-15-preview',
27642789
],
@@ -2771,13 +2796,13 @@ private function getFullConfig(): array
27712796
'api_key' => 'gemini_key_full',
27722797
],
27732798
'openai' => [
2774-
'api_key' => 'openai_key_full',
2799+
'api_key' => 'sk-openai_key_full',
27752800
],
27762801
'mistral' => [
27772802
'api_key' => 'mistral_key_full',
27782803
],
27792804
'openrouter' => [
2780-
'api_key' => 'openrouter_key_full',
2805+
'api_key' => 'sk-openrouter_key_full',
27812806
],
27822807
'lmstudio' => [
27832808
'host_url' => 'http://127.0.0.1:1234',
@@ -2786,7 +2811,7 @@ private function getFullConfig(): array
27862811
'host_url' => 'http://127.0.0.1:11434',
27872812
],
27882813
'cerebras' => [
2789-
'api_key' => 'cerebras_key_full',
2814+
'api_key' => 'csk-cerebras_key_full',
27902815
],
27912816
'voyage' => [
27922817
'api_key' => 'voyage_key_full',

0 commit comments

Comments
 (0)