UNPKG

@langchain/community

Version:
71 lines (70 loc) 2.53 kB
import { __exportAll } from "../../_virtual/_rolldown/runtime.js"; import { Document } from "@langchain/core/documents"; import { Storage } from "@google-cloud/storage"; import { Docstore } from "@langchain/classic/stores/doc/base"; //#region src/stores/doc/gcs.ts var gcs_exports = /* @__PURE__ */ __exportAll({ GoogleCloudStorageDocstore: () => GoogleCloudStorageDocstore }); /** * Class that provides an interface for interacting with Google Cloud * Storage (GCS) as a document store. It extends the Docstore class and * implements methods to search, add, and add a document to the GCS * bucket. */ var GoogleCloudStorageDocstore = class extends Docstore { bucket; prefix = ""; storage; constructor(config) { super(); this.bucket = config.bucket; this.prefix = config.prefix ?? this.prefix; this.storage = new Storage(); } /** * Searches for a document in the GCS bucket and returns it as a Document * instance. * @param search The name of the document to search for in the GCS bucket * @returns A Promise that resolves to a Document instance representing the found document */ async search(search) { const file = this.getFile(search); const [fileMetadata] = await file.getMetadata(); const metadata = fileMetadata?.metadata; const [dataBuffer] = await file.download(); return new Document({ pageContent: dataBuffer.toString(), metadata }); } /** * Adds multiple documents to the GCS bucket. * @param texts An object where each key is the name of a document and the value is the Document instance to be added * @returns A Promise that resolves when all documents have been added */ async add(texts) { await Promise.all(Object.keys(texts).map((key) => this.addDocument(key, texts[key]))); } /** * Adds a single document to the GCS bucket. * @param name The name of the document to be added * @param document The Document instance to be added * @returns A Promise that resolves when the document has been added */ async addDocument(name, document) { const file = this.getFile(name); await file.save(document.pageContent); await file.setMetadata({ metadata: document.metadata }); } /** * Gets a file from the GCS bucket. * @param name The name of the file to get from the GCS bucket * @returns A File instance representing the fetched file */ getFile(name) { const filename = this.prefix + name; return this.storage.bucket(this.bucket).file(filename); } }; //#endregion export { GoogleCloudStorageDocstore, gcs_exports }; //# sourceMappingURL=gcs.js.map