@librechat/agents 2.4.42 → 2.4.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/cjs/common/enum.cjs +4 -2
  2. package/dist/cjs/common/enum.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +2 -2
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/llm/google/index.cjs +73 -1
  6. package/dist/cjs/llm/google/index.cjs.map +1 -1
  7. package/dist/cjs/llm/google/utils/common.cjs +469 -0
  8. package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
  9. package/dist/cjs/llm/providers.cjs +3 -3
  10. package/dist/cjs/llm/providers.cjs.map +1 -1
  11. package/dist/cjs/llm/vertexai/index.cjs +330 -0
  12. package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
  13. package/dist/cjs/stream.cjs +5 -2
  14. package/dist/cjs/stream.cjs.map +1 -1
  15. package/dist/esm/common/enum.mjs +4 -2
  16. package/dist/esm/common/enum.mjs.map +1 -1
  17. package/dist/esm/graphs/Graph.mjs +2 -2
  18. package/dist/esm/graphs/Graph.mjs.map +1 -1
  19. package/dist/esm/llm/google/index.mjs +73 -1
  20. package/dist/esm/llm/google/index.mjs.map +1 -1
  21. package/dist/esm/llm/google/utils/common.mjs +463 -0
  22. package/dist/esm/llm/google/utils/common.mjs.map +1 -0
  23. package/dist/esm/llm/providers.mjs +2 -2
  24. package/dist/esm/llm/providers.mjs.map +1 -1
  25. package/dist/esm/llm/vertexai/index.mjs +328 -0
  26. package/dist/esm/llm/vertexai/index.mjs.map +1 -0
  27. package/dist/esm/stream.mjs +5 -2
  28. package/dist/esm/stream.mjs.map +1 -1
  29. package/dist/types/common/enum.d.ts +5 -3
  30. package/dist/types/llm/google/index.d.ts +10 -5
  31. package/dist/types/llm/google/types.d.ts +32 -0
  32. package/dist/types/llm/google/utils/common.d.ts +19 -0
  33. package/dist/types/llm/google/utils/tools.d.ts +10 -0
  34. package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
  35. package/dist/types/llm/vertexai/index.d.ts +293 -0
  36. package/dist/types/types/llm.d.ts +7 -3
  37. package/dist/types/types/stream.d.ts +5 -0
  38. package/package.json +1 -1
  39. package/src/common/enum.ts +4 -2
  40. package/src/graphs/Graph.ts +10 -6
  41. package/src/llm/google/index.ts +118 -8
  42. package/src/llm/google/types.ts +43 -0
  43. package/src/llm/google/utils/common.ts +632 -0
  44. package/src/llm/google/utils/tools.ts +160 -0
  45. package/src/llm/google/utils/zod_to_genai_parameters.ts +88 -0
  46. package/src/llm/providers.ts +8 -7
  47. package/src/llm/vertexai/index.ts +360 -0
  48. package/src/stream.ts +5 -2
  49. package/src/types/llm.ts +9 -5
  50. package/src/types/stream.ts +6 -0
  51. package/src/utils/llmConfig.ts +2 -2
