Vertex AI
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 supports the following authentication methods:
1. Application Default Credentials (ADC)
Follow the Google cloud authentication guide to set up your credentials.
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.
Configure your Google Cloud project and location:
1 2
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=us-central1
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: $httpClient
);
$messages = new MessageBag(
Message::ofUser('Hello, how are you?')
);
$result = $platform->invoke('gemini-2.5-flash', $messages);
echo $result->getContent();
2. Service Account Key
Similar to the first approach, but instead of authenticating with the `gcloud` command, you provide the service account key directly using an environment variable:
1
GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-key.json"
3. API Key
Similar to the first approach, but instead of authenticating with the `gcloud` command, you provide the API keys when creating the platform:
1 2 3 4 5
$platform = PlatformFactory::create(
$_ENV['GOOGLE_CLOUD_LOCATION'],
$_ENV['GOOGLE_CLOUD_PROJECT'],
apiKey: $_ENV['GOOGLE_CLOUD_VERTEX_API_KEY'],
);
To get an API key, visit: Vertex AI Studio (API keys).
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:
- The model is not available in your specified location
- Try switching to a different location like
us-central1 - Use an alternative model that's available in your location
- 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 trackingtoolcall.php- Using server toolsserver-tools.php- Advanced server tool usage