@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
@@ -789,36 +789,8 @@ function ensureHandler(handler) {
789
789
  }
790
790
  exports.ensureHandler = ensureHandler;
791
791
  /**
792
- * @example
793
- * ```typescript
794
- * const prompt = PromptTemplate.fromTemplate(`What is the answer to {question}?`);
795
- *
796
- * // Example of using LLMChain to process a series of questions
797
- * const chain = new LLMChain({
798
- * llm: new ChatOpenAI({ temperature: 0.9 }),
799
- * prompt,
800
- * });
801
- *
802
- * // Process questions using the chain
803
- * const processQuestions = async (questions) => {
804
- * for (const question of questions) {
805
- * const result = await chain.call({ question });
806
- * console.log(result);
807
- * }
808
- * };
809
- *
810
- * // Example questions
811
- * const questions = [
812
- * "What is your name?",
813
- * "What is your quest?",
814
- * "What is your favorite color?",
815
- * ];
816
- *
817
- * // Run the example
818
- const logFunction = handler.raiseError ? console.error : console.warn;
819
- * processQuestions(questions).catch(consolelogFunction;
820
- *
821
- * ```
792
+ * @deprecated Use [`traceable`](https://docs.smith.langchain.com/observability/how_to_guides/tracing/annotate_code)
793
+ * from "langsmith" instead.
822
794
  */