@@ -0,0 +1,293 @@
1
+ import { ChatGoogle } from '@langchain/google-gauth';
2
+ import type { GoogleAIModelRequestParams, GoogleAbstractedClient } from '@langchain/google-common';
3
+ import type { VertexAIClientOptions } from '@/types';
4
+ /**
5
+ * Integration with Google Vertex AI chat models.
6
+ *
7
+ * Setup:
8
+ * Install `@langchain/google-vertexai` and set your stringified
9
+ * Vertex AI credentials as an environment variable named `GOOGLE_APPLICATION_CREDENTIALS`.
10
+ *
11
+ * ```bash
12
+ * npm install @langchain/google-vertexai
13
+ * export GOOGLE_APPLICATION_CREDENTIALS="path/to/credentials"
14
+ * ```
15
+ *
16
+ * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_google_vertexai.index.ChatVertexAI.html#constructor.new_ChatVertexAI)
17
+ *
18
+ * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_common_types.GoogleAIBaseLanguageModelCallOptions.html)
19
+ *
20
+ * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
21
+ * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
22
+ *
23
+ * ```typescript
24
+ * // When calling `.withConfig`, call options should be passed via the first argument
25
+ * const llmWithArgsBound = llm.withConfig({
26
+ * stop: ["\n"],
27
+ * tools: [...],
28
+ * });
29
+ *
30
+ * // When calling `.bindTools`, call options should be passed via the second argument
31
+ * const llmWithTools = llm.bindTools(
32
+ * [...],
33
+ * {
34
+ * tool_choice: "auto",
35
+ * }
36
+ * );
37
+ * ```
38
+ *
39
+ * ## Examples
40
+ *
41
+ * <details open>
42
+ * <summary><strong>Instantiate</strong></summary>
43
+ *
44
+ * ```typescript
45
+ * import { ChatVertexAI } from '@langchain/google-vertexai';
46
+ *
47
+ * const llm = new ChatVertexAI({
48
+ * model: "gemini-1.5-pro",
49
+ * temperature: 0,
50
+ * // other params...
51
+ * });
52
+ * ```
53
+ * </details>
54
+ *
55
+ * <br />
56
+ *
57
+ * <details>
58
+ * <summary><strong>Invoking</strong></summary>
59
+ *
60
+ * ```typescript
61
+ * const input = `Translate "I love programming" into French.`;
62
+ *
63
+ * // Models also accept a list of chat messages or a formatted prompt
64
+ * const result = await llm.invoke(input);
65
+ * console.log(result);
66
+ * ```
67
+ *
68
+ * ```txt
69
+ * AIMessageChunk {
70
+ * "content": "\"J'adore programmer\" \n\nHere's why this is the best translation:\n\n* **J'adore** means \"I love\" and conveys a strong passion.\n* **Programmer** is the French verb for \"to program.\"\n\nThis translation is natural and idiomatic in French. \n",
71
+ * "additional_kwargs": {},
72
+ * "response_metadata": {},
73
+ * "tool_calls": [],
74
+ * "tool_call_chunks": [],
75
+ * "invalid_tool_calls": [],
76
+ * "usage_metadata": {
77
+ * "input_tokens": 9,
78
+ * "output_tokens": 63,
79
+ * "total_tokens": 72
80
+ * }
81
+ * }
82
+ * ```
83
+ * </details>
84
+ *
85
+ * <br />
86
+ *
87
+ * <details>
88
+ * <summary><strong>Streaming Chunks</strong></summary>
89
+ *
90
+ * ```typescript
91
+ * for await (const chunk of await llm.stream(input)) {
92
+ * console.log(chunk);
93
+ * }
94
+ * ```
95
+ *
96
+ * ```txt
97
+ * AIMessageChunk {
98
+ * "content": "\"",
99
+ * "additional_kwargs": {},
100
+ * "response_metadata": {},
101
+ * "tool_calls": [],
102
+ * "tool_call_chunks": [],
103
+ * "invalid_tool_calls": []
104
+ * }
105
+ * AIMessageChunk {
106
+ * "content": "J'adore programmer\" \n",
107
+ * "additional_kwargs": {},
108
+ * "response_metadata": {},
109
+ * "tool_calls": [],
110
+ * "tool_call_chunks": [],
111
+ * "invalid_tool_calls": []
112
+ * }
113
+ * AIMessageChunk {
114
+ * "content": "",
115
+ * "additional_kwargs": {},
116
+ * "response_metadata": {},
117
+ * "tool_calls": [],
118
+ * "tool_call_chunks": [],
119
+ * "invalid_tool_calls": []
120
+ * }
121
+ * AIMessageChunk {
122
+ * "content": "",
123
+ * "additional_kwargs": {},
124
+ * "response_metadata": {
125
+ * "finishReason": "stop"
126
+ * },
127
+ * "tool_calls": [],
128
+ * "tool_call_chunks": [],
129
+ * "invalid_tool_calls": [],
130
+ * "usage_metadata": {
131
+ * "input_tokens": 9,
132
+ * "output_tokens": 8,
133
+ * "total_tokens": 17
134
+ * }
135
+ * }
136
+ * ```
137
+ * </details>
138
+ *
139
+ * <br />
140
+ *
141
+ * <details>
142
+ * <summary><strong>Aggregate Streamed Chunks</strong></summary>
143
+ *
144
+ * ```typescript
145
+ * import { AIMessageChunk } from '@langchain/core/messages';
146
+ * import { concat } from '@langchain/core/utils/stream';
147
+ *
148
+ * const stream = await llm.stream(input);
149
+ * let full: AIMessageChunk | undefined;
150
+ * for await (const chunk of stream) {
151
+ * full = !full ? chunk : concat(full, chunk);
152
+ * }
153
+ * console.log(full);
154
+ * ```
155
+ *
156
+ * ```txt
157
+ * AIMessageChunk {
158
+ * "content": "\"J'adore programmer\" \n",
159
+ * "additional_kwargs": {},
160
+ * "response_metadata": {
161
+ * "finishReason": "stop"
162
+ * },
163
+ * "tool_calls": [],
164
+ * "tool_call_chunks": [],
165
+ * "invalid_tool_calls": [],
166
+ * "usage_metadata": {
167
+ * "input_tokens": 9,
168
+ * "output_tokens": 8,
169
+ * "total_tokens": 17
170
+ * }
171
+ * }
172
+ * ```
173
+ * </details>
174
+ *
175
+ * <br />
176
+ *
177
+ * <details>
178
+ * <summary><strong>Bind tools</strong></summary>
179
+ *
180
+ * ```typescript
181
+ * import { z } from 'zod';
182
+ *
183
+ * const GetWeather = {
184
+ * name: "GetWeather",
185
+ * description: "Get the current weather in a given location",
186
+ * schema: z.object({
187
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
188
+ * }),
189
+ * }
190
+ *
191
+ * const GetPopulation = {
192
+ * name: "GetPopulation",
193
+ * description: "Get the current population in a given location",
194
+ * schema: z.object({
195
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
196
+ * }),
197
+ * }
198
+ *
199
+ * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
200
+ * const aiMsg = await llmWithTools.invoke(
201
+ * "Which city is hotter today and which is bigger: LA or NY?"
202
+ * );
203
+ * console.log(aiMsg.tool_calls);
204
+ * ```
205
+ *
206
+ * ```txt
207
+ * [
208
+ * {
209
+ * name: 'GetPopulation',
210
+ * args: { location: 'New York City, NY' },
211
+ * id: '33c1c1f47e2f492799c77d2800a43912',
212
+ * type: 'tool_call'
213
+ * }
214
+ * ]
215
+ * ```
216
+ * </details>
217
+ *
218
+ * <br />
219
+ *
220
+ * <details>
221
+ * <summary><strong>Structured Output</strong></summary>
222
+ *
223
+ * ```typescript
224
+ * import { z } from 'zod';
225
+ *
226
+ * const Joke = z.object({
227
+ * setup: z.string().describe("The setup of the joke"),
228
+ * punchline: z.string().describe("The punchline to the joke"),
229
+ * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
230
+ * }).describe('Joke to tell user.');
231
+ *
232
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
233
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
234
+ * console.log(jokeResult);
235
+ * ```
236
+ *
237
+ * ```txt
238
+ * {
239
+ * setup: 'What do you call a cat that loves to bowl?',
240
+ * punchline: 'An alley cat!'
241
+ * }
242
+ * ```
243
+ * </details>
244
+ *
245
+ * <br />
246
+ *
247
+ * <details>
248
+ * <summary><strong>Usage Metadata</strong></summary>
249
+ *
250
+ * ```typescript
251
+ * const aiMsgForMetadata = await llm.invoke(input);
252
+ * console.log(aiMsgForMetadata.usage_metadata);
253
+ * ```
254
+ *
255
+ * ```txt
256
+ * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }
257
+ * ```
258
+ * </details>
259
+ *
260
+ * <br />
261
+ *
262
+ * <details>
263
+ * <summary><strong>Stream Usage Metadata</strong></summary>
264
+ *
265
+ * ```typescript
266
+ * const streamForMetadata = await llm.stream(
267
+ * input,
268
+ * {
269
+ * streamUsage: true
270
+ * }
271
+ * );
272
+ * let fullForMetadata: AIMessageChunk | undefined;
273
+ * for await (const chunk of streamForMetadata) {
274
+ * fullForMetadata = !fullForMetadata ? chunk : concat(fullForMetadata, chunk);
275
+ * }
276
+ * console.log(fullForMetadata?.usage_metadata);
277
+ * ```
278
+ *
279
+ * ```txt
280
+ * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }
281
+ * ```
282
+ * </details>
283
+ *
284
+ * <br />
285
+ */
286
+ export declare class ChatVertexAI extends ChatGoogle {
287
+ lc_namespace: string[];
288
+ dynamicThinkingBudget: boolean;
289
+ static lc_name(): 'ChatVertexAI';
290
+ constructor(fields?: VertexAIClientOptions);
291
+ invocationParams(options?: this['ParsedCallOptions'] | undefined): GoogleAIModelRequestParams;
292
+ buildConnection(fields: VertexAIClientOptions, client: GoogleAbstractedClient): void;
293
+ }
@@ -2,12 +2,12 @@ import { ChatOllama } from '@langchain/ollama';
2
2
  import { ChatAnthropic } from '@langchain/anthropic';
