-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add s3 loader * revert changes to yarn.lock / package.json * install aws-sdk/client-s3 as devDependency * merge * Revert "merge" This reverts commit 2954cd7. * Add s3 loader to new, refactored file structure * Fix after import paths change * Add entrypoint, make an args aobject --------- Co-authored-by: Nuno Campos <nuno@boringbits.io>
- Loading branch information
1 parent
38ed068
commit f975656
Showing
9 changed files
with
1,385 additions
and
2 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
docs/docs/modules/indexes/document_loaders/examples/file_loaders/s3.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
hide_table_of_contents: true | ||
sidebar_class_name: node-only | ||
--- | ||
|
||
# S3 File | ||
|
||
:::tip Compatibility | ||
Only available on Node.js. | ||
::: | ||
|
||
This covers how to load document objects from an s3 file object. | ||
|
||
## Setup | ||
|
||
To run this index you'll need to have Unstructured already set up and ready to use at an available URL endpoint. It can also be configured to run locally. | ||
|
||
See the docs [here](https://js.langchain.com/docs/modules/indexes/document_loaders/examples/file_loaders/unstructured) for information on how to do that. | ||
|
||
## Usage | ||
|
||
Once Unstructured is configured, you can use the S3 loader to load files and then convert them into a Document. | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/document_loaders/s3.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { S3Loader } from "langchain/document_loaders/web/s3"; | ||
|
||
const loader = new S3Loader({ | ||
bucket: "my-document-bucket-123", | ||
key: "AccountingOverview.pdf", | ||
unstructuredAPIURL: "http://localhost:8000/general/v0/general", | ||
}); | ||
|
||
const docs = await loader.load(); | ||
|
||
console.log(docs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* eslint-disable tree-shaking/no-side-effects-in-initialization */ | ||
import { test, jest, expect } from "@jest/globals"; | ||
import S3Client from "@aws-sdk/client-s3"; | ||
import * as fs from "node:fs"; | ||
import * as path from "node:path"; | ||
import { Readable } from "node:stream"; | ||
import { S3Loader } from "../web/s3.js"; | ||
import { UnstructuredLoader } from "../fs/unstructured.js"; | ||
|
||
const fsMock = { | ||
...fs, | ||
mkdtempSync: jest.fn().mockReturnValue("tmp/s3fileloader-12345"), | ||
mkdirSync: jest.fn().mockImplementation(() => {}), | ||
writeFileSync: jest.fn().mockImplementation(() => {}), | ||
}; | ||
|
||
const UnstructuredLoaderMock = jest.fn().mockImplementation(() => ({ | ||
load: jest.fn().mockImplementation(() => ["fake document"]), | ||
})); | ||
|
||
jest.mock("@aws-sdk/client-s3", () => ({ | ||
S3Client: jest.fn().mockImplementation(() => ({ | ||
send: jest.fn().mockImplementation(() => | ||
Promise.resolve({ | ||
Body: new Readable({ | ||
read() { | ||
this.push(Buffer.from("Mock file content")); | ||
this.push(null); | ||
}, | ||
}), | ||
}) | ||
), | ||
})), | ||
GetObjectCommand: jest.fn(), | ||
})); | ||
|
||
test("Test S3 loader", async () => { | ||
if (!S3Client) { | ||
// this is to avoid a linting error. S3Client is mocked above. | ||
} | ||
|
||
const loader = new S3Loader({ | ||
bucket: "test-bucket-123", | ||
key: "AccountingOverview.pdf", | ||
unstructuredAPIURL: "http://localhost:8000/general/v0/general", | ||
fs: fsMock as typeof fs, | ||
UnstructuredLoader: UnstructuredLoaderMock as typeof UnstructuredLoader, | ||
}); | ||
|
||
const result = await loader.load(); | ||
|
||
expect(fsMock.mkdtempSync).toHaveBeenCalled(); | ||
expect(fsMock.mkdirSync).toHaveBeenCalled(); | ||
expect(fsMock.writeFileSync).toHaveBeenCalled(); | ||
expect(UnstructuredLoaderMock).toHaveBeenCalledWith( | ||
"http://localhost:8000/general/v0/general", | ||
path.join("tmp", "s3fileloader-12345", "AccountingOverview.pdf") | ||
); | ||
expect(result).toEqual(["fake document"]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import * as fsDefault from "node:fs"; | ||
import * as path from "node:path"; | ||
import * as os from "node:os"; | ||
import { Readable } from "node:stream"; | ||
import { BaseDocumentLoader } from "../base.js"; | ||
import { UnstructuredLoader as UnstructuredLoaderDefault } from "../fs/unstructured.js"; | ||
|
||
export interface S3LoaderParams { | ||
bucket: string; | ||
key: string; | ||
unstructuredAPIURL: string; | ||
|
||
fs?: typeof fsDefault; | ||
UnstructuredLoader?: typeof UnstructuredLoaderDefault; | ||
} | ||
|
||
export class S3Loader extends BaseDocumentLoader { | ||
private bucket: string; | ||
|
||
private key: string; | ||
|
||
private unstructuredAPIURL: string; | ||
|
||
private _fs: typeof fsDefault; | ||
|
||
private _UnstructuredLoader: typeof UnstructuredLoaderDefault; | ||
|
||
constructor({ | ||
bucket, | ||
key, | ||
unstructuredAPIURL, | ||
fs = fsDefault, | ||
UnstructuredLoader = UnstructuredLoaderDefault, | ||
}: S3LoaderParams) { | ||
super(); | ||
this.bucket = bucket; | ||
this.key = key; | ||
this.unstructuredAPIURL = unstructuredAPIURL; | ||
this._fs = fs; | ||
this._UnstructuredLoader = UnstructuredLoader; | ||
} | ||
|
||
public async load() { | ||
const { S3Client, GetObjectCommand } = await S3LoaderImports(); | ||
|
||
const tempDir = this._fs.mkdtempSync( | ||
path.join(os.tmpdir(), "s3fileloader-") | ||
); | ||
|
||
const filePath = path.join(tempDir, this.key); | ||
|
||
try { | ||
const s3Client = new S3Client({}); | ||
|
||
const getObjectCommand = new GetObjectCommand({ | ||
Bucket: this.bucket, | ||
Key: this.key, | ||
}); | ||
|
||
const response = await s3Client.send(getObjectCommand); | ||
|
||
const objectData = await new Promise<Buffer>((resolve, reject) => { | ||
const chunks: Buffer[] = []; | ||
|
||
// eslint-disable-next-line no-instanceof/no-instanceof | ||
if (response.Body instanceof Readable) { | ||
response.Body.on("data", (chunk: Buffer) => chunks.push(chunk)); | ||
response.Body.on("end", () => resolve(Buffer.concat(chunks))); | ||
response.Body.on("error", reject); | ||
} else { | ||
reject(new Error("Response body is not a readable stream.")); | ||
} | ||
}); | ||
|
||
this._fs.mkdirSync(path.dirname(filePath), { recursive: true }); | ||
|
||
this._fs.writeFileSync(filePath, objectData); | ||
} catch { | ||
throw new Error( | ||
`Failed to download file ${this.key} from S3 bucket ${this.bucket}.` | ||
); | ||
} | ||
|
||
try { | ||
const unstructuredLoader = new this._UnstructuredLoader( | ||
this.unstructuredAPIURL, | ||
filePath | ||
); | ||
|
||
const docs = await unstructuredLoader.load(); | ||
|
||
return docs; | ||
} catch { | ||
throw new Error( | ||
`Failed to load file ${filePath} using unstructured loader.` | ||
); | ||
} | ||
} | ||
} | ||
|
||
async function S3LoaderImports() { | ||
try { | ||
const s3Module = await import("@aws-sdk/client-s3"); | ||
|
||
return s3Module as typeof s3Module; | ||
} catch (e) { | ||
console.error(e); | ||
throw new Error( | ||
"Failed to load @aws-sdk/client-s3'. Please install it eg. `yarn add @aws-sdk/client-s3`." | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
f975656
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
langchainjs-docs – ./
langchainjs-docs-ruddy.vercel.app
js.langchain.com
langchainjs-docs-git-main-langchain.vercel.app
langchainjs-docs-langchain.vercel.app