1
1
import { getEnvironmentVariable } from "@langchain/core/utils/env" ;
2
- import { type BaseMessage } from "@langchain/core/messages" ;
2
+ import { UsageMetadata , type BaseMessage } from "@langchain/core/messages" ;
3
3
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager" ;
4
4
5
5
import {
@@ -150,7 +150,8 @@ export interface ChatGoogleBaseInput<AuthOptions>
150
150
extends BaseChatModelParams ,
151
151
GoogleConnectionParams < AuthOptions > ,
152
152
GoogleAIModelParams ,
153
- GoogleAISafetyParams { }
153
+ GoogleAISafetyParams ,
154
+ Pick < GoogleAIBaseLanguageModelCallOptions , "streamUsage" > { }
154
155
155
156
function convertToGeminiTools (
156
157
structuredTools : ( StructuredToolInterface | Record < string , unknown > ) [ ]
@@ -216,6 +217,8 @@ export abstract class ChatGoogleBase<AuthOptions>
216
217
217
218
safetyHandler : GoogleAISafetyHandler ;
218
219
220
+ streamUsage = true ;
221
+
219
222
protected connection : ChatConnection < AuthOptions > ;
220
223
221
224
protected streamedConnection : ChatConnection < AuthOptions > ;
@@ -226,7 +229,7 @@ export abstract class ChatGoogleBase<AuthOptions>
226
229
copyAndValidateModelParamsInto ( fields , this ) ;
227
230
this . safetyHandler =
228
231
fields ?. safetyHandler ?? new DefaultGeminiSafetyHandler ( ) ;
229
-
232
+ this . streamUsage = fields ?. streamUsage ?? this . streamUsage ;
230
233
const client = this . buildClient ( fields ) ;
231
234
this . buildConnection ( fields ?? { } , client ) ;
232
235
}
@@ -342,12 +345,24 @@ export abstract class ChatGoogleBase<AuthOptions>
342
345
343
346
// Get the streaming parser of the response
344
347
const stream = response . data as JsonStream ;
345
-
348
+ let usageMetadata : UsageMetadata | undefined ;
346
349
// Loop until the end of the stream
347
350
// During the loop, yield each time we get a chunk from the streaming parser
348
351
// that is either available or added to the queue
349
352
while ( ! stream . streamDone ) {
350
353
const output = await stream . nextChunk ( ) ;
354
+ if (
355
+ output &&
356
+ output . usageMetadata &&
357
+ this . streamUsage !== false &&
358
+ options . streamUsage !== false
359
+ ) {
360
+ usageMetadata = {
361
+ input_tokens : output . usageMetadata . promptTokenCount ,
362
+ output_tokens : output . usageMetadata . candidatesTokenCount ,
363
+ total_tokens : output . usageMetadata . totalTokenCount ,
364
+ } ;
365
+ }
351
366
const chunk =
352
367
output !== null
353
368
? safeResponseToChatGeneration ( { data : output } , this . safetyHandler )
@@ -356,6 +371,7 @@ export abstract class ChatGoogleBase<AuthOptions>
356
371
generationInfo : { finishReason : "stop" } ,
357
372
message : new AIMessageChunk ( {
358
373
content : "" ,
374
+ usage_metadata : usageMetadata ,
359
375
} ) ,
360
376
} ) ;
361
377
yield chunk ;
0 commit comments