@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 2.79 kB
Source Map (JSON)
{"version":3,"file":"transformers.cjs","names":["Runnable","input: RunInput","_options?: BaseCallbackConfig","documents: DocumentInterface[]"],"sources":["../../src/documents/transformers.ts"],"sourcesContent":["import { Runnable } from \"../runnables/base.js\";\nimport type { BaseCallbackConfig } from \"../callbacks/manager.js\";\nimport type { DocumentInterface } from \"./document.js\";\n\n/**\n * Abstract base class for document transformation systems.\n *\n * A document transformation system takes an array of Documents and returns an\n * array of transformed Documents. These arrays do not necessarily have to have\n * the same length.\n *\n * One example of this is a text splitter that splits a large document into\n * many smaller documents.\n */\nexport abstract class BaseDocumentTransformer<\n RunInput extends DocumentInterface[] = DocumentInterface[],\n RunOutput extends DocumentInterface[] = DocumentInterface[]\n> extends Runnable<RunInput, RunOutput> {\n lc_namespace = [\"langchain_core\", \"documents\", \"transformers\"];\n\n /**\n * Transform a list of documents.\n * @param documents A sequence of documents to be transformed.\n * @returns A list of transformed documents.\n */\n abstract transformDocuments(documents: RunInput): Promise<RunOutput>;\n\n /**\n * Method to invoke the document transformation. This method calls the\n * transformDocuments method with the provided input.\n * @param input The input documents to be transformed.\n * @param _options Optional configuration object to customize the behavior of callbacks.\n * @returns A Promise that resolves to the transformed documents.\n */\n invoke(input: RunInput, _options?: BaseCallbackConfig): Promise<RunOutput> {\n return this.transformDocuments(input);\n }\n}\n\n/**\n * Class for document transformers that return exactly one transformed document\n * for each input document.\n */\nexport abstract class MappingDocumentTransformer extends BaseDocumentTransformer {\n async transformDocuments(\n documents: DocumentInterface[]\n ): Promise<DocumentInterface[]> {\n const newDocuments = [];\n for (const document of documents) {\n const transformedDocument = await this._transformDocument(document);\n newDocuments.push(transformedDocument);\n }\n return newDocuments;\n }\n\n abstract _transformDocument(\n document: DocumentInterface\n ): Promise<DocumentInterface>;\n}\n"],"mappings":";;;;;;;;;;;;;AAcA,IAAsB,0BAAtB,cAGUA,sBAA8B;CACtC,eAAe;EAAC;EAAkB;EAAa;CAAe;;;;;;;;CAgB9D,OAAOC,OAAiBC,UAAmD;AACzE,SAAO,KAAK,mBAAmB,MAAM;CACtC;AACF;;;;;AAMD,IAAsB,6BAAtB,cAAyD,wBAAwB;CAC/E,MAAM,mBACJC,WAC8B;EAC9B,MAAM,eAAe,CAAE;AACvB,OAAK,MAAM,YAAY,WAAW;GAChC,MAAM,sBAAsB,MAAM,KAAK,mBAAmB,SAAS;GACnE,aAAa,KAAK,oBAAoB;EACvC;AACD,SAAO;CACR;AAKF"}