@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.
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,461 +1,319 @@
1
+ import { getGeminiAPI } from "./utils/gemini.js";
2
+ import { getAnthropicAPI } from "./utils/anthropic.js";
3
+ import { modelToFamily, modelToPublisher } from "./utils/common.js";
4
+ import "./utils/index.js";
1
5
  import { getRuntimeEnvironment } from "@langchain/core/utils/env";
2
6
  import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
3
- import { getGeminiAPI, modelToFamily, modelToPublisher, } from "./utils/index.js";
4
- import { getAnthropicAPI } from "./utils/anthropic.js";
5
- export class GoogleConnection {
6
- constructor(caller, client, streaming) {
7
- Object.defineProperty(this, "caller", {
8
- enumerable: true,
9
- configurable: true,
10
- writable: true,
11
- value: void 0
12
- });
13
- Object.defineProperty(this, "client", {
14
- enumerable: true,
15
- configurable: true,
16
- writable: true,
17
- value: void 0
18
- });
19
- Object.defineProperty(this, "streaming", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: void 0
24
- });
25
- this.caller = caller;
26
- this.client = client;
27
- this.streaming = streaming ?? false;
28
- }
29
- async _clientInfoHeaders() {
30
- const { userAgent, clientLibraryVersion } = await this._getClientInfo();
31
- return {
32
- "User-Agent": userAgent,
33
- "Client-Info": clientLibraryVersion,
34
- };
35
- }
36
- async _getClientInfo() {
37
- const env = await getRuntimeEnvironment();
38
- const langchain = env?.library ?? "langchain-js";
39
- // TODO: Add an API for getting the current LangChain version
40
- const langchainVersion = "0";
41
- const moduleName = await this._moduleName();
42
- let clientLibraryVersion = `${langchain}/${langchainVersion}`;
43
- if (moduleName && moduleName.length) {
44
- clientLibraryVersion = `${clientLibraryVersion}-${moduleName}`;
45
- }
46
- return {
47
- userAgent: clientLibraryVersion,
48
- clientLibraryVersion: `${langchainVersion}-${moduleName}`,
49
- };
50
- }
51
- async _moduleName() {
52
- return this.constructor.name;
53
- }
54
- async additionalHeaders() {
55
- return {};
56
- }
57
- async _buildOpts(data, _options, requestHeaders = {}) {
58
- const url = await this.buildUrl();
59
- const method = this.buildMethod();
60
- const infoHeaders = (await this._clientInfoHeaders()) ?? {};
61
- const additionalHeaders = (await this.additionalHeaders()) ?? {};
62
- const headers = {
63
- ...infoHeaders,
64
- ...additionalHeaders,
65
- ...requestHeaders,
66
- };
67
- const opts = {
68
- url,
69
- method,
70
- headers,
71
- };
72
- if (data && method === "POST") {
73
- opts.data = data;
74
- }
75
- if (this.streaming) {
76
- opts.responseType = "stream";
77
- }
78
- else {
79
- opts.responseType = "json";
80
- }
81
- return opts;
82
- }
83
- async _request(data, options, requestHeaders = {}) {
84
- const opts = await this._buildOpts(data, options, requestHeaders);
85
- const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, async () => this.client.request(opts));
86
- const response = callResponse; // Done for typecast safety, I guess
87
- return response;
88
- }
89
- }
90
- export class GoogleHostConnection extends GoogleConnection {
91
- constructor(fields, caller, client, streaming) {
92
- super(caller, client, streaming);
93
- // This does not default to a value intentionally.
94
- // Use the "platform" getter if you need this.
95
- Object.defineProperty(this, "platformType", {
96
- enumerable: true,
97
- configurable: true,
98
- writable: true,
99
- value: void 0
100
- });
101
- Object.defineProperty(this, "_endpoint", {
102
- enumerable: true,
103
- configurable: true,
104
- writable: true,
105
- value: void 0
106
- });
107
- Object.defineProperty(this, "_location", {
108
- enumerable: true,
109
- configurable: true,
110
- writable: true,
111
- value: void 0
112
- });
113
- Object.defineProperty(this, "_apiVersion", {
114
- enumerable: true,
115
- configurable: true,
116
- writable: true,
117
- value: void 0
118
- });
119
- this.caller = caller;
120
- this.platformType = this.fieldPlatformType(fields);
121
- this._endpoint = fields?.endpoint;
122
- this._location = fields?.location;
123
- this._apiVersion = fields?.apiVersion;
124
- this.client = client;
125
- }
126
- fieldPlatformType(fields) {
127
- if (typeof fields === "undefined") {
128
- return undefined;
129
- }
130
- if (typeof fields.platformType !== "undefined") {
131
- return fields.platformType;
132
- }
133
- if (fields.vertexai === true) {
134
- return "gcp";
135
- }
136
- return undefined;
137
- }
138
- get platform() {
139
- return this.platformType ?? this.computedPlatformType;
140
- }
141
- get computedPlatformType() {
142
- return "gcp";
143
- }
144
- get computedApiVersion() {
145
- return "v1";
146
- }
147
- get apiVersion() {
148
- return this._apiVersion ?? this.computedApiVersion;
149
- }
150
- get location() {
151
- return this._location ?? this.computedLocation;
152
- }
153
- get computedLocation() {
154
- return "us-central1";
155
- }
156
- get endpoint() {
157
- return this._endpoint ?? this.computedEndpoint;
158
- }
159
- get computedEndpoint() {
160
- if (this.location === "global") {
161
- return "aiplatform.googleapis.com";
162
- }
163
- else {
164
- return `${this.location}-aiplatform.googleapis.com`;
165
- }
166
- }
167
- buildMethod() {
168
- return "POST";
169
- }
170
- }
171
- export class GoogleRawConnection extends GoogleHostConnection {
172
- async _buildOpts(data, _options, requestHeaders = {}) {
173
- const opts = await super._buildOpts(data, _options, requestHeaders);
174
- opts.responseType = "blob";
175
- return opts;
176
- }
177
- }
178
- export class GoogleAIConnection extends GoogleHostConnection {
179
- constructor(fields, caller, client, streaming) {
180
- super(fields, caller, client, streaming);
181
- Object.defineProperty(this, "model", {
182
- enumerable: true,
183
- configurable: true,
184
- writable: true,
185
- value: void 0
186
- });
187
- Object.defineProperty(this, "modelName", {
188
- enumerable: true,
189
- configurable: true,
190
- writable: true,
191
- value: void 0
192
- });
193
- Object.defineProperty(this, "client", {
194
- enumerable: true,
195
- configurable: true,
196
- writable: true,
197
- value: void 0
198
- });
199
- Object.defineProperty(this, "_apiName", {
200
- enumerable: true,
201
- configurable: true,
202
- writable: true,
203
- value: void 0
204
- });
205
- Object.defineProperty(this, "apiConfig", {
206
- enumerable: true,
207
- configurable: true,
208
- writable: true,
209
- value: void 0
210
- });
211
- this.client = client;
212
- this.modelName = fields?.model ?? fields?.modelName ?? this.model;
213
- this.model = this.modelName;
214
- this._apiName = fields?.apiName;
215
- this.apiConfig = {
216
- safetyHandler: fields?.safetyHandler, // For backwards compatibility
217
- ...fields?.apiConfig,
218
- };
219
- }
220
- get modelFamily() {
221
- return modelToFamily(this.model);
222
- }
223
- get modelPublisher() {
224
- return modelToPublisher(this.model);
225
- }
226
- get computedAPIName() {
227
- // At least at the moment, model publishers and APIs map the same
228
- return this.modelPublisher;
229
- }
230
- get apiName() {
231
- return this._apiName ?? this.computedAPIName;
232
- }
233
- get api() {
234
- switch (this.apiName) {
235
- case "google":
236
- case "gemma": // TODO: Is this true?
237
- return getGeminiAPI(this.apiConfig);
238
- case "anthropic":
239
- return getAnthropicAPI(this.apiConfig);
240
- default:
241
- throw new Error(`Unknown API: ${this.apiName}`);
242
- }
243
- }
244
- get isApiKey() {
245
- return this.client.clientType === "apiKey";
246
- }
247
- fieldPlatformType(fields) {
248
- const ret = super.fieldPlatformType(fields);
249
- if (typeof ret !== "undefined") {
250
- return ret;
251
- }
252
- if (fields?.vertexai === false) {
253
- return "gai";
254
- }
255
- return undefined;
256
- }
257
- get computedPlatformType() {
258
- // This is not a completely correct assumption, since GCP can
259
- // have an API Key. But if so, then people need to set the platform
260
- // type explicitly.
261
- if (this.isApiKey) {
262
- return "gai";
263
- }
264
- else {
265
- return "gcp";
266
- }
267
- }
268
- get computedApiVersion() {
269
- switch (this.platform) {
270
- case "gai":
271
- return "v1beta";
272
- default:
273
- return "v1";
274
- }
275
- }
276
- get computedLocation() {
277
- switch (this.apiName) {
278
- case "google":
279
- return super.computedLocation;
280
- case "anthropic":
281
- return "us-east5";
282
- default:
283
- throw new Error(`Unknown apiName: ${this.apiName}. Can't get location.`);
284
- }
285
- }
286
- async buildUrlGenerativeLanguage() {
287
- const method = await this.buildUrlMethod();
288
- const url = `https://generativelanguage.googleapis.com/${this.apiVersion}/models/${this.model}:${method}`;
289
- return url;
290
- }
291
- async buildUrlVertexExpress() {
292
- const method = await this.buildUrlMethod();
293
- const publisher = this.modelPublisher;
294
- const url = `https://aiplatform.googleapis.com/${this.apiVersion}/publishers/${publisher}/models/${this.model}:${method}`;
295
- return url;
296
- }
297
- async buildUrlVertexLocation() {
298
- const projectId = await this.client.getProjectId();
299
- const method = await this.buildUrlMethod();
300
- const publisher = this.modelPublisher;
301
- const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/publishers/${publisher}/models/${this.model}:${method}`;
302
- return url;
303
- }
304
- async buildUrlVertex() {
305
- if (this.isApiKey) {
306
- return this.buildUrlVertexExpress();
307
- }
308
- else {
309
- return this.buildUrlVertexLocation();
310
- }
311
- }
312
- async buildUrl() {
313
- switch (this.platform) {
314
- case "gai":
315
- return this.buildUrlGenerativeLanguage();
316
- default:
317
- return this.buildUrlVertex();
318
- }
319
- }
320
- async request(input, parameters, options, runManager) {
321
- const moduleName = this.constructor.name;
322
- const streamingParameters = {
323
- ...parameters,
324
- streaming: this.streaming,
325
- };
326
- const data = await this.formatData(input, streamingParameters);
327
- await runManager?.handleCustomEvent(`google-request-${moduleName}`, {
328
- data,
329
- parameters: streamingParameters,
330
- options,
331
- connection: {
332
- ...this,
333
- url: await this.buildUrl(),
334
- urlMethod: await this.buildUrlMethod(),
335
- modelFamily: this.modelFamily,
336
- modelPublisher: this.modelPublisher,
337
- computedPlatformType: this.computedPlatformType,
338
- },
339
- });
340
- const response = await this._request(data, options);
341
- await runManager?.handleCustomEvent(`google-response-${moduleName}`, {
342
- response,
343
- });
344
- return response;
345
- }
346
- }
347
- export class AbstractGoogleLLMConnection extends GoogleAIConnection {
348
- async buildUrlMethodGemini() {
349
- return this.streaming ? "streamGenerateContent" : "generateContent";
350
- }
351
- async buildUrlMethodClaude() {
352
- return this.streaming ? "streamRawPredict" : "rawPredict";
353
- }
354
- async buildUrlMethod() {
355
- switch (this.modelFamily) {
356
- case "gemini":
357
- case "gemma": // TODO: Is this true?
358
- return this.buildUrlMethodGemini();
359
- case "claude":
360
- return this.buildUrlMethodClaude();
361
- default:
362
- throw new Error(`Unknown model family: ${this.modelFamily}`);
363
- }
364
- }
365
- async formatData(input, parameters) {
366
- // Filter out labels for non-Vertex AI platforms (labels are only supported on Vertex AI)
367
- let filteredParameters = parameters;
368
- if (parameters.labels && this.platform !== "gcp") {
369
- const { labels, ...paramsWithoutLabels } = parameters;
370
- filteredParameters = paramsWithoutLabels;
371
- }
372
- return this.api.formatData(input, filteredParameters);
373
- }
374
- }
375
- export class GoogleRequestCallbackHandler extends BaseCallbackHandler {
376
- customEventInfo(eventName) {
377
- const names = eventName.split("-");
378
- return {
379
- subEvent: names[1],
380
- module: names[2],
381
- };
382
- }
383
- handleCustomEvent(eventName, data, runId, tags, metadata) {
384
- if (!eventName) {
385
- return undefined;
386
- }
387
- const eventInfo = this.customEventInfo(eventName);
388
- switch (eventInfo.subEvent) {
389
- case "request":
390
- return this.handleCustomRequestEvent(eventName, eventInfo, data, runId, tags, metadata);
391
- case "response":
392
- return this.handleCustomResponseEvent(eventName, eventInfo, data, runId, tags, metadata);
393
- case "chunk":
394
- return this.handleCustomChunkEvent(eventName, eventInfo, data, runId, tags, metadata);
395
- default:
396
- console.error(`Unexpected eventInfo for ${eventName} ${JSON.stringify(eventInfo, null, 1)}`);
397
- }
398
- }
399
- }
400
- export class GoogleRequestLogger extends GoogleRequestCallbackHandler {
401
- constructor() {
402
- super(...arguments);
403
- Object.defineProperty(this, "name", {
404
- enumerable: true,
405
- configurable: true,
406
- writable: true,
407
- value: "GoogleRequestLogger"
408
- });
409
- }
410
- log(eventName, data, tags) {
411
- const tagStr = tags ? `[${tags}]` : "[]";
412
- console.log(`${eventName} ${tagStr} ${JSON.stringify(data, null, 1)}`);
413
- }
414
- handleCustomRequestEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
415
- this.log(eventName, data, tags);
416
- }
417
- handleCustomResponseEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
418
- this.log(eventName, data, tags);
419
- }
420
- handleCustomChunkEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
421
- this.log(eventName, data, tags);
422
- }
423
- }
424
- export class GoogleRequestRecorder extends GoogleRequestCallbackHandler {
425
- constructor() {
426
- super(...arguments);
427
- Object.defineProperty(this, "name", {
428
- enumerable: true,
429
- configurable: true,
430
- writable: true,
431
- value: "GoogleRequestRecorder"
432
- });
433
- Object.defineProperty(this, "request", {
434
- enumerable: true,
435
- configurable: true,
436
- writable: true,
437
- value: {}
438
- });
439
- Object.defineProperty(this, "response", {
440
- enumerable: true,
441
- configurable: true,
442
- writable: true,
443
- value: {}
444
- });
445
- Object.defineProperty(this, "chunk", {
446
- enumerable: true,
447
- configurable: true,
448
- writable: true,
449
- value: []
450
- });
451
- }
452
- handleCustomRequestEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
453
- this.request = data;
454
- }
455
- handleCustomResponseEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
456
- this.response = data;
457
- }
458
- handleCustomChunkEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
459
- this.chunk.push(data);
460
- }
461
- }
7
+
8
+ //#region src/connection.ts
9
+ var GoogleConnection = class {
10
+ caller;
11
+ client;
12
+ streaming;
13
+ constructor(caller, client, streaming) {
14
+ this.caller = caller;
15
+ this.client = client;
16
+ this.streaming = streaming ?? false;
17
+ }
18
+ async _clientInfoHeaders() {
19
+ const { userAgent, clientLibraryVersion } = await this._getClientInfo();
20
+ return {
21
+ "User-Agent": userAgent,
22
+ "Client-Info": clientLibraryVersion
23
+ };
24
+ }
25
+ async _getClientInfo() {
26
+ const env = await getRuntimeEnvironment();
27
+ const langchain = env?.library ?? "langchain-js";
28
+ const langchainVersion = "0";
29
+ const moduleName = await this._moduleName();
30
+ let clientLibraryVersion = `${langchain}/${langchainVersion}`;
31
+ if (moduleName && moduleName.length) clientLibraryVersion = `${clientLibraryVersion}-${moduleName}`;
32
+ return {
33
+ userAgent: clientLibraryVersion,
34
+ clientLibraryVersion: `${langchainVersion}-${moduleName}`
35
+ };
36
+ }
37
+ async _moduleName() {
38
+ return this.constructor.name;
39
+ }
40
+ async additionalHeaders() {
41
+ return {};
42
+ }
43
+ async _buildOpts(data, _options, requestHeaders = {}) {
44
+ const url = await this.buildUrl();
45
+ const method = this.buildMethod();
46
+ const infoHeaders = await this._clientInfoHeaders() ?? {};
47
+ const additionalHeaders = await this.additionalHeaders() ?? {};
48
+ const headers = {
49
+ ...infoHeaders,
50
+ ...additionalHeaders,
51
+ ...requestHeaders
52
+ };
53
+ const opts = {
54
+ url,
55
+ method,
56
+ headers
57
+ };
58
+ if (data && method === "POST") opts.data = data;
59
+ if (this.streaming) opts.responseType = "stream";
60
+ else opts.responseType = "json";
61
+ return opts;
62
+ }
63
+ async _request(data, options, requestHeaders = {}) {
64
+ const opts = await this._buildOpts(data, options, requestHeaders);
65
+ const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, async () => this.client.request(opts));
66
+ const response = callResponse;
67
+ return response;
68
+ }
69
+ };
70
+ var GoogleHostConnection = class extends GoogleConnection {
71
+ platformType;
72
+ _endpoint;
73
+ _location;
74
+ _apiVersion;
75
+ constructor(fields, caller, client, streaming) {
76
+ super(caller, client, streaming);
77
+ this.caller = caller;
78
+ this.platformType = this.fieldPlatformType(fields);
79
+ this._endpoint = fields?.endpoint;
80
+ this._location = fields?.location;
81
+ this._apiVersion = fields?.apiVersion;
82
+ this.client = client;
83
+ }
84
+ fieldPlatformType(fields) {
85
+ if (typeof fields === "undefined") return void 0;
86
+ if (typeof fields.platformType !== "undefined") return fields.platformType;
87
+ if (fields.vertexai === true) return "gcp";
88
+ return void 0;
89
+ }
90
+ get platform() {
91
+ return this.platformType ?? this.computedPlatformType;
92
+ }
93
+ get computedPlatformType() {
94
+ return "gcp";
95
+ }
96
+ get computedApiVersion() {
97
+ return "v1";
98
+ }
99
+ get apiVersion() {
100
+ return this._apiVersion ?? this.computedApiVersion;
101
+ }
102
+ get location() {
103
+ return this._location ?? this.computedLocation;
104
+ }
105
+ get computedLocation() {
106
+ return "us-central1";
107
+ }
108
+ get endpoint() {
109
+ return this._endpoint ?? this.computedEndpoint;
110
+ }
111
+ get computedEndpoint() {
112
+ if (this.location === "global") return "aiplatform.googleapis.com";
113
+ else return `${this.location}-aiplatform.googleapis.com`;
114
+ }
115
+ buildMethod() {
116
+ return "POST";
117
+ }
118
+ };
119
+ var GoogleRawConnection = class extends GoogleHostConnection {
120
+ async _buildOpts(data, _options, requestHeaders = {}) {
121
+ const opts = await super._buildOpts(data, _options, requestHeaders);
122
+ opts.responseType = "blob";
123
+ return opts;
124
+ }
125
+ };
126
+ var GoogleAIConnection = class extends GoogleHostConnection {
127
+ model;
128
+ modelName;
129
+ client;
130
+ _apiName;
131
+ apiConfig;
132
+ constructor(fields, caller, client, streaming) {
133
+ super(fields, caller, client, streaming);
134
+ this.client = client;
135
+ this.modelName = fields?.model ?? fields?.modelName ?? this.model;
136
+ this.model = this.modelName;
137
+ this._apiName = fields?.apiName;
138
+ this.apiConfig = {
139
+ safetyHandler: fields?.safetyHandler,
140
+ ...fields?.apiConfig
141
+ };
142
+ }
143
+ get modelFamily() {
144
+ return modelToFamily(this.model);
145
+ }
146
+ get modelPublisher() {
147
+ return modelToPublisher(this.model);
148
+ }
149
+ get computedAPIName() {
150
+ return this.modelPublisher;
151
+ }
152
+ get apiName() {
153
+ return this._apiName ?? this.computedAPIName;
154
+ }
155
+ get api() {
156
+ switch (this.apiName) {
157
+ case "google":
158
+ case "gemma": return getGeminiAPI(this.apiConfig);
159
+ case "anthropic": return getAnthropicAPI(this.apiConfig);
160
+ default: throw new Error(`Unknown API: ${this.apiName}`);
161
+ }
162
+ }
163
+ get isApiKey() {
164
+ return this.client.clientType === "apiKey";
165
+ }
166
+ fieldPlatformType(fields) {
167
+ const ret = super.fieldPlatformType(fields);
168
+ if (typeof ret !== "undefined") return ret;
169
+ if (fields?.vertexai === false) return "gai";
170
+ return void 0;
171
+ }
172
+ get computedPlatformType() {
173
+ if (this.isApiKey) return "gai";
174
+ else return "gcp";
175
+ }
176
+ get computedApiVersion() {
177
+ switch (this.platform) {
178
+ case "gai": return "v1beta";
179
+ default: return "v1";
180
+ }
181
+ }
182
+ get computedLocation() {
183
+ switch (this.apiName) {
184
+ case "google": return super.computedLocation;
185
+ case "anthropic": return "us-east5";
186
+ default: throw new Error(`Unknown apiName: ${this.apiName}. Can't get location.`);
187
+ }
188
+ }
189
+ async buildUrlGenerativeLanguage() {
190
+ const method = await this.buildUrlMethod();
191
+ const url = `https://generativelanguage.googleapis.com/${this.apiVersion}/models/${this.model}:${method}`;
192
+ return url;
193
+ }
194
+ async buildUrlVertexExpress() {
195
+ const method = await this.buildUrlMethod();
196
+ const publisher = this.modelPublisher;
197
+ const url = `https://aiplatform.googleapis.com/${this.apiVersion}/publishers/${publisher}/models/${this.model}:${method}`;
198
+ return url;
199
+ }
200
+ async buildUrlVertexLocation() {
201
+ const projectId = await this.client.getProjectId();
202
+ const method = await this.buildUrlMethod();
203
+ const publisher = this.modelPublisher;
204
+ const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/publishers/${publisher}/models/${this.model}:${method}`;
205
+ return url;
206
+ }
207
+ async buildUrlVertex() {
208
+ if (this.isApiKey) return this.buildUrlVertexExpress();
209
+ else return this.buildUrlVertexLocation();
210
+ }
211
+ async buildUrl() {
212
+ switch (this.platform) {
213
+ case "gai": return this.buildUrlGenerativeLanguage();
214
+ default: return this.buildUrlVertex();
215
+ }
216
+ }
217
+ async request(input, parameters, options, runManager) {
218
+ const moduleName = this.constructor.name;
219
+ const streamingParameters = {
220
+ ...parameters,
221
+ streaming: this.streaming
222
+ };
223
+ const data = await this.formatData(input, streamingParameters);
224
+ await runManager?.handleCustomEvent(`google-request-${moduleName}`, {
225
+ data,
226
+ parameters: streamingParameters,
227
+ options,
228
+ connection: {
229
+ ...this,
230
+ url: await this.buildUrl(),
231
+ urlMethod: await this.buildUrlMethod(),
232
+ modelFamily: this.modelFamily,
233
+ modelPublisher: this.modelPublisher,
234
+ computedPlatformType: this.computedPlatformType
235
+ }
236
+ });
237
+ const response = await this._request(data, options);
238
+ await runManager?.handleCustomEvent(`google-response-${moduleName}`, { response });
239
+ return response;
240
+ }
241
+ };
242
+ var AbstractGoogleLLMConnection = class extends GoogleAIConnection {
243
+ async buildUrlMethodGemini() {
244
+ return this.streaming ? "streamGenerateContent" : "generateContent";
245
+ }
246
+ async buildUrlMethodClaude() {
247
+ return this.streaming ? "streamRawPredict" : "rawPredict";
248
+ }
249
+ async buildUrlMethod() {
250
+ switch (this.modelFamily) {
251
+ case "gemini":
252
+ case "gemma": return this.buildUrlMethodGemini();
253
+ case "claude": return this.buildUrlMethodClaude();
254
+ default: throw new Error(`Unknown model family: ${this.modelFamily}`);
255
+ }
256
+ }
257
+ async formatData(input, parameters) {
258
+ let filteredParameters = parameters;
259
+ if (parameters.labels && this.platform !== "gcp") {
260
+ const { labels,...paramsWithoutLabels } = parameters;
261
+ filteredParameters = paramsWithoutLabels;
262
+ }
263
+ return this.api.formatData(input, filteredParameters);
264
+ }
265
+ };
266
+ var GoogleRequestCallbackHandler = class extends BaseCallbackHandler {
267
+ customEventInfo(eventName) {
268
+ const names = eventName.split("-");
269
+ return {
270
+ subEvent: names[1],
271
+ module: names[2]
272
+ };
273
+ }
274
+ handleCustomEvent(eventName, data, runId, tags, metadata) {
275
+ if (!eventName) return void 0;
276
+ const eventInfo = this.customEventInfo(eventName);
277
+ switch (eventInfo.subEvent) {
278
+ case "request": return this.handleCustomRequestEvent(eventName, eventInfo, data, runId, tags, metadata);
279
+ case "response": return this.handleCustomResponseEvent(eventName, eventInfo, data, runId, tags, metadata);
280
+ case "chunk": return this.handleCustomChunkEvent(eventName, eventInfo, data, runId, tags, metadata);
281
+ default: console.error(`Unexpected eventInfo for ${eventName} ${JSON.stringify(eventInfo, null, 1)}`);
282
+ }
283
+ }
284
+ };
285
+ var GoogleRequestLogger = class extends GoogleRequestCallbackHandler {
286
+ name = "GoogleRequestLogger";
287
+ log(eventName, data, tags) {
288
+ const tagStr = tags ? `[${tags}]` : "[]";
289
+ console.log(`${eventName} ${tagStr} ${JSON.stringify(data, null, 1)}`);
290
+ }
291
+ handleCustomRequestEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
292
+ this.log(eventName, data, tags);
293
+ }
294
+ handleCustomResponseEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
295
+ this.log(eventName, data, tags);
296
+ }
297
+ handleCustomChunkEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
298
+ this.log(eventName, data, tags);
299
+ }
300
+ };
301
+ var GoogleRequestRecorder = class extends GoogleRequestCallbackHandler {
302
+ name = "GoogleRequestRecorder";
303
+ request = {};
304
+ response = {};
305
+ chunk = [];
306
+ handleCustomRequestEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
307
+ this.request = data;
308
+ }
309
+ handleCustomResponseEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
310
+ this.response = data;
311
+ }
312
+ handleCustomChunkEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
313
+ this.chunk.push(data);
314
+ }
315
+ };
316
+
317
+ //#endregion
318
+ export { AbstractGoogleLLMConnection, GoogleAIConnection, GoogleConnection, GoogleHostConnection, GoogleRawConnection, GoogleRequestCallbackHandler, GoogleRequestLogger, GoogleRequestRecorder };
319
+ //# sourceMappingURL=connection.js.map