langchain 0.0.132 → 0.0.133

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 (44) hide show
  1. package/dist/agents/chat/outputParser.cjs +2 -1
  2. package/dist/agents/chat/outputParser.js +2 -1
  3. package/dist/agents/executor.cjs +106 -7
  4. package/dist/agents/executor.d.ts +23 -0
  5. package/dist/agents/executor.js +104 -6
  6. package/dist/agents/mrkl/outputParser.cjs +2 -1
  7. package/dist/agents/mrkl/outputParser.js +2 -1
  8. package/dist/chat_models/googlevertexai.cjs +1 -1
  9. package/dist/chat_models/googlevertexai.d.ts +2 -2
  10. package/dist/chat_models/googlevertexai.js +2 -2
  11. package/dist/chat_models/ollama.cjs +8 -8
  12. package/dist/chat_models/ollama.js +8 -8
  13. package/dist/document_loaders/web/notionapi.cjs +153 -74
  14. package/dist/document_loaders/web/notionapi.d.ts +19 -10
  15. package/dist/document_loaders/web/notionapi.js +154 -75
  16. package/dist/embeddings/googlevertexai.cjs +1 -1
  17. package/dist/embeddings/googlevertexai.d.ts +2 -2
  18. package/dist/embeddings/googlevertexai.js +2 -2
  19. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +1 -1
  20. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -2
  21. package/dist/experimental/multimodal_embeddings/googlevertexai.js +2 -2
  22. package/dist/llms/googlevertexai.cjs +1 -1
  23. package/dist/llms/googlevertexai.js +2 -2
  24. package/dist/load/import_constants.cjs +1 -0
  25. package/dist/load/import_constants.js +1 -0
  26. package/dist/schema/output_parser.cjs +2 -2
  27. package/dist/schema/output_parser.js +2 -2
  28. package/dist/tools/base.cjs +26 -2
  29. package/dist/tools/base.d.ts +9 -0
  30. package/dist/tools/base.js +24 -1
  31. package/dist/types/googlevertexai-types.d.ts +8 -3
  32. package/dist/util/googlevertexai-connection.cjs +49 -15
  33. package/dist/util/googlevertexai-connection.d.ts +12 -4
  34. package/dist/util/googlevertexai-connection.js +46 -13
  35. package/dist/vectorstores/googlevertexai.cjs +550 -0
  36. package/dist/vectorstores/googlevertexai.d.ts +180 -0
  37. package/dist/vectorstores/googlevertexai.js +519 -0
  38. package/dist/vectorstores/vectara.cjs +11 -2
  39. package/dist/vectorstores/vectara.d.ts +10 -1
  40. package/dist/vectorstores/vectara.js +11 -2
  41. package/package.json +10 -2
  42. package/vectorstores/googlevertexai.cjs +1 -0
  43. package/vectorstores/googlevertexai.d.ts +1 -0
  44. package/vectorstores/googlevertexai.js +1 -0
