Skip to content

Commit b8d4af1

Browse files
yyyu-googlecopybara-github
authored andcommitted
feat: infer location if user doesn't specifies it.
PiperOrigin-RevId: 635935845
1 parent de9c4c2 commit b8d4af1

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

src/types/content.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
2424
export declare interface VertexInit {
2525
/** The Google Cloud project name. It is not the numeric project ID. */
2626
project: string;
27-
/** The Google Cloud project location. */
28-
location: string;
27+
/**
28+
* Optional. The Google Cloud project location. If not provided, SDK will
29+
* firtly try to resolve it from run time environment. If no location resolved
30+
* from run time environment, SDK will use default value `us-central1`.
31+
*/
32+
location?: string;
2933
/**
3034
* Optional. The base Vertex AI endpoint to use for the request. If not
3135
* provided, the default regionalized endpoint (i.e.

src/vertex_ai.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class VertexAI {
5252
init.googleAuthOptions
5353
);
5454
this.project = init.project;
55-
this.location = init.location;
55+
this.location = resolveLocation(init.location);
5656
this.googleAuth = new GoogleAuth(opts);
5757
this.apiEndpoint = init.apiEndpoint;
5858
this.preview = new VertexAIPreview(
@@ -222,3 +222,15 @@ function validateGoogleAuthOptions(
222222
}
223223
return opts;
224224
}
225+
226+
function resolveLocation(locationFromInput?: string): string {
227+
if (locationFromInput) {
228+
return locationFromInput;
229+
}
230+
const inferredLocation =
231+
process.env['GOOGLE_CLOUD_REGION'] || process.env['CLOUD_ML_REGION'];
232+
if (inferredLocation) {
233+
return inferredLocation;
234+
}
235+
return 'us-central1';
236+
}

system_test/end_to_end_sample_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
} from '../src';
2828
import {FunctionDeclarationSchemaType} from '../src/types';
2929

30-
const PROJECT = process.env.GCLOUD_PROJECT;
30+
const PROJECT = process.env['GCLOUD_PROJECT'];
3131
const LOCATION = 'us-central1';
3232
const TEXT_REQUEST = {
3333
contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}],

test/vertex_ai_test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,50 @@ describe('VertexAI', () => {
1414
});
1515
});
1616

17+
it('no location given, should instantiate VertexAI and VertexAIPreview', () => {
18+
const vertexaiNoLocation = new VertexAI({project: PROJECT});
19+
const generativeModel = vertexaiNoLocation.getGenerativeModel({
20+
model: 'gemini-pro',
21+
});
22+
const generativeModelPreview =
23+
vertexaiNoLocation.preview.getGenerativeModel({
24+
model: 'gemini-pro',
25+
});
26+
expect(vertexaiNoLocation).toBeInstanceOf(VertexAI);
27+
expect(generativeModel).toBeInstanceOf(GenerativeModel);
28+
expect(generativeModelPreview).toBeInstanceOf(GenerativeModelPreview);
29+
});
30+
31+
it('location in run time env GOOGLE_CLOUD_REGION, should instantiate VertexAI and VertexAIPreview', () => {
32+
process.env['GOOGLE_CLOUD_REGION'] = 'us-central1';
33+
const vertexaiNoLocation = new VertexAI({project: PROJECT});
34+
const generativeModel = vertexaiNoLocation.getGenerativeModel({
35+
model: 'gemini-pro',
36+
});
37+
const generativeModelPreview =
38+
vertexaiNoLocation.preview.getGenerativeModel({
39+
model: 'gemini-pro',
40+
});
41+
expect(vertexaiNoLocation).toBeInstanceOf(VertexAI);
42+
expect(generativeModel).toBeInstanceOf(GenerativeModel);
43+
expect(generativeModelPreview).toBeInstanceOf(GenerativeModelPreview);
44+
});
45+
46+
it('location in run time env CLOUD_ML_REGION, should instantiate VertexAI and VertexAIPreview', () => {
47+
process.env['CLOUD_ML_REGION'] = 'us-central1';
48+
const vertexaiNoLocation = new VertexAI({project: PROJECT});
49+
const generativeModel = vertexaiNoLocation.getGenerativeModel({
50+
model: 'gemini-pro',
51+
});
52+
const generativeModelPreview =
53+
vertexaiNoLocation.preview.getGenerativeModel({
54+
model: 'gemini-pro',
55+
});
56+
expect(vertexaiNoLocation).toBeInstanceOf(VertexAI);
57+
expect(generativeModel).toBeInstanceOf(GenerativeModel);
58+
expect(generativeModelPreview).toBeInstanceOf(GenerativeModelPreview);
59+
});
60+
1761
it('given undefined google auth options, should be instantiated', () => {
1862
expect(vertexai).toBeInstanceOf(VertexAI);
1963
});

0 commit comments

Comments
 (0)