From 43e89dd2a2b72d0646d6640be59f8e908613b973 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Tue, 16 Sep 2025 23:04:55 +0200 Subject: [PATCH] [AI Bundle] Add configurable HTTP client support for platforms Allows configuring custom HTTP client services for all AI platforms (except VertexAI which requires special authentication). This enables customization of timeouts, proxy settings, SSL configuration, and other HTTP-specific options per platform. - Add http_client configuration option to all platforms - Update platform factory logic to use configured HTTP clients - Add comprehensive documentation with examples - Improve code formatting for better readability --- src/ai-bundle/config/options.php | 48 ++++++++++++++++++++++++++++++++ src/ai-bundle/doc/index.rst | 26 +++++++++++++++++ src/ai-bundle/src/AiBundle.php | 24 ++++++++-------- 3 files changed, 86 insertions(+), 12 deletions(-) diff --git a/src/ai-bundle/config/options.php b/src/ai-bundle/config/options.php index 25a7a9184..f97140fb9 100644 --- a/src/ai-bundle/config/options.php +++ b/src/ai-bundle/config/options.php @@ -29,6 +29,10 @@ ->children() ->scalarNode('api_key')->isRequired()->end() ->scalarNode('version')->defaultNull()->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('azure') @@ -39,6 +43,10 @@ ->scalarNode('base_url')->isRequired()->end() ->scalarNode('deployment')->isRequired()->end() ->scalarNode('api_version')->info('The used API version')->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->end() @@ -46,11 +54,19 @@ ->children() ->scalarNode('host')->end() ->scalarNode('api_key')->isRequired()->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('gemini') ->children() ->scalarNode('api_key')->isRequired()->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('vertexai') @@ -70,41 +86,73 @@ ->end() ->info('The region for OpenAI API (EU, US, or null for default)') ->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('mistral') ->children() ->scalarNode('api_key')->isRequired()->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('openrouter') ->children() ->scalarNode('api_key')->isRequired()->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('lmstudio') ->children() ->scalarNode('host_url')->defaultValue('http://127.0.0.1:1234')->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('ollama') ->children() ->scalarNode('host_url')->defaultValue('http://127.0.0.1:11434')->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('cerebras') ->children() ->scalarNode('api_key')->isRequired()->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('voyage') ->children() ->scalarNode('api_key')->isRequired()->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->arrayNode('perplexity') ->children() ->scalarNode('api_key')->isRequired()->end() + ->scalarNode('http_client') + ->defaultValue('http_client') + ->info('Service ID of the HTTP client to use') + ->end() ->end() ->end() ->end() diff --git a/src/ai-bundle/doc/index.rst b/src/ai-bundle/doc/index.rst index 79445b615..d14c6e05c 100644 --- a/src/ai-bundle/doc/index.rst +++ b/src/ai-bundle/doc/index.rst @@ -146,6 +146,32 @@ Configuration vectorizer: 'ai.vectorizer.mistral_embeddings' store: 'ai.store.memory.research' +HTTP Client Configuration +------------------------- + +Each platform can be configured with a custom HTTP client service to handle API requests. +This allows you to customize timeouts, proxy settings, SSL configurations, and other HTTP-specific options. + +By default, all platforms use the standard Symfony HTTP client service (``http_client``): + +.. code-block:: yaml + + ai: + platform: + openai: + api_key: '%env(OPENAI_API_KEY)%' + # http_client: 'http_client' # This is the default + +You can specify a custom HTTP client service for any platform: + +.. code-block:: yaml + + ai: + platform: + openai: + api_key: '%env(OPENAI_API_KEY)%' + http_client: 'app.custom_http_client' + System Prompt Configuration --------------------------- diff --git a/src/ai-bundle/src/AiBundle.php b/src/ai-bundle/src/AiBundle.php index 6e2e2bd64..0cb9a1969 100644 --- a/src/ai-bundle/src/AiBundle.php +++ b/src/ai-bundle/src/AiBundle.php @@ -221,7 +221,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['api_key'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.anthropic'), ]) ->addTag('ai.platform'); @@ -243,7 +243,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB $config['deployment'], $config['api_version'], $config['api_key'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($config['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.openai'), ]) ->addTag('ai.platform'); @@ -263,7 +263,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->setArguments([ $platform['api_key'], $platform['host'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.default'), ]) ->addTag('ai.platform'); @@ -281,7 +281,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['api_key'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.google'), ]) ->addTag('ai.platform'); @@ -338,7 +338,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['api_key'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.openai'), $platform['region'] ?? null, ]) @@ -357,7 +357,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['api_key'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.default'), ]) ->addTag('ai.platform'); @@ -375,7 +375,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['api_key'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.default'), ]) ->addTag('ai.platform'); @@ -393,7 +393,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['host_url'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.default'), ]) ->addTag('ai.platform'); @@ -412,7 +412,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['host_url'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.ollama'), ]) ->addTag('ai.platform'); @@ -430,7 +430,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['api_key'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), ]) ->addTag('ai.platform'); @@ -447,7 +447,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['api_key'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), ]) ->addTag('ai.platform'); @@ -464,7 +464,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB ->addTag('proxy', ['interface' => PlatformInterface::class]) ->setArguments([ $platform['api_key'], - new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE), new Reference('ai.platform.contract.perplexity'), ]) ->addTag('ai.platform');