@langchain/core 0.3.17 → 0.3.19-rc.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 (33) hide show
  1. package/dist/callbacks/manager.cjs +2 -30
  2. package/dist/callbacks/manager.d.ts +2 -30
  3. package/dist/callbacks/manager.js +2 -30
  4. package/dist/callbacks/promises.cjs +4 -43
  5. package/dist/callbacks/promises.d.ts +2 -11
  6. package/dist/callbacks/promises.js +2 -37
  7. package/dist/language_models/base.d.ts +2 -0
  8. package/dist/language_models/chat_models.cjs +3 -0
  9. package/dist/language_models/chat_models.js +3 -0
  10. package/dist/retrievers/index.cjs +52 -3
  11. package/dist/retrievers/index.d.ts +90 -4
  12. package/dist/retrievers/index.js +52 -3
  13. package/dist/singletons/async_local_storage/globals.cjs +12 -0
  14. package/dist/singletons/async_local_storage/globals.d.ts +8 -0
  15. package/dist/singletons/async_local_storage/globals.js +7 -0
  16. package/dist/singletons/async_local_storage/index.cjs +67 -0
  17. package/dist/singletons/async_local_storage/index.d.ts +15 -0
  18. package/dist/singletons/async_local_storage/index.js +63 -0
  19. package/dist/singletons/callbacks.cjs +66 -0
  20. package/dist/singletons/callbacks.d.ts +13 -0
  21. package/dist/singletons/callbacks.js +57 -0
  22. package/dist/singletons/index.cjs +5 -64
  23. package/dist/singletons/index.d.ts +2 -19
  24. package/dist/singletons/index.js +2 -62
  25. package/dist/singletons/tracer.cjs +19 -0
  26. package/dist/singletons/tracer.d.ts +2 -0
  27. package/dist/singletons/tracer.js +15 -0
  28. package/dist/tracers/tracer_langchain.cjs +2 -8
  29. package/dist/tracers/tracer_langchain.js +2 -8
  30. package/dist/vectorstores.cjs +267 -7
  31. package/dist/vectorstores.d.ts +519 -11
  32. package/dist/vectorstores.js +267 -7
  33. package/package.json +1 -1
@@ -4,8 +4,17 @@ exports.SaveableVectorStore = exports.VectorStore = exports.VectorStoreRetriever
4
4
  const index_js_1 = require("./retrievers/index.cjs");
5
5
  const serializable_js_1 = require("./load/serializable.cjs");
6
6
  /**
7
- * Class for performing document retrieval from a VectorStore. Can perform
8
- * similarity search or maximal marginal relevance search.
7
+ * Class for retrieving documents from a `VectorStore` based on vector similarity
8
+ * or maximal marginal relevance (MMR).
9
+ *
10
+ * `VectorStoreRetriever` extends `BaseRetriever`, implementing methods for
11
+ * adding documents to the underlying vector store and performing document
12
+ * retrieval with optional configurations.
13
+ *
14
+ * @class VectorStoreRetriever
15
+ * @extends BaseRetriever
16
+ * @implements VectorStoreRetrieverInterface
17
+ * @template V - Type of vector store implementing `VectorStoreInterface`.
9
18
  */
