-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
base.ts
37 lines (34 loc) · 1.47 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Document } from "../documents/document.js";
import { BaseDocumentTransformer } from "../documents/transformers.js";
/**
* Interface that defines the methods for loading and splitting documents.
*/
export interface DocumentLoader {
load(): Promise<Document[]>;
loadAndSplit(textSplitter?: BaseDocumentTransformer): Promise<Document[]>;
}
/**
* Abstract class that provides a default implementation for the
* loadAndSplit() method from the DocumentLoader interface. The load()
* method is left abstract and needs to be implemented by subclasses.
*/
export abstract class BaseDocumentLoader implements DocumentLoader {
/**
* Loads the documents.
* @returns A Promise that resolves with an array of Document instances.
*/
abstract load(): Promise<Document[]>;
/**
* @deprecated Use `this.load()` and `splitter.splitDocuments()` individually.
* Loads the documents and splits them using a specified text splitter.
* @param textSplitter The TextSplitter instance to use for splitting the loaded documents. Defaults to a RecursiveCharacterTextSplitter instance.
* @returns A Promise that resolves with an array of Document instances, each split according to the provided TextSplitter.
*/
async loadAndSplit(splitter?: BaseDocumentTransformer): Promise<Document[]> {
if (splitter === undefined) {
throw new Error("You must pass a text splitter to use this method.");
}
const docs = await this.load();
return splitter.invoke(docs);
}
}