Skip to content

Commit ea21289

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

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;
@@ -34,6 +35,8 @@
3435
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory as OllamaPlatformFactory;
3536
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory as OpenAiPlatformFactory;
3637
use Symfony\AI\Platform\Bridge\OpenRouter\PlatformFactory as OpenRouterPlatformFactory;
38+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory as VertexAiPlatformFactory;
39+
use Symfony\AI\Platform\Exception\RuntimeException;
3740
use Symfony\AI\Platform\Model;
3841
use Symfony\AI\Platform\ModelClientInterface;
3942
use Symfony\AI\Platform\Platform;
@@ -63,6 +66,7 @@
6366
use Symfony\Component\DependencyInjection\Definition;
6467
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
6568
use Symfony\Component\DependencyInjection\Reference;
69+
use Symfony\Component\HttpClient\EventSourceHttpClient;
6670
use Symfony\Component\HttpClient\HttpClient;
6771
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
6872
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
@@ -224,6 +228,37 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
224228
return;
225229
}
226230

231+
if ('vertexai' === $type && isset($platform['location'], $platform['project_id'])) {
232+
if (!class_exists(ApplicationDefaultCredentials::class)) {
233+
throw new RuntimeException('For using the Vertex AI platform, google/auth package is required. Try running "composer require google/auth".');
234+
}
235+
236+
$credentials = ApplicationDefaultCredentials::getCredentials([
237+
'https://www.googleapis.com/auth/cloud-platform',
238+
]);
239+
240+
$httpClient = new EventSourceHttpClient(HttpClient::create([
241+
'auth_bearer' => $credentials?->fetchAuthToken()['access_token'] ?? null,
242+
]));
243+
244+
$platformId = 'ai.platform.vertexai';
245+
$definition = (new Definition(Platform::class))
246+
->setFactory([VertexAiPlatformFactory::class, 'create'])
247+
->setLazy(true)
248+
->addTag('proxy', ['interface' => PlatformInterface::class])
249+
->setArguments([
250+
$platform['location'],
251+
$platform['project_id'],
252+
$httpClient,
253+
new Reference('ai.platform.contract.vertexai', ContainerInterface::NULL_ON_INVALID_REFERENCE),
254+
])
255+
->addTag('ai.platform');
256+
257+
$container->setDefinition($platformId, $definition);
258+
259+
return;
260+
}
261+
227262
if ('openai' === $type) {
228263
$platformId = 'ai.platform.openai';
229264
$definition = (new Definition(Platform::class))

0 commit comments

Comments
 (0)