3
3
  import { ChatMistralAI } from '@langchain/mistralai';
4
4
  import { ChatBedrockConverse } from '@langchain/aws';
5
- import { ChatVertexAI } from '@langchain/google-vertexai';
6
5
  import { BedrockChat } from '@langchain/community/chat_models/bedrock/web';
7
6
  import type { BindToolsInput, BaseChatModelParams } from '@langchain/core/language_models/chat_models';
8
7
  import type { OpenAIChatInput, ChatOpenAIFields, AzureOpenAIInput, ClientOptions as OAIClientOptions } from '@langchain/openai';
9
8
  import type { BedrockChatFields } from '@langchain/community/chat_models/bedrock/web';
10
9
  import type { GoogleGenerativeAIChatInput } from '@langchain/google-genai';
10
+ import type { GeminiGenerationConfig } from '@langchain/google-common';
11
11
  import type { ChatVertexAIInput } from '@langchain/google-vertexai';
12
12
  import type { ChatDeepSeekCallOptions } from '@langchain/deepseek';
13
13
  import type { ChatOpenRouterCallOptions } from '@/llm/openrouter';
@@ -20,9 +20,10 @@ import type { Runnable } from '@langchain/core/runnables';
20
20
  import type { ChatOllamaInput } from '@langchain/ollama';
