### Complete AI Failover Platform Integration in PHP Source: https://context7.com/symfony/ai-failover-platform/llms.txt Demonstrates a full integration of the Failover Platform in a Symfony application. It includes configuring multiple AI providers (OpenAI, Anthropic) in priority order, setting up rate limiting, and handling potential runtime exceptions for graceful degradation. This example showcases how to use the platform for generating text responses and engaging in chat conversations. ```php 'sliding_window', 'id' => 'ai-failover', 'interval' => '60 seconds', 'limit' => 3, ], new InMemoryStorage()); // Configure platforms in priority order $this->platform = FailoverPlatformFactory::create( platforms: [ new OpenAIPlatform($openAiKey), // Primary new AnthropicPlatform($anthropicKey), // Fallback ], rateLimiterFactory: $rateLimiterFactory, clock: new MonotonicClock(), logger: $logger ); } public function generateResponse(string $prompt, array $options = []): string { try { $result = $this->platform->invoke( model: 'gpt-4', input: $prompt, options: array_merge(['temperature' => 0.7], $options) ); return $result->asText(); } catch (RuntimeException $e) { // All platforms failed - return graceful degradation return 'AI service is temporarily unavailable. Please try again later.'; } } public function chat(array $messages): string { try { $result = $this->platform->invoke( model: 'gpt-4', input: $messages, options: [] ); return $result->asText(); } catch (RuntimeException $e) { throw new \RuntimeException('Chat service unavailable', 0, $e); } } } // Usage $aiService = new AIService( openAiKey: $_ENV['OPENAI_API_KEY'], anthropicKey: $_ENV['ANTHROPIC_API_KEY'], logger: $logger ); $response = $aiService->generateResponse('What are the benefits of using Symfony?'); echo $response; ``` -------------------------------- ### Create Failover Platform Instance using Factory Source: https://context7.com/symfony/ai-failover-platform/llms.txt The `FailoverPlatformFactory::create` static method is the recommended way to instantiate a `FailoverPlatform`. It takes an array of AI platforms, a rate limiter factory, and optional clock and logger instances. The rate limiter is configured to track failures, and platforms are provided in priority order. ```php 'sliding_window', 'id' => 'failover', 'interval' => '60 seconds', 'limit' => 3, // Allow 3 failures before marking platform as failed ], new InMemoryStorage()); // Create platforms in priority order (first = highest priority) $platforms = [ new OpenAIPlatform('your-openai-api-key'), new AnthropicPlatform('your-anthropic-api-key'), ]; // Create failover platform with optional clock and logger $failoverPlatform = FailoverPlatformFactory::create( $platforms, $rateLimiterFactory, new MonotonicClock(), new NullLogger() ); ``` -------------------------------- ### Instantiate Failover Platform Directly with Constructor Source: https://context7.com/symfony/ai-failover-platform/llms.txt The `FailoverPlatform` constructor allows direct instantiation with an iterable of platforms, a rate limiter factory, and optional logger. It enforces that at least one platform must be configured, throwing an `InvalidArgumentException` if the platforms array is empty. Logging can be set up using PSR-3 compatible loggers. ```php pushHandler(new StreamHandler('/var/log/ai-failover.log', Logger::ERROR)); // Configure rate limiter $rateLimiterFactory = new RateLimiterFactory([ 'policy' => 'sliding_window', 'id' => 'failover', 'interval' => '60 seconds', 'limit' => 3, ], new InMemoryStorage()); // Create failover platform with multiple providers $failoverPlatform = new FailoverPlatform( platforms: [ new OpenAIPlatform('openai-key'), new AnthropicPlatform('anthropic-key'), ], rateLimiterFactory: $rateLimiterFactory, logger: $logger ); // This will throw InvalidArgumentException try { $invalidPlatform = new FailoverPlatform([], $rateLimiterFactory); } catch (InvalidArgumentException $e) { echo $e->getMessage(); // Output: "Symfony\AI\Platform\Bridge\Failover\FailoverPlatform" must have at least one platform configured. } ``` -------------------------------- ### FailoverPlatform::__construct Source: https://context7.com/symfony/ai-failover-platform/llms.txt Directly instantiates a FailoverPlatform with an iterable of platforms, a rate limiter factory, and optional clock and logger dependencies. ```APIDOC ## FailoverPlatform::__construct ### Description The constructor directly instantiates a FailoverPlatform with an iterable of platforms, a rate limiter factory, and optional clock and logger dependencies. It validates that at least one platform is configured and throws an `InvalidArgumentException` if the platforms array is empty. ### Method `__construct(iterable $platforms, RateLimiterFactory $rateLimiterFactory, ?ClockInterface $clock = null, ?LoggerInterface $logger = null)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php pushHandler(new StreamHandler('/var/log/ai-failover.log', Logger::ERROR)); // Configure rate limiter $rateLimiterFactory = new RateLimiterFactory([ 'policy' => 'sliding_window', 'id' => 'failover', 'interval' => '60 seconds', 'limit' => 3, ], new InMemoryStorage()); // Create failover platform with multiple providers $failoverPlatform = new FailoverPlatform( platforms: [ new OpenAIPlatform('openai-key'), new AnthropicPlatform('anthropic-key'), ], rateLimiterFactory: $rateLimiterFactory, logger: $logger ); // This will throw InvalidArgumentException try { $invalidPlatform = new FailoverPlatform([], $rateLimiterFactory); } catch (InvalidArgumentException $e) { echo $e->getMessage(); // Output: "Symfony\AI\Platform\Bridge\Failover\FailoverPlatform" must have at least one platform configured. } ``` ### Response #### Success Response (200) - **FailoverPlatform** (object) - An instance of the FailoverPlatform. #### Response Example ```json { "example": "// PHP object representing the FailoverPlatform" } ``` #### Error Response (400) - **InvalidArgumentException** (object) - Thrown if the platforms array is empty. ``` -------------------------------- ### Retrieve Model Catalog with Failover Source: https://context7.com/symfony/ai-failover-platform/llms.txt Retrieves the model catalog from the first available platform, failing over to backups if necessary. Returns a ModelCatalogInterface for querying models. Throws RuntimeException if all platforms fail. ```php getModelCatalog(); // Use the catalog to discover available models // The catalog interface allows querying model capabilities if ($modelCatalog instanceof ModelCatalogInterface) { echo "Model catalog retrieved successfully\n"; } } catch (RuntimeException $e) { // All platforms failed to provide a model catalog echo "Unable to retrieve model catalog: " . $e->getMessage(); } ``` -------------------------------- ### FailoverPlatformFactory::create Source: https://context7.com/symfony/ai-failover-platform/llms.txt A static factory method to create a new FailoverPlatform instance with dependencies. This is the recommended way to instantiate the failover platform. ```APIDOC ## FailoverPlatformFactory::create ### Description A static factory method that creates a new FailoverPlatform instance with all required dependencies. This is the recommended way to instantiate the failover platform as it provides a clean, fluent interface for configuration. ### Method `static create(iterable $platforms, RateLimiterFactory $rateLimiterFactory, ?ClockInterface $clock = null, ?LoggerInterface $logger = null): FailoverPlatform` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php 'sliding_window', 'id' => 'failover', 'interval' => '60 seconds', 'limit' => 3, // Allow 3 failures before marking platform as failed ], new InMemoryStorage()); // Create platforms in priority order (first = highest priority) $platforms = [ new OpenAIPlatform('your-openai-api-key'), new AnthropicPlatform('your-anthropic-api-key'), ]; // Create failover platform with optional clock and logger $failoverPlatform = FailoverPlatformFactory::create( $platforms, $rateLimiterFactory, new MonotonicClock(), new NullLogger() ); ``` ### Response #### Success Response (200) - **FailoverPlatform** (object) - An instance of the FailoverPlatform. #### Response Example ```json { "example": "// PHP object representing the FailoverPlatform" } ``` ``` -------------------------------- ### Invoke AI Model with Failover Source: https://context7.com/symfony/ai-failover-platform/llms.txt Invokes an AI model, automatically failing over to backup platforms if the primary fails. Handles basic text and chat-style inputs. Throws a RuntimeException if all platforms fail. ```php 'sliding_window', 'id' => 'failover', 'interval' => '60 seconds', 'limit' => 3, ], new InMemoryStorage()); // Basic text invocation - will try OpenAI first, then Anthropic if OpenAI fails $result = $failoverPlatform->invoke( model: 'gpt-4', input: 'Explain the concept of dependency injection in PHP.', options: ['temperature' => 0.7] ); // Get the response as text $responseText = $result->asText(); echo $responseText; // Invoke with array input (for chat-style interactions) $chatResult = $failoverPlatform->invoke( model: 'gpt-4', input: [ ['role' => 'system', 'content' => 'You are a helpful assistant.'], ['role' => 'user', 'content' => 'What is Symfony?'], ], options: [] ); echo $chatResult->asText(); // Handle complete platform failure try { $result = $failoverPlatform->invoke('gpt-4', 'Hello'); } catch (RuntimeException $e) { // All platforms failed - implement your own fallback logic echo "All AI platforms are unavailable: " . $e->getMessage(); // Output: All platforms failed. } ``` -------------------------------- ### Configure Rate Limiter for Failover Source: https://context7.com/symfony/ai-failover-platform/llms.txt Configures rate limiters for the failover platform using Symfony's Rate Limiter component. Supports in-memory and distributed (Redis) storage for tracking platform failures. ```php 'sliding_window', 'id' => 'failover', 'interval' => '60 seconds', // Time window for tracking failures 'limit' => 3, // Number of failures before platform is considered failed ], new InMemoryStorage()); // Redis-based storage (recommended for multi-server deployments) $redis = RedisAdapter::createConnection('redis://localhost:6379'); $cacheAdapter = new RedisAdapter($redis); $distributedRateLimiter = new RateLimiterFactory([ 'policy' => 'sliding_window', 'id' => 'ai-failover', 'interval' => '120 seconds', // 2-minute window 'limit' => 5, // 5 failures before failover ], new CacheStorage($cacheAdapter)); // Use with FailoverPlatform $failoverPlatform = new FailoverPlatform( $platforms, $distributedRateLimiter ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.