Skip to main content

如何编写自定义文档加载器

¥How to write a custom document loader

如果你想实现自己的文档加载器,你有几种选择。

¥If you want to implement your own Document Loader, you have a few options.

BaseDocumentLoader 子类化

¥Subclassing BaseDocumentLoader

你可以直接扩展 BaseDocumentLoader 类。BaseDocumentLoader 类提供了一些便捷的方法,用于从各种来源加载文档。

¥You can extend the BaseDocumentLoader class directly. The BaseDocumentLoader class provides a few convenience methods for loading documents from a variety of sources.

abstract class BaseDocumentLoader implements DocumentLoader {
abstract load(): Promise<Document[]>;
}

TextLoader 子类化

¥Subclassing TextLoader

如果你想从文本文件加载文档,可以扩展 TextLoader 类。TextLoader 类负责读取文件,因此你只需实现一个解析方法即可。

¥If you want to load documents from a text file, you can extend the TextLoader class. The TextLoader class takes care of reading the file, so all you have to do is implement a parse method.

abstract class TextLoader extends BaseDocumentLoader {
abstract parse(raw: string): Promise<string[]>;
}

BufferLoader 子类化

¥Subclassing BufferLoader

如果你想从二进制文件加载文档,可以扩展 BufferLoader 类。BufferLoader 类负责读取文件,因此你只需实现一个解析方法即可。

¥If you want to load documents from a binary file, you can extend the BufferLoader class. The BufferLoader class takes care of reading the file, so all you have to do is implement a parse method.

abstract class BufferLoader extends BaseDocumentLoader {
abstract parse(
raw: Buffer,
metadata: Document["metadata"]
): Promise<Document[]>;
}