This guide explains how to set up and use the Vertex AI connector with real Google Cloud credentials for comprehensive testing of Google AI services (Imagen4, Veo3, Multi-modal Streaming API).
- Google Cloud Project: You need a Google Cloud Project with Vertex AI API enabled
- Authentication: One of the following:
- Service Account Key file
- Application Default Credentials (ADC)
- Environment variables
Install the required Google Cloud packages:
npm install @google-cloud/vertexai google-auth-library-
Create a Service Account:
- Go to Google Cloud Console
- Navigate to IAM & Admin > Service Accounts
- Click "Create Service Account"
- Grant Vertex AI User role
-
Download Key File:
- Create and download the JSON key file
- Place it in a secure location (e.g.,
/path/to/service-account-key.json)
-
Configure the Connector:
import { VertexAIConnector } from './src/core/vertex-ai-connector.js';
const config = {
projectId: 'your-gcp-project-id',
location: 'us-central1',
serviceAccountPath: '/path/to/service-account-key.json',
maxConcurrentRequests: 5,
requestTimeout: 30000,
};
const vertexAI = new VertexAIConnector(config);- Set Environment Variables:
export GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
export GOOGLE_CLOUD_LOCATION="us-central1"
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-key.json"- Configure the Connector:
const config = {
projectId: process.env.GOOGLE_CLOUD_PROJECT,
location: process.env.GOOGLE_CLOUD_LOCATION || 'us-central1',
maxConcurrentRequests: 10,
requestTimeout: 30000,
};
const vertexAI = new VertexAIConnector(config);const config = {
projectId: 'your-gcp-project-id',
location: 'us-central1',
credentials: {
type: 'service_account',
project_id: 'your-gcp-project-id',
private_key_id: 'your-private-key-id',
private_key: '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n',
client_email: 'your-service-account@your-project.iam.gserviceaccount.com',
client_id: 'your-client-id',
auth_uri: 'https://accounts.google.com/o/oauth2/auth',
token_uri: 'https://oauth2.googleapis.com/token',
auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs',
client_x509_cert_url: 'https://www.googleapis.com/robot/v1/metadata/x509/...',
},
maxConcurrentRequests: 5,
requestTimeout: 30000,
};
const vertexAI = new VertexAIConnector(config);const response = await vertexAI.predict({
model: 'gemini-2.5-flash',
instances: ['What is machine learning?'],
parameters: {
maxOutputTokens: 100,
temperature: 0.7,
},
});
console.log(response.predictions[0].content);const instances = [
'Explain quantum computing',
'What is artificial intelligence?',
'Describe machine learning',
];
const response = await vertexAI.batchPredict(
'gemini-2.5-flash',
instances,
{ maxOutputTokens: 100, temperature: 0.7 },
2, // chunk size
);
console.log('Processed', response.predictions.length, 'requests');const healthStatus = await vertexAI.healthCheck();
console.log('Health Status:', healthStatus);The connector supports these Vertex AI models:
| Model | Description | Context Window | Best For |
|---|---|---|---|
gemini-2.5-pro |
Advanced reasoning and code | 2M tokens | Complex tasks, coding |
gemini-2.5-flash |
Fast responses | 1M tokens | Quick interactions |
gemini-2.0-flash |
Balanced performance | 1M tokens | General use |
gemini-2.5-deep-think |
Deep reasoning (Preview) | 2M tokens | Complex problem-solving |
The connector provides comprehensive error handling:
try {
const response = await vertexAI.predict({
model: 'gemini-2.5-flash',
instances: ['Hello, Vertex AI!'],
});
console.log('Success:', response);
} catch (error) {
console.error('Error:', error.message);
// Common error scenarios:
if (error.message.includes('PERMISSION_DENIED')) {
console.log('Check your service account permissions');
} else if (error.message.includes('QUOTA_EXCEEDED')) {
console.log('API quota exceeded');
} else if (error.message.includes('INVALID_ARGUMENT')) {
console.log('Check your request parameters');
}
}The connector provides built-in performance monitoring:
// Get performance metrics
const metrics = vertexAI.getMetrics();
console.log('Total Requests:', metrics.totalRequests);
console.log('Success Rate:', metrics.successRate);
console.log('Average Latency:', metrics.avgLatency);
// Listen to events
vertexAI.on('request_completed', (data) => {
console.log('Request completed:', data.model, data.latency + 'ms');
});
vertexAI.on('request_failed', (data) => {
console.log('Request failed:', data.model, data.error);
});| Option | Type | Default | Description |
|---|---|---|---|
projectId |
string | Required | Your Google Cloud Project ID |
location |
string | Required | Vertex AI location (e.g., 'us-central1') |
apiEndpoint |
string | Optional | Custom API endpoint |
credentials |
object | Optional | Inline credentials |
serviceAccountPath |
string | Optional | Path to service account key file |
maxConcurrentRequests |
number | 10 | Maximum concurrent requests |
requestTimeout |
number | 30000 | Request timeout in milliseconds |
- Never commit credentials to version control
- Use environment variables for sensitive configuration
- Rotate service account keys regularly
- Grant minimal permissions to service accounts
- Monitor API usage for unusual activity
-
Authentication Errors:
- Verify your service account has Vertex AI User permissions
- Check that your credentials file is valid JSON
- Ensure the service account key hasn't expired
-
Quota Errors:
- Check your Google Cloud quotas in the console
- Implement retry logic with exponential backoff
- Consider upgrading your billing plan
-
Model Not Found:
- Verify the model name is correct
- Check if the model is available in your region
- Ensure your project has access to the model
-
Network Issues:
- Check your internet connection
- Verify firewall settings allow HTTPS traffic
- Consider using a proxy if needed
Enable detailed logging:
// The connector uses the Logger class for debugging
// Set log level to 'debug' for detailed output
const logger = new Logger('VertexAIConnector', 'debug');- Set up your Google Cloud Project and authentication
- Run the example scripts to test connectivity
- Integrate the connector into your test suites
- Monitor performance and costs in Google Cloud Console
- Implement proper error handling and retries
For issues related to:
- Google Cloud Setup: Check Google Cloud Documentation
- Vertex AI API: See Vertex AI Documentation
- Authentication: Review Google Auth Library Documentation
Vertex AI pricing varies by model and usage. Monitor costs in:
- Google Cloud Console > Billing
- Vertex AI > Monitor > Quotas and limits
- Set up budget alerts for cost control