@langchain/google 0.2.10 → 0.2.12

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 (84) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/chat_models/{api-types.d.ts → api_types/generate_content.d.cts} +199 -199
  3. package/dist/chat_models/{api-types.d.cts → api_types/generate_content.d.ts} +199 -199
  4. package/dist/chat_models/base.cjs +53 -383
  5. package/dist/chat_models/base.cjs.map +1 -1
  6. package/dist/chat_models/base.d.cts +13 -198
  7. package/dist/chat_models/base.d.ts +15 -200
  8. package/dist/chat_models/base.js +55 -383
  9. package/dist/chat_models/base.js.map +1 -1
  10. package/dist/chat_models/handlers/base.cjs +63 -0
  11. package/dist/chat_models/handlers/base.cjs.map +1 -0
  12. package/dist/chat_models/handlers/base.d.cts +48 -0
  13. package/dist/chat_models/handlers/base.d.ts +48 -0
  14. package/dist/chat_models/handlers/base.js +63 -0
  15. package/dist/chat_models/handlers/base.js.map +1 -0
  16. package/dist/chat_models/handlers/generate_content.cjs +345 -0
  17. package/dist/chat_models/handlers/generate_content.cjs.map +1 -0
  18. package/dist/chat_models/handlers/generate_content.d.cts +34 -0
  19. package/dist/chat_models/handlers/generate_content.d.ts +34 -0
  20. package/dist/chat_models/handlers/generate_content.js +345 -0
  21. package/dist/chat_models/handlers/generate_content.js.map +1 -0
  22. package/dist/chat_models/handlers/index.cjs +4 -0
  23. package/dist/chat_models/handlers/index.d.cts +3 -0
  24. package/dist/chat_models/handlers/index.d.ts +3 -0
  25. package/dist/chat_models/handlers/index.js +3 -0
  26. package/dist/chat_models/index.cjs +11 -4
  27. package/dist/chat_models/index.cjs.map +1 -1
  28. package/dist/chat_models/index.d.cts +6 -1
  29. package/dist/chat_models/index.d.ts +6 -1
  30. package/dist/chat_models/index.js +6 -2
  31. package/dist/chat_models/index.js.map +1 -1
  32. package/dist/chat_models/node.cjs +2 -2
  33. package/dist/chat_models/node.cjs.map +1 -1
  34. package/dist/chat_models/node.js +2 -2
  35. package/dist/chat_models/node.js.map +1 -1
  36. package/dist/chat_models/profiles.cjs +14 -0
  37. package/dist/chat_models/profiles.cjs.map +1 -1
  38. package/dist/chat_models/profiles.js +14 -0
  39. package/dist/chat_models/profiles.js.map +1 -1
  40. package/dist/chat_models/types.cjs.map +1 -1
  41. package/dist/chat_models/types.d.cts +281 -94
  42. package/dist/chat_models/types.d.ts +281 -94
  43. package/dist/chat_models/types.js.map +1 -1
  44. package/dist/converters/{messages.cjs → generate_content/messages.cjs} +4 -4
  45. package/dist/converters/generate_content/messages.cjs.map +1 -0
  46. package/dist/converters/{messages.js → generate_content/messages.js} +4 -4
  47. package/dist/converters/generate_content/messages.js.map +1 -0
  48. package/dist/converters/generate_content/params.cjs +136 -0
  49. package/dist/converters/generate_content/params.cjs.map +1 -0
  50. package/dist/converters/generate_content/params.js +132 -0
  51. package/dist/converters/generate_content/params.js.map +1 -0
  52. package/dist/{utils → converters/generate_content}/stream_events.cjs +2 -2
  53. package/dist/converters/generate_content/stream_events.cjs.map +1 -0
  54. package/dist/{utils → converters/generate_content}/stream_events.js +2 -2
  55. package/dist/converters/generate_content/stream_events.js.map +1 -0
  56. package/dist/converters/{tools.cjs → generate_content/tools.cjs} +4 -4
  57. package/dist/converters/generate_content/tools.cjs.map +1 -0
  58. package/dist/converters/{tools.js → generate_content/tools.js} +4 -4
  59. package/dist/converters/generate_content/tools.js.map +1 -0
  60. package/dist/converters/{params.cjs → shared/params.cjs} +16 -93
  61. package/dist/converters/shared/params.cjs.map +1 -0
  62. package/dist/converters/{params.js → shared/params.js} +15 -91
  63. package/dist/converters/shared/params.js.map +1 -0
  64. package/dist/index.cjs +6 -0
  65. package/dist/index.d.cts +5 -1
  66. package/dist/index.d.ts +5 -1
  67. package/dist/index.js +4 -1
  68. package/dist/utils/metadata.cjs +8 -0
  69. package/dist/utils/metadata.cjs.map +1 -1
  70. package/dist/utils/metadata.js +8 -1
  71. package/dist/utils/metadata.js.map +1 -1
  72. package/dist/utils/misc.cjs.map +1 -1
  73. package/dist/utils/misc.d.cts +2 -1
  74. package/dist/utils/misc.d.ts +2 -1
  75. package/dist/utils/misc.js.map +1 -1
  76. package/package.json +4 -4
  77. package/dist/converters/messages.cjs.map +0 -1
  78. package/dist/converters/messages.js.map +0 -1
  79. package/dist/converters/params.cjs.map +0 -1
  80. package/dist/converters/params.js.map +0 -1
  81. package/dist/converters/tools.cjs.map +0 -1
  82. package/dist/converters/tools.js.map +0 -1
  83. package/dist/utils/stream_events.cjs.map +0 -1
  84. package/dist/utils/stream_events.js.map +0 -1
