From bf7b9abf610bd1f63ce68562b14283a40cf0c596 Mon Sep 17 00:00:00 2001 From: valtzu Date: Wed, 3 Sep 2025 20:57:52 +0300 Subject: [PATCH] [AiBundle] Fix VertexAI services --- src/ai-bundle/composer.json | 1 + src/ai-bundle/src/AiBundle.php | 22 +++++++++++++------ .../DependencyInjection/AiBundleTest.php | 4 ++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/ai-bundle/composer.json b/src/ai-bundle/composer.json index 2d14cd7da..8251431fd 100644 --- a/src/ai-bundle/composer.json +++ b/src/ai-bundle/composer.json @@ -25,6 +25,7 @@ "symfony/string": "^6.4 || ^7.0" }, "require-dev": { + "google/auth": "^1.47", "phpstan/phpstan": "^2.1", "phpunit/phpunit": "^11.5", "symfony/expression-language": "^6.4 || ^7.0", diff --git a/src/ai-bundle/src/AiBundle.php b/src/ai-bundle/src/AiBundle.php index b5d446d06..4c832664a 100644 --- a/src/ai-bundle/src/AiBundle.php +++ b/src/ai-bundle/src/AiBundle.php @@ -12,6 +12,7 @@ namespace Symfony\AI\AiBundle; use Google\Auth\ApplicationDefaultCredentials; +use Google\Auth\FetchAuthTokenInterface; use Symfony\AI\Agent\Agent; use Symfony\AI\Agent\AgentInterface; use Symfony\AI\Agent\Attribute\AsInputProcessor; @@ -73,7 +74,6 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\HttpClient\EventSourceHttpClient; use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\HttpKernel\Bundle\AbstractBundle; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; @@ -279,13 +279,21 @@ private function processPlatformConfig(string $type, array $platform, ContainerB throw new RuntimeException('For using the Vertex AI platform, google/auth package is required. Try running "composer require google/auth".'); } - $credentials = ApplicationDefaultCredentials::getCredentials([ - 'https://www.googleapis.com/auth/cloud-platform', - ]); + $credentials = (new Definition(FetchAuthTokenInterface::class)) + ->setFactory([ApplicationDefaultCredentials::class, 'getCredentials']) + ->setArguments([ + 'https://www.googleapis.com/auth/cloud-platform', + ]) + ; - $httpClient = new EventSourceHttpClient(HttpClient::create([ - 'auth_bearer' => $credentials?->fetchAuthToken()['access_token'] ?? null, - ])); + $credentialsObject = new Definition(\ArrayObject::class, [(new Definition('array'))->setFactory([$credentials, 'fetchAuthToken'])]); + + $httpClient = (new Definition(HttpClientInterface::class)) + ->setFactory([HttpClient::class, 'create']) + ->setArgument(0, [ + 'auth_bearer' => (new Definition('string', ['access_token']))->setFactory([$credentialsObject, 'offsetGet']), + ]) + ; $platformId = 'ai.platform.vertexai'; $definition = (new Definition(Platform::class)) diff --git a/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php b/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php index 7db3c4b25..e588e2f04 100644 --- a/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php +++ b/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php @@ -369,6 +369,10 @@ private function getFullConfig(): array 'voyage' => [ 'api_key' => 'voyage_key_full', ], + 'vertexai' => [ + 'location' => 'global', + 'project_id' => '123', + ], ], 'agent' => [ 'my_chat_agent' => [