Skip to content

Vertex AI

Edit this page

Google Cloud Vertex AI is a machine learning platform that provides access to Google's Gemini models and other AI services. The Symfony AI Platform component provides a bridge to interact with Vertex AI models.

For comprehensive information about Vertex AI, see the Vertex AI documentation and Vertex AI API reference.

Installation

To use Vertex AI with Symfony AI Platform, you need to install the platform component and set up Google Cloud authentication:

1
$ composer require symfony/ai-platform

Setup

Authentication

Vertex AI requires Google Cloud authentication. Follow the Google cloud authentication guide to set up your credentials.

You can authenticate using:

  1. Application Default Credentials (ADC) - Recommended for production
  2. Service Account Key - For development or specific service accounts

For ADC, install the Google Cloud SDK and authenticate:

1
$ gcloud auth application-default login

For detailed authentication setup, see Setting up authentication for Vertex AI.

Environment Variables

Configure your Google Cloud project and location:

1
2
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=us-central1

Usage

Basic usage example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model;
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

$platform = PlatformFactory::create(
    $_ENV['GOOGLE_CLOUD_LOCATION'],
    $_ENV['GOOGLE_CLOUD_PROJECT'],
    $httpClient
);

$messages = new MessageBag(
    Message::ofUser('Hello, how are you?')
);

$result = $platform->invoke('gemini-2.5-flash', $messages);
echo $result->getContent();

Model Availability by Location

Note

Model availability varies by Google Cloud location. Not all models are available in all regions.

Common model availability:

  • us-central1: Most comprehensive model availability, recommended for development
  • us-east1: Good model availability
  • europe-west1: Good model availability
  • global: Limited model availability, some newer models may not be available

Troubleshooting Model Availability

If you encounter an error like:

1
Publisher Model `projects/your-project/locations/global/publishers/google/models/gemini-2.0-flash-lite` not found

This typically means:

  1. The model is not available in your specified location
  2. Try switching to a different location like us-central1
  3. Use an alternative model that's available in your location
  4. Check the Google Cloud Console for Vertex AI for model availability in your region

Checking Model Availability

You can check which models are available in your location using the Google Cloud Console or gcloud CLI:

1
gcloud ai models list --region=us-central1

Location Configuration

Configure your location in your environment file:

1
2
3
4
5
# Recommended: Use a region with comprehensive model support
GOOGLE_CLOUD_LOCATION=us-central1

# Avoid: Global location has limited model availability
# GOOGLE_CLOUD_LOCATION=global

Token Usage Tracking

Track token usage with the TokenOutputProcessor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\VertexAi\TokenOutputProcessor;

$agent = new Agent(
    $platform,
    $model,
    outputProcessors: [new TokenOutputProcessor()],
    logger: $logger
);

$result = $agent->call($messages);
$tokenUsage = $result->getMetadata()->get('token_usage');

assert($tokenUsage instanceof TokenUsage);

echo 'Prompt Tokens: ' . $tokenUsage->promptTokens . PHP_EOL;
echo 'Completion Tokens: ' . $tokenUsage->completionTokens . PHP_EOL;
echo 'Total Tokens: ' . $tokenUsage->totalTokens . PHP_EOL;

Server Tools

Vertex AI provides built-in server tools. See Vertex AI Server Tools for detailed information about:

  • URL Context
  • Grounding with Google Search
  • Code Execution

Examples

See the examples/vertexai/ directory for complete working examples:

  • token-metadata.php - Token usage tracking
  • toolcall.php - Using server tools
  • server-tools.php - Advanced server tool usage
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version