@@ -101,43 +101,27 @@ export class VertexAI_Preview {
101
101
this . googleAuth = new GoogleAuth ( opts ) ;
102
102
}
103
103
104
- /**
105
- * Get access token from GoogleAuth. Throws GoogleAuthError when fails.
106
- * @return {Promise<any> } Promise of token
107
- */
108
- get token ( ) : Promise < any > {
109
- const credential_error_message =
110
- '\nUnable to authenticate your request\
111
- \nDepending on your run time environment, you can get authentication by\
112
- \n- if in local instance or cloud shell: `!gcloud auth login`\
113
- \n- if in Colab:\
114
- \n -`from google.colab import auth`\
115
- \n -`auth.authenticate_user()`\
116
- \n- if in service account or other: please follow guidance in https://cloud.google.com/docs/authentication' ;
117
- const tokenPromise = this . googleAuth . getAccessToken ( ) . catch ( e => {
118
- throw new GoogleAuthError ( credential_error_message , e ) ;
119
- } ) ;
120
- return tokenPromise ;
121
- }
122
-
123
104
/**
124
105
* @param {ModelParams } modelParams - {@link ModelParams} Parameters to specify the generative model.
125
106
* @return {GenerativeModel } Instance of the GenerativeModel class. {@link GenerativeModel}
126
107
*/
127
108
getGenerativeModel ( modelParams : ModelParams ) : GenerativeModel {
109
+ const getGenerativeModelParams : GetGenerativeModelParams = {
110
+ model : modelParams . model ,
111
+ project : this . project ,
112
+ location : this . location ,
113
+ googleAuth : this . googleAuth ,
114
+ apiEndpoint : this . apiEndpoint ,
115
+ safety_settings : modelParams . safety_settings ,
116
+ tools : modelParams . tools ,
117
+ } ;
128
118
if ( modelParams . generation_config ) {
129
- modelParams . generation_config = validateGenerationConfig (
119
+ getGenerativeModelParams . generation_config = validateGenerationConfig (
130
120
modelParams . generation_config
131
121
) ;
132
122
}
133
123
134
- return new GenerativeModel (
135
- this ,
136
- modelParams . model ,
137
- modelParams . generation_config ,
138
- modelParams . safety_settings ,
139
- modelParams . tools
140
- ) ;
124
+ return new GenerativeModel ( getGenerativeModelParams ) ;
141
125
}
142
126
143
127
validateGoogleAuthOptions (
@@ -200,10 +184,36 @@ export declare interface StartChatParams {
200
184
* @property {GenerativeModel } - _model_instance {@link GenerativeModel}
201
185
*/
202
186
export declare interface StartChatSessionRequest extends StartChatParams {
203
- _vertex_instance : VertexAI_Preview ;
187
+ project : string ;
188
+ location : string ;
204
189
_model_instance : GenerativeModel ;
205
190
}
206
191
192
+ /**
193
+ * @property {string } model - model name
194
+ * @property {string } project - project The Google Cloud project to use for the request
195
+ * @property {string } location - The Google Cloud project location to use for the request
196
+ * @property {GoogleAuth } googleAuth - GoogleAuth class instance that handles authentication.
197
+ * Details about GoogleAuth is referred to https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts
198
+ * @property {string } - [apiEndpoint] The base Vertex AI endpoint to use for the request. If
199
+ * not provided, the default regionalized endpoint
200
+ * (i.e. us-central1-aiplatform.googleapis.com) will be used.
201
+ * @property {GenerationConfig } [generation_config] - {@link
202
+ * GenerationConfig}
203
+ * @property {SafetySetting[] } [safety_settings] - {@link SafetySetting}
204
+ * @property {Tool[] } [tools] - {@link Tool}
205
+ */
206
+ export declare interface GetGenerativeModelParams extends ModelParams {
207
+ model : string ;
208
+ project : string ;
209
+ location : string ;
210
+ googleAuth : GoogleAuth ;
211
+ apiEndpoint ?: string ;
212
+ generation_config ?: GenerationConfig ;
213
+ safety_settings ?: SafetySetting [ ] ;
214
+ tools ?: Tool [ ] ;
215
+ }
216
+
207
217
/**
208
218
* Chat session to make multi-turn send message request.
209
219
* `sendMessage` method makes async call to get response of a chat message.
@@ -214,7 +224,6 @@ export class ChatSession {
214
224
private location : string ;
215
225
216
226
private historyInternal : Content [ ] ;
217
- private _vertex_instance : VertexAI_Preview ;
218
227
private _model_instance : GenerativeModel ;
219
228
private _send_stream_promise : Promise < void > = Promise . resolve ( ) ;
220
229
generation_config ?: GenerationConfig ;
@@ -230,11 +239,10 @@ export class ChatSession {
230
239
* @param {StartChatSessionRequest } request - {@link StartChatSessionRequest}
231
240
*/
232
241
constructor ( request : StartChatSessionRequest ) {
233
- this . project = request . _vertex_instance . project ;
234
- this . location = request . _vertex_instance . location ;
242
+ this . project = request . project ;
243
+ this . location = request . location ;
235
244
this . _model_instance = request . _model_instance ;
236
245
this . historyInternal = request . history ?? [ ] ;
237
- this . _vertex_instance = request . _vertex_instance ;
238
246
this . generation_config = request . generation_config ;
239
247
this . safety_settings = request . safety_settings ;
240
248
this . tools = request . tools ;
@@ -347,36 +355,51 @@ export class GenerativeModel {
347
355
generation_config ?: GenerationConfig ;
348
356
safety_settings ?: SafetySetting [ ] ;
349
357
tools ?: Tool [ ] ;
350
- private _vertex_instance : VertexAI_Preview ;
358
+ private project : string ;
359
+ private location : string ;
360
+ private googleAuth : GoogleAuth ;
351
361
private publisherModelEndpoint : string ;
362
+ private apiEndpoint ?: string ;
352
363
353
364
/**
354
365
* @constructor
355
- * @param {VertexAI_Preview } vertex_instance - {@link VertexAI_Preview}
356
- * @param {string } model - model name
357
- * @param {GenerationConfig } generation_config - Optional. {@link
358
- * GenerationConfig}
359
- * @param {SafetySetting[] } safety_settings - Optional. {@link SafetySetting}
366
+ * @param {GetGenerativeModelParams } getGenerativeModelParams - {@link GetGenerativeModelParams}
360
367
*/
361
- constructor (
362
- vertex_instance : VertexAI_Preview ,
363
- model : string ,
364
- generation_config ?: GenerationConfig ,
365
- safety_settings ?: SafetySetting [ ] ,
366
- tools ?: Tool [ ]
367
- ) {
368
- this . _vertex_instance = vertex_instance ;
369
- this . model = model ;
370
- this . generation_config = generation_config ;
371
- this . safety_settings = safety_settings ;
372
- this . tools = tools ;
373
- if ( model . startsWith ( 'models/' ) ) {
368
+ constructor ( getGenerativeModelParams : GetGenerativeModelParams ) {
369
+ this . project = getGenerativeModelParams . project ;
370
+ this . location = getGenerativeModelParams . location ;
371
+ this . apiEndpoint = getGenerativeModelParams . apiEndpoint ;
372
+ this . googleAuth = getGenerativeModelParams . googleAuth ;
373
+ this . model = getGenerativeModelParams . model ;
374
+ this . generation_config = getGenerativeModelParams . generation_config ;
375
+ this . safety_settings = getGenerativeModelParams . safety_settings ;
376
+ this . tools = getGenerativeModelParams . tools ;
377
+ if ( this . model . startsWith ( 'models/' ) ) {
374
378
this . publisherModelEndpoint = `publishers/google/${ this . model } ` ;
375
379
} else {
376
380
this . publisherModelEndpoint = `publishers/google/models/${ this . model } ` ;
377
381
}
378
382
}
379
383
384
+ /**
385
+ * Get access token from GoogleAuth. Throws GoogleAuthError when fails.
386
+ * @return {Promise<any> } Promise of token
387
+ */
388
+ get token ( ) : Promise < any > {
389
+ const credential_error_message =
390
+ '\nUnable to authenticate your request\
391
+ \nDepending on your run time environment, you can get authentication by\
392
+ \n- if in local instance or cloud shell: `!gcloud auth login`\
393
+ \n- if in Colab:\
394
+ \n -`from google.colab import auth`\
395
+ \n -`auth.authenticate_user()`\
396
+ \n- if in service account or other: please follow guidance in https://cloud.google.com/docs/authentication' ;
397
+ const tokenPromise = this . googleAuth . getAccessToken ( ) . catch ( e => {
398
+ throw new GoogleAuthError ( credential_error_message , e ) ;
399
+ } ) ;
400
+ return tokenPromise ;
401
+ }
402
+
380
403
/**
381
404
* Make a async call to generate content.
382
405
* @param request A GenerateContentRequest object with the request contents.
@@ -407,13 +430,13 @@ export class GenerativeModel {
407
430
} ;
408
431
409
432
const response : Response | undefined = await postRequest ( {
410
- region : this . _vertex_instance . location ,
411
- project : this . _vertex_instance . project ,
433
+ region : this . location ,
434
+ project : this . project ,
412
435
resourcePath : this . publisherModelEndpoint ,
413
436
resourceMethod : constants . GENERATE_CONTENT_METHOD ,
414
- token : await this . _vertex_instance . token ,
437
+ token : await this . token ,
415
438
data : generateContentRequest ,
416
- apiEndpoint : this . _vertex_instance . apiEndpoint ,
439
+ apiEndpoint : this . apiEndpoint ,
417
440
} ) . catch ( e => {
418
441
throw new GoogleGenerativeAIError ( 'exception posting request' , e ) ;
419
442
} ) ;
@@ -450,13 +473,13 @@ export class GenerativeModel {
450
473
tools : request . tools ?? [ ] ,
451
474
} ;
452
475
const response = await postRequest ( {
453
- region : this . _vertex_instance . location ,
454
- project : this . _vertex_instance . project ,
476
+ region : this . location ,
477
+ project : this . project ,
455
478
resourcePath : this . publisherModelEndpoint ,
456
479
resourceMethod : constants . STREAMING_GENERATE_CONTENT_METHOD ,
457
- token : await this . _vertex_instance . token ,
480
+ token : await this . token ,
458
481
data : generateContentRequest ,
459
- apiEndpoint : this . _vertex_instance . apiEndpoint ,
482
+ apiEndpoint : this . apiEndpoint ,
460
483
} ) . catch ( e => {
461
484
throw new GoogleGenerativeAIError ( 'exception posting request' , e ) ;
462
485
} ) ;
@@ -472,13 +495,13 @@ export class GenerativeModel {
472
495
*/
473
496
async countTokens ( request : CountTokensRequest ) : Promise < CountTokensResponse > {
474
497
const response = await postRequest ( {
475
- region : this . _vertex_instance . location ,
476
- project : this . _vertex_instance . project ,
498
+ region : this . location ,
499
+ project : this . project ,
477
500
resourcePath : this . publisherModelEndpoint ,
478
501
resourceMethod : 'countTokens' ,
479
- token : await this . _vertex_instance . token ,
502
+ token : await this . token ,
480
503
data : request ,
481
- apiEndpoint : this . _vertex_instance . apiEndpoint ,
504
+ apiEndpoint : this . apiEndpoint ,
482
505
} ) . catch ( e => {
483
506
throw new GoogleGenerativeAIError ( 'exception posting request' , e ) ;
484
507
} ) ;
@@ -495,7 +518,8 @@ export class GenerativeModel {
495
518
*/
496
519
startChat ( request ?: StartChatParams ) : ChatSession {
497
520
const startChatRequest : StartChatSessionRequest = {
498
- _vertex_instance : this . _vertex_instance ,
521
+ project : this . project ,
522
+ location : this . location ,
499
523
_model_instance : this ,
500
524
} ;
501
525
0 commit comments