Skip to content

Commit c1dabae

Browse files
feat(platform): Add support for Google vertex AI
- Adds ai bundle integration
1 parent 5eb7655 commit c1dabae

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/ai-bundle/src/AiBundle.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\AI\AiBundle;
1313

14+
use Google\Auth\ApplicationDefaultCredentials;
1415
use Symfony\AI\Agent\Agent;
1516
use Symfony\AI\Agent\AgentInterface;
1617
use Symfony\AI\Agent\InputProcessor\SystemPromptInputProcessor;
@@ -35,6 +36,8 @@
3536
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory as OllamaPlatformFactory;
3637
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory as OpenAiPlatformFactory;
3738
use Symfony\AI\Platform\Bridge\OpenRouter\PlatformFactory as OpenRouterPlatformFactory;
39+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory as VertexAiPlatformFactory;
40+
use Symfony\AI\Platform\Exception\RuntimeException;
3841
use Symfony\AI\Platform\Model;
3942
use Symfony\AI\Platform\ModelClientInterface;
4043
use Symfony\AI\Platform\Platform;
@@ -65,6 +68,7 @@
6568
use Symfony\Component\DependencyInjection\Definition;
6669
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
6770
use Symfony\Component\DependencyInjection\Reference;
71+
use Symfony\Component\HttpClient\EventSourceHttpClient;
6872
use Symfony\Component\HttpClient\HttpClient;
6973
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
7074
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
@@ -244,6 +248,37 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
244248
return;
245249
}
246250

251+
if ('vertexai' === $type && isset($platform['location'], $platform['project_id'])) {
252+
if (!class_exists(ApplicationDefaultCredentials::class)) {
253+
throw new RuntimeException('For using the Vertex AI platform, google/auth package is required. Try running "composer require google/auth".');
254+
}
255+
256+
$credentials = ApplicationDefaultCredentials::getCredentials([
257+
'https://www.googleapis.com/auth/cloud-platform',
258+
]);
259+
260+
$httpClient = new EventSourceHttpClient(HttpClient::create([
261+
'auth_bearer' => $credentials?->fetchAuthToken()['access_token'] ?? null,
262+
]));
263+
264+
$platformId = 'ai.platform.vertexai';
265+
$definition = (new Definition(Platform::class))
266+
->setFactory([VertexAiPlatformFactory::class, 'create'])
267+
->setLazy(true)
268+
->addTag('proxy', ['interface' => PlatformInterface::class])
269+
->setArguments([
270+
$platform['location'],
271+
$platform['project_id'],
272+
$httpClient,
273+
new Reference('ai.platform.contract.vertexai', ContainerInterface::NULL_ON_INVALID_REFERENCE),
274+
])
275+
->addTag('ai.platform');
276+
277+
$container->setDefinition($platformId, $definition);
278+
279+
return;
280+
}
281+
247282
if ('openai' === $type) {
248283
$platformId = 'ai.platform.openai';
249284
$definition = (new Definition(Platform::class))

0 commit comments

Comments
 (0)