UNPKG

@langchain/core

Version:
1 lines 7.64 kB
{"version":3,"file":"index.cjs","names":["Runnable","fields?: BaseRetrieverInput","_query: string","_callbacks?: CallbackManagerForRetrieverRun","input: string","options?: RunnableConfig","ensureConfig","parseCallbackConfigArg","CallbackManager"],"sources":["../../src/retrievers/index.ts"],"sourcesContent":["import {\n CallbackManager,\n CallbackManagerForRetrieverRun,\n Callbacks,\n parseCallbackConfigArg,\n} from \"../callbacks/manager.js\";\nimport type { DocumentInterface } from \"../documents/document.js\";\nimport { Runnable, type RunnableInterface } from \"../runnables/base.js\";\nimport { RunnableConfig, ensureConfig } from \"../runnables/config.js\";\n\n/**\n * Input configuration options for initializing a retriever that extends\n * the `BaseRetriever` class. This interface provides base properties\n * common to all retrievers, allowing customization of callback functions,\n * tagging, metadata, and logging verbosity.\n *\n * Fields:\n * - `callbacks` (optional): An array of callback functions that handle various\n * events during retrieval, such as logging, error handling, or progress updates.\n *\n * - `tags` (optional): An array of strings used to add contextual tags to\n * retrieval operations, allowing for easier categorization and tracking.\n *\n * - `metadata` (optional): A record of key-value pairs to store additional\n * contextual information for retrieval operations, which can be useful\n * for logging or auditing purposes.\n *\n * - `verbose` (optional): A boolean flag that, if set to `true`, enables\n * detailed logging and output during the retrieval process. Defaults to `false`.\n */\nexport interface BaseRetrieverInput {\n callbacks?: Callbacks;\n tags?: string[];\n metadata?: Record<string, unknown>;\n verbose?: boolean;\n}\n\n/**\n * Interface for a base retriever that defines core functionality for\n * retrieving relevant documents from a source based on a query.\n *\n * The `BaseRetrieverInterface` standardizes the `getRelevantDocuments` method,\n * enabling retrieval of documents that match the query criteria.\n *\n * @template Metadata - The type of metadata associated with each document,\n * defaulting to `Record<string, any>`.\n */\nexport interface BaseRetrieverInterface<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Metadata extends Record<string, any> = Record<string, any>\n> extends RunnableInterface<string, DocumentInterface<Metadata>[]> {}\n\n/**\n * Abstract base class for a document retrieval system, designed to\n * process string queries and return the most relevant documents from a source.\n *\n * `BaseRetriever` provides common properties and methods for derived retrievers,\n * such as callbacks, tagging, and verbose logging. Custom retrieval systems\n * should extend this class and implement `_getRelevantDocuments` to define\n * the specific retrieval logic.\n *\n * @template Metadata - The type of metadata associated with each document,\n * defaulting to `Record<string, any>`.\n */\nexport abstract class BaseRetriever<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Metadata extends Record<string, any> = Record<string, any>\n >\n extends Runnable<string, DocumentInterface<Metadata>[]>\n implements BaseRetrieverInterface\n{\n /**\n * Optional callbacks to handle various events in the retrieval process.\n */\n callbacks?: Callbacks;\n\n /**\n * Tags to label or categorize the retrieval operation.\n */\n tags?: string[];\n\n /**\n * Metadata to provide additional context or information about the retrieval\n * operation.\n */\n metadata?: Record<string, unknown>;\n\n /**\n * If set to `true`, enables verbose logging for the retrieval process.\n */\n verbose?: boolean;\n\n /**\n * Constructs a new `BaseRetriever` instance with optional configuration fields.\n *\n * @param fields - Optional input configuration that can include `callbacks`,\n * `tags`, `metadata`, and `verbose` settings for custom retriever behavior.\n */\n constructor(fields?: BaseRetrieverInput) {\n super(fields);\n this.callbacks = fields?.callbacks;\n this.tags = fields?.tags ?? [];\n this.metadata = fields?.metadata ?? {};\n this.verbose = fields?.verbose ?? false;\n }\n\n /**\n * TODO: This should be an abstract method, but we'd like to avoid breaking\n * changes to people currently using subclassed custom retrievers.\n * Change it on next major release.\n */\n /**\n * Placeholder method for retrieving relevant documents based on a query.\n *\n * This method is intended to be implemented by subclasses and will be\n * converted to an abstract method in the next major release. Currently, it\n * throws an error if not implemented, ensuring that custom retrievers define\n * the specific retrieval logic.\n *\n * @param _query - The query string used to search for relevant documents.\n * @param _callbacks - (optional) Callback manager for managing callbacks\n * during retrieval.\n * @returns A promise resolving to an array of `DocumentInterface` instances relevant to the query.\n * @throws {Error} Throws an error indicating the method is not implemented.\n */\n _getRelevantDocuments(\n _query: string,\n _callbacks?: CallbackManagerForRetrieverRun\n ): Promise<DocumentInterface<Metadata>[]> {\n throw new Error(\"Not implemented!\");\n }\n\n /**\n * Executes a retrieval operation.\n *\n * @param input - The query string used to search for relevant documents.\n * @param options - (optional) Configuration options for the retrieval run,\n * which may include callbacks, tags, and metadata.\n * @returns A promise that resolves to an array of `DocumentInterface` instances\n * representing the most relevant documents to the query.\n */\n async invoke(\n input: string,\n options?: RunnableConfig\n ): Promise<DocumentInterface<Metadata>[]> {\n const parsedConfig = ensureConfig(parseCallbackConfigArg(options));\n const callbackManager_ = await CallbackManager.configure(\n parsedConfig.callbacks,\n this.callbacks,\n parsedConfig.tags,\n this.tags,\n parsedConfig.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n const runManager = await callbackManager_?.handleRetrieverStart(\n this.toJSON(),\n input,\n parsedConfig.runId,\n undefined,\n undefined,\n undefined,\n parsedConfig.runName\n );\n try {\n const results = await this._getRelevantDocuments(input, runManager);\n await runManager?.handleRetrieverEnd(results);\n return results;\n } catch (error) {\n await runManager?.handleRetrieverError(error);\n throw error;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAgEA,IAAsB,gBAAtB,cAIUA,sBAEV;;;;CAIE;;;;CAKA;;;;;CAMA;;;;CAKA;;;;;;;CAQA,YAAYC,QAA6B;EACvC,MAAM,OAAO;EACb,KAAK,YAAY,QAAQ;EACzB,KAAK,OAAO,QAAQ,QAAQ,CAAE;EAC9B,KAAK,WAAW,QAAQ,YAAY,CAAE;EACtC,KAAK,UAAU,QAAQ,WAAW;CACnC;;;;;;;;;;;;;;;;;;;;CAqBD,sBACEC,QACAC,YACwC;AACxC,QAAM,IAAI,MAAM;CACjB;;;;;;;;;;CAWD,MAAM,OACJC,OACAC,SACwC;EACxC,MAAM,eAAeC,4BAAaC,iDAAuB,QAAQ,CAAC;EAClE,MAAM,mBAAmB,MAAMC,0CAAgB,UAC7C,aAAa,WACb,KAAK,WACL,aAAa,MACb,KAAK,MACL,aAAa,UACb,KAAK,UACL,EAAE,SAAS,KAAK,QAAS,EAC1B;EACD,MAAM,aAAa,MAAM,kBAAkB,qBACzC,KAAK,QAAQ,EACb,OACA,aAAa,OACb,QACA,QACA,QACA,aAAa,QACd;AACD,MAAI;GACF,MAAM,UAAU,MAAM,KAAK,sBAAsB,OAAO,WAAW;GACnE,MAAM,YAAY,mBAAmB,QAAQ;AAC7C,UAAO;EACR,SAAQ,OAAO;GACd,MAAM,YAAY,qBAAqB,MAAM;AAC7C,SAAM;EACP;CACF;AACF"}