-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathfile-loader.ts
More file actions
208 lines (182 loc) · 6.86 KB
/
file-loader.ts
File metadata and controls
208 lines (182 loc) · 6.86 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { CSVLoader } from "@langchain/community/document_loaders/fs/csv";
import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf";
import { CheerioWebBaseLoader } from "@langchain/community/document_loaders/web/cheerio";
import { HtmlToTextTransformer } from "@langchain/community/document_transformers/html_to_text";
import { Document } from "@langchain/core/documents";
import { TextLoader } from "langchain/document_loaders/fs/text";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { nanoid } from "nanoid";
import { UnstructuredClient } from "unstructured-client";
import type { DatasWithFileSource, FilePath, ProcessorType, URL } from "./database";
type Element = {
type: string;
text: string;
// this is purposefully loosely typed
metadata: Record<string, unknown>;
};
export class FileDataLoader {
private config: DatasWithFileSource;
constructor(config: DatasWithFileSource) {
this.config = config;
}
async loadFile(args: any) {
const loader = this.createLoader(args);
const _loader = await loader;
const documents = await _loader.load();
return (args: any) => this.transformDocument(documents, args);
}
private async createLoader(args: any) {
if (hasProcessor(this.config)) {
return await this.createLoaderForProcessors();
}
switch (this.config.type) {
case "pdf": {
return new PDFLoader(
this.config.fileSource,
args satisfies Extract<DatasWithFileSource, { type: "pdf" }>
);
}
case "csv": {
return new CSVLoader(
this.config.fileSource,
args satisfies Extract<DatasWithFileSource, { type: "csv" }>
);
}
case "text-file": {
return new TextLoader(this.config.fileSource);
}
case "html": {
return this.isURL(this.config.source)
? new CheerioWebBaseLoader(this.config.source)
: new TextLoader(this.config.source);
}
default: {
//@ts-expect-error TS can't pick up the correct type due to complex union
throw new Error(`Unsupported data type: ${this.config.type}`);
}
}
}
private async createLoaderForProcessors() {
// Without this check typescript complains about types because of unions
if (!hasProcessor(this.config)) throw new Error("Only processors are allowed");
const client = new UnstructuredClient({
serverURL: "https://api.unstructuredapp.io",
security: {
apiKeyAuth: this.config.processor.options.apiKey,
},
});
//@ts-expect-error TS can't pick up the correct type due to complex union
const fileData = await Bun.file(this.config.fileSource).text();
const response = await client.general.partition({
//@ts-expect-error Will be fixed soon
partitionParameters: {
files: {
content: fileData,
//@ts-expect-error TS can't pick up the correct type due to complex union
fileName: this.config.fileSource,
},
...this.config.processor.options,
},
});
const elements = response.elements?.filter(
(element) => typeof element.text === "string"
) as Element[];
return {
// eslint-disable-next-line @typescript-eslint/require-await
load: async (): Promise<Document[]> => {
const documents: Document[] = [];
for (const element of elements) {
const { metadata, text } = element;
if (typeof text === "string" && text !== "") {
documents.push(
new Document({
pageContent: text,
metadata: {
...metadata,
category: element.type,
},
})
);
}
}
return documents;
},
};
}
private isURL(source: FilePath | Blob): source is URL {
return typeof source === "string" && source.startsWith("http");
}
private async transformDocument(documents: Document[], args: any) {
switch (this.config.type) {
case "pdf": {
const splitter = new RecursiveCharacterTextSplitter(args);
const splittedDocuments = await splitter.splitDocuments(documents);
return this.mapDocumentsIntoInsertPayload(
splittedDocuments,
(metadata: any, index: number) => ({
source: metadata.source,
timestamp: new Date().toISOString(),
paragraphNumber: index + 1,
pageNumber: metadata.loc?.pageNumber || undefined,
author: metadata.pdf?.info?.Author || undefined,
title: metadata.pdf?.info?.Title || undefined,
totalPages: metadata.pdf?.totalPages || undefined,
language: metadata.pdf?.metadata?._metadata?.["dc:language"] || undefined,
})
);
}
case "csv": {
return this.mapDocumentsIntoInsertPayload(documents);
}
case "text-file": {
const splitter = new RecursiveCharacterTextSplitter(args);
const splittedDocuments = await splitter.splitDocuments(documents);
return this.mapDocumentsIntoInsertPayload(splittedDocuments);
}
case "html": {
const splitter = RecursiveCharacterTextSplitter.fromLanguage("html", args);
const transformer = new HtmlToTextTransformer();
const sequence = splitter.pipe(transformer);
const newDocuments = await sequence.invoke(documents);
return this.mapDocumentsIntoInsertPayload(newDocuments);
}
// Processors will be handled here. E.g. "unstructured", "llama-parse"
case undefined: {
const documents_ = documents.map(
(item) => new Document({ pageContent: item.pageContent, metadata: item.metadata })
);
return documents_.map((document) => ({
data: document.pageContent,
metadata: document.metadata,
id: nanoid(),
}));
}
default: {
// @ts-expect-error config type is set as never
throw new Error(`Unsupported data type: ${this.config.type}`);
}
}
}
private mapDocumentsIntoInsertPayload(
splittedDocuments: Document[],
metadataMapper?: (metadata: any, index: number) => Record<string, any>
) {
return splittedDocuments.map((document, index) => ({
data: document.pageContent,
id: nanoid(),
metadata: {
...(metadataMapper ? metadataMapper(document.metadata, index) : {}),
...this.config.options?.metadata,
},
}));
}
}
function hasProcessor(
data: DatasWithFileSource
): data is DatasWithFileSource & { processor: ProcessorType } {
return "processor" in data && typeof data.processor === "object" && "options" in data.processor;
}