-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
feat(community): Support google cloud storage document loader #7740
Merged
jacoblee93
merged 5 commits into
langchain-ai:main
from
AllenFang:feat/google-cloud-storage-loader
Feb 27, 2025
+219
−4
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...re_docs/docs/integrations/document_loaders/web_loaders/google_cloud_storage.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,33 @@ | ||
--- | ||
hide_table_of_contents: true | ||
sidebar_class_name: node-only | ||
--- | ||
|
||
# Google Cloud Storage | ||
|
||
:::tip Compatibility | ||
Only available on Node.js. | ||
::: | ||
|
||
This covers how to load a Google Cloud Storage File into LangChain documents. | ||
|
||
## Setup | ||
|
||
To use this loader, 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](/docs/integrations/document_loaders/file_loaders/unstructured) for information on how to do that. | ||
|
||
You'll also need to install the official Google Cloud Storage SDK: | ||
|
||
```bash npm2yarn | ||
npm install @langchain/community @langchain/core @google-cloud/storage | ||
``` | ||
|
||
## Usage | ||
|
||
Once Unstructured is configured, you can use the Google Cloud Storage loader to load files and then convert them into a Document. | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/document_loaders/google_cloud_storage.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,17 @@ | ||
import { GoogleCloudStorageLoader } from "@langchain/community/document_loaders/web/google_cloud_storage"; | ||
|
||
const loader = new GoogleCloudStorageLoader({ | ||
bucket: "my-bucket-123", | ||
file: "path/to/file.pdf", | ||
storageOptions: { | ||
keyFilename: "/path/to/keyfile.json", | ||
}, | ||
unstructuredLoaderOptions: { | ||
apiUrl: "http://localhost:8000/general/v0/general", | ||
apiKey: "", // this will be soon required | ||
}, | ||
}); | ||
|
||
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
104 changes: 104 additions & 0 deletions
104
libs/langchain-community/src/document_loaders/web/google_cloud_storage.ts
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,104 @@ | ||
import { Storage, StorageOptions } from "@google-cloud/storage"; | ||
import * as os from "node:os"; | ||
import * as path from "node:path"; | ||
import * as fsDefault from "node:fs"; | ||
import { BaseDocumentLoader } from "@langchain/core/document_loaders/base"; | ||
import { Document } from "@langchain/core/documents"; | ||
import { | ||
UnstructuredLoaderOptions, | ||
UnstructuredLoader, | ||
} from "../fs/unstructured.js"; | ||
|
||
/** | ||
* Represents the parameters for the GoogleCloudStorageLoader class. It includes | ||
* properties such as the GCS bucket, file destination, the options for the UnstructuredLoader and | ||
* the options for Google Cloud Storage | ||
*/ | ||
export type GcsLoaderConfig = { | ||
fs?: typeof fsDefault; | ||
bucket: string; | ||
file: string; | ||
unstructuredLoaderOptions: UnstructuredLoaderOptions; | ||
storageOptions: StorageOptions; | ||
}; | ||
|
||
/** | ||
* A class that extends the BaseDocumentLoader class. It represents a | ||
* document loader for loading files from a google cloud storage bucket. | ||
* @example | ||
* ```typescript | ||
* const loader = new GoogleCloudStorageLoader({ | ||
* bucket: "<my-bucket-name>", | ||
* file: "<file-path>", | ||
* storageOptions: { | ||
* keyFilename: "<key-file-name-path>" | ||
* } | ||
* unstructuredConfig: { | ||
* apiUrl: "<unstructured-API-URL>", | ||
* apiKey: "<unstructured-API-key>" | ||
* } | ||
* }); | ||
* const docs = await loader.load(); | ||
* ``` | ||
*/ | ||
export class GoogleCloudStorageLoader extends BaseDocumentLoader { | ||
private bucket: string; | ||
|
||
private file: string; | ||
|
||
private storageOptions: StorageOptions; | ||
|
||
private _fs: typeof fsDefault; | ||
|
||
private unstructuredLoaderOptions: UnstructuredLoaderOptions; | ||
|
||
constructor({ | ||
fs = fsDefault, | ||
file, | ||
bucket, | ||
unstructuredLoaderOptions, | ||
storageOptions, | ||
}: GcsLoaderConfig) { | ||
super(); | ||
this._fs = fs; | ||
this.bucket = bucket; | ||
this.file = file; | ||
this.unstructuredLoaderOptions = unstructuredLoaderOptions; | ||
this.storageOptions = storageOptions; | ||
} | ||
|
||
async load(): Promise<Document<Record<string, any>>[]> { | ||
const tempDir = this._fs.mkdtempSync( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could we do this without the filesystem as a passthrough? |
||
path.join(os.tmpdir(), "googlecloudstoragefileloader-") | ||
); | ||
const filePath = path.join(tempDir, this.file); | ||
|
||
try { | ||
const storage = new Storage(this.storageOptions); | ||
const bucket = storage.bucket(this.bucket); | ||
|
||
const [buffer] = await bucket.file(this.file).download(); | ||
this._fs.mkdirSync(path.dirname(filePath), { recursive: true }); | ||
|
||
this._fs.writeFileSync(filePath, buffer); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (e: any) { | ||
throw new Error( | ||
`Failed to download file ${this.file} from google cloud storage bucket ${this.bucket}: ${e.message}` | ||
); | ||
} | ||
|
||
try { | ||
const unstructuredLoader = new UnstructuredLoader( | ||
filePath, | ||
this.unstructuredLoaderOptions | ||
); | ||
const docs = await unstructuredLoader.load(); | ||
return docs; | ||
} catch { | ||
throw new Error( | ||
`Failed to load file ${filePath} using unstructured loader.` | ||
); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
StorageOptions seems to be used just for authentication. Does this also work with Application Default Credentials? If so - this should be an optional field.