21
21
  import type { OpenAI as OpenAIClient } from 'openai';
22
22
  import type { ChatXAIInput } from '@langchain/xai';
23
- import { ChatXAI, ChatOpenAI, ChatDeepSeek, AzureChatOpenAI } from '@/llm/openai';
23
+ import { AzureChatOpenAI, ChatDeepSeek, ChatOpenAI, ChatXAI } from '@/llm/openai';
24
24
  import { CustomChatGoogleGenerativeAI } from '@/llm/google';
25
25
  import { ChatOpenRouter } from '@/llm/openrouter';
26
+ import { ChatVertexAI } from '@/llm/vertexai';
26
27
  import { Providers } from '@/common';
27
28
  export type AzureClientOptions = Partial<OpenAIChatInput> & Partial<AzureOpenAIInput> & {
28
29
  openAIApiKey?: string;
@@ -43,7 +44,9 @@ export type OpenAIClientOptions = ChatOpenAIFields;
43
44
  export type OllamaClientOptions = ChatOllamaInput;
44
45
  export type AnthropicClientOptions = AnthropicInput;
45
46
  export type MistralAIClientOptions = ChatMistralAIInput;
46
- export type VertexAIClientOptions = ChatVertexAIInput;
47
+ export type VertexAIClientOptions = ChatVertexAIInput & {
48
+ includeThoughts?: boolean;
49
+ };
47
50
  export type BedrockClientOptions = BedrockChatFields;
48
51
  export type BedrockAnthropicInput = ChatBedrockConverseInput & {
49
52
  additionalModelRequestFields?: ChatBedrockConverseInput['additionalModelRequestFields'] & AnthropicReasoning;
@@ -51,6 +54,7 @@ export type BedrockAnthropicInput = ChatBedrockConverseInput & {
51
54
  export type BedrockConverseClientOptions = ChatBedrockConverseInput;
52
55
  export type GoogleClientOptions = GoogleGenerativeAIChatInput & {
53
56
  customHeaders?: RequestOptions['customHeaders'];
57
+ thinkingConfig?: GeminiGenerationConfig['thinkingConfig'];
54
58
  };
55
59
  export type DeepSeekClientOptions = ChatDeepSeekCallOptions;
56
60
  export type XAIClientOptions = ChatXAIInput;
@@ -191,6 +191,11 @@ export type ReasoningContentText = {
191
191
  type: ContentTypes.THINK;
192
192
  think: string;
193
193
  };
194
+ /** Vertex AI / Google Common - Reasoning Content Block Format */
195
+ export type GoogleReasoningContentText = {
196
+ type: ContentTypes.REASONING;
197
+ reasoning: string;
198
+ };
194
199
  /** Anthropic's Reasoning Content Block Format */
195
200
  export type ThinkingContentText = {
196
201
  type: ContentTypes.THINKING;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@librechat/agents",
3
- "version": "2.4.42",
3
+ "version": "2.4.44",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
@@ -117,10 +117,12 @@ export enum ContentTypes {
117
117
  IMAGE_FILE = 'image_file',
118
118
  /** Anthropic */
119
119
  THINKING = 'thinking',
120
- /** Bedrock */
121
- REASONING_CONTENT = 'reasoning_content',
120
+ /** Vertex AI / Google Common */
121
+ REASONING = 'reasoning',
122
122
  /** Multi-Agent Switch */
123
123
  AGENT_UPDATE = 'agent_update',
124
+ /** Bedrock */
125
+ REASONING_CONTENT = 'reasoning_content',
124
126
  }
125
127
 
126
128
  export enum ToolCallTypes {
@@ -426,13 +426,17 @@ export class StandardGraph extends Graph<t.BaseGraphState, GraphNode> {
426
426
  omitOptions?: Set<string>;
427
427
  }): t.ChatModelInstance {
428
428
  const ChatModelClass = getChatModelClass(provider);
429
- const options = omitOptions
430
- ? Object.fromEntries(
431
- Object.entries(clientOptions ?? this.clientOptions).filter(
432
- ([key]) => !omitOptions.has(key)
429
+ const options =
430
+ omitOptions && clientOptions == null
431
+ ? Object.assign(
432
+ Object.fromEntries(
433
+ Object.entries(this.clientOptions).filter(
434
+ ([key]) => !omitOptions.has(key)
435
+ )
436
+ ),
437
+ clientOptions
433
438
  )
434
- )
435
- : (clientOptions ?? this.clientOptions);
439
+ : (clientOptions ?? this.clientOptions);
436
440
  return new ChatModelClass(options);
437
441
  }
438
442
 
@@ -1,15 +1,25 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
1
2
  import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
2
3
  import { getEnvironmentVariable } from '@langchain/core/utils/env';
3
4
  import { GoogleGenerativeAI as GenerativeAI } from '@google/generative-ai';
4
- import type { GoogleGenerativeAIChatInput } from '@langchain/google-genai';
5
- import type { RequestOptions, SafetySetting } from '@google/generative-ai';
5
+ import type {
6
+ GenerateContentRequest,
7
+ SafetySetting,
8
+ } from '@google/generative-ai';
9
+ import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
10
+ import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
11
+ import type { GeminiGenerationConfig } from '@langchain/google-common';
12
+ import type { ChatGenerationChunk } from '@langchain/core/outputs';
13
+ import type { GeminiApiUsageMetadata } from './types';
14
+ import type { GoogleClientOptions } from '@/types';
15
+ import {
16
+ convertResponseContentToChatGenerationChunk,
17
+ convertBaseMessagesToContent,
18
+ } from './utils/common';
6
19
 
7
20
  export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
8
- constructor(
9
- fields: GoogleGenerativeAIChatInput & {
10
- customHeaders?: RequestOptions['customHeaders'];
11
- }
12
- ) {
21
+ thinkingConfig?: GeminiGenerationConfig['thinkingConfig'];
22
+ constructor(fields: GoogleClientOptions) {
13
23
  super(fields);
14
24
 
15
25
  this.model = fields.model.replace(/^models\//, '');
@@ -66,10 +76,11 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
66
76
  }
67
77
  }
68
78
 
79
+ this.thinkingConfig = fields.thinkingConfig ?? this.thinkingConfig;
80
+
69
81
  this.streaming = fields.streaming ?? this.streaming;
70
82
  this.json = fields.json;
71
83
 
72
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
73
84
  // @ts-ignore - Accessing private property from parent class
74
85
  this.client = new GenerativeAI(this.apiKey).getGenerativeModel(
75
86
  {
@@ -94,4 +105,103 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
94
105
  );
95
106
  this.streamUsage = fields.streamUsage ?? this.streamUsage;
96
107
  }
108
+
109
+ invocationParams(
110
+ options?: this['ParsedCallOptions']
111
+ ): Omit<GenerateContentRequest, 'contents'> {
112
+ const params = super.invocationParams(options);
113
+ return {
114
+ ...params,
115
+ generationConfig: {
116
+ ...params.generationConfig,
117
+
118
+ /** @ts-ignore */
119
+ thinkingConfig: this.thinkingConfig,
120
+ },
121
+ };
122
+ }
123
+
124
+ async *_streamResponseChunks(
125
+ messages: BaseMessage[],
126
+ options: this['ParsedCallOptions'],
127
+ runManager?: CallbackManagerForLLMRun
128
+ ): AsyncGenerator<ChatGenerationChunk> {
129
+ const prompt = convertBaseMessagesToContent(
130
+ messages,
131
+ this._isMultimodalModel,
132
+ this.useSystemInstruction
133
+ );
134
+ let actualPrompt = prompt;
135
+ if (prompt[0].role === 'system') {
136
+ const [systemInstruction] = prompt;
137
+ /** @ts-ignore */
138
+ this.client.systemInstruction = systemInstruction;
139
+ actualPrompt = prompt.slice(1);
140
+ }
141
+ const parameters = this.invocationParams(options);
142
+ const request = {
143
+ ...parameters,
144
+ contents: actualPrompt,
145
+ };
146
+ const stream = await this.caller.callWithOptions(
147
+ { signal: options.signal },
148
+ async () => {
149
+ /** @ts-ignore */
150
+ const { stream } = await this.client.generateContentStream(request);
151
+ return stream;
152
+ }
153
+ );
154
+
155
+ let usageMetadata: UsageMetadata | undefined;
156
+ let index = 0;
157
+ for await (const response of stream) {
158
+ if (
159
+ 'usageMetadata' in response &&
160
+ this.streamUsage !== false &&
161
+ options.streamUsage !== false
162
+ ) {
163
+ const genAIUsageMetadata = response.usageMetadata as
164
+ | GeminiApiUsageMetadata
165
+ | undefined;
166
+ const output_tokens =
167
+ (genAIUsageMetadata?.candidatesTokenCount ?? 0) +
168
+ (genAIUsageMetadata?.thoughtsTokenCount ?? 0);
169
+ if (!usageMetadata) {
170
+ usageMetadata = {
171
+ input_tokens: genAIUsageMetadata?.promptTokenCount ?? 0,
172
+ output_tokens,
173
+ total_tokens: genAIUsageMetadata?.totalTokenCount ?? 0,
174
+ };
175
+ } else {
176
+ // Under the hood, LangChain combines the prompt tokens. Google returns the updated
177
+ // total each time, so we need to find the difference between the tokens.
178
+ const outputTokenDiff = output_tokens - usageMetadata.output_tokens;
179
+ usageMetadata = {
180
+ input_tokens: 0,
181
+ output_tokens: outputTokenDiff,
182
+ total_tokens: outputTokenDiff,
183
+ };
184
+ }
185
+ }
186
+
187
+ const chunk = convertResponseContentToChatGenerationChunk(response, {
188
+ usageMetadata,
189
+ index,
190
+ });
191
+ index += 1;
192
+ if (!chunk) {
193
+ continue;
194
+ }
195
+
196
+ yield chunk;
197
+ await runManager?.handleLLMNewToken(
198
+ chunk.text || '',
199
+ undefined,
200
+ undefined,
201
+ undefined,
202
+ undefined,
203
+ { chunk }
204
+ );
205
+ }
206
+ }
97
207
  }
@@ -0,0 +1,43 @@
1
+ import {
2
+ CodeExecutionTool,
3
+ FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,
4
+ GoogleSearchRetrievalTool,
5
+ } from '@google/generative-ai';
6
+ import { BindToolsInput } from '@langchain/core/language_models/chat_models';
7
+
8
+ export type GoogleGenerativeAIToolType =
9
+ | BindToolsInput
10
+ | GoogleGenerativeAIFunctionDeclarationsTool
11
+ | CodeExecutionTool
12
+ | GoogleSearchRetrievalTool;
13
+
14
+ /** Enum for content modality types */
15
+ enum Modality {
16
+ MODALITY_UNSPECIFIED = 'MODALITY_UNSPECIFIED',
17
+ TEXT = 'TEXT',
18
+ IMAGE = 'IMAGE',
19
+ VIDEO = 'VIDEO',
20
+ AUDIO = 'AUDIO',
21
+ DOCUMENT = 'DOCUMENT',
22
+ }
23
+
24
+ /** Interface for modality token count */
25
+ interface ModalityTokenCount {
26
+ modality: Modality;
27
+ tokenCount: number;
28
+ }
29
+
30
+ /** Main interface for Gemini API usage metadata */
31
+ export interface GeminiApiUsageMetadata {
32
+ promptTokenCount?: number;
33
+ totalTokenCount?: number;
34
+ thoughtsTokenCount?: number;
35
+ candidatesTokenCount?: number;
36
+ toolUsePromptTokenCount?: number;
37
+ cachedContentTokenCount?: number;
38
+ promptTokensDetails: ModalityTokenCount[];
39
+ candidatesTokensDetails?: ModalityTokenCount[];
40
+ cacheTokensDetails?: ModalityTokenCount[];
41
+ toolUsePromptTokensDetails?: ModalityTokenCount[];
42
+ trafficType?: string;
43
+ }