Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: add strategy to unstructured options #1208

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions langchain/src/document_loaders/fs/unstructured.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type UnstructuredLoaderOptions = {
type UnstructuredDirectoryLoaderOptions = UnstructuredLoaderOptions & {
recursive?: boolean;
unknown?: UnknownHandling;
strategy?: string;
};

export class UnstructuredLoader extends BaseDocumentLoader {
Expand All @@ -51,6 +52,8 @@ export class UnstructuredLoader extends BaseDocumentLoader {

private apiKey?: string;

private strategy?: string;
jacoblee93 marked this conversation as resolved.
Show resolved Hide resolved

constructor(
filePathOrLegacyApiUrl: string,
optionsOrLegacyFilePath: UnstructuredLoaderOptions | string = {}
Expand Down Expand Up @@ -84,6 +87,7 @@ export class UnstructuredLoader extends BaseDocumentLoader {

const headers = {
"UNSTRUCTURED-API-KEY": this.apiKey ?? "",
strategy: this.strategy ?? "hi_res",
};

const response = await fetch(this.apiUrl, {
Expand Down
20 changes: 20 additions & 0 deletions langchain/src/document_loaders/tests/unstructured.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ test("Test Unstructured base loader", async () => {
}
});

test("Test Unstructured base loader with fast strategy", async () => {
const filePath = path.resolve(
path.dirname(url.fileURLToPath(import.meta.url)),
"./example_data/1706.03762.pdf"
);

const options = {
apiKey: "MY_API_KEY",
strategy: "fast",
};

const loader = new UnstructuredLoader(filePath, options);
const docs = await loader.load();

expect(docs.length).toBeGreaterThan(10);
for (const doc of docs) {
expect(typeof doc.pageContent).toBe("string");
}
});

test("Test Unstructured directory loader legacy syntax", async () => {
const directoryPath = path.resolve(
path.dirname(url.fileURLToPath(import.meta.url)),
Expand Down