-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
assemblyai.ts
227 lines (208 loc) Β· 7.7 KB
/
assemblyai.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import {
AssemblyAI,
BaseServiceParams,
TranscribeParams,
SubtitleFormat,
Transcript,
TranscriptParagraph,
TranscriptSentence,
CreateTranscriptParameters,
} from "assemblyai";
import { Document } from "@langchain/core/documents";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { BaseDocumentLoader } from "../base.js";
import { AssemblyAIOptions } from "../../types/assemblyai-types.js";
import { logVersion020MigrationWarning } from "../../util/entrypoint_deprecation.js";
/* #__PURE__ */ logVersion020MigrationWarning({
oldEntrypointName: "document_loaders/web/assemblyai",
newPackageName: "@langchain/community",
});
export type * from "../../types/assemblyai-types.js";
/**
* Base class for AssemblyAI loaders.
*/
abstract class AssemblyAILoader extends BaseDocumentLoader {
protected client: AssemblyAI;
/**
* Create a new AssemblyAI loader.
* @param assemblyAIOptions The options to configure the AssemblyAI loader.
* Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
*/
constructor(assemblyAIOptions?: AssemblyAIOptions) {
super();
let options = assemblyAIOptions;
if (!options) {
options = {};
}
if (!options.apiKey) {
options.apiKey = getEnvironmentVariable("ASSEMBLYAI_API_KEY");
}
if (!options.apiKey) {
throw new Error("No AssemblyAI API key provided");
}
this.client = new AssemblyAI(options as BaseServiceParams);
}
}
abstract class CreateTranscriptLoader extends AssemblyAILoader {
protected transcribeParams?: TranscribeParams | CreateTranscriptParameters;
protected transcriptId?: string;
/**
* Transcribe audio or retrieve an existing transcript by its ID.
* @param params The parameters to transcribe audio, or the ID of the transcript to retrieve.
* @param assemblyAIOptions The options to configure the AssemblyAI loader.
* Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
*/
constructor(
params: TranscribeParams | CreateTranscriptParameters | string,
assemblyAIOptions?: AssemblyAIOptions
) {
super(assemblyAIOptions);
if (typeof params === "string") {
this.transcriptId = params;
} else {
this.transcribeParams = params;
}
}
protected async transcribeOrGetTranscript() {
if (this.transcriptId) {
return await this.client.transcripts.get(this.transcriptId);
}
if (this.transcribeParams) {
let transcribeParams: TranscribeParams;
if ("audio_url" in this.transcribeParams) {
transcribeParams = {
...this.transcribeParams,
audio: this.transcribeParams.audio_url,
};
} else {
transcribeParams = this.transcribeParams;
}
return await this.client.transcripts.transcribe(transcribeParams);
} else {
throw new Error("No transcript ID or transcribe parameters provided");
}
}
}
/**
* Transcribe audio and load the transcript as a document using AssemblyAI.
*/
export class AudioTranscriptLoader extends CreateTranscriptLoader {
/**
* Transcribe audio and load the transcript as a document using AssemblyAI.
* @returns A promise that resolves to a single document containing the transcript text
* as the page content, and the transcript object as the metadata.
*/
override async load(): Promise<Document<Transcript>[]> {
const transcript = await this.transcribeOrGetTranscript();
return [
new Document({
pageContent: transcript.text as string,
metadata: transcript,
}),
];
}
}
/**
* Transcribe audio and load the paragraphs of the transcript, creating a document for each paragraph.
*/
export class AudioTranscriptParagraphsLoader extends CreateTranscriptLoader {
/**
* Transcribe audio and load the paragraphs of the transcript, creating a document for each paragraph.
* @returns A promise that resolves to an array of documents, each containing a paragraph of the transcript.
*/
override async load(): Promise<Document<TranscriptParagraph>[]> {
const transcript = await this.transcribeOrGetTranscript();
const paragraphsResponse = await this.client.transcripts.paragraphs(
transcript.id
);
return paragraphsResponse.paragraphs.map(
(p: TranscriptParagraph) =>
new Document({
pageContent: p.text,
metadata: p,
})
);
}
}
/**
* Transcribe audio and load the sentences of the transcript, creating a document for each sentence.
*/
export class AudioTranscriptSentencesLoader extends CreateTranscriptLoader {
/**
* Transcribe audio and load the sentences of the transcript, creating a document for each sentence.
* @returns A promise that resolves to an array of documents, each containing a sentence of the transcript.
*/
override async load(): Promise<Document<TranscriptSentence>[]> {
const transcript = await this.transcribeOrGetTranscript();
const sentencesResponse = await this.client.transcripts.sentences(
transcript.id
);
return sentencesResponse.sentences.map(
(p: TranscriptSentence) =>
new Document({
pageContent: p.text,
metadata: p,
})
);
}
}
/**
* Transcribe audio and load subtitles for the transcript as `srt` or `vtt` format.
*/
export class AudioSubtitleLoader extends CreateTranscriptLoader {
/**
* Create a new AudioSubtitleLoader.
* @param transcribeParams The parameters to transcribe audio.
* @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.
* @param assemblyAIOptions The options to configure the AssemblyAI loader.
* Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
*/
constructor(
transcribeParams: TranscribeParams | CreateTranscriptParameters,
subtitleFormat: SubtitleFormat,
assemblyAIOptions?: AssemblyAIOptions
);
/**
* Create a new AudioSubtitleLoader.
* @param transcriptId The ID of the transcript to retrieve.
* @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.
* @param assemblyAIOptions The options to configure the AssemblyAI loader.
* Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
*/
constructor(
transcriptId: string,
subtitleFormat: SubtitleFormat,
assemblyAIOptions?: AssemblyAIOptions
);
/**
* Create a new AudioSubtitleLoader.
* @param params The parameters to transcribe audio, or the ID of the transcript to retrieve.
* @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.
* @param assemblyAIOptions The options to configure the AssemblyAI loader.
* Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
*/
constructor(
params: TranscribeParams | CreateTranscriptParameters | string,
private subtitleFormat: SubtitleFormat = "srt",
assemblyAIOptions?: AssemblyAIOptions
) {
super(params, assemblyAIOptions);
this.subtitleFormat = subtitleFormat;
}
/**
* Transcribe audio and load subtitles for the transcript as `srt` or `vtt` format.
* @returns A promise that resolves a document containing the subtitles as the page content.
*/
override async load(): Promise<Document[]> {
const transcript = await this.transcribeOrGetTranscript();
const subtitles = await this.client.transcripts.subtitles(
transcript.id,
this.subtitleFormat
);
return [
new Document({
pageContent: subtitles,
}),
];
}
}