@langchain/core 0.3.17 → 0.3.18

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