10
19
  class VectorStoreRetriever extends index_js_1.BaseRetriever {
11
20
  static lc_name() {
@@ -14,35 +23,114 @@ class VectorStoreRetriever extends index_js_1.BaseRetriever {
14
23
  get lc_namespace() {
15
24
  return ["langchain_core", "vectorstores"];
16
25
  }
26
+ /**
27
+ * Returns the type of vector store, as defined by the `vectorStore` instance.
28
+ *
29
+ * @returns {string} The vector store type.
30
+ */
17
31
  _vectorstoreType() {
18
32
  return this.vectorStore._vectorstoreType();
19
33
  }
34
+ /**
35
+ * Initializes a new instance of `VectorStoreRetriever` with the specified configuration.
36
+ *
37
+ * This constructor configures the retriever to interact with a given `VectorStore`
38
+ * and supports different retrieval strategies, including similarity search and maximal
39
+ * marginal relevance (MMR) search. Various options allow customization of the number
40
+ * of documents retrieved per query, filtering based on conditions, and fine-tuning
41
+ * MMR-specific parameters.
42
+ *
43
+ * @param fields - Configuration options for setting up the retriever:
44
+ *
45
+ * - `vectorStore` (required): The `VectorStore` instance implementing `VectorStoreInterface`
46
+ * that will be used to store and retrieve document embeddings. This is the core component
47
+ * of the retriever, enabling vector-based similarity and MMR searches.
48
+ *
49
+ * - `k` (optional): Specifies the number of documents to retrieve per search query. If not
50
+ * provided, defaults to 4. This count determines the number of most relevant documents returned
51
+ * for each search operation, balancing performance with comprehensiveness.
52
+ *
53
+ * - `searchType` (optional): Defines the search approach used by the retriever, allowing for
54
+ * flexibility between two methods:
55
+ * - `"similarity"` (default): A similarity-based search, retrieving documents with high vector
56
+ * similarity to the query. This type prioritizes relevance and is often used when diversity
57
+ * among results is less critical.
58
+ * - `"mmr"`: Maximal Marginal Relevance search, which combines relevance with diversity. MMR
59
+ * is useful for scenarios where varied content is essential, as it selects results that
60
+ * both match the query and introduce content diversity.
61
+ *
62
+ * - `filter` (optional): A filter of type `FilterType`, defined by the vector store, that allows
63
+ * for refined and targeted search results. This filter applies specified conditions to limit
64
+ * which documents are eligible for retrieval, offering control over the scope of results.
65
+ *
66
+ * - `searchKwargs` (optional, applicable only if `searchType` is `"mmr"`): Additional settings
67
+ * for configuring MMR-specific behavior. These parameters allow further tuning of the MMR
68
+ * search process:
69
+ * - `fetchK`: The initial number of documents fetched from the vector store before the MMR
70
+ * algorithm is applied. Fetching a larger set enables the algorithm to select a more
71
+ * diverse subset of documents.
72
+ * - `lambda`: A parameter controlling the relevance-diversity balance, where 0 emphasizes
73
+ * diversity and 1 prioritizes relevance. Intermediate values provide a blend of the two,
74
+ * allowing customization based on the importance of content variety relative to query relevance.
75
+ */
20
76
  constructor(fields) {
21
77
  super(fields);
78
+ /**
79
+ * The instance of `VectorStore` used for storing and retrieving document embeddings.
80
+ * This vector store must implement the `VectorStoreInterface` to be compatible
81
+ * with the retriever’s operations.
82
+ */
22
83
  Object.defineProperty(this, "vectorStore", {
23
84
  enumerable: true,
24
85
  configurable: true,
25
86
  writable: true,
26
87
  value: void 0
27
88
  });
89
+ /**
90
+ * Specifies the number of documents to retrieve for each search query.
91
+ * Defaults to 4 if not specified, providing a basic result count for similarity or MMR searches.
92
+ */
28
93
  Object.defineProperty(this, "k", {
29
94
  enumerable: true,
30
95
  configurable: true,
31
96
  writable: true,
32
97
  value: 4
33
98
  });
99
+ /**
100
+ * Determines the type of search operation to perform on the vector store.
101
+ *
102
+ * - `"similarity"` (default): Conducts a similarity search based purely on vector similarity
103
+ * to the query.
104
+ * - `"mmr"`: Executes a maximal marginal relevance (MMR) search, balancing relevance and
105
+ * diversity in the retrieved results.
106
+ */
34
107
  Object.defineProperty(this, "searchType", {
35
108
  enumerable: true,
36
109
  configurable: true,
37
110
  writable: true,
38
111
  value: "similarity"
39
112
  });
113
+ /**
114
+ * Additional options specific to maximal marginal relevance (MMR) search, applicable
115
+ * only if `searchType` is set to `"mmr"`.
116
+ *
117
+ * Includes:
118
+ * - `fetchK`: The initial number of documents fetched before applying the MMR algorithm,
119
+ * allowing for a larger selection from which to choose the most diverse results.
120
+ * - `lambda`: A parameter between 0 and 1 to adjust the relevance-diversity balance,
121
+ * where 0 prioritizes diversity and 1 prioritizes relevance.
122
+ */
40
123
  Object.defineProperty(this, "searchKwargs", {
41
124
  enumerable: true,
42
125
  configurable: true,
43
126
  writable: true,
44
127
  value: void 0
45
128
  });
129
+ /**
130
+ * Optional filter applied to search results, defined by the `FilterType` of the vector store.
131
+ * Allows for refined, targeted results by restricting the returned documents based
132
+ * on specified filter criteria.
133
+ */
46
134
  Object.defineProperty(this, "filter", {
47
135
  enumerable: true,
48
136
  configurable: true,
@@ -57,6 +145,22 @@ class VectorStoreRetriever extends index_js_1.BaseRetriever {
57
145
  this.searchKwargs = fields.searchKwargs;
58
146
  }
59
147
  }
148
+ /**
149
+ * Retrieves relevant documents based on the specified query, using either
150
+ * similarity or maximal marginal relevance (MMR) search.
151
+ *
152
+ * If `searchType` is set to `"mmr"`, performs an MMR search to balance
153
+ * similarity and diversity among results. If `searchType` is `"similarity"`,
154
+ * retrieves results purely based on similarity to the query.
155
+ *
156
+ * @param query - The query string used to find relevant documents.
157
+ * @param runManager - Optional callback manager for tracking retrieval progress.
158
+ * @returns A promise that resolves to an array of `DocumentInterface` instances
159
+ * representing the most relevant documents to the query.
160
+ * @throws {Error} Throws an error if MMR search is requested but not supported
161
+ * by the vector store.
162
+ * @protected
163
+ */
60
164
  async _getRelevantDocuments(query, runManager) {
61
165
  if (this.searchType === "mmr") {
62
166
  if (typeof this.vectorStore.maxMarginalRelevanceSearch !== "function") {
@@ -70,20 +174,52 @@ class VectorStoreRetriever extends index_js_1.BaseRetriever {
70
174
  }
71
175
  return this.vectorStore.similaritySearch(query, this.k, this.filter, runManager?.getChild("vectorstore"));
72
176
  }
177
+ /**
178
+ * Adds an array of documents to the vector store, embedding them as part of
179
+ * the storage process.
180
+ *
181
+ * This method delegates document embedding and storage to the `addDocuments`
182
+ * method of the underlying vector store.
183
+ *
184
+ * @param documents - An array of documents to embed and add to the vector store.
185
+ * @param options - Optional settings to customize document addition.
186
+ * @returns A promise that resolves to an array of document IDs or `void`,
187
+ * depending on the vector store's implementation.
188
+ */
73
189
  async addDocuments(documents, options) {
74
190
  return this.vectorStore.addDocuments(documents, options);
75
191
  }
76
192
  }
77
193
  exports.VectorStoreRetriever = VectorStoreRetriever;
78
194
  /**
79
- * Abstract class representing a store of vectors. Provides methods for
80
- * adding vectors and documents, deleting from the store, and searching
81
- * the store.
195
+ * Abstract class representing a vector storage system for performing
196
+ * similarity searches on embedded documents.
197
+ *
198
+ * `VectorStore` provides methods for adding precomputed vectors or documents,
199
+ * removing documents based on criteria, and performing similarity searches
200
+ * with optional scoring. Subclasses are responsible for implementing specific
201
+ * storage mechanisms and the exact behavior of certain abstract methods.
202
+ *
203
+ * @abstract
204
+ * @extends Serializable
205
+ * @implements VectorStoreInterface
82
206
  */
83
207
  class VectorStore extends serializable_js_1.Serializable {
208
+ /**
209
+ * Initializes a new vector store with embeddings and database configuration.
210
+ *
211
+ * @param embeddings - Instance of `EmbeddingsInterface` used to embed queries.
212
+ * @param dbConfig - Configuration settings for the database or storage system.
213
+ */
84
214
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
215
  constructor(embeddings, dbConfig) {
86
216
  super(dbConfig);
217
+ /**
218
+ * Namespace within LangChain to uniquely identify this vector store's
219
+ * location, based on the vector store type.
220
+ *
221
+ * @internal
222
+ */
87
223
  // Only ever instantiated in main LangChain
88
224
  Object.defineProperty(this, "lc_namespace", {
89
225
  enumerable: true,
@@ -91,6 +227,10 @@ class VectorStore extends serializable_js_1.Serializable {
91
227
  writable: true,
92
228
  value: ["langchain", "vectorstores", this._vectorstoreType()]
93
229
  });
230
+ /**
231
+ * Embeddings interface for generating vector embeddings from text queries,
232
+ * enabling vector-based similarity searches.
233
+ */
94
234
  Object.defineProperty(this, "embeddings", {
95
235
  enumerable: true,
96
236
  configurable: true,
@@ -99,29 +239,122 @@ class VectorStore extends serializable_js_1.Serializable {
99
239
  });
100
240
  this.embeddings = embeddings;
101
241
  }
242
+ /**
243
+ * Deletes documents from the vector store based on the specified parameters.
244
+ *
245
+ * @param _params - Flexible key-value pairs defining conditions for document deletion.
246
+ * @returns A promise that resolves once the deletion is complete.
247
+ */
102
248
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
249
  async delete(_params) {
104
250
  throw new Error("Not implemented.");
105
251
  }
252
+ /**
253
+ * Searches for documents similar to a text query by embedding the query and
254
+ * performing a similarity search on the resulting vector.
255
+ *
256
+ * @param query - Text query for finding similar documents.
257
+ * @param k - Number of similar results to return. Defaults to 4.
258
+ * @param filter - Optional filter based on `FilterType`.
259
+ * @param _callbacks - Optional callbacks for monitoring search progress
260
+ * @returns A promise resolving to an array of `DocumentInterface` instances representing similar documents.
261
+ */
106
262
  async similaritySearch(query, k = 4, filter = undefined, _callbacks = undefined // implement passing to embedQuery later
107
263
  ) {
108
264
  const results = await this.similaritySearchVectorWithScore(await this.embeddings.embedQuery(query), k, filter);
109
265
  return results.map((result) => result[0]);
110
266
  }
267
+ /**
268
+ * Searches for documents similar to a text query by embedding the query,
269
+ * and returns results with similarity scores.
270
+ *
271
+ * @param query - Text query for finding similar documents.
272
+ * @param k - Number of similar results to return. Defaults to 4.
273
+ * @param filter - Optional filter based on `FilterType`.
274
+ * @param _callbacks - Optional callbacks for monitoring search progress
275
+ * @returns A promise resolving to an array of tuples, each containing a
276
+ * document and its similarity score.
277
+ */
111
278
  async similaritySearchWithScore(query, k = 4, filter = undefined, _callbacks = undefined // implement passing to embedQuery later
112
279
  ) {
113
280
  return this.similaritySearchVectorWithScore(await this.embeddings.embedQuery(query), k, filter);
114
281
  }
282
+ /**
283
+ * Creates a `VectorStore` instance from an array of text strings and optional
284
+ * metadata, using the specified embeddings and database configuration.
285
+ *
286
+ * Subclasses must implement this method to define how text and metadata
287
+ * are embedded and stored in the vector store. Throws an error if not overridden.
288
+ *
289
+ * @param _texts - Array of strings representing the text documents to be stored.
290
+ * @param _metadatas - Metadata for the texts, either as an array (one for each text)
291
+ * or a single object (applied to all texts).
292
+ * @param _embeddings - Instance of `EmbeddingsInterface` to embed the texts.
293
+ * @param _dbConfig - Database configuration settings.
294
+ * @returns A promise that resolves to a new `VectorStore` instance.
295
+ * @throws {Error} Throws an error if this method is not overridden by a subclass.
296
+ */
115
297
  static fromTexts(_texts, _metadatas, _embeddings,
116
298
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
117
299
  _dbConfig) {
118
300
  throw new Error("the Langchain vectorstore implementation you are using forgot to override this, please report a bug");
119
301
  }
302
+ /**
303
+ * Creates a `VectorStore` instance from an array of documents, using the specified
304
+ * embeddings and database configuration.
305
+ *
306
+ * Subclasses must implement this method to define how documents are embedded
307
+ * and stored. Throws an error if not overridden.
308
+ *
309
+ * @param _docs - Array of `DocumentInterface` instances representing the documents to be stored.
310
+ * @param _embeddings - Instance of `EmbeddingsInterface` to embed the documents.
311
+ * @param _dbConfig - Database configuration settings.
312
+ * @returns A promise that resolves to a new `VectorStore` instance.
313
+ * @throws {Error} Throws an error if this method is not overridden by a subclass.
314
+ */
120
315
  static fromDocuments(_docs, _embeddings,
121
316
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
122
317
  _dbConfig) {
123
318
  throw new Error("the Langchain vectorstore implementation you are using forgot to override this, please report a bug");
124
319
  }
320
+ /**
321
+ * Creates a `VectorStoreRetriever` instance with flexible configuration options.
322
+ *
323
+ * @param kOrFields
324
+ * - If a number is provided, it sets the `k` parameter (number of items to retrieve).
325
+ * - If an object is provided, it should contain various configuration options.
326
+ * @param filter
327
+ * - Optional filter criteria to limit the items retrieved based on the specified filter type.
328
+ * @param callbacks
329
+ * - Optional callbacks that may be triggered at specific stages of the retrieval process.
330
+ * @param tags
331
+ * - Tags to categorize or label the `VectorStoreRetriever`. Defaults to an empty array if not provided.
332
+ * @param metadata
333
+ * - Additional metadata as key-value pairs to add contextual information for the retrieval process.
334
+ * @param verbose
335
+ * - If `true`, enables detailed logging for the retrieval process. Defaults to `false`.
336
+ *
337
+ * @returns
338
+ * - A configured `VectorStoreRetriever` instance based on the provided parameters.
339
+ *
340
+ * @example
341
+ * Basic usage with a `k` value:
342
+ * ```typescript
343
+ * const retriever = myVectorStore.asRetriever(5);
344
+ * ```
345
+ *
346
+ * Usage with a configuration object:
347
+ * ```typescript
348
+ * const retriever = myVectorStore.asRetriever({
349
+ * k: 10,
350
+ * filter: myFilter,
351
+ * tags: ['example', 'test'],
352
+ * verbose: true,
353
+ * searchType: 'mmr',
354
+ * searchKwargs: { alpha: 0.5 },
355
+ * });
356
+ * ```
357
+ */
125
358
  asRetriever(kOrFields, filter, callbacks, tags, metadata, verbose) {
126
359
  if (typeof kOrFields === "number") {
127
360
  return new VectorStoreRetriever({
@@ -157,10 +390,37 @@ class VectorStore extends serializable_js_1.Serializable {
157
390
  }
158
391
  exports.VectorStore = VectorStore;
159
392
  /**
160
- * Abstract class extending VectorStore with functionality for saving and
161
- * loading the vector store.
393
+ * Abstract class extending `VectorStore` that defines a contract for saving
394
+ * and loading vector store instances.
395
+ *
396
+ * The `SaveableVectorStore` class allows vector store implementations to
397
+ * persist their data and retrieve it when needed.The format for saving and
398
+ * loading data is left to the implementing subclass.
399
+ *
400
+ * Subclasses must implement the `save` method to handle their custom
401
+ * serialization logic, while the `load` method enables reconstruction of a
402
+ * vector store from saved data, requiring compatible embeddings through the
403
+ * `EmbeddingsInterface`.
404
+ *
405
+ * @abstract
406
+ * @extends VectorStore
162
407
  */
163
408
  class SaveableVectorStore extends VectorStore {
409
+ /**
410
+ * Loads a vector store instance from the specified directory, using the
411
+ * provided embeddings to ensure compatibility.
412
+ *
413
+ * This static method reconstructs a `SaveableVectorStore` from previously
414
+ * saved data. Implementations should interpret the saved data format to
415
+ * recreate the vector store instance.
416
+ *
417
+ * @param _directory - The directory path from which the vector store
418
+ * data will be loaded.
419
+ * @param _embeddings - An instance of `EmbeddingsInterface` to align
420
+ * the embeddings with the loaded vector data.
421
+ * @returns A promise that resolves to a `SaveableVectorStore` instance
422
+ * constructed from the saved data.
423
+ */
164
424
  static load(_directory, _embeddings) {
165
425
  throw new Error("Not implemented");
166
426
  }