@@ -0,0 +1,550 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.MatchingEngine = exports.IdDocument = void 0;
30
+ const uuid = __importStar(require("uuid"));
31
+ const flat_1 = __importDefault(require("flat"));
32
+ const base_js_1 = require("./base.cjs");
33
+ const document_js_1 = require("../document.cjs");
34
+ const googlevertexai_connection_js_1 = require("../util/googlevertexai-connection.cjs");
35
+ const async_caller_js_1 = require("../util/async_caller.cjs");
36
+ /**
37
+ * A Document that optionally includes the ID of the document.
38
+ */
39
+ class IdDocument extends document_js_1.Document {
40
+ constructor(fields) {
41
+ super(fields);
42
+ Object.defineProperty(this, "id", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ this.id = fields.id;
49
+ }
50
+ }
51
+ exports.IdDocument = IdDocument;
52
+ class IndexEndpointConnection extends googlevertexai_connection_js_1.GoogleVertexAIConnection {
53
+ constructor(fields, caller) {
54
+ super(fields, caller);
55
+ Object.defineProperty(this, "indexEndpoint", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: void 0
60
+ });
61
+ this.indexEndpoint = fields.indexEndpoint;
62
+ }
63
+ async buildUrl() {
64
+ const projectId = await this.auth.getProjectId();
65
+ const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexEndpoints/${this.indexEndpoint}`;
66
+ return url;
67
+ }
68
+ buildMethod() {
69
+ return "GET";
70
+ }
71
+ async request(options) {
72
+ return this._request(undefined, options);
73
+ }
74
+ }
75
+ class RemoveDatapointConnection extends googlevertexai_connection_js_1.GoogleVertexAIConnection {
76
+ constructor(fields, caller) {
77
+ super(fields, caller);
78
+ Object.defineProperty(this, "index", {
79
+ enumerable: true,
80
+ configurable: true,
81
+ writable: true,
82
+ value: void 0
83
+ });
84
+ this.index = fields.index;
85
+ }
86
+ async buildUrl() {
87
+ const projectId = await this.auth.getProjectId();
88
+ const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexes/${this.index}:removeDatapoints`;
89
+ return url;
90
+ }
91
+ buildMethod() {
92
+ return "POST";
93
+ }
94
+ async request(datapointIds, options) {
95
+ const data = {
96
+ datapointIds,
97
+ };
98
+ return this._request(data, options);
99
+ }
100
+ }
101
+ class UpsertDatapointConnection extends googlevertexai_connection_js_1.GoogleVertexAIConnection {
102
+ constructor(fields, caller) {
103
+ super(fields, caller);
104
+ Object.defineProperty(this, "index", {
105
+ enumerable: true,
106
+ configurable: true,
107
+ writable: true,
108
+ value: void 0
109
+ });
110
+ this.index = fields.index;
111
+ }
112
+ async buildUrl() {
113
+ const projectId = await this.auth.getProjectId();
114
+ const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexes/${this.index}:upsertDatapoints`;
115
+ return url;
116
+ }
117
+ buildMethod() {
118
+ return "POST";
119
+ }
120
+ async request(datapoints, options) {
121
+ const data = {
122
+ datapoints,
123
+ };
124
+ return this._request(data, options);
125
+ }
126
+ }
127
+ class FindNeighborsConnection extends googlevertexai_connection_js_1.GoogleVertexAIConnection {
128
+ constructor(params, caller) {
129
+ super(params, caller);
130
+ Object.defineProperty(this, "indexEndpoint", {
131
+ enumerable: true,
132
+ configurable: true,
133
+ writable: true,
134
+ value: void 0
135
+ });
136
+ Object.defineProperty(this, "deployedIndexId", {
137
+ enumerable: true,
138
+ configurable: true,
139
+ writable: true,
140
+ value: void 0
141
+ });
142
+ this.indexEndpoint = params.indexEndpoint;
143
+ this.deployedIndexId = params.deployedIndexId;
144
+ }
145
+ async buildUrl() {
146
+ const projectId = await this.auth.getProjectId();
147
+ const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexEndpoints/${this.indexEndpoint}:findNeighbors`;
148
+ return url;
149
+ }
150
+ buildMethod() {
151
+ return "POST";
152
+ }
153
+ async request(request, options) {
154
+ return this._request(request, options);
155
+ }
156
+ }
157
+ /**
158
+ * A class that represents a connection to a Google Vertex AI Matching Engine
159
+ * instance.
160
+ */
161
+ class MatchingEngine extends base_js_1.VectorStore {
162
+ constructor(embeddings, args) {
163
+ super(embeddings, args);
164
+ /**
165
+ * Docstore that retains the document, stored by ID
166
+ */
167
+ Object.defineProperty(this, "docstore", {
168
+ enumerable: true,
169
+ configurable: true,
170
+ writable: true,
171
+ value: void 0
172
+ });
173
+ /**
174
+ * The host to connect to for queries and upserts.
175
+ */
176
+ Object.defineProperty(this, "apiEndpoint", {
177
+ enumerable: true,
178
+ configurable: true,
179
+ writable: true,
180
+ value: void 0
181
+ });
182
+ Object.defineProperty(this, "apiVersion", {
183
+ enumerable: true,
184
+ configurable: true,
185
+ writable: true,
186
+ value: "v1"
187
+ });
188
+ Object.defineProperty(this, "endpoint", {
189
+ enumerable: true,
190
+ configurable: true,
191
+ writable: true,
192
+ value: "us-central1-aiplatform.googleapis.com"
193
+ });
194
+ Object.defineProperty(this, "location", {
195
+ enumerable: true,
196
+ configurable: true,
197
+ writable: true,
198
+ value: "us-central1"
199
+ });
200
+ /**
201
+ * The id for the index endpoint
202
+ */
203
+ Object.defineProperty(this, "indexEndpoint", {
204
+ enumerable: true,
205
+ configurable: true,
206
+ writable: true,
207
+ value: void 0
208
+ });
209
+ /**
210
+ * The id for the index
211
+ */
212
+ Object.defineProperty(this, "index", {
213
+ enumerable: true,
214
+ configurable: true,
215
+ writable: true,
216
+ value: void 0
217
+ });
218
+ /**
219
+ * The id for the "deployed index", which is an identifier in the
220
+ * index endpoint that references the index (but is not the index id)
221
+ */
222
+ Object.defineProperty(this, "deployedIndexId", {
223
+ enumerable: true,
224
+ configurable: true,
225
+ writable: true,
226
+ value: void 0
227
+ });
228
+ Object.defineProperty(this, "callerParams", {
229
+ enumerable: true,
230
+ configurable: true,
231
+ writable: true,
232
+ value: void 0
233
+ });
234
+ Object.defineProperty(this, "callerOptions", {
235
+ enumerable: true,
236
+ configurable: true,
237
+ writable: true,
238
+ value: void 0
239
+ });
240
+ Object.defineProperty(this, "caller", {
241
+ enumerable: true,
242
+ configurable: true,
243
+ writable: true,
244
+ value: void 0
245
+ });
246
+ Object.defineProperty(this, "indexEndpointClient", {
247
+ enumerable: true,
248
+ configurable: true,
249
+ writable: true,
250
+ value: void 0
251
+ });
252
+ Object.defineProperty(this, "removeDatapointClient", {
253
+ enumerable: true,
254
+ configurable: true,
255
+ writable: true,
256
+ value: void 0
257
+ });
258
+ Object.defineProperty(this, "upsertDatapointClient", {
259
+ enumerable: true,
260
+ configurable: true,
261
+ writable: true,
262
+ value: void 0
263
+ });
264
+ this.embeddings = embeddings;
265
+ this.docstore = args.docstore;
266
+ this.apiEndpoint = args.apiEndpoint ?? this.apiEndpoint;
267
+ this.deployedIndexId = args.deployedIndexId ?? this.deployedIndexId;
268
+ this.apiVersion = args.apiVersion ?? this.apiVersion;
269
+ this.endpoint = args.endpoint ?? this.endpoint;
270
+ this.location = args.location ?? this.location;
271
+ this.indexEndpoint = args.indexEndpoint ?? this.indexEndpoint;
272
+ this.index = args.index ?? this.index;
273
+ this.callerParams = args.callerParams ?? this.callerParams;
274
+ this.callerOptions = args.callerOptions ?? this.callerOptions;
275
+ this.caller = new async_caller_js_1.AsyncCaller(this.callerParams || {});
276
+ const indexClientParams = {
277
+ endpoint: this.endpoint,
278
+ location: this.location,
279
+ apiVersion: this.apiVersion,
280
+ indexEndpoint: this.indexEndpoint,
281
+ };
282
+ this.indexEndpointClient = new IndexEndpointConnection(indexClientParams, this.caller);
283
+ const removeClientParams = {
284
+ endpoint: this.endpoint,
285
+ location: this.location,
286
+ apiVersion: this.apiVersion,
287
+ index: this.index,
288
+ };
289
+ this.removeDatapointClient = new RemoveDatapointConnection(removeClientParams, this.caller);
290
+ const upsertClientParams = {
291
+ endpoint: this.endpoint,
292
+ location: this.location,
293
+ apiVersion: this.apiVersion,
294
+ index: this.index,
295
+ };
296
+ this.upsertDatapointClient = new UpsertDatapointConnection(upsertClientParams, this.caller);
297
+ }
298
+ _vectorstoreType() {
299
+ return "googlevertexai";
300
+ }
301
+ async addDocuments(documents) {
302
+ const texts = documents.map((doc) => doc.pageContent);
303
+ const vectors = await this.embeddings.embedDocuments(texts);
304
+ return this.addVectors(vectors, documents);
305
+ }
306
+ async addVectors(vectors, documents) {
307
+ if (vectors.length !== documents.length) {
308
+ throw new Error(`Vectors and metadata must have the same length`);
309
+ }
310
+ const datapoints = vectors.map((vector, idx) => this.buildDatapoint(vector, documents[idx]));
311
+ const options = {};
312
+ const response = await this.upsertDatapointClient.request(datapoints, options);
313
+ if (Object.keys(response?.data ?? {}).length === 0) {
314
+ // Nothing in the response in the body means we saved it ok
315
+ const idDoc = documents;
316
+ const docsToStore = {};
317
+ idDoc.forEach((doc) => {
318
+ if (doc.id) {
319
+ docsToStore[doc.id] = doc;
320
+ }
321
+ });
322
+ await this.docstore.add(docsToStore);
323
+ }
324
+ }
325
+ // TODO: Refactor this into a utility type and use with pinecone as well?
326
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
327
+ cleanMetadata(documentMetadata) {
328
+ function getStringArrays(prefix,
329
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
330
+ m) {
331
+ let ret = {};
332
+ Object.keys(m).forEach((key) => {
333
+ const newPrefix = prefix.length > 0 ? `${prefix}.${key}` : key;
334
+ const val = m[key];
335
+ if (!val) {
336
+ // Ignore it
337
+ }
338
+ else if (Array.isArray(val)) {
339
+ // Make sure everything in the array is a string
340
+ ret[newPrefix] = val.map((v) => `${v}`);
341
+ }
342
+ else if (typeof val === "object") {
343
+ const subArrays = getStringArrays(newPrefix, val);
344
+ ret = { ...ret, ...subArrays };
345
+ }
346
+ });
347
+ return ret;
348
+ }
349
+ const stringArrays = getStringArrays("", documentMetadata);
350
+ const flatMetadata = (0, flat_1.default)(documentMetadata);
351
+ Object.keys(flatMetadata).forEach((key) => {
352
+ Object.keys(stringArrays).forEach((arrayKey) => {
353
+ const matchKey = `${arrayKey}.`;
354
+ if (key.startsWith(matchKey)) {
355
+ delete flatMetadata[key];
356
+ }
357
+ });
358
+ });
359
+ const metadata = {
360
+ ...flatMetadata,
361
+ ...stringArrays,
362
+ };
363
+ return metadata;
364
+ }
365
+ /**
366
+ * Given the metadata from a document, convert it to an array of Restriction
367
+ * objects that may be passed to the Matching Engine and stored.
368
+ * The default implementation flattens any metadata and includes it as
369
+ * an "allowList". Subclasses can choose to convert some of these to
370
+ * "denyList" items or to add additional restrictions (for example, to format
371
+ * dates into a different structure or to add additional restrictions
372
+ * based on the date).
373
+ * @param documentMetadata - The metadata from a document
374
+ * @returns a Restriction[] (or an array of a subclass, from the FilterType)
375
+ */
376
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
377
+ metadataToRestrictions(documentMetadata) {
378
+ const metadata = this.cleanMetadata(documentMetadata);
379
+ const restrictions = [];
380
+ for (const key of Object.keys(metadata)) {
381
+ // Make sure the value is an array (or that we'll ignore it)
382
+ let valArray;
383
+ const val = metadata[key];
384
+ if (val === null) {
385
+ valArray = null;
386
+ }
387
+ else if (Array.isArray(val) && val.length > 0) {
388
+ valArray = val;
389
+ }
390
+ else {
391
+ valArray = [`${val}`];
392
+ }
393
+ // Add to the restrictions if we do have a valid value
394
+ if (valArray) {
395
+ // Determine if this key is for the allowList or denyList
396
+ // TODO: get which ones should be on the deny list
397
+ const listType = "allowList";
398
+ // Create the restriction
399
+ const restriction = {
400
+ namespace: key,
401
+ [listType]: valArray,
402
+ };
403
+ // Add it to the restriction list
404
+ restrictions.push(restriction);
405
+ }
406
+ }
407
+ return restrictions;
408
+ }
409
+ /**
410
+ * Create an index datapoint for the vector and document id.
411
+ * If an id does not exist, create it and set the document to its value.
412
+ * @param vector
413
+ * @param document
414
+ */
415
+ buildDatapoint(vector, document) {
416
+ if (!document.id) {
417
+ // eslint-disable-next-line no-param-reassign
418
+ document.id = uuid.v4();
419
+ }
420
+ const ret = {
421
+ datapointId: document.id,
422
+ featureVector: vector,
423
+ };
424
+ const restrictions = this.metadataToRestrictions(document.metadata);
425
+ if (restrictions?.length > 0) {
426
+ ret.restricts = restrictions;
427
+ }
428
+ return ret;
429
+ }
430
+ async delete(params) {
431
+ const options = {};
432
+ await this.removeDatapointClient.request(params.ids, options);
433
+ }
434
+ async similaritySearchVectorWithScore(query, k, filter) {
435
+ // Format the query into the request
436
+ const deployedIndexId = await this.getDeployedIndexId();
437
+ const requestQuery = {
438
+ neighborCount: k,
439
+ datapoint: {
440
+ datapointId: `0`,
441
+ featureVector: query,
442
+ },
443
+ };
444
+ if (filter) {
445
+ requestQuery.datapoint.restricts = filter;
446
+ }
447
+ const request = {
448
+ deployedIndexId,
449
+ queries: [requestQuery],
450
+ };
451
+ // Build the connection.
452
+ // Has to be done here, since we defer getting the endpoint until
453
+ // we need it.
454
+ const apiEndpoint = await this.getPublicAPIEndpoint();
455
+ const findNeighborsParams = {
456
+ endpoint: apiEndpoint,
457
+ indexEndpoint: this.indexEndpoint,
458
+ apiVersion: this.apiVersion,
459
+ location: this.location,
460
+ deployedIndexId,
461
+ };
462
+ const connection = new FindNeighborsConnection(findNeighborsParams, this.caller);
463
+ // Make the call
464
+ const options = {};
465
+ const response = await connection.request(request, options);
466
+ // Get the document for each datapoint id and return them
467
+ const nearestNeighbors = response?.data?.nearestNeighbors ?? [];
468
+ const nearestNeighbor = nearestNeighbors[0];
469
+ const neighbors = nearestNeighbor?.neighbors ?? [];
470
+ const ret = await Promise.all(neighbors.map(async (neighbor) => {
471
+ const id = neighbor?.datapoint?.datapointId;
472
+ const distance = neighbor?.distance;
473
+ let doc;
474
+ try {
475
+ doc = await this.docstore.search(id);
476
+ }
477
+ catch (xx) {
478
+ // Documents that are in the index are returned, even if they
479
+ // are not in the document store, to allow for some way to get
480
+ // the id so they can be deleted.
481
+ console.error(xx);
482
+ console.warn([
483
+ `Document with id "${id}" is missing from the backing docstore.`,
484
+ `This can occur if you clear the docstore without deleting from the corresponding Matching Engine index.`,
485
+ `To resolve this, you should call .delete() with this id as part of the "ids" parameter.`,
486
+ ].join("\n"));
487
+ doc = new document_js_1.Document({ pageContent: `Missing document ${id}` });
488
+ }
489
+ doc.id ??= id;
490
+ return [doc, distance];
491
+ }));
492
+ return ret;
493
+ }
494
+ /**
495
+ * For this index endpoint, figure out what API Endpoint URL and deployed
496
+ * index ID should be used to do upserts and queries.
497
+ * Also sets the `apiEndpoint` and `deployedIndexId` property for future use.
498
+ * @return The URL
499
+ */
500
+ async determinePublicAPIEndpoint() {
501
+ const response = await this.indexEndpointClient.request(this.callerOptions);
502
+ // Get the endpoint
503
+ const publicEndpointDomainName = response?.data?.publicEndpointDomainName;
504
+ this.apiEndpoint = publicEndpointDomainName;
505
+ // Determine which of the deployed indexes match the index id
506
+ // and get the deployed index id. The list of deployed index ids
507
+ // contain the "index name" or path, but not the index id by itself,
508
+ // so we need to extract it from the name
509
+ const indexPathPattern = /projects\/.+\/locations\/.+\/indexes\/(.+)$/;
510
+ const deployedIndexes = response?.data?.deployedIndexes ?? [];
511
+ const deployedIndex = deployedIndexes.find((index) => {
512
+ const deployedIndexPath = index.index;
513
+ const match = deployedIndexPath.match(indexPathPattern);
514
+ if (match) {
515
+ const [, potentialIndexId] = match;
516
+ if (potentialIndexId === this.index) {
517
+ return true;
518
+ }
519
+ }
520
+ return false;
521
+ });
522
+ if (deployedIndex) {
523
+ this.deployedIndexId = deployedIndex.id;
524
+ }
525
+ return {
526
+ apiEndpoint: this.apiEndpoint,
527
+ deployedIndexId: this.deployedIndexId,
528
+ };
529
+ }
530
+ async getPublicAPIEndpoint() {
531
+ return (this.apiEndpoint ?? (await this.determinePublicAPIEndpoint()).apiEndpoint);
532
+ }
533
+ async getDeployedIndexId() {
534
+ return (this.deployedIndexId ??
535
+ (await this.determinePublicAPIEndpoint()).deployedIndexId);
536
+ }
537
+ static async fromTexts(texts, metadatas, embeddings, dbConfig) {
538
+ const docs = texts.map((text, index) => ({
539
+ pageContent: text,
540
+ metadata: Array.isArray(metadatas) ? metadatas[index] : metadatas,
541
+ }));
542
+ return this.fromDocuments(docs, embeddings, dbConfig);
543
+ }
544
+ static async fromDocuments(docs, embeddings, dbConfig) {
545
+ const ret = new MatchingEngine(embeddings, dbConfig);
546
+ await ret.addDocuments(docs);
547
+ return ret;
548
+ }
549
+ }
550
+ exports.MatchingEngine = MatchingEngine;