@langchain/google-common 0.2.18 → 1.0.0

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 (169) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +6 -6
  3. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  4. package/dist/auth.cjs +82 -116
  5. package/dist/auth.cjs.map +1 -0
  6. package/dist/auth.d.cts +46 -0
  7. package/dist/auth.d.cts.map +1 -0
  8. package/dist/auth.d.ts +41 -36
  9. package/dist/auth.d.ts.map +1 -0
  10. package/dist/auth.js +80 -110
  11. package/dist/auth.js.map +1 -0
  12. package/dist/chat_models.cjs +251 -466
  13. package/dist/chat_models.cjs.map +1 -0
  14. package/dist/chat_models.d.cts +98 -0
  15. package/dist/chat_models.d.cts.map +1 -0
  16. package/dist/chat_models.d.ts +87 -73
  17. package/dist/chat_models.d.ts.map +1 -0
  18. package/dist/chat_models.js +245 -457
  19. package/dist/chat_models.js.map +1 -0
  20. package/dist/connection.cjs +321 -466
  21. package/dist/connection.cjs.map +1 -0
  22. package/dist/connection.d.cts +109 -0
  23. package/dist/connection.d.cts.map +1 -0
  24. package/dist/connection.d.ts +98 -91
  25. package/dist/connection.d.ts.map +1 -0
  26. package/dist/connection.js +317 -459
  27. package/dist/connection.js.map +1 -0
  28. package/dist/embeddings.cjs +135 -186
  29. package/dist/embeddings.cjs.map +1 -0
  30. package/dist/embeddings.d.cts +44 -0
  31. package/dist/embeddings.d.cts.map +1 -0
  32. package/dist/embeddings.d.ts +38 -32
  33. package/dist/embeddings.d.ts.map +1 -0
  34. package/dist/embeddings.js +133 -181
  35. package/dist/embeddings.js.map +1 -0
  36. package/dist/experimental/media.cjs +380 -482
  37. package/dist/experimental/media.cjs.map +1 -0
  38. package/dist/experimental/media.d.cts +198 -0
  39. package/dist/experimental/media.d.cts.map +1 -0
  40. package/dist/experimental/media.d.ts +190 -202
  41. package/dist/experimental/media.d.ts.map +1 -0
  42. package/dist/experimental/media.js +369 -468
  43. package/dist/experimental/media.js.map +1 -0
  44. package/dist/experimental/utils/media_core.cjs +403 -517
  45. package/dist/experimental/utils/media_core.cjs.map +1 -0
  46. package/dist/experimental/utils/media_core.d.cts +215 -0
  47. package/dist/experimental/utils/media_core.d.cts.map +1 -0
  48. package/dist/experimental/utils/media_core.d.ts +171 -165
  49. package/dist/experimental/utils/media_core.d.ts.map +1 -0
  50. package/dist/experimental/utils/media_core.js +395 -506
  51. package/dist/experimental/utils/media_core.js.map +1 -0
  52. package/dist/index.cjs +58 -27
  53. package/dist/index.d.cts +13 -0
  54. package/dist/index.d.ts +13 -11
  55. package/dist/index.js +13 -11
  56. package/dist/llms.cjs +157 -244
  57. package/dist/llms.cjs.map +1 -0
  58. package/dist/llms.d.cts +72 -0
  59. package/dist/llms.d.cts.map +1 -0
  60. package/dist/llms.d.ts +64 -54
  61. package/dist/llms.d.ts.map +1 -0
  62. package/dist/llms.js +154 -238
  63. package/dist/llms.js.map +1 -0
  64. package/dist/output_parsers.cjs +148 -173
  65. package/dist/output_parsers.cjs.map +1 -0
  66. package/dist/output_parsers.d.cts +53 -0
  67. package/dist/output_parsers.d.cts.map +1 -0
  68. package/dist/output_parsers.d.ts +46 -42
  69. package/dist/output_parsers.d.ts.map +1 -0
  70. package/dist/output_parsers.js +146 -168
  71. package/dist/output_parsers.js.map +1 -0
  72. package/dist/types-anthropic.d.cts +229 -0
  73. package/dist/types-anthropic.d.cts.map +1 -0
  74. package/dist/types-anthropic.d.ts +221 -215
  75. package/dist/types-anthropic.d.ts.map +1 -0
  76. package/dist/types.cjs +51 -62
  77. package/dist/types.cjs.map +1 -0
  78. package/dist/types.d.cts +748 -0
  79. package/dist/types.d.cts.map +1 -0
  80. package/dist/types.d.ts +669 -656
  81. package/dist/types.d.ts.map +1 -0
  82. package/dist/types.js +46 -45
  83. package/dist/types.js.map +1 -0
  84. package/dist/utils/anthropic.cjs +598 -821
  85. package/dist/utils/anthropic.cjs.map +1 -0
  86. package/dist/utils/anthropic.js +597 -818
  87. package/dist/utils/anthropic.js.map +1 -0
  88. package/dist/utils/common.cjs +130 -211
  89. package/dist/utils/common.cjs.map +1 -0
  90. package/dist/utils/common.d.cts +13 -0
  91. package/dist/utils/common.d.cts.map +1 -0
  92. package/dist/utils/common.d.ts +12 -7
  93. package/dist/utils/common.d.ts.map +1 -0
  94. package/dist/utils/common.js +128 -207
  95. package/dist/utils/common.js.map +1 -0
  96. package/dist/utils/failed_handler.cjs +28 -30
  97. package/dist/utils/failed_handler.cjs.map +1 -0
  98. package/dist/utils/failed_handler.d.cts +9 -0
  99. package/dist/utils/failed_handler.d.cts.map +1 -0
  100. package/dist/utils/failed_handler.d.ts +8 -2
  101. package/dist/utils/failed_handler.d.ts.map +1 -0
  102. package/dist/utils/failed_handler.js +28 -28
  103. package/dist/utils/failed_handler.js.map +1 -0
  104. package/dist/utils/gemini.cjs +1020 -1488
  105. package/dist/utils/gemini.cjs.map +1 -0
  106. package/dist/utils/gemini.d.cts +51 -0
  107. package/dist/utils/gemini.d.cts.map +1 -0
  108. package/dist/utils/gemini.d.ts +51 -48
  109. package/dist/utils/gemini.d.ts.map +1 -0
  110. package/dist/utils/gemini.js +1015 -1479
  111. package/dist/utils/gemini.js.map +1 -0
  112. package/dist/utils/index.cjs +38 -23
  113. package/dist/utils/index.d.cts +8 -0
  114. package/dist/utils/index.d.ts +8 -7
  115. package/dist/utils/index.js +8 -7
  116. package/dist/utils/palm.d.cts +11 -0
  117. package/dist/utils/palm.d.cts.map +1 -0
  118. package/dist/utils/palm.d.ts +9 -4
  119. package/dist/utils/palm.d.ts.map +1 -0
  120. package/dist/utils/safety.cjs +13 -22
  121. package/dist/utils/safety.cjs.map +1 -0
  122. package/dist/utils/safety.d.cts +12 -0
  123. package/dist/utils/safety.d.cts.map +1 -0
  124. package/dist/utils/safety.d.ts +10 -4
  125. package/dist/utils/safety.d.ts.map +1 -0
  126. package/dist/utils/safety.js +13 -19
  127. package/dist/utils/safety.js.map +1 -0
  128. package/dist/utils/stream.cjs +296 -475
  129. package/dist/utils/stream.cjs.map +1 -0
  130. package/dist/utils/stream.d.cts +165 -0
  131. package/dist/utils/stream.d.cts.map +1 -0
  132. package/dist/utils/stream.d.ts +156 -131
  133. package/dist/utils/stream.d.ts.map +1 -0
  134. package/dist/utils/stream.js +293 -469
  135. package/dist/utils/stream.js.map +1 -0
  136. package/dist/utils/zod_to_gemini_parameters.cjs +43 -81
  137. package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -0
  138. package/dist/utils/zod_to_gemini_parameters.d.cts +22 -0
  139. package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -0
  140. package/dist/utils/zod_to_gemini_parameters.d.ts +21 -6
  141. package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -0
  142. package/dist/utils/zod_to_gemini_parameters.js +40 -76
  143. package/dist/utils/zod_to_gemini_parameters.js.map +1 -0
  144. package/package.json +69 -85
  145. package/dist/types-anthropic.cjs +0 -2
  146. package/dist/types-anthropic.js +0 -1
  147. package/dist/utils/anthropic.d.ts +0 -4
  148. package/dist/utils/palm.cjs +0 -2
  149. package/dist/utils/palm.js +0 -1
  150. package/experimental/media.cjs +0 -1
  151. package/experimental/media.d.cts +0 -1
  152. package/experimental/media.d.ts +0 -1
  153. package/experimental/media.js +0 -1
  154. package/experimental/utils/media_core.cjs +0 -1
  155. package/experimental/utils/media_core.d.cts +0 -1
  156. package/experimental/utils/media_core.d.ts +0 -1
  157. package/experimental/utils/media_core.js +0 -1
  158. package/index.cjs +0 -1
  159. package/index.d.cts +0 -1
  160. package/index.d.ts +0 -1
  161. package/index.js +0 -1
  162. package/types.cjs +0 -1
  163. package/types.d.cts +0 -1
  164. package/types.d.ts +0 -1
  165. package/types.js +0 -1
  166. package/utils.cjs +0 -1
  167. package/utils.d.cts +0 -1
  168. package/utils.d.ts +0 -1
  169. package/utils.js +0 -1