823
795
  class TraceGroup {
824
796
  constructor(groupName, options) {
@@ -157,36 +157,8 @@ export declare class CallbackManager extends BaseCallbackManager implements Base
157
157
  }
158
158
  export declare function ensureHandler(handler: BaseCallbackHandler | CallbackHandlerMethods): BaseCallbackHandler;
159
159
  /**
160
- * @example
161
- * ```typescript
162
- * const prompt = PromptTemplate.fromTemplate(`What is the answer to {question}?`);
163
- *
164
- * // Example of using LLMChain to process a series of questions
165
- * const chain = new LLMChain({
166
- * llm: new ChatOpenAI({ temperature: 0.9 }),
167
- * prompt,
168
- * });
169
- *
170
- * // Process questions using the chain
171
- * const processQuestions = async (questions) => {
172
- * for (const question of questions) {
173
- * const result = await chain.call({ question });
174
- * console.log(result);
175
- * }
176
- * };
177
- *
178
- * // Example questions
179
- * const questions = [
180
- * "What is your name?",
181
- * "What is your quest?",
182
- * "What is your favorite color?",
183
- * ];
184
- *
185
- * // Run the example
186
- const logFunction = handler.raiseError ? console.error : console.warn;
187
- * processQuestions(questions).catch(consolelogFunction;
188
- *
189
- * ```
160
+ * @deprecated Use [`traceable`](https://docs.smith.langchain.com/observability/how_to_guides/tracing/annotate_code)
161
+ * from "langsmith" instead.
190
162
  */
191
163
  export declare class TraceGroup {
192
164
  private groupName;
@@ -777,36 +777,8 @@ export function ensureHandler(handler) {
777
777
  return BaseCallbackHandler.fromMethods(handler);
778
778
  }
779
779
  /**
780
- * @example
781
- * ```typescript
782
- * const prompt = PromptTemplate.fromTemplate(`What is the answer to {question}?`);
783
- *
784
- * // Example of using LLMChain to process a series of questions
785
- * const chain = new LLMChain({
786
- * llm: new ChatOpenAI({ temperature: 0.9 }),
787
- * prompt,
788
- * });
789
- *
790
- * // Process questions using the chain
791
- * const processQuestions = async (questions) => {
792
- * for (const question of questions) {
793
- * const result = await chain.call({ question });
794
- * console.log(result);
795
- * }
796
- * };
797
- *
798
- * // Example questions
799
- * const questions = [
800
- * "What is your name?",
801
- * "What is your quest?",
802
- * "What is your favorite color?",
803
- * ];
804
- *
805
- * // Run the example
806
- const logFunction = handler.raiseError ? console.error : console.warn;
807
- * processQuestions(questions).catch(consolelogFunction;
808
- *
809
- * ```
780
+ * @deprecated Use [`traceable`](https://docs.smith.langchain.com/observability/how_to_guides/tracing/annotate_code)
781
+ * from "langsmith" instead.
810
782
  */
811
783
  export class TraceGroup {
812
784
  constructor(groupName, options) {
@@ -1,45 +1,6 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.awaitAllCallbacks = exports.consumeCallback = void 0;
7
- const p_queue_1 = __importDefault(require("p-queue"));
8
- let queue;
9
- /**
10
- * Creates a queue using the p-queue library. The queue is configured to
11
- * auto-start and has a concurrency of 1, meaning it will process tasks
12
- * one at a time.
13
- */
14
- function createQueue() {
15
- const PQueue = "default" in p_queue_1.default ? p_queue_1.default.default : p_queue_1.default;
16
- return new PQueue({
17
- autoStart: true,
18
- concurrency: 1,
19
- });
20
- }
21
- /**
22
- * Consume a promise, either adding it to the queue or waiting for it to resolve
23
- * @param promiseFn Promise to consume
24
- * @param wait Whether to wait for the promise to resolve or resolve immediately
25
- */
26
- async function consumeCallback(promiseFn, wait) {
27
- if (wait === true) {
28
- await promiseFn();
29
- }
30
- else {
31
- if (typeof queue === "undefined") {
32
- queue = createQueue();
33
- }
34
- void queue.add(promiseFn);
35
- }
36
- }
37
- exports.consumeCallback = consumeCallback;
38
- /**
39
- * Waits for all promises in the queue to resolve. If the queue is
40
- * undefined, it immediately resolves a promise.
41
- */
42
- function awaitAllCallbacks() {
43
- return typeof queue !== "undefined" ? queue.onIdle() : Promise.resolve();
44
- }
45
- exports.awaitAllCallbacks = awaitAllCallbacks;
3
+ exports.consumeCallback = exports.awaitAllCallbacks = void 0;
4
+ const callbacks_js_1 = require("../singletons/callbacks.cjs");
5
+ Object.defineProperty(exports, "awaitAllCallbacks", { enumerable: true, get: function () { return callbacks_js_1.awaitAllCallbacks; } });
6
+ Object.defineProperty(exports, "consumeCallback", { enumerable: true, get: function () { return callbacks_js_1.consumeCallback; } });
@@ -1,11 +1,2 @@
1
- /**
2
- * Consume a promise, either adding it to the queue or waiting for it to resolve
3
- * @param promiseFn Promise to consume
4
- * @param wait Whether to wait for the promise to resolve or resolve immediately
5
- */
6
- export declare function consumeCallback<T>(promiseFn: () => Promise<T> | T | void, wait: boolean): Promise<void>;
7
- /**
8
- * Waits for all promises in the queue to resolve. If the queue is
9
- * undefined, it immediately resolves a promise.
10
- */
11
- export declare function awaitAllCallbacks(): Promise<void>;
1
+ import { awaitAllCallbacks, consumeCallback } from "../singletons/callbacks.js";
2
+ export { awaitAllCallbacks, consumeCallback };
@@ -1,37 +1,2 @@
1
- import PQueueMod from "p-queue";
2
- let queue;
3
- /**
4
- * Creates a queue using the p-queue library. The queue is configured to
5
- * auto-start and has a concurrency of 1, meaning it will process tasks
6
- * one at a time.
7
- */
8
- function createQueue() {
9
- const PQueue = "default" in PQueueMod ? PQueueMod.default : PQueueMod;
10
- return new PQueue({
11
- autoStart: true,
12
- concurrency: 1,
13
- });
14
- }
15
- /**
16
- * Consume a promise, either adding it to the queue or waiting for it to resolve
17
- * @param promiseFn Promise to consume
18
- * @param wait Whether to wait for the promise to resolve or resolve immediately
19
- */
20
- export async function consumeCallback(promiseFn, wait) {
21
- if (wait === true) {
22
- await promiseFn();
23
- }
24
- else {
25
- if (typeof queue === "undefined") {
26
- queue = createQueue();
27
- }
28
- void queue.add(promiseFn);
29
- }
30
- }
31
- /**
32
- * Waits for all promises in the queue to resolve. If the queue is
33
- * undefined, it immediately resolves a promise.
34
- */
35
- export function awaitAllCallbacks() {
36
- return typeof queue !== "undefined" ? queue.onIdle() : Promise.resolve();
37
- }
1
+ import { awaitAllCallbacks, consumeCallback } from "../singletons/callbacks.js";
2
+ export { awaitAllCallbacks, consumeCallback };
@@ -107,6 +107,8 @@ export type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =
107
107
  name?: string;
108
108
  method?: "functionCalling" | "jsonMode" | "jsonSchema" | string;
109
109
  includeRaw?: IncludeRaw;
110
+ /** Whether to use strict mode. Currently only supported by OpenAI models. */
111
+ strict?: boolean;
110
112
  };
111
113
  /** @deprecated Use StructuredOutputMethodOptions instead */
112
114
  export type StructuredOutputMethodParams<RunOutput, IncludeRaw extends boolean = false> = {
@@ -466,6 +466,9 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
466
466
  if (typeof this.bindTools !== "function") {
467
467
  throw new Error(`Chat model must implement ".bindTools()" to use withStructuredOutput.`);
468
468
  }
469
+ if (config?.strict) {
470
+ throw new Error(`"strict" mode is not supported for this model by default.`);
471
+ }
469
472
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
470
473
  const schema = outputSchema;
471
474
  const name = config?.name;
@@ -462,6 +462,9 @@ export class BaseChatModel extends BaseLanguageModel {
462
462
  if (typeof this.bindTools !== "function") {
463
463
  throw new Error(`Chat model must implement ".bindTools()" to use withStructuredOutput.`);
464
464
  }
465
+ if (config?.strict) {
466
+ throw new Error(`"strict" mode is not supported for this model by default.`);
467
+ }
465
468
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
466
469
  const schema = outputSchema;
467
470
  const name = config?.name;
@@ -5,31 +5,57 @@ const manager_js_1 = require("../callbacks/manager.cjs");
5
5
  const base_js_1 = require("../runnables/base.cjs");
6
6
  const config_js_1 = require("../runnables/config.cjs");
7
7
  /**
8
- * Abstract base class for a Document retrieval system. A retrieval system
9
- * is defined as something that can take string queries and return the
10
- * most 'relevant' Documents from some source.
8
+ * Abstract base class for a document retrieval system, designed to
9
+ * process string queries and return the most relevant documents from a source.
10
+ *
11
+ * `BaseRetriever` provides common properties and methods for derived retrievers,
12
+ * such as callbacks, tagging, and verbose logging. Custom retrieval systems
13
+ * should extend this class and implement `_getRelevantDocuments` to define
14
+ * the specific retrieval logic.
15
+ *
16
+ * @template Metadata - The type of metadata associated with each document,
17
+ * defaulting to `Record<string, any>`.
11
18
  */
12
19
  class BaseRetriever extends base_js_1.Runnable {
20
+ /**
21
+ * Constructs a new `BaseRetriever` instance with optional configuration fields.
22
+ *
23
+ * @param fields - Optional input configuration that can include `callbacks`,
24
+ * `tags`, `metadata`, and `verbose` settings for custom retriever behavior.
25
+ */
13
26
  constructor(fields) {
14
27
  super(fields);
28
+ /**
29
+ * Optional callbacks to handle various events in the retrieval process.
30
+ */
15
31
  Object.defineProperty(this, "callbacks", {
16
32
  enumerable: true,
17
33
  configurable: true,
18
34
  writable: true,
19
35
  value: void 0
20
36
  });
37
+ /**
38
+ * Tags to label or categorize the retrieval operation.
39
+ */
21
40
  Object.defineProperty(this, "tags", {
22
41
  enumerable: true,
23
42
  configurable: true,
24
43
  writable: true,
25
44
  value: void 0
26
45
  });
46
+ /**
47
+ * Metadata to provide additional context or information about the retrieval
48
+ * operation.
49
+ */
27
50
  Object.defineProperty(this, "metadata", {
28
51
  enumerable: true,
29
52
  configurable: true,
30
53
  writable: true,
31
54
  value: void 0
32
55
  });
56
+ /**
57
+ * If set to `true`, enables verbose logging for the retrieval process.
58
+ */
33
59
  Object.defineProperty(this, "verbose", {
34
60
  enumerable: true,
35
61
  configurable: true,
@@ -46,9 +72,32 @@ class BaseRetriever extends base_js_1.Runnable {
46
72
  * changes to people currently using subclassed custom retrievers.
47
73
  * Change it on next major release.
48
74
  */
75
+ /**
76
+ * Placeholder method for retrieving relevant documents based on a query.
77
+ *
78
+ * This method is intended to be implemented by subclasses and will be
79
+ * converted to an abstract method in the next major release. Currently, it
80
+ * throws an error if not implemented, ensuring that custom retrievers define
81
+ * the specific retrieval logic.
82
+ *
83
+ * @param _query - The query string used to search for relevant documents.
84
+ * @param _callbacks - (optional) Callback manager for managing callbacks
85
+ * during retrieval.
86
+ * @returns A promise resolving to an array of `DocumentInterface` instances relevant to the query.
87
+ * @throws {Error} Throws an error indicating the method is not implemented.
88
+ */
49
89
  _getRelevantDocuments(_query, _callbacks) {
50
90
  throw new Error("Not implemented!");
51
91
  }
92
+ /**
93
+ * Executes a retrieval operation.
94
+ *
95
+ * @param input - The query string used to search for relevant documents.
96
+ * @param options - (optional) Configuration options for the retrieval run,
97
+ * which may include callbacks, tags, and metadata.
98
+ * @returns A promise that resolves to an array of `DocumentInterface` instances
99
+ * representing the most relevant documents to the query.
100
+ */
52
101
  async invoke(input, options) {
53
102
  return this.getRelevantDocuments(input, (0, config_js_1.ensureConfig)(options));
54
103
  }
@@ -3,7 +3,24 @@ import type { DocumentInterface } from "../documents/document.js";
3
3
  import { Runnable, type RunnableInterface } from "../runnables/base.js";
4
4
  import { RunnableConfig } from "../runnables/config.js";
5
5
  /**
6
- * Base Retriever class. All indexes should extend this class.
6
+ * Input configuration options for initializing a retriever that extends
7
+ * the `BaseRetriever` class. This interface provides base properties
8
+ * common to all retrievers, allowing customization of callback functions,
9
+ * tagging, metadata, and logging verbosity.
10
+ *
11
+ * Fields:
12
+ * - `callbacks` (optional): An array of callback functions that handle various
13
+ * events during retrieval, such as logging, error handling, or progress updates.
14
+ *
15
+ * - `tags` (optional): An array of strings used to add contextual tags to
16
+ * retrieval operations, allowing for easier categorization and tracking.
17
+ *
18
+ * - `metadata` (optional): A record of key-value pairs to store additional
19
+ * contextual information for retrieval operations, which can be useful
20
+ * for logging or auditing purposes.
21
+ *
22
+ * - `verbose` (optional): A boolean flag that, if set to `true`, enables
23
+ * detailed logging and output during the retrieval process. Defaults to `false`.
7
24
  */
8
25
  export interface BaseRetrieverInput {
9
26
  callbacks?: Callbacks;
@@ -11,26 +28,95 @@ export interface BaseRetrieverInput {
11
28
  metadata?: Record<string, unknown>;
12
29
  verbose?: boolean;
13
30
  }
31
+ /**
32
+ * Interface for a base retriever that defines core functionality for
33
+ * retrieving relevant documents from a source based on a query.
34
+ *
35
+ * The `BaseRetrieverInterface` standardizes the `getRelevantDocuments` method,
36
+ * enabling retrieval of documents that match the query criteria.
37
+ *
38
+ * @template Metadata - The type of metadata associated with each document,
39
+ * defaulting to `Record<string, any>`.
40
+ */
14
41
  export interface BaseRetrieverInterface<Metadata extends Record<string, any> = Record<string, any>> extends RunnableInterface<string, DocumentInterface<Metadata>[]> {
42
+ /**
43
+ * Retrieves documents relevant to a given query, allowing optional
44
+ * configurations for customization.
45
+ *
46
+ * @param query - A string representing the query to search for relevant documents.
47
+ * @param config - (optional) Configuration options for the retrieval process,
48
+ * which may include callbacks and additional context settings.
49
+ * @returns A promise that resolves to an array of `DocumentInterface` instances,
50
+ * each containing metadata specified by the `Metadata` type parameter.
51
+ */
15
52
  getRelevantDocuments(query: string, config?: Callbacks | BaseCallbackConfig): Promise<DocumentInterface<Metadata>[]>;
16
53
  }
17
54
  /**
18
- * Abstract base class for a Document retrieval system. A retrieval system
19
- * is defined as something that can take string queries and return the
20
- * most 'relevant' Documents from some source.
55
+ * Abstract base class for a document retrieval system, designed to
56
+ * process string queries and return the most relevant documents from a source.
57
+ *
58
+ * `BaseRetriever` provides common properties and methods for derived retrievers,
59
+ * such as callbacks, tagging, and verbose logging. Custom retrieval systems
60
+ * should extend this class and implement `_getRelevantDocuments` to define
61
+ * the specific retrieval logic.
62
+ *
63
+ * @template Metadata - The type of metadata associated with each document,
64
+ * defaulting to `Record<string, any>`.
21
65
  */
22
66
  export declare abstract class BaseRetriever<Metadata extends Record<string, any> = Record<string, any>> extends Runnable<string, DocumentInterface<Metadata>[]> implements BaseRetrieverInterface {
67
+ /**
68
+ * Optional callbacks to handle various events in the retrieval process.
69
+ */
23
70
  callbacks?: Callbacks;
71
+ /**
72
+ * Tags to label or categorize the retrieval operation.
73
+ */
24
74
  tags?: string[];
75
+ /**
76
+ * Metadata to provide additional context or information about the retrieval
77
+ * operation.
78
+ */
25
79
  metadata?: Record<string, unknown>;
80
+ /**
81
+ * If set to `true`, enables verbose logging for the retrieval process.
82
+ */
26
83
  verbose?: boolean;
84
+ /**
85
+ * Constructs a new `BaseRetriever` instance with optional configuration fields.
86
+ *
87
+ * @param fields - Optional input configuration that can include `callbacks`,
88
+ * `tags`, `metadata`, and `verbose` settings for custom retriever behavior.
89
+ */
27
90
  constructor(fields?: BaseRetrieverInput);
28
91
  /**
29
92
  * TODO: This should be an abstract method, but we'd like to avoid breaking
30
93
  * changes to people currently using subclassed custom retrievers.
31
94
  * Change it on next major release.
32
95
  */
96
+ /**
97
+ * Placeholder method for retrieving relevant documents based on a query.
98
+ *
99
+ * This method is intended to be implemented by subclasses and will be
100
+ * converted to an abstract method in the next major release. Currently, it
101
+ * throws an error if not implemented, ensuring that custom retrievers define
102
+ * the specific retrieval logic.
103
+ *
104
+ * @param _query - The query string used to search for relevant documents.
105
+ * @param _callbacks - (optional) Callback manager for managing callbacks
106
+ * during retrieval.
107
+ * @returns A promise resolving to an array of `DocumentInterface` instances relevant to the query.
108
+ * @throws {Error} Throws an error indicating the method is not implemented.
109
+ */
33
110
  _getRelevantDocuments(_query: string, _callbacks?: CallbackManagerForRetrieverRun): Promise<DocumentInterface<Metadata>[]>;
111
+ /**
112
+ * Executes a retrieval operation.
113
+ *
114
+ * @param input - The query string used to search for relevant documents.
115
+ * @param options - (optional) Configuration options for the retrieval run,
116
+ * which may include callbacks, tags, and metadata.
117
+ * @returns A promise that resolves to an array of `DocumentInterface` instances
118
+ * representing the most relevant documents to the query.
119
+ */
34
120
  invoke(input: string, options?: RunnableConfig): Promise<DocumentInterface<Metadata>[]>;
35
121
  /**
36
122
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
@@ -2,31 +2,57 @@ import { CallbackManager, parseCallbackConfigArg, } from "../callbacks/manager.j
2
2
  import { Runnable } from "../runnables/base.js";
3
3
  import { ensureConfig } from "../runnables/config.js";
4
4
  /**
5
- * Abstract base class for a Document retrieval system. A retrieval system
6
- * is defined as something that can take string queries and return the
7
- * most 'relevant' Documents from some source.
5
+ * Abstract base class for a document retrieval system, designed to
6
+ * process string queries and return the most relevant documents from a source.
7
+ *
8
+ * `BaseRetriever` provides common properties and methods for derived retrievers,
9
+ * such as callbacks, tagging, and verbose logging. Custom retrieval systems
10
+ * should extend this class and implement `_getRelevantDocuments` to define
11
+ * the specific retrieval logic.
12
+ *
13
+ * @template Metadata - The type of metadata associated with each document,
14
+ * defaulting to `Record<string, any>`.
8
15
  */
9
16
  export class BaseRetriever extends Runnable {
17
+ /**
18
+ * Constructs a new `BaseRetriever` instance with optional configuration fields.
19
+ *
20
+ * @param fields - Optional input configuration that can include `callbacks`,
21
+ * `tags`, `metadata`, and `verbose` settings for custom retriever behavior.
22
+ */
10
23
  constructor(fields) {
11
24
  super(fields);
25
+ /**
26
+ * Optional callbacks to handle various events in the retrieval process.
27
+ */
12
28
  Object.defineProperty(this, "callbacks", {
13
29
  enumerable: true,
14
30
  configurable: true,
15
31
  writable: true,
16
32
  value: void 0
17
33
  });
34
+ /**
35
+ * Tags to label or categorize the retrieval operation.
36
+ */
18
37
  Object.defineProperty(this, "tags", {
19
38
  enumerable: true,
20
39
  configurable: true,
21
40
  writable: true,
22
41
  value: void 0
23
42
  });
43
+ /**
44
+ * Metadata to provide additional context or information about the retrieval
45
+ * operation.
46
+ */
24
47
  Object.defineProperty(this, "metadata", {
25
48
  enumerable: true,
26
49
  configurable: true,
27
50
  writable: true,
28
51
  value: void 0
29
52
  });
53
+ /**
54
+ * If set to `true`, enables verbose logging for the retrieval process.
55
+ */
30
56
  Object.defineProperty(this, "verbose", {
31
57
  enumerable: true,
32
58
  configurable: true,
@@ -43,9 +69,32 @@ export class BaseRetriever extends Runnable {
43
69
  * changes to people currently using subclassed custom retrievers.
44
70
  * Change it on next major release.
45
71
  */
72
+ /**
73
+ * Placeholder method for retrieving relevant documents based on a query.
74
+ *
75
+ * This method is intended to be implemented by subclasses and will be
76
+ * converted to an abstract method in the next major release. Currently, it
77
+ * throws an error if not implemented, ensuring that custom retrievers define
78
+ * the specific retrieval logic.
79
+ *
80
+ * @param _query - The query string used to search for relevant documents.
81
+ * @param _callbacks - (optional) Callback manager for managing callbacks
82
+ * during retrieval.
83
+ * @returns A promise resolving to an array of `DocumentInterface` instances relevant to the query.
84
+ * @throws {Error} Throws an error indicating the method is not implemented.
85
+ */
46
86
  _getRelevantDocuments(_query, _callbacks) {
47
87
  throw new Error("Not implemented!");
48
88
  }
89
+ /**
90
+ * Executes a retrieval operation.
91
+ *
92
+ * @param input - The query string used to search for relevant documents.
93
+ * @param options - (optional) Configuration options for the retrieval run,
94
+ * which may include callbacks, tags, and metadata.
95
+ * @returns A promise that resolves to an array of `DocumentInterface` instances
96
+ * representing the most relevant documents to the query.
97
+ */
49
98
  async invoke(input, options) {
50
99
  return this.getRelevantDocuments(input, ensureConfig(options));
51
100
  }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getGlobalAsyncLocalStorageInstance = exports.setGlobalAsyncLocalStorageInstance = exports.TRACING_ALS_KEY = void 0;
4
+ exports.TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
5
+ const setGlobalAsyncLocalStorageInstance = (instance) => {
6
+ globalThis[exports.TRACING_ALS_KEY] = instance;
7
+ };
8
+ exports.setGlobalAsyncLocalStorageInstance = setGlobalAsyncLocalStorageInstance;
9
+ const getGlobalAsyncLocalStorageInstance = () => {
10
+ return globalThis[exports.TRACING_ALS_KEY];
11
+ };
12
+ exports.getGlobalAsyncLocalStorageInstance = getGlobalAsyncLocalStorageInstance;
@@ -0,0 +1,8 @@
1
+ export interface AsyncLocalStorageInterface {
2
+ getStore: () => any | undefined;
3
+ run: <T>(store: any, callback: () => T) => T;
4
+ enterWith: (store: any) => void;
5
+ }
6
+ export declare const TRACING_ALS_KEY: unique symbol;
7
+ export declare const setGlobalAsyncLocalStorageInstance: (instance: AsyncLocalStorageInterface) => void;
8
+ export declare const getGlobalAsyncLocalStorageInstance: () => any;
@@ -0,0 +1,7 @@
1
+ export const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
2
+ export const setGlobalAsyncLocalStorageInstance = (instance) => {
3
+ globalThis[TRACING_ALS_KEY] = instance;
4
+ };
5
+ export const getGlobalAsyncLocalStorageInstance = () => {
6
+ return globalThis[TRACING_ALS_KEY];
7
+ };
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AsyncLocalStorageProviderSingleton = exports._CONTEXT_VARIABLES_KEY = exports.MockAsyncLocalStorage = void 0;
4
+ /* eslint-disable @typescript-eslint/no-explicit-any */
5
+ const langsmith_1 = require("langsmith");
6
+ const globals_js_1 = require("./globals.cjs");
7
+ const manager_js_1 = require("../../callbacks/manager.cjs");
8
+ class MockAsyncLocalStorage {
9
+ getStore() {
10
+ return undefined;
11
+ }
12
+ run(_store, callback) {
13
+ return callback();
14
+ }
15
+ enterWith(_store) {
16
+ return undefined;
17
+ }
18
+ }
19
+ exports.MockAsyncLocalStorage = MockAsyncLocalStorage;
20
+ const mockAsyncLocalStorage = new MockAsyncLocalStorage();
21
+ const LC_CHILD_KEY = Symbol.for("lc:child_config");
22
+ exports._CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
23
+ class AsyncLocalStorageProvider {
24
+ getInstance() {
25
+ return (0, globals_js_1.getGlobalAsyncLocalStorageInstance)() ?? mockAsyncLocalStorage;
26
+ }
27
+ getRunnableConfig() {
28
+ const storage = this.getInstance();
29
+ // this has the runnable config
30
+ // which means that we should also have an instance of a LangChainTracer
31
+ // with the run map prepopulated
32
+ return storage.getStore()?.extra?.[LC_CHILD_KEY];
33
+ }
34
+ runWithConfig(config, callback, avoidCreatingRootRunTree) {
35
+ const callbackManager = manager_js_1.CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
36
+ const storage = this.getInstance();
37
+ const previousValue = storage.getStore();
38
+ const parentRunId = callbackManager?.getParentRunId();
39
+ const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
40
+ let runTree;
41
+ if (langChainTracer && parentRunId) {
42
+ runTree = langChainTracer.convertToRunTree(parentRunId);
43
+ }
44
+ else if (!avoidCreatingRootRunTree) {
45
+ runTree = new langsmith_1.RunTree({
46
+ name: "<runnable_lambda>",
47
+ tracingEnabled: false,
48
+ });
49
+ }
50
+ if (runTree) {
51
+ runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
52
+ }
53
+ if (previousValue !== undefined &&
54
+ previousValue[exports._CONTEXT_VARIABLES_KEY] !== undefined) {
55
+ runTree[exports._CONTEXT_VARIABLES_KEY] =
56
+ previousValue[exports._CONTEXT_VARIABLES_KEY];
57
+ }
58
+ return storage.run(runTree, callback);
59
+ }
60
+ initializeGlobalInstance(instance) {
61
+ if ((0, globals_js_1.getGlobalAsyncLocalStorageInstance)() === undefined) {
62
+ (0, globals_js_1.setGlobalAsyncLocalStorageInstance)(instance);
63
+ }
64
+ }
65
+ }
66
+ const AsyncLocalStorageProviderSingleton = new AsyncLocalStorageProvider();
67
+ exports.AsyncLocalStorageProviderSingleton = AsyncLocalStorageProviderSingleton;