-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
unstructured.ts
382 lines (341 loc) Β· 11.3 KB
/
unstructured.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import type { basename as BasenameT } from "node:path";
import type { readFile as ReadFileT } from "node:fs/promises";
import { Document } from "@langchain/core/documents";
import { getEnv } from "@langchain/core/utils/env";
import { StringWithAutocomplete } from "@langchain/core/utils/types";
import {
DirectoryLoader,
UnknownHandling,
LoadersMapping,
} from "./directory.js";
import { BaseDocumentLoader } from "../base.js";
import { logVersion020MigrationWarning } from "../../util/entrypoint_deprecation.js";
/* #__PURE__ */ logVersion020MigrationWarning({
oldEntrypointName: "document_loaders/fs/unstructured",
newPackageName: "@langchain/community",
});
const UNSTRUCTURED_API_FILETYPES = [
".txt",
".text",
".pdf",
".docx",
".doc",
".jpg",
".jpeg",
".eml",
".html",
".htm",
".md",
".pptx",
".ppt",
".msg",
".rtf",
".xlsx",
".xls",
".odt",
".epub",
];
/**
* Represents an element returned by the Unstructured API. It has
* properties for the element type, text content, and metadata.
*/
type Element = {
type: string;
text: string;
// this is purposefully loosely typed
metadata: {
[key: string]: unknown;
};
};
/**
* Represents the available strategies for the UnstructuredLoader. It can
* be one of "hi_res", "fast", "ocr_only", or "auto".
*/
export type UnstructuredLoaderStrategy =
| "hi_res"
| "fast"
| "ocr_only"
| "auto";
/**
* Represents the available hi-res models for the UnstructuredLoader. It can
* be one of "chipper".
*/
export type HiResModelName = "chipper";
/**
* To enable or disable table extraction for file types other than PDF, set
* the skipInferTableTypes property in the UnstructuredLoaderOptions object.
* The skipInferTableTypes property is an array of file types for which table
* extraction is disabled. For example, to disable table extraction for .docx
* and .doc files, set the skipInferTableTypes property to ["docx", "doc"].
* You can also disable table extraction for all file types other than PDF by
* setting the skipInferTableTypes property to [].
*/
export type SkipInferTableTypes =
| "txt"
| "text"
| "pdf"
| "docx"
| "doc"
| "jpg"
| "jpeg"
| "eml"
| "html"
| "htm"
| "md"
| "pptx"
| "ppt"
| "msg"
| "rtf"
| "xlsx"
| "xls"
| "odt"
| "epub";
/**
* Set the chunking_strategy to chunk text into larger or smaller elements. Defaults to None with optional arg of by_title
*/
type ChunkingStrategy = "None" | "by_title";
export type UnstructuredLoaderOptions = {
apiKey?: string;
apiUrl?: string;
strategy?: StringWithAutocomplete<UnstructuredLoaderStrategy>;
encoding?: string;
ocrLanguages?: Array<string>;
coordinates?: boolean;
pdfInferTableStructure?: boolean;
xmlKeepTags?: boolean;
skipInferTableTypes?: Array<StringWithAutocomplete<SkipInferTableTypes>>;
hiResModelName?: StringWithAutocomplete<HiResModelName>;
includePageBreaks?: boolean;
chunkingStrategy?: StringWithAutocomplete<ChunkingStrategy>;
multiPageSections?: boolean;
combineUnderNChars?: number;
newAfterNChars?: number;
maxCharacters?: number;
};
type UnstructuredDirectoryLoaderOptions = UnstructuredLoaderOptions & {
recursive?: boolean;
unknown?: UnknownHandling;
};
/**
* A document loader that uses the Unstructured API to load unstructured
* documents. It supports both the new syntax with options object and the
* legacy syntax for backward compatibility. The load() method sends a
* partitioning request to the Unstructured API and retrieves the
* partitioned elements. It creates a Document instance for each element
* and returns an array of Document instances.
*/
export class UnstructuredLoader extends BaseDocumentLoader {
public filePath: string;
private apiUrl = "https://api.unstructured.io/general/v0/general";
private apiKey?: string;
private strategy: StringWithAutocomplete<UnstructuredLoaderStrategy> =
"hi_res";
private encoding?: string;
private ocrLanguages: Array<string> = [];
private coordinates?: boolean;
private pdfInferTableStructure?: boolean;
private xmlKeepTags?: boolean;
private skipInferTableTypes?: Array<
StringWithAutocomplete<SkipInferTableTypes>
>;
private hiResModelName?: StringWithAutocomplete<HiResModelName>;
private includePageBreaks?: boolean;
private chunkingStrategy?: StringWithAutocomplete<ChunkingStrategy>;
private multiPageSections?: boolean;
private combineUnderNChars?: number;
private newAfterNChars?: number;
private maxCharacters?: number;
constructor(
filePathOrLegacyApiUrl: string,
optionsOrLegacyFilePath: UnstructuredLoaderOptions | string = {}
) {
super();
// Temporary shim to avoid breaking existing users
// Remove when API keys are enforced by Unstructured and existing code will break anyway
const isLegacySyntax = typeof optionsOrLegacyFilePath === "string";
if (isLegacySyntax) {
this.filePath = optionsOrLegacyFilePath;
this.apiUrl = filePathOrLegacyApiUrl;
} else {
this.filePath = filePathOrLegacyApiUrl;
const options = optionsOrLegacyFilePath;
this.apiKey = options.apiKey;
this.apiUrl = options.apiUrl ?? this.apiUrl;
this.strategy = options.strategy ?? this.strategy;
this.encoding = options.encoding;
this.ocrLanguages = options.ocrLanguages ?? this.ocrLanguages;
this.coordinates = options.coordinates;
this.pdfInferTableStructure = options.pdfInferTableStructure;
this.xmlKeepTags = options.xmlKeepTags;
this.skipInferTableTypes = options.skipInferTableTypes;
this.hiResModelName = options.hiResModelName;
this.includePageBreaks = options.includePageBreaks;
this.chunkingStrategy = options.chunkingStrategy;
this.multiPageSections = options.multiPageSections;
this.combineUnderNChars = options.combineUnderNChars;
this.newAfterNChars = options.newAfterNChars;
this.maxCharacters = options.maxCharacters;
}
}
async _partition() {
const { readFile, basename } = await this.imports();
const buffer = await readFile(this.filePath);
const fileName = basename(this.filePath);
// I'm aware this reads the file into memory first, but we have lots of work
// to do on then consuming Documents in a streaming fashion anyway, so not
// worried about this for now.
const formData = new FormData();
formData.append("files", new Blob([buffer]), fileName);
formData.append("strategy", this.strategy);
this.ocrLanguages.forEach((language) => {
formData.append("ocr_languages", language);
});
if (this.encoding) {
formData.append("encoding", this.encoding);
}
if (this.coordinates === true) {
formData.append("coordinates", "true");
}
if (this.pdfInferTableStructure === true) {
formData.append("pdf_infer_table_structure", "true");
}
if (this.xmlKeepTags === true) {
formData.append("xml_keep_tags", "true");
}
if (this.skipInferTableTypes) {
formData.append(
"skip_infer_table_types",
JSON.stringify(this.skipInferTableTypes)
);
}
if (this.hiResModelName) {
formData.append("hi_res_model_name", this.hiResModelName);
}
if (this.includePageBreaks) {
formData.append("include_page_breaks", "true");
}
if (this.chunkingStrategy) {
formData.append("chunking_strategy", this.chunkingStrategy);
}
if (this.multiPageSections !== undefined) {
formData.append(
"multipage_sections",
this.multiPageSections ? "true" : "false"
);
}
if (this.combineUnderNChars !== undefined) {
formData.append("combine_under_n_chars", String(this.combineUnderNChars));
}
if (this.newAfterNChars !== undefined) {
formData.append("new_after_n_chars", String(this.newAfterNChars));
}
if (this.maxCharacters !== undefined) {
formData.append("max_characters", String(this.maxCharacters));
}
const headers = {
"UNSTRUCTURED-API-KEY": this.apiKey ?? "",
};
const response = await fetch(this.apiUrl, {
method: "POST",
body: formData,
headers,
});
if (!response.ok) {
throw new Error(
`Failed to partition file ${this.filePath} with error ${
response.status
} and message ${await response.text()}`
);
}
const elements = await response.json();
if (!Array.isArray(elements)) {
throw new Error(
`Expected partitioning request to return an array, but got ${elements}`
);
}
return elements.filter((el) => typeof el.text === "string") as Element[];
}
async load(): Promise<Document[]> {
const elements = await this._partition();
const documents: Document[] = [];
for (const element of elements) {
const { metadata, text } = element;
if (typeof text === "string") {
documents.push(
new Document({
pageContent: text,
metadata: {
...metadata,
category: element.type,
},
})
);
}
}
return documents;
}
async imports(): Promise<{
readFile: typeof ReadFileT;
basename: typeof BasenameT;
}> {
try {
const { readFile } = await import("node:fs/promises");
const { basename } = await import("node:path");
return { readFile, basename };
} catch (e) {
console.error(e);
throw new Error(
`Failed to load fs/promises. TextLoader available only on environment 'node'. It appears you are running environment '${getEnv()}'. See https://<link to docs> for alternatives.`
);
}
}
}
/**
* A document loader that loads unstructured documents from a directory
* using the UnstructuredLoader. It creates a UnstructuredLoader instance
* for each supported file type and passes it to the DirectoryLoader
* constructor.
* @example
* ```typescript
* const loader = new UnstructuredDirectoryLoader("path/to/directory", {
* apiKey: "MY_API_KEY",
* });
* const docs = await loader.load();
* ```
*/
export class UnstructuredDirectoryLoader extends DirectoryLoader {
constructor(
directoryPathOrLegacyApiUrl: string,
optionsOrLegacyDirectoryPath: UnstructuredDirectoryLoaderOptions | string,
legacyOptionRecursive = true,
legacyOptionUnknown: UnknownHandling = UnknownHandling.Warn
) {
let directoryPath;
let options: UnstructuredDirectoryLoaderOptions;
// Temporary shim to avoid breaking existing users
// Remove when API keys are enforced by Unstructured and existing code will break anyway
const isLegacySyntax = typeof optionsOrLegacyDirectoryPath === "string";
if (isLegacySyntax) {
directoryPath = optionsOrLegacyDirectoryPath;
options = {
apiUrl: directoryPathOrLegacyApiUrl,
recursive: legacyOptionRecursive,
unknown: legacyOptionUnknown,
};
} else {
directoryPath = directoryPathOrLegacyApiUrl;
options = optionsOrLegacyDirectoryPath;
}
const loader = (p: string) => new UnstructuredLoader(p, options);
const loaders = UNSTRUCTURED_API_FILETYPES.reduce(
(loadersObject: LoadersMapping, filetype: string) => {
// eslint-disable-next-line no-param-reassign
loadersObject[filetype] = loader;
return loadersObject;
},
{}
);
super(directoryPath, loaders, options.recursive, options.unknown);
}
}
export { UnknownHandling };