18
18
/* tslint:disable */
19
19
import { GoogleAuth } from 'google-auth-library' ;
20
20
21
- import { processNonStream , processStream } from './process_stream' ;
21
+ import {
22
+ processCountTokenResponse ,
23
+ processNonStream ,
24
+ processStream ,
25
+ } from './process_stream' ;
22
26
import {
23
27
Content ,
24
28
CountTokensRequest ,
@@ -32,7 +36,11 @@ import {
32
36
StreamGenerateContentResult ,
33
37
VertexInit ,
34
38
} from './types/content' ;
35
- import { GoogleAuthError } from './types/errors' ;
39
+ import {
40
+ ClientError ,
41
+ GoogleAuthError ,
42
+ GoogleGenerativeAIError ,
43
+ } from './types/errors' ;
36
44
import { constants , postRequest } from './util' ;
37
45
export * from './types' ;
38
46
@@ -101,7 +109,7 @@ export class VertexAI_Preview {
101
109
\n -`auth.authenticate_user()`\
102
110
\n- if in service account or other: please follow guidance in https://cloud.google.com/docs/authentication' ;
103
111
const tokenPromise = this . googleAuth . getAccessToken ( ) . catch ( e => {
104
- throw new GoogleAuthError ( ` ${ credential_error_message } \n ${ e } ` ) ;
112
+ throw new GoogleAuthError ( credential_error_message , e ) ;
105
113
} ) ;
106
114
return tokenPromise ;
107
115
}
@@ -194,10 +202,13 @@ export class ChatSession {
194
202
generation_config : this . generation_config ,
195
203
} ;
196
204
197
- const generateContentResult = await this . _model_instance . generateContent (
198
- generateContentrequest
199
- ) ;
200
- const generateContentResponse = await generateContentResult . response ;
205
+ const generateContentResult : GenerateContentResult =
206
+ await this . _model_instance
207
+ . generateContent ( generateContentrequest )
208
+ . catch ( e => {
209
+ throw e ;
210
+ } ) ;
211
+ const generateContentResponse = generateContentResult . response ;
201
212
// Only push the latest message to history if the response returned a result
202
213
if ( generateContentResponse . candidates . length !== 0 ) {
203
214
this . historyInternal . push ( newContent ) ;
@@ -253,13 +264,18 @@ export class ChatSession {
253
264
generation_config : this . generation_config ,
254
265
} ;
255
266
256
- const streamGenerateContentResultPromise =
257
- this . _model_instance . generateContentStream ( generateContentrequest ) ;
267
+ const streamGenerateContentResultPromise = this . _model_instance
268
+ . generateContentStream ( generateContentrequest )
269
+ . catch ( e => {
270
+ throw e ;
271
+ } ) ;
258
272
259
273
this . _send_stream_promise = this . appendHistory (
260
274
streamGenerateContentResultPromise ,
261
275
newContent
262
- ) ;
276
+ ) . catch ( e => {
277
+ throw new GoogleGenerativeAIError ( 'exception appending chat history' , e ) ;
278
+ } ) ;
263
279
return streamGenerateContentResultPromise ;
264
280
}
265
281
}
@@ -320,7 +336,9 @@ export class GenerativeModel {
320
336
321
337
if ( ! this . _use_non_stream ) {
322
338
const streamGenerateContentResult : StreamGenerateContentResult =
323
- await this . generateContentStream ( request ) ;
339
+ await this . generateContentStream ( request ) . catch ( e => {
340
+ throw e ;
341
+ } ) ;
324
342
const result : GenerateContentResult = {
325
343
response : await streamGenerateContentResult . response ,
326
344
} ;
@@ -333,27 +351,18 @@ export class GenerativeModel {
333
351
safety_settings : request . safety_settings ?? this . safety_settings ,
334
352
} ;
335
353
336
- let response ;
337
- try {
338
- response = await postRequest ( {
339
- region : this . _vertex_instance . location ,
340
- project : this . _vertex_instance . project ,
341
- resourcePath : this . publisherModelEndpoint ,
342
- resourceMethod : constants . GENERATE_CONTENT_METHOD ,
343
- token : await this . _vertex_instance . token ,
344
- data : generateContentRequest ,
345
- apiEndpoint : this . _vertex_instance . apiEndpoint ,
346
- } ) ;
347
- if ( response === undefined ) {
348
- throw new Error ( 'did not get a valid response.' ) ;
349
- }
350
- if ( ! response . ok ) {
351
- throw new Error ( `${ response . status } ${ response . statusText } ` ) ;
352
- }
353
- } catch ( e ) {
354
- console . log ( e ) ;
355
- }
356
-
354
+ const response : Response | undefined = await postRequest ( {
355
+ region : this . _vertex_instance . location ,
356
+ project : this . _vertex_instance . project ,
357
+ resourcePath : this . publisherModelEndpoint ,
358
+ resourceMethod : constants . GENERATE_CONTENT_METHOD ,
359
+ token : await this . _vertex_instance . token ,
360
+ data : generateContentRequest ,
361
+ apiEndpoint : this . _vertex_instance . apiEndpoint ,
362
+ } ) . catch ( e => {
363
+ throw new GoogleGenerativeAIError ( 'exception posting request' , e ) ;
364
+ } ) ;
365
+ throwErrorIfNotOK ( response ) ;
357
366
const result : GenerateContentResult = processNonStream ( response ) ;
358
367
return Promise . resolve ( result ) ;
359
368
}
@@ -379,27 +388,18 @@ export class GenerativeModel {
379
388
generation_config : request . generation_config ?? this . generation_config ,
380
389
safety_settings : request . safety_settings ?? this . safety_settings ,
381
390
} ;
382
- let response ;
383
- try {
384
- response = await postRequest ( {
385
- region : this . _vertex_instance . location ,
386
- project : this . _vertex_instance . project ,
387
- resourcePath : this . publisherModelEndpoint ,
388
- resourceMethod : constants . STREAMING_GENERATE_CONTENT_METHOD ,
389
- token : await this . _vertex_instance . token ,
390
- data : generateContentRequest ,
391
- apiEndpoint : this . _vertex_instance . apiEndpoint ,
392
- } ) ;
393
- if ( response === undefined ) {
394
- throw new Error ( 'did not get a valid response.' ) ;
395
- }
396
- if ( ! response . ok ) {
397
- throw new Error ( `${ response . status } ${ response . statusText } ` ) ;
398
- }
399
- } catch ( e ) {
400
- console . log ( e ) ;
401
- }
402
-
391
+ const response = await postRequest ( {
392
+ region : this . _vertex_instance . location ,
393
+ project : this . _vertex_instance . project ,
394
+ resourcePath : this . publisherModelEndpoint ,
395
+ resourceMethod : constants . STREAMING_GENERATE_CONTENT_METHOD ,
396
+ token : await this . _vertex_instance . token ,
397
+ data : generateContentRequest ,
398
+ apiEndpoint : this . _vertex_instance . apiEndpoint ,
399
+ } ) . catch ( e => {
400
+ throw new GoogleGenerativeAIError ( 'exception posting request' , e ) ;
401
+ } ) ;
402
+ throwErrorIfNotOK ( response ) ;
403
403
const streamResult = processStream ( response ) ;
404
404
return Promise . resolve ( streamResult ) ;
405
405
}
@@ -410,32 +410,19 @@ export class GenerativeModel {
410
410
* @return The CountTokensResponse object with the token count.
411
411
*/
412
412
async countTokens ( request : CountTokensRequest ) : Promise < CountTokensResponse > {
413
- let response ;
414
- try {
415
- response = await postRequest ( {
416
- region : this . _vertex_instance . location ,
417
- project : this . _vertex_instance . project ,
418
- resourcePath : this . publisherModelEndpoint ,
419
- resourceMethod : 'countTokens' ,
420
- token : await this . _vertex_instance . token ,
421
- data : request ,
422
- apiEndpoint : this . _vertex_instance . apiEndpoint ,
423
- } ) ;
424
- if ( response === undefined ) {
425
- throw new Error ( 'did not get a valid response.' ) ;
426
- }
427
- if ( ! response . ok ) {
428
- throw new Error ( `${ response . status } ${ response . statusText } ` ) ;
429
- }
430
- } catch ( e ) {
431
- console . log ( e ) ;
432
- }
433
- if ( response ) {
434
- const responseJson = await response . json ( ) ;
435
- return responseJson as CountTokensResponse ;
436
- } else {
437
- throw new Error ( 'did not get a valid response.' ) ;
438
- }
413
+ const response = await postRequest ( {
414
+ region : this . _vertex_instance . location ,
415
+ project : this . _vertex_instance . project ,
416
+ resourcePath : this . publisherModelEndpoint ,
417
+ resourceMethod : 'countTokens' ,
418
+ token : await this . _vertex_instance . token ,
419
+ data : request ,
420
+ apiEndpoint : this . _vertex_instance . apiEndpoint ,
421
+ } ) . catch ( e => {
422
+ throw new GoogleGenerativeAIError ( 'exception posting request' , e ) ;
423
+ } ) ;
424
+ throwErrorIfNotOK ( response ) ;
425
+ return processCountTokenResponse ( response ) ;
439
426
}
440
427
441
428
/**
@@ -481,6 +468,21 @@ function formulateNewContent(request: string | Array<string | Part>): Content {
481
468
return newContent ;
482
469
}
483
470
471
+ function throwErrorIfNotOK ( response : Response | undefined ) {
472
+ if ( response === undefined ) {
473
+ throw new GoogleGenerativeAIError ( 'response is undefined' ) ;
474
+ }
475
+ const status : number = response . status ;
476
+ const statusText : string = response . statusText ;
477
+ const errorMessage = `got status: ${ status } ${ statusText } ` ;
478
+ if ( status >= 400 && status < 500 ) {
479
+ throw new ClientError ( errorMessage ) ;
480
+ }
481
+ if ( ! response . ok ) {
482
+ throw new GoogleGenerativeAIError ( errorMessage ) ;
483
+ }
484
+ }
485
+
484
486
function validateGcsInput ( contents : Content [ ] ) {
485
487
for ( const content of contents ) {
486
488
for ( const part of content . parts ) {
0 commit comments