UNPKG

@langchain/community

Version:
1 lines 5.07 kB
{"version":3,"file":"azure_blob_storage_file.cjs","names":["BaseDocumentLoader","fs","path","os","BlobServiceClient","UnstructuredLoader"],"sources":["../../../src/document_loaders/web/azure_blob_storage_file.ts"],"sourcesContent":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport * as os from \"node:os\";\nimport { BlobServiceClient } from \"@azure/storage-blob\";\nimport { BaseDocumentLoader } from \"@langchain/core/document_loaders/base\";\nimport {\n UnstructuredLoader,\n UnstructuredLoaderOptions,\n} from \"../fs/unstructured.js\";\n\n/**\n * Interface representing the configuration for accessing a specific file\n * in Azure Blob Storage.\n */\ninterface AzureBlobStorageFileConfig {\n connectionString: string;\n container: string;\n blobName: string;\n}\n\n/**\n * Interface representing the configuration for the\n * AzureBlobStorageFileLoader. It contains the Azure Blob Storage file\n * configuration and the options for the UnstructuredLoader.\n */\ninterface AzureBlobStorageFileLoaderConfig {\n azureConfig: AzureBlobStorageFileConfig;\n unstructuredConfig?: UnstructuredLoaderOptions;\n}\n\n/**\n * Class representing a document loader that loads a specific file from\n * Azure Blob Storage. It extends the BaseDocumentLoader class and\n * implements the DocumentLoader interface.\n * @example\n * ```typescript\n * const loader = new AzureBlobStorageFileLoader({\n * azureConfig: {\n * connectionString: \"{connectionString}\",\n * container: \"{containerName}\",\n * blobName: \"{blobName}\",\n * },\n * });\n * const docs = await loader.load();\n * ```\n */\nexport class AzureBlobStorageFileLoader extends BaseDocumentLoader {\n get lc_secrets(): { [key: string]: string } {\n return {\n connectionString: \"AZURE_BLOB_CONNECTION_STRING\",\n };\n }\n\n private readonly connectionString: string;\n\n private readonly container: string;\n\n private readonly blobName: string;\n\n private readonly unstructuredConfig?: UnstructuredLoaderOptions;\n\n constructor({\n azureConfig,\n unstructuredConfig,\n }: AzureBlobStorageFileLoaderConfig) {\n super();\n this.connectionString = azureConfig.connectionString;\n this.container = azureConfig.container;\n this.blobName = azureConfig.blobName;\n this.unstructuredConfig = unstructuredConfig;\n }\n\n /**\n * Method to load a specific file from Azure Blob Storage. It creates a\n * temporary directory, constructs the file path, downloads the file, and\n * loads the documents using the UnstructuredLoader. The loaded documents\n * are returned, and the temporary directory is deleted.\n * @returns An array of documents loaded from the file in Azure Blob Storage.\n */\n public async load() {\n const tempDir = fs.mkdtempSync(\n path.join(os.tmpdir(), \"azureblobfileloader-\")\n );\n\n const filePath = path.join(tempDir, this.blobName);\n\n try {\n const blobServiceClient = BlobServiceClient.fromConnectionString(\n this.connectionString,\n {\n userAgentOptions: {\n userAgentPrefix: \"langchainjs-blob-storage-file\",\n },\n }\n );\n\n const containerClient = blobServiceClient.getContainerClient(\n this.container\n );\n\n const blobClient = containerClient.getBlobClient(this.blobName);\n\n fs.mkdirSync(path.dirname(filePath), { recursive: true });\n await blobClient.downloadToFile(filePath);\n } catch (e: unknown) {\n throw new Error(\n `Failed to download file ${\n this.blobName\n } from Azure Blob Storage container ${this.container}: ${\n (e as Error).message\n }`\n );\n }\n\n try {\n const unstructuredLoader = new UnstructuredLoader(\n filePath,\n this.unstructuredConfig\n );\n\n const docs = await unstructuredLoader.load();\n return docs;\n } catch {\n throw new Error(\n `Failed to load file ${filePath} using unstructured loader.`\n );\n } finally {\n fs.rmSync(path.dirname(filePath), { recursive: true, force: true });\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,IAAa,6BAAb,cAAgDA,sCAAAA,mBAAmB;CACjE,IAAI,aAAwC;AAC1C,SAAO,EACL,kBAAkB,gCACnB;;CAGH;CAEA;CAEA;CAEA;CAEA,YAAY,EACV,aACA,sBACmC;AACnC,SAAO;AACP,OAAK,mBAAmB,YAAY;AACpC,OAAK,YAAY,YAAY;AAC7B,OAAK,WAAW,YAAY;AAC5B,OAAK,qBAAqB;;;;;;;;;CAU5B,MAAa,OAAO;EAClB,MAAM,UAAUC,QAAG,YACjBC,UAAK,KAAKC,QAAG,QAAQ,EAAE,uBAAuB,CAC/C;EAED,MAAM,WAAWD,UAAK,KAAK,SAAS,KAAK,SAAS;AAElD,MAAI;GAcF,MAAM,aAboBE,oBAAAA,kBAAkB,qBAC1C,KAAK,kBACL,EACE,kBAAkB,EAChB,iBAAiB,iCAClB,EACF,CACF,CAEyC,mBACxC,KAAK,UACN,CAEkC,cAAc,KAAK,SAAS;AAE/D,WAAG,UAAUF,UAAK,QAAQ,SAAS,EAAE,EAAE,WAAW,MAAM,CAAC;AACzD,SAAM,WAAW,eAAe,SAAS;WAClC,GAAY;AACnB,SAAM,IAAI,MACR,2BACE,KAAK,SACN,qCAAqC,KAAK,UAAU,IAClD,EAAY,UAEhB;;AAGH,MAAI;AAOF,UADa,MALc,IAAIG,yCAAAA,mBAC7B,UACA,KAAK,mBACN,CAEqC,MAAM;UAEtC;AACN,SAAM,IAAI,MACR,uBAAuB,SAAS,6BACjC;YACO;AACR,WAAG,OAAOH,UAAK,QAAQ,SAAS,EAAE;IAAE,WAAW;IAAM,OAAO;IAAM,CAAC"}