@@ -96,9 +96,9 @@ export abstract class GoogleConnection<
96
96
data : unknown | undefined ,
97
97
_options : CallOptions ,
98
98
requestHeaders : Record < string , string > = { } ,
99
- callMethod ? : string
99
+ callMethod : string | undefined = undefined
100
100
) : Promise < GoogleAbstractedClientOps > {
101
- const url = await this . buildUrl ( ) ;
101
+ const url = await this . buildUrl ( callMethod ) ;
102
102
const method = this . buildMethod ( ) ;
103
103
const infoHeaders = ( await this . _clientInfoHeaders ( ) ) ?? { } ;
104
104
const additionalHeaders = ( await this . additionalHeaders ( ) ) ?? { } ;
@@ -127,17 +127,25 @@ export abstract class GoogleConnection<
127
127
async _request (
128
128
data : unknown | undefined ,
129
129
options : CallOptions ,
130
- requestHeaders : Record < string , string > = { } ,
131
- callMethod ?: string
130
+ requestHeaders : Record < string , string > = { }
132
131
) : Promise < ResponseType > {
133
- const opts = await this . _buildOpts ( data , options , requestHeaders , callMethod ) ;
132
+ const opts = await this . _buildOpts ( data , options , requestHeaders ) ;
134
133
const callResponse = await this . caller . callWithOptions (
135
134
{ signal : options ?. signal } ,
136
135
async ( ) => this . client . request ( opts )
137
136
) ;
138
137
const response : unknown = callResponse ; // Done for typecast safety, I guess
139
138
return < ResponseType > response ;
140
139
}
140
+
141
+ async _requestCountTokens ( data : unknown | undefined ) : Promise < number > {
142
+ const opts = await this . _buildOpts ( data , { } as CallOptions , { } , 'countTokens' ) ;
143
+ const { totalTokens } = await this . caller . callWithOptions (
144
+ { } ,
145
+ async ( ) => this . client . request ( opts )
146
+ ) as { totalTokens : number } ;
147
+ return totalTokens ;
148
+ }
141
149
}
142
150
143
151
export abstract class GoogleHostConnection <
@@ -334,11 +342,11 @@ export abstract class GoogleAIConnection<
334
342
return url ;
335
343
}
336
344
337
- async buildUrlVertex ( ) : Promise < string > {
345
+ async buildUrlVertex ( callMethod ?: string ) : Promise < string > {
338
346
if ( this . isApiKey ) {
339
- return this . buildUrlVertexExpress ( ) ;
347
+ return this . buildUrlVertexExpress ( callMethod ) ;
340
348
} else {
341
- return this . buildUrlVertexLocation ( ) ;
349
+ return this . buildUrlVertexLocation ( callMethod ) ;
342
350
}
343
351
}
344
352
@@ -356,6 +364,14 @@ export abstract class GoogleAIConnection<
356
364
parameters : GoogleAIModelRequestParams
357
365
) : Promise < unknown > ;
358
366
367
+ async requestCountTokens (
368
+ input : InputType ,
369
+ parameters : GoogleAIModelRequestParams
370
+ ) : Promise < number > {
371
+ const data = await this . formatData ( input , parameters ) ;
372
+ return await this . _requestCountTokens ( data ) ;
373
+ }
374
+
359
375
async request (
360
376
input : InputType ,
361
377
parameters : GoogleAIModelRequestParams ,
@@ -428,12 +444,6 @@ export abstract class AbstractGoogleLLMConnection<
428
444
) : Promise < unknown > {
429
445
return this . api . formatData ( input , parameters ) ;
430
446
}
431
-
432
- async getNumTokens ( input : MessageType ) {
433
- const data = this . formatData ( input )
434
- const { totalTokens } = await this . _request ( data , { } , { } , 'countTokens' ) ;
435
- return totalTokens
436
- }
437
447
}
438
448
439
449
export interface GoogleCustomEventInfo {
0 commit comments