|
11 | 11 |
|
12 | 12 | namespace Symfony\AI\AiBundle; |
13 | 13 |
|
| 14 | +use Google\Auth\ApplicationDefaultCredentials; |
14 | 15 | use Symfony\AI\Agent\Agent; |
15 | 16 | use Symfony\AI\Agent\AgentInterface; |
16 | 17 | use Symfony\AI\Agent\InputProcessor\SystemPromptInputProcessor; |
|
35 | 36 | use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory as OllamaPlatformFactory; |
36 | 37 | use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory as OpenAiPlatformFactory; |
37 | 38 | 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; |
38 | 41 | use Symfony\AI\Platform\Model; |
39 | 42 | use Symfony\AI\Platform\ModelClientInterface; |
40 | 43 | use Symfony\AI\Platform\Platform; |
|
65 | 68 | use Symfony\Component\DependencyInjection\Definition; |
66 | 69 | use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
67 | 70 | use Symfony\Component\DependencyInjection\Reference; |
| 71 | +use Symfony\Component\HttpClient\EventSourceHttpClient; |
68 | 72 | use Symfony\Component\HttpClient\HttpClient; |
69 | 73 | use Symfony\Component\HttpKernel\Bundle\AbstractBundle; |
70 | 74 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
@@ -244,6 +248,37 @@ private function processPlatformConfig(string $type, array $platform, ContainerB |
244 | 248 | return; |
245 | 249 | } |
246 | 250 |
|
| 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 | + |
247 | 282 | if ('openai' === $type) { |
248 | 283 | $platformId = 'ai.platform.openai'; |
249 | 284 | $definition = (new Definition(Platform::class)) |
|
0 commit comments