Skip to content

Commit dafee7a

Browse files
committed
bug #423 [AiBundle][VertexAI] Fix service definition (valtzu)
This PR was merged into the main branch. Discussion ---------- [AiBundle][VertexAI] Fix service definition | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | Fix #419 | License | MIT Fix a couple of VertexAI issues: 1. Bundle config was not working at all since instances were created build-time instead of building `Definition`s 2. Access token was fetched build-time To add tests, we'd need to add `google/auth` to dev dependencies. If that's ok, I'll do that and remove exclusion from `phpstan.dist.neon`. Commits ------- bf7b9ab [AiBundle] Fix VertexAI services
2 parents c7fdb2a + bf7b9ab commit dafee7a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/ai-bundle/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"symfony/string": "^6.4 || ^7.0"
2626
},
2727
"require-dev": {
28+
"google/auth": "^1.47",
2829
"phpstan/phpstan": "^2.1",
2930
"phpunit/phpunit": "^11.5",
3031
"symfony/expression-language": "^6.4 || ^7.0",

src/ai-bundle/src/AiBundle.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\AI\AiBundle;
1313

1414
use Google\Auth\ApplicationDefaultCredentials;
15+
use Google\Auth\FetchAuthTokenInterface;
1516
use Symfony\AI\Agent\Agent;
1617
use Symfony\AI\Agent\AgentInterface;
1718
use Symfony\AI\Agent\Attribute\AsInputProcessor;
@@ -75,7 +76,6 @@
7576
use Symfony\Component\DependencyInjection\Definition;
7677
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
7778
use Symfony\Component\DependencyInjection\Reference;
78-
use Symfony\Component\HttpClient\EventSourceHttpClient;
7979
use Symfony\Component\HttpClient\HttpClient;
8080
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
8181
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
@@ -281,13 +281,21 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
281281
throw new RuntimeException('For using the Vertex AI platform, google/auth package is required. Try running "composer require google/auth".');
282282
}
283283

284-
$credentials = ApplicationDefaultCredentials::getCredentials([
285-
'https://www.googleapis.com/auth/cloud-platform',
286-
]);
284+
$credentials = (new Definition(FetchAuthTokenInterface::class))
285+
->setFactory([ApplicationDefaultCredentials::class, 'getCredentials'])
286+
->setArguments([
287+
'https://www.googleapis.com/auth/cloud-platform',
288+
])
289+
;
287290

288-
$httpClient = new EventSourceHttpClient(HttpClient::create([
289-
'auth_bearer' => $credentials?->fetchAuthToken()['access_token'] ?? null,
290-
]));
291+
$credentialsObject = new Definition(\ArrayObject::class, [(new Definition('array'))->setFactory([$credentials, 'fetchAuthToken'])]);
292+
293+
$httpClient = (new Definition(HttpClientInterface::class))
294+
->setFactory([HttpClient::class, 'create'])
295+
->setArgument(0, [
296+
'auth_bearer' => (new Definition('string', ['access_token']))->setFactory([$credentialsObject, 'offsetGet']),
297+
])
298+
;
291299

292300
$platformId = 'ai.platform.vertexai';
293301
$definition = (new Definition(Platform::class))

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,10 @@ private function getFullConfig(): array
620620
'voyage' => [
621621
'api_key' => 'voyage_key_full',
622622
],
623+
'vertexai' => [
624+
'location' => 'global',
625+
'project_id' => '123',
626+
],
623627
],
624628
'agent' => [
625629
'my_chat_agent' => [

0 commit comments

Comments
 (0)