@@ -1,23 +1,14 @@
1
- const require_misc = require("../utils/misc.cjs");
2
1
  const require_errors = require("../utils/errors.cjs");
3
- const require_stream_events = require("../utils/stream_events.cjs");
4
2
  const require_profiles = require("./profiles.cjs");
5
- const require_types = require("./types.cjs");
6
- const require_stream = require("../utils/stream.cjs");
7
- const require_messages = require("../converters/messages.cjs");
8
- const require_tools = require("../converters/tools.cjs");
9
- const require_params = require("../converters/params.cjs");
10
3
  const require_metadata = require("../utils/metadata.cjs");
4
+ const require_params = require("../converters/shared/params.cjs");
5
+ const require_generate_content = require("./handlers/generate_content.cjs");
6
+ require("./handlers/index.cjs");
11
7
  let _langchain_core_utils_env = require("@langchain/core/utils/env");
12
8
  let _langchain_core_language_models_chat_models = require("@langchain/core/language_models/chat_models");
13
9
  let _langchain_core_messages = require("@langchain/core/messages");
14
10
  let _langchain_core_utils_stream = require("@langchain/core/utils/stream");
15
- let _langchain_core_outputs = require("@langchain/core/outputs");
16
- let eventsource_parser_stream = require("eventsource-parser/stream");
17
11
  let _langchain_core_runnables = require("@langchain/core/runnables");
18
- let _langchain_core_utils_json_schema = require("@langchain/core/utils/json_schema");
19
- let _langchain_core_utils_types = require("@langchain/core/utils/types");
20
- let _langchain_core_utils_standard_schema = require("@langchain/core/utils/standard_schema");
21
12
  let _langchain_core_language_models_structured_output = require("@langchain/core/language_models/structured_output");
22
13
  let _langchain_core_utils_gateway = require("@langchain/core/utils/gateway");
23
14
  //#region src/chat_models/base.ts