@@ -1,468 +1,253 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChatGoogleBase = exports.ChatConnection = void 0;
4
- const env_1 = require("@langchain/core/utils/env");
5
- const chat_models_1 = require("@langchain/core/language_models/chat_models");
6
- const outputs_1 = require("@langchain/core/outputs");
7
- const messages_1 = require("@langchain/core/messages");
8
- const runnables_1 = require("@langchain/core/runnables");
9
- const openai_tools_1 = require("@langchain/core/output_parsers/openai_tools");
10
- const stream_1 = require("@langchain/core/utils/stream");
11
- const types_1 = require("@langchain/core/utils/types");
12
- const common_js_1 = require("./utils/common.cjs");
13
- const connection_js_1 = require("./connection.cjs");
14
- const gemini_js_1 = require("./utils/gemini.cjs");
15
- const auth_js_1 = require("./auth.cjs");
16
- const failed_handler_js_1 = require("./utils/failed_handler.cjs");
17
- const zod_to_gemini_parameters_js_1 = require("./utils/zod_to_gemini_parameters.cjs");
18
- class ChatConnection extends connection_js_1.AbstractGoogleLLMConnection {
19
- constructor(fields, caller, client, streaming) {
20
- super(fields, caller, client, streaming);
21
- Object.defineProperty(this, "convertSystemMessageToHumanContent", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: void 0
26
- });
27
- this.convertSystemMessageToHumanContent =
28
- fields?.convertSystemMessageToHumanContent;
29
- }
30
- get useSystemInstruction() {
31
- return typeof this.convertSystemMessageToHumanContent === "boolean"
32
- ? !this.convertSystemMessageToHumanContent
33
- : this.computeUseSystemInstruction;
34
- }
35
- get computeUseSystemInstruction() {
36
- // This works on models from April 2024 and later
37
- // Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later
38
- // AI Studio: gemini-1.5-pro-latest
39
- if (this.modelFamily === "palm") {
40
- return false;
41
- }
42
- else if (this.modelName === "gemini-1.0-pro-001") {
43
- return false;
44
- }
45
- else if (this.modelName.startsWith("gemini-pro-vision")) {
46
- return false;
47
- }
48
- else if (this.modelName.startsWith("gemini-1.0-pro-vision")) {
49
- return false;
50
- }
51
- else if (this.modelName === "gemini-pro" && this.platform === "gai") {
52
- // on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001
53
- return false;
54
- }
55
- else if (this.modelFamily === "gemma") {
56
- // At least as of 12 Mar 2025 gemma 3 on AIS, trying to use system instructions yields an error:
57
- // "Developer instruction is not enabled for models/gemma-3-27b-it"
58
- return false;
59
- }
60
- return true;
61
- }
62
- computeGoogleSearchToolAdjustmentFromModel() {
63
- if (this.modelName.startsWith("gemini-1.0")) {
64
- return "googleSearchRetrieval";
65
- }
66
- else if (this.modelName.startsWith("gemini-1.5")) {
67
- return "googleSearchRetrieval";
68
- }
69
- else {
70
- return "googleSearch";
71
- }
72
- }
73
- computeGoogleSearchToolAdjustment(apiConfig) {
74
- const adj = apiConfig.googleSearchToolAdjustment;
75
- if (adj === undefined || adj === true) {
76
- return this.computeGoogleSearchToolAdjustmentFromModel();
77
- }
78
- else {
79
- return adj;
80
- }
81
- }
82
- buildGeminiAPI() {
83
- const apiConfig = this.apiConfig ?? {};
84
- const googleSearchToolAdjustment = this.computeGoogleSearchToolAdjustment(apiConfig);
85
- const geminiConfig = {
86
- useSystemInstruction: this.useSystemInstruction,
87
- googleSearchToolAdjustment,
88
- ...apiConfig,
89
- };
90
- return (0, gemini_js_1.getGeminiAPI)(geminiConfig);
91
- }
92
- get api() {
93
- switch (this.apiName) {
94
- case "google":
95
- return this.buildGeminiAPI();
96
- default:
97
- return super.api;
98
- }
99
- }
100
- }
101
- exports.ChatConnection = ChatConnection;
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ const require_zod_to_gemini_parameters = require('./utils/zod_to_gemini_parameters.cjs');
3
+ const require_gemini = require('./utils/gemini.cjs');
4
+ const require_common = require('./utils/common.cjs');
5
+ const require_failed_handler = require('./utils/failed_handler.cjs');
6
+ const require_connection = require('./connection.cjs');
7
+ const require_auth = require('./auth.cjs');
8
+ const __langchain_core_utils_env = require_rolldown_runtime.__toESM(require("@langchain/core/utils/env"));
9
+ const __langchain_core_language_models_chat_models = require_rolldown_runtime.__toESM(require("@langchain/core/language_models/chat_models"));
10
+ const __langchain_core_outputs = require_rolldown_runtime.__toESM(require("@langchain/core/outputs"));
11
+ const __langchain_core_messages = require_rolldown_runtime.__toESM(require("@langchain/core/messages"));
12
+ const __langchain_core_runnables = require_rolldown_runtime.__toESM(require("@langchain/core/runnables"));
13
+ const __langchain_core_output_parsers_openai_tools = require_rolldown_runtime.__toESM(require("@langchain/core/output_parsers/openai_tools"));
14
+ const __langchain_core_utils_stream = require_rolldown_runtime.__toESM(require("@langchain/core/utils/stream"));
15
+ const __langchain_core_utils_types = require_rolldown_runtime.__toESM(require("@langchain/core/utils/types"));
16
+
17
+ //#region src/chat_models.ts
18
+ var ChatConnection = class extends require_connection.AbstractGoogleLLMConnection {
19
+ convertSystemMessageToHumanContent;
20
+ constructor(fields, caller, client, streaming) {
21
+ super(fields, caller, client, streaming);
22
+ this.convertSystemMessageToHumanContent = fields?.convertSystemMessageToHumanContent;
23
+ }
24
+ get useSystemInstruction() {
25
+ return typeof this.convertSystemMessageToHumanContent === "boolean" ? !this.convertSystemMessageToHumanContent : this.computeUseSystemInstruction;
26
+ }
27
+ get computeUseSystemInstruction() {
28
+ if (this.modelFamily === "palm") return false;
29
+ else if (this.modelName === "gemini-1.0-pro-001") return false;
30
+ else if (this.modelName.startsWith("gemini-pro-vision")) return false;
31
+ else if (this.modelName.startsWith("gemini-1.0-pro-vision")) return false;
32
+ else if (this.modelName === "gemini-pro" && this.platform === "gai") return false;
33
+ else if (this.modelFamily === "gemma") return false;
34
+ return true;
35
+ }
36
+ computeGoogleSearchToolAdjustmentFromModel() {
37
+ if (this.modelName.startsWith("gemini-1.0")) return "googleSearchRetrieval";
38
+ else if (this.modelName.startsWith("gemini-1.5")) return "googleSearchRetrieval";
39
+ else return "googleSearch";
40
+ }
41
+ computeGoogleSearchToolAdjustment(apiConfig) {
42
+ const adj = apiConfig.googleSearchToolAdjustment;
43
+ if (adj === void 0 || adj === true) return this.computeGoogleSearchToolAdjustmentFromModel();
44
+ else return adj;
45
+ }
46
+ buildGeminiAPI() {
47
+ const apiConfig = this.apiConfig ?? {};
48
+ const googleSearchToolAdjustment = this.computeGoogleSearchToolAdjustment(apiConfig);
49
+ const geminiConfig = {
50
+ useSystemInstruction: this.useSystemInstruction,
51
+ googleSearchToolAdjustment,
52
+ ...apiConfig
53
+ };
54
+ return require_gemini.getGeminiAPI(geminiConfig);
55
+ }
56
+ get api() {
57
+ switch (this.apiName) {
58
+ case "google": return this.buildGeminiAPI();
59
+ default: return super.api;
60
+ }
61
+ }
62
+ };
102
63
  /**
103
- * Integration with a Google chat model.
104
- */
105
- class ChatGoogleBase extends chat_models_1.BaseChatModel {
106
- // Used for tracing, replace with the same name as your class
107
- static lc_name() {
108
- return "ChatGoogle";
109
- }
110
- get lc_secrets() {
111
- return {
112
- authOptions: "GOOGLE_AUTH_OPTIONS",
113
- };
114
- }
115
- constructor(fields) {
116
- super((0, failed_handler_js_1.ensureParams)(fields));
117
- Object.defineProperty(this, "lc_serializable", {
118
- enumerable: true,
119
- configurable: true,
120
- writable: true,
121
- value: true
122
- });
123
- // Set based on modelName
124
- Object.defineProperty(this, "model", {
125
- enumerable: true,
126
- configurable: true,
127
- writable: true,
128
- value: void 0
129
- });
130
- Object.defineProperty(this, "modelName", {
131
- enumerable: true,
132
- configurable: true,
133
- writable: true,
134
- value: "gemini-pro"
135
- });
136
- Object.defineProperty(this, "temperature", {
137
- enumerable: true,
138
- configurable: true,
139
- writable: true,
140
- value: void 0
141
- });
142
- Object.defineProperty(this, "maxOutputTokens", {
143
- enumerable: true,
144
- configurable: true,
145
- writable: true,
146
- value: void 0
147
- });
148
- Object.defineProperty(this, "maxReasoningTokens", {
149
- enumerable: true,
150
- configurable: true,
151
- writable: true,
152
- value: void 0
153
- });
154
- Object.defineProperty(this, "topP", {
155
- enumerable: true,
156
- configurable: true,
157
- writable: true,
158
- value: void 0
159
- });
160
- Object.defineProperty(this, "topK", {
161
- enumerable: true,
162
- configurable: true,
163
- writable: true,
164
- value: void 0
165
- });
166
- Object.defineProperty(this, "seed", {
167
- enumerable: true,
168
- configurable: true,
169
- writable: true,
170
- value: void 0
171
- });
172
- Object.defineProperty(this, "presencePenalty", {
173
- enumerable: true,
174
- configurable: true,
175
- writable: true,
176
- value: void 0
177
- });
178
- Object.defineProperty(this, "frequencyPenalty", {
179
- enumerable: true,
180
- configurable: true,
181
- writable: true,
182
- value: void 0
183
- });
184
- Object.defineProperty(this, "stopSequences", {
185
- enumerable: true,
186
- configurable: true,
187
- writable: true,
188
- value: []
189
- });
190
- Object.defineProperty(this, "logprobs", {
191
- enumerable: true,
192
- configurable: true,
193
- writable: true,
194
- value: void 0
195
- });
196
- Object.defineProperty(this, "topLogprobs", {
197
- enumerable: true,
198
- configurable: true,
199
- writable: true,
200
- value: 0
201
- });
202
- Object.defineProperty(this, "safetySettings", {
203
- enumerable: true,
204
- configurable: true,
205
- writable: true,
206
- value: []
207
- });
208
- Object.defineProperty(this, "responseModalities", {
209
- enumerable: true,
210
- configurable: true,
211
- writable: true,
212
- value: void 0
213
- });
214
- // May intentionally be undefined, meaning to compute this.
215
- Object.defineProperty(this, "convertSystemMessageToHumanContent", {
216
- enumerable: true,
217
- configurable: true,
218
- writable: true,
219
- value: void 0
220
- });
221
- Object.defineProperty(this, "safetyHandler", {
222
- enumerable: true,
223
- configurable: true,
224
- writable: true,
225
- value: void 0
226
- });
227
- Object.defineProperty(this, "speechConfig", {
228
- enumerable: true,
229
- configurable: true,
230
- writable: true,
231
- value: void 0
232
- });
233
- Object.defineProperty(this, "streamUsage", {
234
- enumerable: true,
235
- configurable: true,
236
- writable: true,
237
- value: true
238
- });
239
- Object.defineProperty(this, "streaming", {
240
- enumerable: true,
241
- configurable: true,
242
- writable: true,
243
- value: false
244
- });
245
- Object.defineProperty(this, "labels", {
246
- enumerable: true,
247
- configurable: true,
248
- writable: true,
249
- value: void 0
250
- });
251
- Object.defineProperty(this, "connection", {
252
- enumerable: true,
253
- configurable: true,
254
- writable: true,
255
- value: void 0
256
- });
257
- Object.defineProperty(this, "streamedConnection", {
258
- enumerable: true,
259
- configurable: true,
260
- writable: true,
261
- value: void 0
262
- });
263
- (0, common_js_1.copyAndValidateModelParamsInto)(fields, this);
264
- this.safetyHandler =
265
- fields?.safetyHandler ?? new gemini_js_1.DefaultGeminiSafetyHandler();
266
- this.streamUsage = fields?.streamUsage ?? this.streamUsage;
267
- const client = this.buildClient(fields);
268
- this.buildConnection(fields ?? {}, client);
269
- }
270
- getLsParams(options) {
271
- const params = this.invocationParams(options);
272
- return {
273
- ls_provider: "google_vertexai",
274
- ls_model_name: this.model,
275
- ls_model_type: "chat",
276
- ls_temperature: params.temperature ?? undefined,
277
- ls_max_tokens: params.maxOutputTokens ?? undefined,
278
- ls_stop: options.stop,
279
- };
280
- }
281
- buildApiKeyClient(apiKey) {
282
- return new auth_js_1.ApiKeyGoogleAuth(apiKey);
283
- }
284
- buildApiKey(fields) {
285
- return fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("GOOGLE_API_KEY");
286
- }
287
- buildClient(fields) {
288
- const apiKey = this.buildApiKey(fields);
289
- if (apiKey) {
290
- return this.buildApiKeyClient(apiKey);
291
- }
292
- else {
293
- return this.buildAbstractedClient(fields);
294
- }
295
- }
296
- buildConnection(fields, client) {
297
- this.connection = new ChatConnection({ ...fields, ...this }, this.caller, client, false);
298
- this.streamedConnection = new ChatConnection({ ...fields, ...this }, this.caller, client, true);
299
- }
300
- get platform() {
301
- return this.connection.platform;
302
- }
303
- bindTools(tools, kwargs) {
304
- return this.withConfig({ tools: (0, common_js_1.convertToGeminiTools)(tools), ...kwargs });
305
- }
306
- // Replace
307
- _llmType() {
308
- return "chat_integration";
309
- }
310
- /**
311
- * Get the parameters used to invoke the model
312
- */
313
- invocationParams(options) {
314
- return (0, common_js_1.copyAIModelParams)(this, options);
315
- }
316
- async _generate(messages, options, runManager) {
317
- const parameters = this.invocationParams(options);
318
- if (this.streaming) {
319
- const stream = this._streamResponseChunks(messages, options, runManager);
320
- let finalChunk = null;
321
- for await (const chunk of stream) {
322
- finalChunk = !finalChunk ? chunk : (0, stream_1.concat)(finalChunk, chunk);
323
- }
324
- if (!finalChunk) {
325
- throw new Error("No chunks were returned from the stream.");
326
- }
327
- return {
328
- generations: [finalChunk],
329
- };
330
- }
331
- const response = await this.connection.request(messages, parameters, options, runManager);
332
- const ret = this.connection.api.responseToChatResult(response);
333
- const chunk = ret?.generations?.[0];
334
- if (chunk) {
335
- await runManager?.handleLLMNewToken(chunk.text || "");
336
- }
337
- return ret;
338
- }
339
- async *_streamResponseChunks(_messages, options, runManager) {
340
- // Make the call as a streaming request
341
- const parameters = this.invocationParams(options);
342
- const response = await this.streamedConnection.request(_messages, parameters, options, runManager);
343
- // Get the streaming parser of the response
344
- const stream = response.data;
345
- let usageMetadata;
346
- // Loop until the end of the stream
347
- // During the loop, yield each time we get a chunk from the streaming parser
348
- // that is either available or added to the queue
349
- while (!stream.streamDone) {
350
- const output = await stream.nextChunk();
351
- await runManager?.handleCustomEvent(`google-chunk-${this.constructor.name}`, {
352
- output,
353
- });
354
- if (output &&
355
- output.usageMetadata &&
356
- this.streamUsage !== false &&
357
- options.streamUsage !== false) {
358
- usageMetadata = {
359
- input_tokens: output.usageMetadata.promptTokenCount,
360
- output_tokens: output.usageMetadata.candidatesTokenCount,
361
- total_tokens: output.usageMetadata.totalTokenCount,
362
- };
363
- }
364
- const chunk = output !== null
365
- ? this.connection.api.responseToChatGeneration({ data: output })
366
- : new outputs_1.ChatGenerationChunk({
367
- text: "",
368
- generationInfo: { finishReason: "stop" },
369
- message: new messages_1.AIMessageChunk({
370
- content: "",
371
- usage_metadata: usageMetadata,
372
- }),
373
- });
374
- if (chunk) {
375
- yield chunk;
376
- await runManager?.handleLLMNewToken(chunk.text ?? "", undefined, undefined, undefined, undefined, { chunk });
377
- }
378
- }
379
- }
380
- /** @ignore */
381
- _combineLLMOutput() {
382
- return [];
383
- }
384
- withStructuredOutput(outputSchema, config) {
385
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
386
- const schema = outputSchema;
387
- const name = config?.name;
388
- const method = config?.method;
389
- const includeRaw = config?.includeRaw;
390
- if (method === "jsonMode") {
391
- throw new Error(`Google only supports "functionCalling" as a method.`);
392
- }
393
- let functionName = name ?? "extract";
394
- let outputParser;
395
- let tools;
396
- if ((0, types_1.isInteropZodSchema)(schema)) {
397
- const jsonSchema = (0, zod_to_gemini_parameters_js_1.schemaToGeminiParameters)(schema);
398
- tools = [
399
- {
400
- functionDeclarations: [
401
- {
402
- name: functionName,
403
- description: jsonSchema.description ?? "A function available to call.",
404
- parameters: jsonSchema,
405
- },
406
- ],
407
- },
408
- ];
409
- outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
410
- returnSingle: true,
411
- keyName: functionName,
412
- zodSchema: schema,
413
- });
414
- }
415
- else {
416
- let geminiFunctionDefinition;
417
- if (typeof schema.name === "string" &&
418
- typeof schema.parameters === "object" &&
419
- schema.parameters != null) {
420
- geminiFunctionDefinition = schema;
421
- functionName = schema.name;
422
- }
423
- else {
424
- // We are providing the schema for *just* the parameters, probably
425
- const parameters = (0, zod_to_gemini_parameters_js_1.removeAdditionalProperties)(schema);
426
- geminiFunctionDefinition = {
427
- name: functionName,
428
- description: schema.description ?? "",
429
- parameters,
430
- };
431
- }
432
- tools = [
433
- {
434
- functionDeclarations: [geminiFunctionDefinition],
435
- },
436
- ];
437
- outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
438
- returnSingle: true,
439
- keyName: functionName,
440
- });
441
- }
442
- const llm = this.bindTools(tools).withConfig({ tool_choice: functionName });
443
- if (!includeRaw) {
444
- return llm.pipe(outputParser).withConfig({
445
- runName: "ChatGoogleStructuredOutput",
446
- });
447
- }
448
- const parserAssign = runnables_1.RunnablePassthrough.assign({
449
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
450
- parsed: (input, config) => outputParser.invoke(input.raw, config),
451
- });
452
- const parserNone = runnables_1.RunnablePassthrough.assign({
453
- parsed: () => null,
454
- });
455
- const parsedWithFallback = parserAssign.withFallbacks({
456
- fallbacks: [parserNone],
457
- });
458
- return runnables_1.RunnableSequence.from([
459
- {
460
- raw: llm,
461
- },
462
- parsedWithFallback,
463
- ]).withConfig({
464
- runName: "StructuredOutputRunnable",
465
- });
466
- }
467
- }
64
+ * Integration with a Google chat model.
65
+ */
66
+ var ChatGoogleBase = class extends __langchain_core_language_models_chat_models.BaseChatModel {
67
+ static lc_name() {
68
+ return "ChatGoogle";
69
+ }
70
+ get lc_secrets() {
71
+ return { authOptions: "GOOGLE_AUTH_OPTIONS" };
72
+ }
73
+ lc_serializable = true;
74
+ model;
75
+ modelName = "gemini-pro";
76
+ temperature;
77
+ maxOutputTokens;
78
+ maxReasoningTokens;
79
+ topP;
80
+ topK;
81
+ seed;
82
+ presencePenalty;
83
+ frequencyPenalty;
84
+ stopSequences = [];
85
+ logprobs;
86
+ topLogprobs = 0;
87
+ safetySettings = [];
88
+ responseModalities;
89
+ convertSystemMessageToHumanContent;
90
+ safetyHandler;
91
+ speechConfig;
92
+ streamUsage = true;
93
+ streaming = false;
94
+ labels;
95
+ connection;
96
+ streamedConnection;
97
+ constructor(fields) {
98
+ super(require_failed_handler.ensureParams(fields));
99
+ require_common.copyAndValidateModelParamsInto(fields, this);
100
+ this.safetyHandler = fields?.safetyHandler ?? new require_gemini.DefaultGeminiSafetyHandler();
101
+ this.streamUsage = fields?.streamUsage ?? this.streamUsage;
102
+ const client = this.buildClient(fields);
103
+ this.buildConnection(fields ?? {}, client);
104
+ }
105
+ getLsParams(options) {
106
+ const params = this.invocationParams(options);
107
+ return {
108
+ ls_provider: "google_vertexai",
109
+ ls_model_name: this.model,
110
+ ls_model_type: "chat",
111
+ ls_temperature: params.temperature ?? void 0,
112
+ ls_max_tokens: params.maxOutputTokens ?? void 0,
113
+ ls_stop: options.stop
114
+ };
115
+ }
116
+ buildApiKeyClient(apiKey) {
117
+ return new require_auth.ApiKeyGoogleAuth(apiKey);
118
+ }
119
+ buildApiKey(fields) {
120
+ return fields?.apiKey ?? (0, __langchain_core_utils_env.getEnvironmentVariable)("GOOGLE_API_KEY");
121
+ }
122
+ buildClient(fields) {
123
+ const apiKey = this.buildApiKey(fields);
124
+ if (apiKey) return this.buildApiKeyClient(apiKey);
125
+ else return this.buildAbstractedClient(fields);
126
+ }
127
+ buildConnection(fields, client) {
128
+ this.connection = new ChatConnection({
129
+ ...fields,
130
+ ...this
131
+ }, this.caller, client, false);
132
+ this.streamedConnection = new ChatConnection({
133
+ ...fields,
134
+ ...this
135
+ }, this.caller, client, true);
136
+ }
137
+ get platform() {
138
+ return this.connection.platform;
139
+ }
140
+ bindTools(tools, kwargs) {
141
+ return this.withConfig({
142
+ tools: require_common.convertToGeminiTools(tools),
143
+ ...kwargs
144
+ });
145
+ }
146
+ _llmType() {
147
+ return "chat_integration";
148
+ }
149
+ /**
150
+ * Get the parameters used to invoke the model
151
+ */
152
+ invocationParams(options) {
153
+ return require_common.copyAIModelParams(this, options);
154
+ }
155
+ async _generate(messages, options, runManager) {
156
+ const parameters = this.invocationParams(options);
157
+ if (this.streaming) {
158
+ const stream = this._streamResponseChunks(messages, options, runManager);
159
+ let finalChunk = null;
160
+ for await (const chunk$1 of stream) finalChunk = !finalChunk ? chunk$1 : (0, __langchain_core_utils_stream.concat)(finalChunk, chunk$1);
161
+ if (!finalChunk) throw new Error("No chunks were returned from the stream.");
162
+ return { generations: [finalChunk] };
163
+ }
164
+ const response = await this.connection.request(messages, parameters, options, runManager);
165
+ const ret = this.connection.api.responseToChatResult(response);
166
+ const chunk = ret?.generations?.[0];
167
+ if (chunk) await runManager?.handleLLMNewToken(chunk.text || "");
168
+ return ret;
169
+ }
170
+ async *_streamResponseChunks(_messages, options, runManager) {
171
+ const parameters = this.invocationParams(options);
172
+ const response = await this.streamedConnection.request(_messages, parameters, options, runManager);
173
+ const stream = response.data;
174
+ let usageMetadata;
175
+ while (!stream.streamDone) {
176
+ const output = await stream.nextChunk();
177
+ await runManager?.handleCustomEvent(`google-chunk-${this.constructor.name}`, { output });
178
+ if (output && output.usageMetadata && this.streamUsage !== false && options.streamUsage !== false) usageMetadata = {
179
+ input_tokens: output.usageMetadata.promptTokenCount,
180
+ output_tokens: output.usageMetadata.candidatesTokenCount,
181
+ total_tokens: output.usageMetadata.totalTokenCount
182
+ };
183
+ const chunk = output !== null ? this.connection.api.responseToChatGeneration({ data: output }) : new __langchain_core_outputs.ChatGenerationChunk({
184
+ text: "",
185
+ generationInfo: { finishReason: "stop" },
186
+ message: new __langchain_core_messages.AIMessageChunk({
187
+ content: "",
188
+ usage_metadata: usageMetadata
189
+ })
190
+ });
191
+ if (chunk) {
192
+ yield chunk;
193
+ await runManager?.handleLLMNewToken(chunk.text ?? "", void 0, void 0, void 0, void 0, { chunk });
194
+ }
195
+ }
196
+ }
197
+ /** @ignore */
198
+ _combineLLMOutput() {
199
+ return [];
200
+ }
201
+ withStructuredOutput(outputSchema, config) {
202
+ const schema = outputSchema;
203
+ const name = config?.name;
204
+ const method = config?.method;
205
+ const includeRaw = config?.includeRaw;
206
+ if (method === "jsonMode") throw new Error(`Google only supports "functionCalling" as a method.`);
207
+ let functionName = name ?? "extract";
208
+ let outputParser;
209
+ let tools;
210
+ if ((0, __langchain_core_utils_types.isInteropZodSchema)(schema)) {
211
+ const jsonSchema = require_zod_to_gemini_parameters.schemaToGeminiParameters(schema);
212
+ tools = [{ functionDeclarations: [{
213
+ name: functionName,
214
+ description: jsonSchema.description ?? "A function available to call.",
215
+ parameters: jsonSchema
216
+ }] }];
217
+ outputParser = new __langchain_core_output_parsers_openai_tools.JsonOutputKeyToolsParser({
218
+ returnSingle: true,
219
+ keyName: functionName,
220
+ zodSchema: schema
221
+ });
222
+ } else {
223
+ let geminiFunctionDefinition;
224
+ if (typeof schema.name === "string" && typeof schema.parameters === "object" && schema.parameters != null) {
225
+ geminiFunctionDefinition = schema;
226
+ functionName = schema.name;
227
+ } else {
228
+ const parameters = require_zod_to_gemini_parameters.removeAdditionalProperties(schema);
229
+ geminiFunctionDefinition = {
230
+ name: functionName,
231
+ description: schema.description ?? "",
232
+ parameters
233
+ };
234
+ }
235
+ tools = [{ functionDeclarations: [geminiFunctionDefinition] }];
236
+ outputParser = new __langchain_core_output_parsers_openai_tools.JsonOutputKeyToolsParser({
237
+ returnSingle: true,
238
+ keyName: functionName
239
+ });
240
+ }
241
+ const llm = this.bindTools(tools).withConfig({ tool_choice: functionName });
242
+ if (!includeRaw) return llm.pipe(outputParser).withConfig({ runName: "ChatGoogleStructuredOutput" });
243
+ const parserAssign = __langchain_core_runnables.RunnablePassthrough.assign({ parsed: (input, config$1) => outputParser.invoke(input.raw, config$1) });
244
+ const parserNone = __langchain_core_runnables.RunnablePassthrough.assign({ parsed: () => null });
245
+ const parsedWithFallback = parserAssign.withFallbacks({ fallbacks: [parserNone] });
246
+ return __langchain_core_runnables.RunnableSequence.from([{ raw: llm }, parsedWithFallback]).withConfig({ runName: "StructuredOutputRunnable" });
247
+ }
248
+ };
249
+
250
+ //#endregion
251
+ exports.ChatConnection = ChatConnection;
468
252
  exports.ChatGoogleBase = ChatGoogleBase;
253
+ //# sourceMappingURL=chat_models.cjs.map