8787use Symfony \Component \DependencyInjection \Reference ;
8888use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
8989use Symfony \Component \HttpClient \HttpClient ;
90+ use Symfony \Component \RateLimiter \RateLimiterFactory ;
91+ use Symfony \Component \RateLimiter \Storage \InMemoryStorage ;
9092use Symfony \Contracts \HttpClient \HttpClientInterface ;
9193
9294class AiBundleTest extends TestCase
@@ -4039,6 +4041,7 @@ public function testFailoverPlatformCanBeCreated()
40394041 'ai.platform.ollama ' ,
40404042 'ai.platform.openai ' ,
40414043 ],
4044+ 'rate_limiter ' => 'limiter.failover_platform ' ,
40424045 ],
40434046 ],
40444047 ],
@@ -4059,56 +4062,9 @@ public function testFailoverPlatformCanBeCreated()
40594062 new Reference ('ai.platform.openai ' ),
40604063 ], $ definition ->getArgument (0 ));
40614064 $ this ->assertInstanceOf (Reference::class, $ definition ->getArgument (1 ));
4062- $ this ->assertSame (ClockInterface::class, (string ) $ definition ->getArgument (1 ));
4063- $ this ->assertSame (60 , $ definition ->getArgument (2 ));
4064- $ this ->assertInstanceOf (Reference::class, $ definition ->getArgument (3 ));
4065- $ this ->assertSame (LoggerInterface::class, (string ) $ definition ->getArgument (3 ));
4066-
4067- $ this ->assertTrue ($ definition ->hasTag ('proxy ' ));
4068- $ this ->assertSame ([['interface ' => PlatformInterface::class]], $ definition ->getTag ('proxy ' ));
4069- $ this ->assertTrue ($ definition ->hasTag ('ai.platform ' ));
4070- $ this ->assertSame ([['name ' => 'failover ' ]], $ definition ->getTag ('ai.platform ' ));
4071-
4072- $ this ->assertTrue ($ container ->hasAlias ('Symfony\AI\Platform\PlatformInterface $main ' ));
4073-
4074- $ container = $ this ->buildContainer ([
4075- 'ai ' => [
4076- 'platform ' => [
4077- 'ollama ' => [
4078- 'host_url ' => 'http://127.0.0.1:11434 ' ,
4079- ],
4080- 'openai ' => [
4081- 'api_key ' => 'sk-openai_key_full ' ,
4082- ],
4083- 'failover ' => [
4084- 'main ' => [
4085- 'platforms ' => [
4086- 'ai.platform.ollama ' ,
4087- 'ai.platform.openai ' ,
4088- ],
4089- 'retry_period ' => 120 ,
4090- ],
4091- ],
4092- ],
4093- ],
4094- ]);
4095-
4096- $ this ->assertTrue ($ container ->hasDefinition ('ai.platform.failover.main ' ));
4097-
4098- $ definition = $ container ->getDefinition ('ai.platform.failover.main ' );
4099-
4100- $ this ->assertTrue ($ definition ->isLazy ());
4101- $ this ->assertSame (FailoverPlatform::class, $ definition ->getClass ());
4102-
4103- $ this ->assertCount (4 , $ definition ->getArguments ());
4104- $ this ->assertCount (2 , $ definition ->getArgument (0 ));
4105- $ this ->assertEquals ([
4106- new Reference ('ai.platform.ollama ' ),
4107- new Reference ('ai.platform.openai ' ),
4108- ], $ definition ->getArgument (0 ));
4109- $ this ->assertInstanceOf (Reference::class, $ definition ->getArgument (1 ));
4110- $ this ->assertSame (ClockInterface::class, (string ) $ definition ->getArgument (1 ));
4111- $ this ->assertSame (120 , $ definition ->getArgument (2 ));
4065+ $ this ->assertSame ('limiter.failover_platform ' , (string ) $ definition ->getArgument (1 ));
4066+ $ this ->assertInstanceOf (Reference::class, $ definition ->getArgument (2 ));
4067+ $ this ->assertSame (ClockInterface::class, (string ) $ definition ->getArgument (2 ));
41124068 $ this ->assertInstanceOf (Reference::class, $ definition ->getArgument (3 ));
41134069 $ this ->assertSame (LoggerInterface::class, (string ) $ definition ->getArgument (3 ));
41144070
@@ -7115,6 +7071,15 @@ private function buildContainer(array $configuration): ContainerBuilder
71157071 $ container ->setParameter ('kernel.build_dir ' , 'public ' );
71167072 $ container ->setDefinition (ClockInterface::class, new Definition (MonotonicClock::class));
71177073 $ container ->setDefinition (LoggerInterface::class, new Definition (NullLogger::class));
7074+ $ container ->setDefinition ('limiter.failover_platform ' , new Definition (RateLimiterFactory::class, [
7075+ [
7076+ 'policy ' => 'sliding_window ' ,
7077+ 'id ' => 'test ' ,
7078+ 'interval ' => '60 seconds ' ,
7079+ 'limit ' => 1 ,
7080+ ],
7081+ new Definition (InMemoryStorage::class),
7082+ ]));
71187083
71197084 $ extension = (new AiBundle ())->getContainerExtension ();
71207085 $ extension ->load ($ configuration , $ container );
@@ -7176,13 +7141,7 @@ private function getFullConfig(): array
71767141 'ai.platform.ollama ' ,
71777142 'ai.platform.openai ' ,
71787143 ],
7179- ],
7180- 'main_with_custom_retry_period ' => [
7181- 'platforms ' => [
7182- 'ai.platform.ollama ' ,
7183- 'ai.platform.openai ' ,
7184- ],
7185- 'retry_period ' => 120 ,
7144+ 'rate_limiter ' => 'limiter.failover_platform ' ,
71867145 ],
71877146 ],
71887147 'gemini ' => [
0 commit comments