@@ -44,8 +35,9 @@ function hasVertexCredentials(params) {
44
35
  * @returns a new params object with gateway routing applied when applicable.
45
36
  */
46
37
  function applyGeminiGatewayParams(params) {
47
- if (typeof params.endpoint !== "undefined") return { ...params };
48
- const platform = require_params.convertParamsToPlatformType(params) ?? (hasVertexCredentials(params) ? "gcp" : "gai");
38
+ const p = params;
39
+ if (typeof p.endpoint !== "undefined") return { ...params };
40
+ const platform = require_params.convertParamsToPlatformType(p) ?? (hasVertexCredentials(p) ? "gcp" : "gai");
49
41
  const gatewayConfig = (0, _langchain_core_utils_gateway.resolveLangSmithGatewayConfig)({ providerPath: platform === "gcp" ? "vertex" : "gemini" });
50
42
  if (typeof gatewayConfig.baseURL === "undefined") return { ...params };
51
43
  const gatewayUrl = new URL(gatewayConfig.baseURL);
@@ -53,7 +45,7 @@ function applyGeminiGatewayParams(params) {
53
45
  const endpoint = gatewayUrl.protocol === "https:" ? `${gatewayUrl.host}${path}` : `${gatewayUrl.protocol}//${gatewayUrl.host}${path}`;
54
46
  return {
55
47
  ...params,
56
- apiKey: params.apiKey ?? gatewayConfig.apiKey,
48
+ apiKey: p.apiKey ?? gatewayConfig.apiKey,
57
49
  endpoint,
58
50
  platformType: platform
59
51
  };
@@ -63,20 +55,6 @@ function getPlatformType(platform, hasApiKey) {
63
55
  else if (hasApiKey) return "gai";
64
56
  else return "gcp";
65
57
  }
66
- function usageMetadataToTokenUsage(usageMetadata) {
67
- return {
68
- promptTokens: usageMetadata.input_tokens,
69
- completionTokens: usageMetadata.output_tokens,
70
- totalTokens: usageMetadata.total_tokens
71
- };
72
- }
73
- function mapDetailToMediaResolution(detail) {
74
- switch (detail) {
75
- case "low": return "MEDIA_RESOLUTION_LOW";
76
- case "high": return "MEDIA_RESOLUTION_HIGH";
77
- default: return;
78
- }
79
- }
80
58
  var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.BaseChatModel {
81
59
  params;
82
60
  model;
@@ -92,7 +70,7 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
92
70
  constructor(params) {
93
71
  super(params);
94
72
  this.params = params;
95
- this._addVersion("@langchain/google", "0.2.10");
73
+ this._addVersion("@langchain/google", "0.2.12");
96
74
  if (!params.apiClient) throw new require_errors.ConfigurationError("BaseChatGoogle requires an apiClient. This should be provided automatically by ChatGoogle constructors. If you're extending BaseChatGoogle directly, please provide an apiClient instance.");
97
75
  this.apiClient = params.apiClient;
98
76
  this.model = params.model;
@@ -139,8 +117,37 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
139
117
  get publisher() {
140
118
  return "google";
141
119
  }
142
- get urlMethod() {
143
- return this.streaming ? "streamGenerateContent?alt=sse" : "generateContent";
120
+ _apiHandler;
121
+ _handlerParams;
122
+ syncHandlerParams() {
123
+ const nextParams = {
124
+ ...this.params,
125
+ model: this.model,
126
+ platform: this.platform,
127
+ endpoint: this.endpoint,
128
+ location: this.location,
129
+ apiVersion: this.apiVersion,
130
+ isVertexExpress: this.isVertexExpress,
131
+ publisher: this.publisher,
132
+ streaming: this.streaming,
133
+ streamUsage: this.streamUsage,
134
+ customHeaders: this._customHeaders,
135
+ apiClient: this.apiClient,
136
+ caller: this.caller
137
+ };
138
+ if (this._handlerParams) return Object.assign(this._handlerParams, nextParams);
139
+ this._handlerParams = nextParams;
140
+ return this._handlerParams;
141
+ }
142
+ get apiHandler() {
143
+ const handlerParams = this.syncHandlerParams();
144
+ if (!this._apiHandler) {
145
+ const handlerConfig = this.params.apiHandler ?? "generateContent";
146
+ if (typeof handlerConfig === "function") this._apiHandler = handlerConfig(handlerParams);
147
+ else if (handlerConfig === "generateContent") this._apiHandler = new require_generate_content.GenerateContentApiHandler(handlerParams);
148
+ else throw new require_errors.ConfigurationError(`Unknown apiHandler: ${handlerConfig}`);
149
+ }
150
+ return this._apiHandler;
144
151
  }
145
152
  /**
146
153
  * Returns the model profile for this instance's model, describing its
@@ -158,96 +165,24 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
158
165
  get profile() {
159
166
  return require_profiles.default[this.model] ?? {};
160
167
  }
161
- async buildUrlGemini(urlMethod) {
162
- const url = this.endpoint.includes("://") ? new URL(this.endpoint) : new URL(`https://${this.endpoint}`);
163
- const [method, search] = (urlMethod ?? this.urlMethod).split("?", 2);
164
- url.pathname = `${url.pathname.replace(/\/+$/, "")}/${this.apiVersion}/models/${this.model}:${method}`;
165
- if (search) url.search = search;
166
- return url.toString();
167
- }
168
- get endpointUrl() {
169
- return this.endpoint.includes("://") ? this.endpoint : `https://${this.endpoint}`;
170
- }
171
- async buildUrlVertexExpress(urlMethod) {
172
- return `${this.endpointUrl}/${this.apiVersion}/publishers/${this.publisher}/models/${this.model}:${urlMethod ?? this.urlMethod}`;
173
- }
174
- async buildUrlVertexLocation(urlMethod) {
175
- const projectId = await this.apiClient.getProjectId();
176
- return `${this.endpointUrl}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/publishers/${this.publisher}/models/${this.model}:${urlMethod ?? this.urlMethod}`;
177
- }
178
- async buildUrlVertex(urlMethod) {
179
- if (this.isVertexExpress) return this.buildUrlVertexExpress(urlMethod);
180
- else return this.buildUrlVertexLocation(urlMethod);
181
- }
182
168
  async buildUrl(urlMethod) {
183
- switch (this.platform) {
184
- case "gai": return this.buildUrlGemini(urlMethod);
185
- default: return this.buildUrlVertex(urlMethod);
186
- }
169
+ return this.apiHandler.buildUrl(urlMethod);
187
170
  }
188
171
  invocationParams(options) {
189
- const fields = combineGoogleChatModelFields(this.params, options);
190
- const mediaResolution = fields.mediaResolution ?? mapDetailToMediaResolution(fields.detail);
191
- const tools = fields.tools ? require_tools.convertToolsToGeminiTools(fields.tools) : void 0;
192
- let toolConfig = require_tools.convertToolChoiceToGeminiConfig(options.tool_choice, !!(tools && tools.length > 0));
193
- if (tools && require_tools.mixesBuiltinAndFunctionTools(tools)) toolConfig = {
194
- ...toolConfig,
195
- includeServerSideToolInvocations: true
196
- };
197
- let responseJsonSchema;
198
- let responseMimeType;
199
- if (fields.responseSchema) {
200
- if ((0, _langchain_core_utils_types.isInteropZodSchema)(fields.responseSchema) || (0, _langchain_core_utils_standard_schema.isSerializableSchema)(fields.responseSchema)) responseJsonSchema = (0, _langchain_core_utils_json_schema.toJsonSchema)(fields.responseSchema);
201
- else responseJsonSchema = fields.responseSchema;
202
- responseMimeType = "application/json";
203
- }
204
- return {
205
- ...tools && tools.length > 0 ? { tools } : {},
206
- ...toolConfig ? { toolConfig } : {},
207
- safetySettings: fields.safetySettings,
208
- generationConfig: {
209
- temperature: fields.temperature,
210
- topP: fields.topP,
211
- topK: fields.topK,
212
- maxOutputTokens: fields.maxOutputTokens,
213
- presencePenalty: fields.presencePenalty,
214
- frequencyPenalty: fields.frequencyPenalty,
215
- stopSequences: fields.stopSequences,
216
- ...responseMimeType ? { responseMimeType } : {},
217
- ...responseJsonSchema ? { responseJsonSchema } : {},
218
- ...fields.responseModalities && fields.responseModalities.length > 0 ? { responseModalities: fields.responseModalities } : {},
219
- candidateCount: 1,
220
- seed: fields.seed,
221
- responseLogprobs: fields.responseLogprobs,
222
- logprobs: fields.logprobs,
223
- ...fields.enableEnhancedCivicAnswers !== void 0 ? { enableEnhancedCivicAnswers: fields.enableEnhancedCivicAnswers } : {},
224
- thinkingConfig: require_params.convertFieldsToThinkingConfig(this.model, fields),
225
- speechConfig: require_params.convertFieldsToSpeechConfig(fields),
226
- ...fields.imageConfig ? { imageConfig: fields.imageConfig } : {},
227
- ...mediaResolution ? { mediaResolution } : {}
228
- },
229
- ...require_params.convertFieldsToServiceTier(this.platform, fields)
230
- };
172
+ return this.apiHandler.invocationParams(options);
231
173
  }
232
174
  getLsParams(options) {
233
- const params = this.invocationParams(options);
234
175
  return {
235
176
  ls_provider: this.platform === "gcp" ? "google_vertexai" : "google_genai",
236
177
  ls_model_name: this.model,
237
178
  ls_model_type: "chat",
238
- ls_temperature: params.generationConfig?.temperature ?? void 0,
239
- ls_max_tokens: params.generationConfig?.maxOutputTokens ?? void 0,
240
- ls_stop: options.stop
179
+ ls_temperature: options?.temperature ?? this.params.temperature,
180
+ ls_max_tokens: options?.maxOutputTokens ?? this.params.maxOutputTokens,
181
+ ls_stop: options?.stopSequences ?? options?.stop ?? this.params.stopSequences
241
182
  };
242
183
  }
243
184
  getHeaders(options) {
244
- const fields = combineGoogleChatModelFields(this.params, options);
245
- return {
246
- "Content-Type": "application/json",
247
- ...this.platform === "gcp" && typeof fields.serviceTier !== "undefined" && require_types.settableServiceTier.includes(fields.serviceTier) ? { "X-Vertex-AI-LLM-Shared-Request-Type": fields.serviceTier } : {},
248
- ...this._customHeaders,
249
- ...options.customHeaders
250
- };
185
+ return this.apiHandler.getHeaders(options);
251
186
  }
252
187
  async _generate(messages, options, runManager) {
253
188
  if (this.streaming) {
@@ -255,228 +190,22 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
255
190
  let finalChunk = null;
256
191
  for await (const chunk of stream) finalChunk = !finalChunk ? chunk : (0, _langchain_core_utils_stream.concat)(finalChunk, chunk);
257
192
  if (typeof finalChunk?.message?.content === "string" && typeof (finalChunk?.message?.additional_kwargs?.originalTextContentBlock)?.text === "string") finalChunk.message.additional_kwargs.originalTextContentBlock.text = finalChunk.message.content;
258
- const usageMetadata = finalChunk?.message?.usage_metadata;
193
+ const usageMetadata = (finalChunk?.message)?.usage_metadata;
259
194
  return {
260
195
  generations: finalChunk ? [finalChunk] : [],
261
196
  ...usageMetadata ? { llmOutput: {
262
- tokenUsage: usageMetadataToTokenUsage(usageMetadata),
197
+ tokenUsage: require_metadata.usageMetadataToTokenUsage(usageMetadata),
263
198
  usageMetadata
264
199
  } } : {}
265
200
  };
266
201
  }
267
- const url = await this.buildUrl();
268
- const headers = this.getHeaders(options);
269
- const body = {
270
- ...this.invocationParams(options),
271
- systemInstruction: require_messages.convertMessagesToGeminiSystemInstruction(messages),
272
- contents: require_messages.convertMessagesToGeminiContents(messages)
273
- };
274
- const moduleName = this.constructor.name;
275
- await runManager?.handleCustomEvent(`google-request-${moduleName}`, {
276
- url,
277
- headers,
278
- body
279
- });
280
- let response;
281
- try {
282
- response = await this.caller.callWithOptions({
283
- signal: options.signal,
284
- maxRetries: options.maxRetries
285
- }, async () => {
286
- const nextResponse = await this.apiClient.fetch(new Request(url, {
287
- method: "POST",
288
- headers,
289
- body: JSON.stringify(body),
290
- signal: options.signal
291
- }));
292
- if (!nextResponse.ok) throw await require_errors.RequestError.fromResponse(nextResponse);
293
- return nextResponse;
294
- });
295
- } catch (error) {
296
- await runManager?.handleCustomEvent(`google-response-${moduleName}`, { error });
297
- throw error;
298
- }
299
- const data = await response.json();
300
- await runManager?.handleCustomEvent(`google-response-${moduleName}`, {
301
- data,
302
- url: response.url,
303
- headers: Array.from(response.headers.entries()),
304
- status: response.status,
305
- statusText: response.statusText
306
- });
307
- if (data.promptFeedback?.blockReason) throw require_errors.PromptBlockedError.fromPromptFeedback(data.promptFeedback);
308
- if (!data.candidates || data.candidates.length === 0) throw new require_errors.NoCandidatesError();
309
- const candidate = data.candidates[0];
310
- const message = require_messages.convertGeminiCandidateToAIMessage(candidate);
311
- const text = require_messages.convertAIMessageToText(message);
312
- const usageMetadata = require_messages.convertGeminiGenerateContentResponseToUsageMetadata(data);
313
- message.usage_metadata = usageMetadata;
314
- const serviceTier = require_misc.iife(() => {
315
- const trafficType = data.usageMetadata?.trafficType;
316
- const serviceTierHeader = response.headers.get("x-gemini-service-tier");
317
- if (trafficType?.startsWith("ON_DEMAND_")) return trafficType?.substring(10).toLowerCase();
318
- return serviceTierHeader || "standard";
319
- });
320
- return {
321
- generations: [{
322
- text,
323
- message,
324
- generationInfo: {
325
- finishReason: candidate.finishReason,
326
- finishMessage: candidate.finishMessage,
327
- safetyRatings: candidate.safetyRatings,
328
- citationMetadata: candidate.citationMetadata,
329
- tokenCount: candidate.tokenCount
330
- }
331
- }],
332
- llmOutput: {
333
- tokenUsage: usageMetadataToTokenUsage(usageMetadata),
334
- model: data.modelVersion,
335
- responseId: data.responseId,
336
- usageMetadata,
337
- serviceTier
338
- }
339
- };
202
+ return this.apiHandler.generate(messages, options, runManager);
340
203
  }
341
- async *_streamChatModelEvents(messages, options, _runManager) {
342
- const body = {
343
- ...this.invocationParams(options),
344
- systemInstruction: require_messages.convertMessagesToGeminiSystemInstruction(messages),
345
- contents: require_messages.convertMessagesToGeminiContents(messages)
346
- };
347
- const url = await this.buildUrl("streamGenerateContent?alt=sse");
348
- const headers = this.getHeaders(options);
349
- const response = await this.apiClient.fetch(new Request(url, {
350
- method: "POST",
351
- headers,
352
- body: JSON.stringify(body),
353
- signal: options.signal
354
- }));
355
- if (!response.ok) throw await require_errors.RequestError.fromResponse(response);
356
- if (!response.body) return;
357
- const eventStream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new eventsource_parser_stream.EventSourceParserStream()).pipeThrough(new require_stream.SafeJsonEventParserStream());
358
- const shouldStreamUsage = this.streamUsage !== false && options.streamUsage !== false;
359
- async function* geminiChunks(stream, signal) {
360
- const reader = stream.getReader();
361
- try {
362
- while (true) {
363
- if (signal?.aborted) return;
364
- const { done, value } = await reader.read();
365
- if (done) break;
366
- if (value !== null) yield value;
367
- }
368
- } finally {
369
- reader.releaseLock();
370
- }
371
- }
372
- yield* require_stream_events.convertGoogleGeminiStream(geminiChunks(eventStream, options.signal), { streamUsage: shouldStreamUsage });
204
+ async *_streamChatModelEvents(messages, options, runManager) {
205
+ yield* this.apiHandler.streamChatModelEvents(messages, options, runManager);
373
206
  }
374
207
  async *_streamResponseChunks(messages, options, runManager) {
375
- const streamUsage = this.streamUsage ?? true;
376
- const body = {
377
- ...this.invocationParams(options),
378
- systemInstruction: require_messages.convertMessagesToGeminiSystemInstruction(messages),
379
- contents: require_messages.convertMessagesToGeminiContents(messages)
380
- };
381
- const url = await this.buildUrl("streamGenerateContent?alt=sse");
382
- const headers = this.getHeaders(options);
383
- const moduleName = this.constructor.name;
384
- await runManager?.handleCustomEvent(`google-request-${moduleName}`, {
385
- url,
386
- headers,
387
- body
388
- });
389
- let response;
390
- try {
391
- response = await this.caller.callWithOptions({
392
- signal: options.signal,
393
- maxRetries: options.maxRetries
394
- }, async () => {
395
- const nextResponse = await this.apiClient.fetch(new Request(url, {
396
- method: "POST",
397
- headers,
398
- body: JSON.stringify(body),
399
- signal: options.signal
400
- }));
401
- if (!nextResponse.ok) throw await require_errors.RequestError.fromResponse(nextResponse);
402
- return nextResponse;
403
- });
404
- } catch (error) {
405
- await runManager?.handleCustomEvent(`google-response-${moduleName}`, { error });
406
- throw error;
407
- }
408
- await runManager?.handleCustomEvent(`google-response-${moduleName}`, {
409
- url: response.url,
410
- headers: response.headers,
411
- status: response.status,
412
- statusText: response.statusText
413
- });
414
- if (response.body) {
415
- let previousUsage;
416
- const reader = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new eventsource_parser_stream.EventSourceParserStream()).pipeThrough(new require_stream.SafeJsonEventParserStream()).pipeThrough(new TransformStream({ transform(chunk, controller) {
417
- runManager?.handleCustomEvent(`google-chunk-${moduleName}`, { chunk });
418
- if (chunk === null) {
419
- controller.enqueue(new _langchain_core_outputs.ChatGenerationChunk({
420
- text: "",
421
- message: new _langchain_core_messages.AIMessageChunk({ content: "" }),
422
- generationInfo: { finishReason: "stop" }
423
- }));
424
- return;
425
- }
426
- const candidate = chunk.candidates?.[0];
427
- if (!candidate) return;
428
- const message = require_messages.convertGeminiCandidateToAIMessage(candidate);
429
- const text = require_messages.convertAIMessageToText(message);
430
- const parts = candidate.content?.parts ?? [];
431
- const toolCalls = require_messages.convertGeminiPartsToToolCalls(parts);
432
- if (parts.length > 0 || candidate.finishReason) {
433
- const messageChunkParams = {
434
- content: message.content,
435
- tool_calls: toolCalls,
436
- response_metadata: { model_provider: "google" },
437
- additional_kwargs: {
438
- ...message.additional_kwargs.originalTextContentBlock ? { originalTextContentBlock: message.additional_kwargs.originalTextContentBlock } : {},
439
- ...candidate.finishReason ? {
440
- finishReason: candidate.finishReason,
441
- finishMessage: candidate.finishMessage
442
- } : {}
443
- }
444
- };
445
- if (chunk?.usageMetadata && streamUsage) {
446
- const cumulative = require_messages.convertGeminiGenerateContentResponseToUsageMetadata(chunk);
447
- messageChunkParams.usage_metadata = require_metadata.subtractUsageMetadata(cumulative, previousUsage);
448
- previousUsage = cumulative;
449
- }
450
- const messageChunk = new _langchain_core_messages.AIMessageChunk(messageChunkParams);
451
- controller.enqueue(new _langchain_core_outputs.ChatGenerationChunk({
452
- text,
453
- message: messageChunk,
454
- generationInfo: {
455
- ...candidate.finishReason && {
456
- finishReason: candidate.finishReason,
457
- finishMessage: candidate.finishMessage
458
- },
459
- ...candidate.safetyRatings && { safetyRatings: candidate.safetyRatings },
460
- ...candidate.citationMetadata && { citationMetadata: candidate.citationMetadata },
461
- ...candidate.groundingMetadata && {
462
- groundingMetadata: candidate.groundingMetadata,
463
- groundingSupport: candidate.groundingMetadata.groundingSupports?.filter((s) => (s?.segment?.partIndex ?? 0) === 0)
464
- }
465
- }
466
- }));
467
- }
468
- } })).getReader();
469
- try {
470
- while (true) {
471
- const { done, value } = await reader.read();
472
- if (done) break;
473
- yield value;
474
- await runManager?.handleLLMNewToken(value.text ?? "", void 0, void 0, void 0, void 0, { chunk: value });
475
- }
476
- } finally {
477
- reader.releaseLock();
478
- }
479
- }
208
+ yield* this.apiHandler.streamResponseChunks(messages, options, runManager);
480
209
  }
481
210
  /**
482
211
  * Bind tool-like objects to this chat model.
@@ -522,75 +251,16 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
522
251
  return await (0, _langchain_core_language_models_structured_output.createContentParser)(schema).parse(input.text);
523
252
  });
524
253
  } else if (method === "functionCalling") {
525
- let functionName = name ?? "extract";
526
- let geminiFunctionDeclaration;
527
- if ((0, _langchain_core_utils_types.isInteropZodSchema)(schema) || (0, _langchain_core_utils_standard_schema.isSerializableSchema)(schema)) {
528
- const jsonSchema = require_tools.schemaToGeminiParameters(schema);
529
- const description = typeof jsonSchema.description === "string" ? jsonSchema.description : "A function available to call.";
530
- geminiFunctionDeclaration = {
531
- name: functionName,
532
- description,
533
- parameters: jsonSchema
534
- };
535
- } else if (typeof schema.name === "string" && typeof schema.parameters === "object" && schema.parameters != null) {
536
- geminiFunctionDeclaration = schema;
537
- functionName = schema.name;
538
- } else {
539
- const parameters = require_tools.schemaToGeminiParameters(schema);
540
- geminiFunctionDeclaration = {
541
- name: functionName,
542
- description: schema.description ?? "",
543
- parameters
544
- };
545
- }
546
- const tools = [{ functionDeclarations: [geminiFunctionDeclaration] }];
547
- llm = this.bindTools(tools).withConfig({ tool_choice: functionName });
254
+ const { tools, toolChoice, functionName } = this.apiHandler.formatStructuredOutputTool(schema, { name });
255
+ llm = this.bindTools(tools).withConfig({ tool_choice: toolChoice });
548
256
  outputParser = (0, _langchain_core_language_models_structured_output.createFunctionCallingParser)(schema, functionName);
549
257
  } else throw new require_errors.ConfigurationError(`Unrecognized structured output method '${method}'. Expected 'functionCalling' or 'jsonSchema'`);
550
258
  return (0, _langchain_core_language_models_structured_output.assembleStructuredOutputPipeline)(llm, outputParser, includeRaw, includeRaw ? "ChatGoogleStructuredOutputRunnable" : "ChatGoogleStructuredOutput");
551
259
  }
552
260
  };
553
- function combineGoogleChatModelFields(a, b, ...rest) {
554
- const combined = {
555
- temperature: b.temperature ?? a.temperature,
556
- topP: b.topP ?? a.topP,
557
- topK: b.topK ?? a.topK,
558
- maxOutputTokens: b.maxOutputTokens ?? a.maxOutputTokens,
559
- presencePenalty: b.presencePenalty ?? a.presencePenalty,
560
- frequencyPenalty: b.frequencyPenalty ?? a.frequencyPenalty,
561
- stopSequences: b.stopSequences ?? a.stopSequences,
562
- seed: b.seed ?? a.seed,
563
- responseLogprobs: b.responseLogprobs ?? a.responseLogprobs,
564
- logprobs: b.logprobs ?? a.logprobs,
565
- safetySettings: b.safetySettings ?? a.safetySettings,
566
- responseSchema: b.responseSchema ?? a.responseSchema,
567
- tools: b.tools ?? a.tools,
568
- responseModalities: b.responseModalities ?? a.responseModalities,
569
- enableEnhancedCivicAnswers: b.enableEnhancedCivicAnswers ?? a.enableEnhancedCivicAnswers,
570
- speechConfig: b.speechConfig ?? a.speechConfig,
571
- imageConfig: b.imageConfig ?? a.imageConfig,
572
- mediaResolution: b.mediaResolution ?? a.mediaResolution,
573
- detail: b.detail ?? a.detail,
574
- maxReasoningTokens: b.maxReasoningTokens ?? a.maxReasoningTokens,
575
- thinkingBudget: b.thinkingBudget ?? a.thinkingBudget,
576
- reasoningEffort: b.reasoningEffort ?? a.reasoningEffort,
577
- thinkingLevel: b.thinkingLevel ?? a.thinkingLevel,
578
- serviceTier: b.serviceTier ?? a.serviceTier
579
- };
580
- if (rest.length > 0) return combineGoogleChatModelFields(combined, rest[0], ...rest.slice(1));
581
- return combined;
582
- }
583
- function getGoogleChatModelParams(modelOrParams, paramsArg) {
584
- return {
585
- model: typeof modelOrParams === "string" ? modelOrParams : modelOrParams.model,
586
- ...typeof modelOrParams === "string" ? paramsArg : modelOrParams
587
- };
588
- }
589
261
  //#endregion
590
262
  exports.BaseChatGoogle = BaseChatGoogle;
591
263
  exports.applyGeminiGatewayParams = applyGeminiGatewayParams;
592
- exports.combineGoogleChatModelFields = combineGoogleChatModelFields;
593
- exports.getGoogleChatModelParams = getGoogleChatModelParams;
594
264
  exports.getPlatformType = getPlatformType;
595
265
 
596
266
  //# sourceMappingURL=base.cjs.map