@langchain/google-common 0.2.17 → 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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +6 -6
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/auth.cjs +82 -116
- package/dist/auth.cjs.map +1 -0
- package/dist/auth.d.cts +46 -0
- package/dist/auth.d.cts.map +1 -0
- package/dist/auth.d.ts +41 -36
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +80 -110
- package/dist/auth.js.map +1 -0
- package/dist/chat_models.cjs +251 -466
- package/dist/chat_models.cjs.map +1 -0
- package/dist/chat_models.d.cts +98 -0
- package/dist/chat_models.d.cts.map +1 -0
- package/dist/chat_models.d.ts +87 -73
- package/dist/chat_models.d.ts.map +1 -0
- package/dist/chat_models.js +245 -457
- package/dist/chat_models.js.map +1 -0
- package/dist/connection.cjs +321 -466
- package/dist/connection.cjs.map +1 -0
- package/dist/connection.d.cts +109 -0
- package/dist/connection.d.cts.map +1 -0
- package/dist/connection.d.ts +98 -91
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +317 -459
- package/dist/connection.js.map +1 -0
- package/dist/embeddings.cjs +135 -186
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +44 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +38 -32
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +133 -181
- package/dist/embeddings.js.map +1 -0
- package/dist/experimental/media.cjs +380 -482
- package/dist/experimental/media.cjs.map +1 -0
- package/dist/experimental/media.d.cts +198 -0
- package/dist/experimental/media.d.cts.map +1 -0
- package/dist/experimental/media.d.ts +190 -202
- package/dist/experimental/media.d.ts.map +1 -0
- package/dist/experimental/media.js +369 -468
- package/dist/experimental/media.js.map +1 -0
- package/dist/experimental/utils/media_core.cjs +403 -517
- package/dist/experimental/utils/media_core.cjs.map +1 -0
- package/dist/experimental/utils/media_core.d.cts +215 -0
- package/dist/experimental/utils/media_core.d.cts.map +1 -0
- package/dist/experimental/utils/media_core.d.ts +171 -165
- package/dist/experimental/utils/media_core.d.ts.map +1 -0
- package/dist/experimental/utils/media_core.js +395 -506
- package/dist/experimental/utils/media_core.js.map +1 -0
- package/dist/index.cjs +58 -27
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -11
- package/dist/index.js +13 -11
- package/dist/llms.cjs +157 -244
- package/dist/llms.cjs.map +1 -0
- package/dist/llms.d.cts +72 -0
- package/dist/llms.d.cts.map +1 -0
- package/dist/llms.d.ts +64 -54
- package/dist/llms.d.ts.map +1 -0
- package/dist/llms.js +154 -238
- package/dist/llms.js.map +1 -0
- package/dist/output_parsers.cjs +148 -173
- package/dist/output_parsers.cjs.map +1 -0
- package/dist/output_parsers.d.cts +53 -0
- package/dist/output_parsers.d.cts.map +1 -0
- package/dist/output_parsers.d.ts +46 -42
- package/dist/output_parsers.d.ts.map +1 -0
- package/dist/output_parsers.js +146 -168
- package/dist/output_parsers.js.map +1 -0
- package/dist/types-anthropic.d.cts +229 -0
- package/dist/types-anthropic.d.cts.map +1 -0
- package/dist/types-anthropic.d.ts +221 -215
- package/dist/types-anthropic.d.ts.map +1 -0
- package/dist/types.cjs +51 -62
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +748 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +669 -656
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +46 -45
- package/dist/types.js.map +1 -0
- package/dist/utils/anthropic.cjs +598 -821
- package/dist/utils/anthropic.cjs.map +1 -0
- package/dist/utils/anthropic.js +597 -818
- package/dist/utils/anthropic.js.map +1 -0
- package/dist/utils/common.cjs +130 -211
- package/dist/utils/common.cjs.map +1 -0
- package/dist/utils/common.d.cts +13 -0
- package/dist/utils/common.d.cts.map +1 -0
- package/dist/utils/common.d.ts +12 -7
- package/dist/utils/common.d.ts.map +1 -0
- package/dist/utils/common.js +128 -207
- package/dist/utils/common.js.map +1 -0
- package/dist/utils/failed_handler.cjs +28 -30
- package/dist/utils/failed_handler.cjs.map +1 -0
- package/dist/utils/failed_handler.d.cts +9 -0
- package/dist/utils/failed_handler.d.cts.map +1 -0
- package/dist/utils/failed_handler.d.ts +8 -2
- package/dist/utils/failed_handler.d.ts.map +1 -0
- package/dist/utils/failed_handler.js +28 -28
- package/dist/utils/failed_handler.js.map +1 -0
- package/dist/utils/gemini.cjs +1020 -1488
- package/dist/utils/gemini.cjs.map +1 -0
- package/dist/utils/gemini.d.cts +51 -0
- package/dist/utils/gemini.d.cts.map +1 -0
- package/dist/utils/gemini.d.ts +51 -48
- package/dist/utils/gemini.d.ts.map +1 -0
- package/dist/utils/gemini.js +1015 -1479
- package/dist/utils/gemini.js.map +1 -0
- package/dist/utils/index.cjs +38 -23
- package/dist/utils/index.d.cts +8 -0
- package/dist/utils/index.d.ts +8 -7
- package/dist/utils/index.js +8 -7
- package/dist/utils/palm.d.cts +11 -0
- package/dist/utils/palm.d.cts.map +1 -0
- package/dist/utils/palm.d.ts +9 -4
- package/dist/utils/palm.d.ts.map +1 -0
- package/dist/utils/safety.cjs +13 -22
- package/dist/utils/safety.cjs.map +1 -0
- package/dist/utils/safety.d.cts +12 -0
- package/dist/utils/safety.d.cts.map +1 -0
- package/dist/utils/safety.d.ts +10 -4
- package/dist/utils/safety.d.ts.map +1 -0
- package/dist/utils/safety.js +13 -19
- package/dist/utils/safety.js.map +1 -0
- package/dist/utils/stream.cjs +296 -475
- package/dist/utils/stream.cjs.map +1 -0
- package/dist/utils/stream.d.cts +165 -0
- package/dist/utils/stream.d.cts.map +1 -0
- package/dist/utils/stream.d.ts +156 -131
- package/dist/utils/stream.d.ts.map +1 -0
- package/dist/utils/stream.js +293 -469
- package/dist/utils/stream.js.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.cjs +43 -81
- package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.d.cts +22 -0
- package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.d.ts +21 -6
- package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.js +40 -76
- package/dist/utils/zod_to_gemini_parameters.js.map +1 -0
- package/package.json +69 -85
- package/dist/types-anthropic.cjs +0 -2
- package/dist/types-anthropic.js +0 -1
- package/dist/utils/anthropic.d.ts +0 -4
- package/dist/utils/palm.cjs +0 -2
- package/dist/utils/palm.js +0 -1
- package/experimental/media.cjs +0 -1
- package/experimental/media.d.cts +0 -1
- package/experimental/media.d.ts +0 -1
- package/experimental/media.js +0 -1
- package/experimental/utils/media_core.cjs +0 -1
- package/experimental/utils/media_core.d.cts +0 -1
- package/experimental/utils/media_core.d.ts +0 -1
- package/experimental/utils/media_core.js +0 -1
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/types.cjs +0 -1
- package/types.d.cts +0 -1
- package/types.d.ts +0 -1
- package/types.js +0 -1
- package/utils.cjs +0 -1
- package/utils.d.cts +0 -1
- package/utils.d.ts +0 -1
- package/utils.js +0 -1
package/dist/chat_models.cjs
CHANGED
|
@@ -1,468 +1,253 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|