This repository was archived by the owner on Jan 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathvertex-ai-setup-example.js
More file actions
193 lines (156 loc) · 5.42 KB
/
vertex-ai-setup-example.js
File metadata and controls
193 lines (156 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Vertex AI Connector Setup Example
*
* This file demonstrates how to configure and use the Vertex AI connector
* with real Google Cloud credentials for Google AI services testing.
*/
import { VertexAIConnector } from './src/core/vertex-ai-connector.js';
// Example 1: Using Service Account Key File
async function setupWithServiceAccount() {
const config = {
projectId: 'your-gcp-project-id',
location: 'us-central1',
serviceAccountPath: '/path/to/service-account-key.json',
maxConcurrentRequests: 5,
requestTimeout: 30000,
};
try {
const vertexAI = new VertexAIConnector(config);
// Wait for initialization
await new Promise((resolve) => {
vertexAI.once('initialized', resolve);
});
console.log('✅ Vertex AI connector initialized successfully');
// Test with a simple request
const response = await vertexAI.predict({
model: 'gemini-2.5-flash',
instances: ['Hello, Vertex AI!'],
parameters: {
maxOutputTokens: 100,
temperature: 0.7,
},
});
console.log('✅ Test request successful:', response.predictions[0]);
} catch (error) {
console.error('❌ Failed to initialize Vertex AI:', error.message);
}
}
// Example 2: Using Environment Variables (ADC - Application Default Credentials)
async function setupWithEnvironmentVariables() {
// Set environment variables (in your shell or .env file):
// export GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
// export GOOGLE_CLOUD_LOCATION="us-central1"
// export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-key.json"
const config = {
projectId: process.env.GOOGLE_CLOUD_PROJECT,
location: process.env.GOOGLE_CLOUD_LOCATION || 'us-central1',
maxConcurrentRequests: 10,
requestTimeout: 30000,
};
try {
const vertexAI = new VertexAIConnector(config);
await new Promise((resolve) => {
vertexAI.once('initialized', resolve);
});
console.log('✅ Vertex AI connector initialized with environment credentials');
} catch (error) {
console.error('❌ Failed to initialize Vertex AI:', error.message);
}
}
// Example 3: Using Inline Credentials
async function setupWithInlineCredentials() {
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: '[email protected]',
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,
};
try {
const vertexAI = new VertexAIConnector(config);
await new Promise((resolve) => {
vertexAI.once('initialized', resolve);
});
console.log('✅ Vertex AI connector initialized with inline credentials');
} catch (error) {
console.error('❌ Failed to initialize Vertex AI:', error.message);
}
}
// Example 4: Error Handling and Health Checks
async function demonstrateErrorHandling() {
const config = {
projectId: 'invalid-project-id',
location: 'us-central1',
maxConcurrentRequests: 5,
requestTimeout: 30000,
};
const vertexAI = new VertexAIConnector(config);
try {
await new Promise((resolve, reject) => {
vertexAI.once('initialized', resolve);
vertexAI.once('error', reject);
});
} catch (error) {
console.log('Expected error caught:', error.message);
}
// Health check
const healthStatus = await vertexAI.healthCheck();
console.log('Health check result:', healthStatus);
}
// Example 5: Batch Processing
async function demonstrateBatchProcessing() {
const config = {
projectId: 'your-gcp-project-id',
location: 'us-central1',
serviceAccountPath: '/path/to/service-account-key.json',
};
const vertexAI = new VertexAIConnector(config);
await new Promise((resolve) => {
vertexAI.once('initialized', resolve);
});
// Process multiple requests in batch
const instances = [
'What is machine learning?',
'Explain quantum computing',
'How does AI work?',
];
const response = await vertexAI.batchPredict(
'gemini-2.5-flash',
instances,
{ maxOutputTokens: 100, temperature: 0.7 },
2, // chunk size
);
console.log('✅ Batch processing completed:', response.predictions.length, 'responses');
response.predictions.forEach((prediction, index) => {
console.log(`Response ${index + 1}:`, prediction.content.substring(0, 100), '...');
});
}
// Usage Examples:
// 1. Run with service account file
// setupWithServiceAccount().catch(console.error);
// 2. Run with environment variables
// setupWithEnvironmentVariables().catch(console.error);
// 3. Run with inline credentials
// setupWithInlineCredentials().catch(console.error);
// 4. Demonstrate error handling
// demonstrateErrorHandling().catch(console.error);
// 5. Demonstrate batch processing
// demonstrateBatchProcessing().catch(console.error);
export {
setupWithServiceAccount,
setupWithEnvironmentVariables,
setupWithInlineCredentials,
demonstrateErrorHandling,
demonstrateBatchProcessing,
};