UNPKG

@langchain/community

Version:
1 lines 9.97 kB
{"version":3,"file":"assemblyai.cjs","names":["BaseDocumentLoader","AssemblyAI","Document"],"sources":["../../../src/document_loaders/web/assemblyai.ts"],"sourcesContent":["import {\n AssemblyAI,\n BaseServiceParams,\n TranscribeParams,\n SubtitleFormat,\n Transcript,\n TranscriptParagraph,\n TranscriptSentence,\n CreateTranscriptParameters,\n} from \"assemblyai\";\nimport { Document } from \"@langchain/core/documents\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { BaseDocumentLoader } from \"@langchain/core/document_loaders/base\";\nimport { AssemblyAIOptions } from \"../../types/assemblyai-types.js\";\n\nexport type * from \"../../types/assemblyai-types.js\";\n\nconst defaultOptions = {\n userAgent: {\n integration: { name: \"LangChainJS\", version: \"1.0.1\" },\n },\n};\n\n/**\n * Base class for AssemblyAI loaders.\n */\nabstract class AssemblyAILoader extends BaseDocumentLoader {\n protected client: AssemblyAI;\n\n /**\n * Create a new AssemblyAI loader.\n * @param assemblyAIOptions The options to configure the AssemblyAI loader.\n * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.\n */\n constructor(assemblyAIOptions?: AssemblyAIOptions) {\n super();\n let options = assemblyAIOptions;\n if (!options) {\n options = {};\n }\n if (!options.apiKey) {\n options.apiKey = getEnvironmentVariable(\"ASSEMBLYAI_API_KEY\");\n }\n if (!options.apiKey) {\n throw new Error(\"No AssemblyAI API key provided\");\n }\n\n this.client = new AssemblyAI({\n ...defaultOptions,\n ...options,\n } as BaseServiceParams);\n }\n}\n\nabstract class CreateTranscriptLoader extends AssemblyAILoader {\n protected transcribeParams?: TranscribeParams | CreateTranscriptParameters;\n\n protected transcriptId?: string;\n\n /**\n * Transcribe audio or retrieve an existing transcript by its ID.\n * @param params The parameters to transcribe audio, or the ID of the transcript to retrieve.\n * @param assemblyAIOptions The options to configure the AssemblyAI loader.\n * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.\n */\n constructor(\n params: TranscribeParams | CreateTranscriptParameters | string,\n assemblyAIOptions?: AssemblyAIOptions\n ) {\n super(assemblyAIOptions);\n if (typeof params === \"string\") {\n this.transcriptId = params;\n } else {\n this.transcribeParams = params;\n }\n }\n\n protected async transcribeOrGetTranscript() {\n if (this.transcriptId) {\n return await this.client.transcripts.get(this.transcriptId);\n }\n if (this.transcribeParams) {\n let transcribeParams: TranscribeParams;\n if (\"audio_url\" in this.transcribeParams) {\n transcribeParams = {\n ...this.transcribeParams,\n audio: this.transcribeParams.audio_url,\n };\n } else {\n transcribeParams = this.transcribeParams;\n }\n\n return await this.client.transcripts.transcribe(transcribeParams);\n } else {\n throw new Error(\"No transcript ID or transcribe parameters provided\");\n }\n }\n}\n\n/**\n * Transcribe audio and load the transcript as a document using AssemblyAI.\n */\nexport class AudioTranscriptLoader extends CreateTranscriptLoader {\n /**\n * Transcribe audio and load the transcript as a document using AssemblyAI.\n * @returns A promise that resolves to a single document containing the transcript text\n * as the page content, and the transcript object as the metadata.\n */\n override async load(): Promise<Document<Transcript>[]> {\n const transcript = await this.transcribeOrGetTranscript();\n\n return [\n new Document({\n pageContent: transcript.text as string,\n metadata: transcript,\n }),\n ];\n }\n}\n\n/**\n * Transcribe audio and load the paragraphs of the transcript, creating a document for each paragraph.\n */\nexport class AudioTranscriptParagraphsLoader extends CreateTranscriptLoader {\n /**\n * Transcribe audio and load the paragraphs of the transcript, creating a document for each paragraph.\n * @returns A promise that resolves to an array of documents, each containing a paragraph of the transcript.\n */\n override async load(): Promise<Document<TranscriptParagraph>[]> {\n const transcript = await this.transcribeOrGetTranscript();\n const paragraphsResponse = await this.client.transcripts.paragraphs(\n transcript.id\n );\n return paragraphsResponse.paragraphs.map(\n (p: TranscriptParagraph) =>\n new Document({\n pageContent: p.text,\n metadata: p,\n })\n );\n }\n}\n\n/**\n * Transcribe audio and load the sentences of the transcript, creating a document for each sentence.\n */\nexport class AudioTranscriptSentencesLoader extends CreateTranscriptLoader {\n /**\n * Transcribe audio and load the sentences of the transcript, creating a document for each sentence.\n * @returns A promise that resolves to an array of documents, each containing a sentence of the transcript.\n */\n override async load(): Promise<Document<TranscriptSentence>[]> {\n const transcript = await this.transcribeOrGetTranscript();\n const sentencesResponse = await this.client.transcripts.sentences(\n transcript.id\n );\n return sentencesResponse.sentences.map(\n (p: TranscriptSentence) =>\n new Document({\n pageContent: p.text,\n metadata: p,\n })\n );\n }\n}\n\n/**\n * Transcribe audio and load subtitles for the transcript as `srt` or `vtt` format.\n */\nexport class AudioSubtitleLoader extends CreateTranscriptLoader {\n /**\n * Create a new AudioSubtitleLoader.\n * @param transcribeParams The parameters to transcribe audio.\n * @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.\n * @param assemblyAIOptions The options to configure the AssemblyAI loader.\n * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.\n */\n constructor(\n transcribeParams: TranscribeParams | CreateTranscriptParameters,\n subtitleFormat: SubtitleFormat,\n assemblyAIOptions?: AssemblyAIOptions\n );\n\n /**\n * Create a new AudioSubtitleLoader.\n * @param transcriptId The ID of the transcript to retrieve.\n * @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.\n * @param assemblyAIOptions The options to configure the AssemblyAI loader.\n * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.\n */\n constructor(\n transcriptId: string,\n subtitleFormat: SubtitleFormat,\n assemblyAIOptions?: AssemblyAIOptions\n );\n\n /**\n * Create a new AudioSubtitleLoader.\n * @param params The parameters to transcribe audio, or the ID of the transcript to retrieve.\n * @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.\n * @param assemblyAIOptions The options to configure the AssemblyAI loader.\n * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.\n */\n constructor(\n params: TranscribeParams | CreateTranscriptParameters | string,\n private subtitleFormat: SubtitleFormat = \"srt\",\n assemblyAIOptions?: AssemblyAIOptions\n ) {\n super(params, assemblyAIOptions);\n this.subtitleFormat = subtitleFormat;\n }\n\n /**\n * Transcribe audio and load subtitles for the transcript as `srt` or `vtt` format.\n * @returns A promise that resolves a document containing the subtitles as the page content.\n */\n override async load(): Promise<Document[]> {\n const transcript = await this.transcribeOrGetTranscript();\n const subtitles = await this.client.transcripts.subtitles(\n transcript.id,\n this.subtitleFormat\n );\n\n return [\n new Document({\n pageContent: subtitles,\n }),\n ];\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAiBA,MAAM,iBAAiB,EACrB,WAAW,EACT,aAAa;CAAE,MAAM;CAAe,SAAS;CAAS,EACvD,EACF;;;;AAKD,IAAe,mBAAf,cAAwCA,sCAAAA,mBAAmB;CACzD;;;;;;CAOA,YAAY,mBAAuC;AACjD,SAAO;EACP,IAAI,UAAU;AACd,MAAI,CAAC,QACH,WAAU,EAAE;AAEd,MAAI,CAAC,QAAQ,OACX,SAAQ,UAAA,GAAA,0BAAA,wBAAgC,qBAAqB;AAE/D,MAAI,CAAC,QAAQ,OACX,OAAM,IAAI,MAAM,iCAAiC;AAGnD,OAAK,SAAS,IAAIC,WAAAA,WAAW;GAC3B,GAAG;GACH,GAAG;GACJ,CAAsB;;;AAI3B,IAAe,yBAAf,cAA8C,iBAAiB;CAC7D;CAEA;;;;;;;CAQA,YACE,QACA,mBACA;AACA,QAAM,kBAAkB;AACxB,MAAI,OAAO,WAAW,SACpB,MAAK,eAAe;MAEpB,MAAK,mBAAmB;;CAI5B,MAAgB,4BAA4B;AAC1C,MAAI,KAAK,aACP,QAAO,MAAM,KAAK,OAAO,YAAY,IAAI,KAAK,aAAa;AAE7D,MAAI,KAAK,kBAAkB;GACzB,IAAI;AACJ,OAAI,eAAe,KAAK,iBACtB,oBAAmB;IACjB,GAAG,KAAK;IACR,OAAO,KAAK,iBAAiB;IAC9B;OAED,oB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