langchain 0.0.169 → 0.0.171

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/README.md +2 -2
  2. package/agents/format_scratchpad.cjs +1 -0
  3. package/agents/format_scratchpad.d.ts +1 -0
  4. package/agents/format_scratchpad.js +1 -0
  5. package/chat_models/llama_cpp.cjs +1 -0
  6. package/chat_models/llama_cpp.d.ts +1 -0
  7. package/chat_models/llama_cpp.js +1 -0
  8. package/dist/agents/agent.cjs +49 -1
  9. package/dist/agents/agent.d.ts +19 -1
  10. package/dist/agents/agent.js +47 -0
  11. package/dist/agents/executor.cjs +10 -1
  12. package/dist/agents/executor.d.ts +22 -8
  13. package/dist/agents/executor.js +11 -2
  14. package/dist/agents/format_scratchpad.cjs +25 -0
  15. package/dist/agents/format_scratchpad.d.ts +10 -0
  16. package/dist/agents/format_scratchpad.js +21 -0
  17. package/dist/agents/toolkits/aws_sfn.d.ts +4 -1
  18. package/dist/agents/toolkits/conversational_retrieval/openai_functions.d.ts +1 -1
  19. package/dist/agents/toolkits/json/json.d.ts +4 -1
  20. package/dist/agents/toolkits/openapi/openapi.cjs +8 -0
  21. package/dist/agents/toolkits/openapi/openapi.d.ts +12 -1
  22. package/dist/agents/toolkits/openapi/openapi.js +8 -0
  23. package/dist/agents/toolkits/sql/sql.d.ts +4 -1
  24. package/dist/agents/toolkits/vectorstore/vectorstore.d.ts +8 -2
  25. package/dist/agents/types.d.ts +13 -1
  26. package/dist/chains/sql_db/sql_db_chain.cjs +11 -0
  27. package/dist/chains/sql_db/sql_db_chain.d.ts +11 -0
  28. package/dist/chains/sql_db/sql_db_chain.js +11 -0
  29. package/dist/chat_models/baiduwenxin.cjs +12 -1
  30. package/dist/chat_models/baiduwenxin.d.ts +3 -1
  31. package/dist/chat_models/baiduwenxin.js +12 -1
  32. package/dist/chat_models/llama_cpp.cjs +243 -0
  33. package/dist/chat_models/llama_cpp.d.ts +94 -0
  34. package/dist/chat_models/llama_cpp.js +239 -0
  35. package/dist/document_loaders/web/assemblyai.cjs +63 -114
  36. package/dist/document_loaders/web/assemblyai.d.ts +38 -57
  37. package/dist/document_loaders/web/assemblyai.js +63 -100
  38. package/dist/document_loaders/web/pdf.cjs +23 -5
  39. package/dist/document_loaders/web/pdf.d.ts +9 -1
  40. package/dist/document_loaders/web/pdf.js +20 -2
  41. package/dist/graphs/neo4j_graph.cjs +14 -0
  42. package/dist/graphs/neo4j_graph.d.ts +14 -0
  43. package/dist/graphs/neo4j_graph.js +14 -0
  44. package/dist/llms/googlepalm.cjs +3 -0
  45. package/dist/llms/googlepalm.js +3 -0
  46. package/dist/load/import_constants.cjs +2 -0
  47. package/dist/load/import_constants.js +2 -0
  48. package/dist/load/import_map.cjs +3 -2
  49. package/dist/load/import_map.d.ts +1 -0
  50. package/dist/load/import_map.js +1 -0
  51. package/dist/memory/index.d.ts +1 -1
  52. package/dist/memory/index.js +1 -1
  53. package/dist/retrievers/parent_document.cjs +22 -2
  54. package/dist/retrievers/parent_document.d.ts +8 -1
  55. package/dist/retrievers/parent_document.js +22 -2
  56. package/dist/retrievers/time_weighted.cjs +1 -1
  57. package/dist/retrievers/time_weighted.d.ts +1 -1
  58. package/dist/retrievers/time_weighted.js +1 -1
  59. package/dist/schema/runnable/base.cjs +4 -1
  60. package/dist/schema/runnable/base.d.ts +1 -0
  61. package/dist/schema/runnable/base.js +4 -1
  62. package/dist/schema/runnable/passthrough.cjs +35 -1
  63. package/dist/schema/runnable/passthrough.d.ts +11 -1
  64. package/dist/schema/runnable/passthrough.js +34 -1
  65. package/dist/sql_db.cjs +14 -0
  66. package/dist/sql_db.d.ts +14 -0
  67. package/dist/sql_db.js +14 -0
  68. package/dist/storage/ioredis.cjs +2 -1
  69. package/dist/storage/ioredis.js +2 -1
  70. package/dist/storage/upstash_redis.cjs +155 -0
  71. package/dist/storage/upstash_redis.d.ts +59 -0
  72. package/dist/storage/upstash_redis.js +151 -0
  73. package/dist/storage/vercel_kv.cjs +2 -1
  74. package/dist/storage/vercel_kv.js +2 -1
  75. package/dist/types/assemblyai-types.cjs +0 -150
  76. package/dist/types/assemblyai-types.d.ts +4 -670
  77. package/dist/types/assemblyai-types.js +1 -149
  78. package/dist/util/stream.cjs +3 -0
  79. package/dist/util/stream.js +3 -0
  80. package/dist/vectorstores/cassandra.cjs +25 -4
  81. package/dist/vectorstores/cassandra.d.ts +11 -1
  82. package/dist/vectorstores/cassandra.js +25 -4
  83. package/dist/vectorstores/momento_vector_index.cjs +3 -15
  84. package/dist/vectorstores/momento_vector_index.d.ts +0 -8
  85. package/dist/vectorstores/momento_vector_index.js +3 -15
  86. package/dist/vectorstores/neo4j_vector.cjs +14 -0
  87. package/dist/vectorstores/neo4j_vector.d.ts +14 -0
  88. package/dist/vectorstores/neo4j_vector.js +14 -0
  89. package/dist/vectorstores/pgvector.cjs +1 -1
  90. package/dist/vectorstores/pgvector.js +1 -1
  91. package/package.json +37 -8
  92. package/storage/upstash_redis.cjs +1 -0
  93. package/storage/upstash_redis.d.ts +1 -0
  94. package/storage/upstash_redis.js +1 -0
  95. package/dist/util/assemblyai-client.cjs +0 -173
  96. package/dist/util/assemblyai-client.d.ts +0 -63
  97. package/dist/util/assemblyai-client.js +0 -170
@@ -1,27 +1,10 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
3
  exports.AudioSubtitleLoader = exports.AudioTranscriptSentencesLoader = exports.AudioTranscriptParagraphsLoader = exports.AudioTranscriptLoader = void 0;
18
- const node_fs_1 = require("node:fs");
4
+ const assemblyai_1 = require("assemblyai");
19
5
  const document_js_1 = require("../../document.cjs");
20
6
  const base_js_1 = require("../base.cjs");
21
7
  const env_js_1 = require("../../util/env.cjs");
22
- const assemblyai_client_js_1 = require("../../util/assemblyai-client.cjs");
23
- const assemblyai_types_js_1 = require("../../types/assemblyai-types.cjs");
24
- __exportStar(require("../../types/assemblyai-types.cjs"), exports);
25
8
  /**
26
9
  * Base class for AssemblyAI loaders.
27
10
  */
@@ -39,57 +22,67 @@ class AssemblyAILoader extends base_js_1.BaseDocumentLoader {
39
22
  writable: true,
40
23
  value: void 0
41
24
  });
42
- const apiKey = assemblyAIOptions?.apiKey ?? (0, env_js_1.getEnvironmentVariable)("ASSEMBLYAI_API_KEY");
43
- this.client = new assemblyai_client_js_1.AssemblyAIClient(apiKey);
44
- }
45
- /**
46
- * Attempts to upload the file to AssemblyAI if it is a local file.
47
- * If `audio_url` starts with `http://` or `https://`, it is assumed to be a remote file.
48
- * Otherwise, it is assumed to be a local file and is uploaded to AssemblyAI.
49
- * @param createTranscriptOptions
50
- */
51
- async uploadFile(createTranscriptOptions) {
52
- let path = createTranscriptOptions.audio_url;
53
- if (path.startsWith("http://") || path.startsWith("https://"))
54
- return;
55
- if (path.startsWith("file://"))
56
- path = path.slice("file://".length);
57
- const file = await node_fs_1.promises.readFile(path);
58
- const uploadUrl = await this.client.uploadFile(file);
59
- // eslint-disable-next-line no-param-reassign
60
- createTranscriptOptions.audio_url = uploadUrl;
25
+ let options = assemblyAIOptions;
26
+ if (!options) {
27
+ options = {};
28
+ }
29
+ if (!options.apiKey) {
30
+ options.apiKey = (0, env_js_1.getEnvironmentVariable)("ASSEMBLYAI_API_KEY");
31
+ }
32
+ if (!options.apiKey) {
33
+ throw new Error("No AssemblyAI API key provided");
34
+ }
35
+ this.client = new assemblyai_1.AssemblyAI(options);
61
36
  }
62
37
  }
63
- /**
64
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
65
- * and loads the transcript as a document using AssemblyAI.
66
- */
67
- class AudioTranscriptLoader extends AssemblyAILoader {
38
+ class CreateTranscriptLoader extends AssemblyAILoader {
68
39
  /**
69
- * Creates a new AudioTranscriptLoader.
70
- * @param createTranscriptParams The parameters to create the transcript.
40
+ * Retrevies an existing transcript by its ID.
41
+ * @param params The parameters to create the transcript, or the ID of the transcript to retrieve.
71
42
  * @param assemblyAIOptions The options to configure the AssemblyAI loader.
72
43
  * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
73
44
  */
74
- constructor(createTranscriptParams, assemblyAIOptions) {
45
+ constructor(params, assemblyAIOptions) {
75
46
  super(assemblyAIOptions);
76
- Object.defineProperty(this, "createTranscriptParams", {
47
+ Object.defineProperty(this, "CreateTranscriptParameters", {
77
48
  enumerable: true,
78
49
  configurable: true,
79
50
  writable: true,
80
- value: createTranscriptParams
51
+ value: void 0
81
52
  });
53
+ Object.defineProperty(this, "transcriptId", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ if (typeof params === "string") {
60
+ this.transcriptId = params;
61
+ }
62
+ else {
63
+ this.CreateTranscriptParameters = params;
64
+ }
65
+ }
66
+ async getOrCreateTranscript() {
67
+ if (this.transcriptId) {
68
+ return await this.client.transcripts.get(this.transcriptId);
69
+ }
70
+ if (this.CreateTranscriptParameters) {
71
+ return await this.client.transcripts.create(this.CreateTranscriptParameters);
72
+ }
82
73
  }
74
+ }
75
+ /**
76
+ * Creates and loads the transcript as a document using AssemblyAI.
77
+ */
78
+ class AudioTranscriptLoader extends CreateTranscriptLoader {
83
79
  /**
84
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
85
- * and loads the transcript as a document using AssemblyAI.
80
+ * Creates a transcript and loads the transcript as a document using AssemblyAI.
86
81
  * @returns A promise that resolves to a single document containing the transcript text
87
82
  * as the page content, and the transcript object as the metadata.
88
83
  */
89
84
  async load() {
90
- await this.uploadFile(this.createTranscriptParams);
91
- let transcript = await this.client.createTranscript(this.createTranscriptParams);
92
- transcript = await this.client.waitForTranscriptToComplete(transcript.id);
85
+ const transcript = await this.getOrCreateTranscript();
93
86
  return [
94
87
  new document_js_1.Document({
95
88
  pageContent: transcript.text,
@@ -100,34 +93,16 @@ class AudioTranscriptLoader extends AssemblyAILoader {
100
93
  }
101
94
  exports.AudioTranscriptLoader = AudioTranscriptLoader;
102
95
  /**
103
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
104
- * and loads the paragraphs of the transcript, creating a document for each paragraph.
96
+ * Creates a transcript and loads the paragraphs of the transcript, creating a document for each paragraph.
105
97
  */
106
- class AudioTranscriptParagraphsLoader extends AssemblyAILoader {
98
+ class AudioTranscriptParagraphsLoader extends CreateTranscriptLoader {
107
99
  /**
108
- * Creates a new AudioTranscriptParagraphsLoader.
109
- * @param createTranscriptParams The parameters to create the transcript.
110
- * @param assemblyAIOptions The options to configure the AssemblyAI loader.
111
- * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
112
- */
113
- constructor(createTranscriptParams, assemblyAIOptions) {
114
- super(assemblyAIOptions);
115
- Object.defineProperty(this, "createTranscriptParams", {
116
- enumerable: true,
117
- configurable: true,
118
- writable: true,
119
- value: createTranscriptParams
120
- });
121
- }
122
- /**
123
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
124
- * and loads the paragraphs of the transcript, creating a document for each paragraph.
100
+ * Creates a transcript and loads the paragraphs of the transcript, creating a document for each paragraph.
125
101
  * @returns A promise that resolves to an array of documents, each containing a paragraph of the transcript.
126
102
  */
127
103
  async load() {
128
- let transcript = await this.client.createTranscript(this.createTranscriptParams);
129
- transcript = await this.client.waitForTranscriptToComplete(transcript.id);
130
- const paragraphsResponse = await this.client.getParagraphs(transcript.id);
104
+ const transcript = await this.getOrCreateTranscript();
105
+ const paragraphsResponse = await this.client.transcripts.paragraphs(transcript.id);
131
106
  return paragraphsResponse.paragraphs.map((p) => new document_js_1.Document({
132
107
  pageContent: p.text,
133
108
  metadata: p,
@@ -136,34 +111,17 @@ class AudioTranscriptParagraphsLoader extends AssemblyAILoader {
136
111
  }
137
112
  exports.AudioTranscriptParagraphsLoader = AudioTranscriptParagraphsLoader;
138
113
  /**
139
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
114
+ * Creates a transcript for the given `CreateTranscriptParameters.audio_url`,
140
115
  * and loads the sentences of the transcript, creating a document for each sentence.
141
116
  */
142
- class AudioTranscriptSentencesLoader extends AssemblyAILoader {
143
- /**
144
- * Creates a new AudioTranscriptSentencesLoader.
145
- * @param createTranscriptParams The parameters to create the transcript.
146
- * @param assemblyAIOptions The options to configure the AssemblyAI loader.
147
- * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
148
- */
149
- constructor(createTranscriptParams, assemblyAIOptions) {
150
- super(assemblyAIOptions);
151
- Object.defineProperty(this, "createTranscriptParams", {
152
- enumerable: true,
153
- configurable: true,
154
- writable: true,
155
- value: createTranscriptParams
156
- });
157
- }
117
+ class AudioTranscriptSentencesLoader extends CreateTranscriptLoader {
158
118
  /**
159
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
160
- * and loads the sentences of the transcript, creating a document for each sentence.
119
+ * Creates a transcript and loads the sentences of the transcript, creating a document for each sentence.
161
120
  * @returns A promise that resolves to an array of documents, each containing a sentence of the transcript.
162
121
  */
163
122
  async load() {
164
- let transcript = await this.client.createTranscript(this.createTranscriptParams);
165
- transcript = await this.client.waitForTranscriptToComplete(transcript.id);
166
- const sentencesResponse = await this.client.getSentences(transcript.id);
123
+ const transcript = await this.getOrCreateTranscript();
124
+ const sentencesResponse = await this.client.transcripts.sentences(transcript.id);
167
125
  return sentencesResponse.sentences.map((p) => new document_js_1.Document({
168
126
  pageContent: p.text,
169
127
  metadata: p,
@@ -172,25 +130,18 @@ class AudioTranscriptSentencesLoader extends AssemblyAILoader {
172
130
  }
173
131
  exports.AudioTranscriptSentencesLoader = AudioTranscriptSentencesLoader;
174
132
  /**
175
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
176
- * and loads subtitles for the transcript as `srt` or `vtt` format.
133
+ * Creates a transcript and loads subtitles for the transcript as `srt` or `vtt` format.
177
134
  */
178
- class AudioSubtitleLoader extends AssemblyAILoader {
135
+ class AudioSubtitleLoader extends CreateTranscriptLoader {
179
136
  /**
180
137
  * Creates a new AudioSubtitleLoader.
181
- * @param createTranscriptParams The parameters to create the transcript.
138
+ * @param params The parameters to create the transcript, or the ID of the transcript to retrieve.
182
139
  * @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.
183
140
  * @param assemblyAIOptions The options to configure the AssemblyAI loader.
184
141
  * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
185
142
  */
186
- constructor(createTranscriptParams, subtitleFormat = assemblyai_types_js_1.SubtitleFormat.Srt, assemblyAIOptions) {
187
- super(assemblyAIOptions);
188
- Object.defineProperty(this, "createTranscriptParams", {
189
- enumerable: true,
190
- configurable: true,
191
- writable: true,
192
- value: createTranscriptParams
193
- });
143
+ constructor(params, subtitleFormat = "srt", assemblyAIOptions) {
144
+ super(params, assemblyAIOptions);
194
145
  Object.defineProperty(this, "subtitleFormat", {
195
146
  enumerable: true,
196
147
  configurable: true,
@@ -200,14 +151,12 @@ class AudioSubtitleLoader extends AssemblyAILoader {
200
151
  this.subtitleFormat = subtitleFormat;
201
152
  }
202
153
  /**
203
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
204
- * and loads subtitles for the transcript as `srt` or `vtt` format.
154
+ * Creates a transcript and loads subtitles for the transcript as `srt` or `vtt` format.
205
155
  * @returns A promise that resolves a document containing the subtitles as the page content.
206
156
  */
207
157
  async load() {
208
- let transcript = await this.client.createTranscript(this.createTranscriptParams);
209
- transcript = await this.client.waitForTranscriptToComplete(transcript.id);
210
- const subtitles = await this.client.getSubtitles(transcript.id, this.subtitleFormat);
158
+ const transcript = await this.getOrCreateTranscript();
159
+ const subtitles = await this.client.transcripts.subtitles(transcript.id, this.subtitleFormat);
211
160
  return [
212
161
  new document_js_1.Document({
213
162
  pageContent: subtitles,
@@ -1,106 +1,87 @@
1
+ import { AssemblyAI, CreateTranscriptParameters, SubtitleFormat, Transcript, TranscriptParagraph, TranscriptSentence } from "assemblyai";
1
2
  import { Document } from "../../document.js";
2
3
  import { BaseDocumentLoader } from "../base.js";
3
- import { AssemblyAIClient } from "../../util/assemblyai-client.js";
4
- import { AssemblyAIOptions, CreateTranscriptParams, SubtitleFormat, Transcript, TranscriptSegment } from "../../types/assemblyai-types.js";
5
- export * from "../../types/assemblyai-types.js";
4
+ import { AssemblyAIOptions } from "../../types/assemblyai-types.js";
5
+ export type * from "../../types/assemblyai-types.js";
6
6
  /**
7
7
  * Base class for AssemblyAI loaders.
8
8
  */
9
9
  declare abstract class AssemblyAILoader extends BaseDocumentLoader {
10
- protected client: AssemblyAIClient;
10
+ protected client: AssemblyAI;
11
11
  /**
12
12
  * Creates a new AssemblyAI loader.
13
13
  * @param assemblyAIOptions The options to configure the AssemblyAI loader.
14
14
  * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
15
15
  */
16
16
  constructor(assemblyAIOptions?: AssemblyAIOptions);
17
- /**
18
- * Attempts to upload the file to AssemblyAI if it is a local file.
19
- * If `audio_url` starts with `http://` or `https://`, it is assumed to be a remote file.
20
- * Otherwise, it is assumed to be a local file and is uploaded to AssemblyAI.
21
- * @param createTranscriptOptions
22
- */
23
- protected uploadFile(createTranscriptOptions: CreateTranscriptParams): Promise<void>;
24
17
  }
25
- /**
26
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
27
- * and loads the transcript as a document using AssemblyAI.
28
- */
29
- export declare class AudioTranscriptLoader extends AssemblyAILoader {
30
- private createTranscriptParams;
18
+ declare abstract class CreateTranscriptLoader extends AssemblyAILoader {
19
+ protected CreateTranscriptParameters?: CreateTranscriptParameters;
20
+ protected transcriptId?: string;
31
21
  /**
32
- * Creates a new AudioTranscriptLoader.
33
- * @param createTranscriptParams The parameters to create the transcript.
22
+ * Retrevies an existing transcript by its ID.
23
+ * @param params The parameters to create the transcript, or the ID of the transcript to retrieve.
34
24
  * @param assemblyAIOptions The options to configure the AssemblyAI loader.
35
25
  * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
36
26
  */
37
- constructor(createTranscriptParams: CreateTranscriptParams, assemblyAIOptions?: AssemblyAIOptions);
27
+ constructor(params: CreateTranscriptParameters | string, assemblyAIOptions?: AssemblyAIOptions);
28
+ protected getOrCreateTranscript(): Promise<any>;
29
+ }
30
+ /**
31
+ * Creates and loads the transcript as a document using AssemblyAI.
32
+ */
33
+ export declare class AudioTranscriptLoader extends CreateTranscriptLoader {
38
34
  /**
39
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
40
- * and loads the transcript as a document using AssemblyAI.
35
+ * Creates a transcript and loads the transcript as a document using AssemblyAI.
41
36
  * @returns A promise that resolves to a single document containing the transcript text
42
37
  * as the page content, and the transcript object as the metadata.
43
38
  */
44
39
  load(): Promise<Document<Transcript>[]>;
45
40
  }
46
41
  /**
47
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
48
- * and loads the paragraphs of the transcript, creating a document for each paragraph.
42
+ * Creates a transcript and loads the paragraphs of the transcript, creating a document for each paragraph.
49
43
  */
50
- export declare class AudioTranscriptParagraphsLoader extends AssemblyAILoader {
51
- private createTranscriptParams;
52
- /**
53
- * Creates a new AudioTranscriptParagraphsLoader.
54
- * @param createTranscriptParams The parameters to create the transcript.
55
- * @param assemblyAIOptions The options to configure the AssemblyAI loader.
56
- * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
57
- */
58
- constructor(createTranscriptParams: CreateTranscriptParams, assemblyAIOptions?: AssemblyAIOptions);
44
+ export declare class AudioTranscriptParagraphsLoader extends CreateTranscriptLoader {
59
45
  /**
60
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
61
- * and loads the paragraphs of the transcript, creating a document for each paragraph.
46
+ * Creates a transcript and loads the paragraphs of the transcript, creating a document for each paragraph.
62
47
  * @returns A promise that resolves to an array of documents, each containing a paragraph of the transcript.
63
48
  */
64
- load(): Promise<Document<TranscriptSegment>[]>;
49
+ load(): Promise<Document<TranscriptParagraph>[]>;
65
50
  }
66
51
  /**
67
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
52
+ * Creates a transcript for the given `CreateTranscriptParameters.audio_url`,
68
53
  * and loads the sentences of the transcript, creating a document for each sentence.
69
54
  */
70
- export declare class AudioTranscriptSentencesLoader extends AssemblyAILoader {
71
- private createTranscriptParams;
55
+ export declare class AudioTranscriptSentencesLoader extends CreateTranscriptLoader {
72
56
  /**
73
- * Creates a new AudioTranscriptSentencesLoader.
74
- * @param createTranscriptParams The parameters to create the transcript.
75
- * @param assemblyAIOptions The options to configure the AssemblyAI loader.
76
- * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
77
- */
78
- constructor(createTranscriptParams: CreateTranscriptParams, assemblyAIOptions?: AssemblyAIOptions);
79
- /**
80
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
81
- * and loads the sentences of the transcript, creating a document for each sentence.
57
+ * Creates a transcript and loads the sentences of the transcript, creating a document for each sentence.
82
58
  * @returns A promise that resolves to an array of documents, each containing a sentence of the transcript.
83
59
  */
84
- load(): Promise<Document<TranscriptSegment>[]>;
60
+ load(): Promise<Document<TranscriptSentence>[]>;
85
61
  }
86
62
  /**
87
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
88
- * and loads subtitles for the transcript as `srt` or `vtt` format.
63
+ * Creates a transcript and loads subtitles for the transcript as `srt` or `vtt` format.
89
64
  */
90
- export declare class AudioSubtitleLoader extends AssemblyAILoader {
91
- private createTranscriptParams;
65
+ export declare class AudioSubtitleLoader extends CreateTranscriptLoader {
92
66
  private subtitleFormat;
93
67
  /**
94
68
  * Creates a new AudioSubtitleLoader.
95
- * @param createTranscriptParams The parameters to create the transcript.
69
+ * @param CreateTranscriptParameters The parameters to create the transcript.
70
+ * @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.
71
+ * @param assemblyAIOptions The options to configure the AssemblyAI loader.
72
+ * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
73
+ */
74
+ constructor(CreateTranscriptParameters: CreateTranscriptParameters, subtitleFormat: SubtitleFormat, assemblyAIOptions?: AssemblyAIOptions);
75
+ /**
76
+ * Creates a new AudioSubtitleLoader.
77
+ * @param transcriptId The ID of the transcript to retrieve.
96
78
  * @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.
97
79
  * @param assemblyAIOptions The options to configure the AssemblyAI loader.
98
80
  * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
99
81
  */
100
- constructor(createTranscriptParams: CreateTranscriptParams, subtitleFormat?: (typeof SubtitleFormat)[keyof typeof SubtitleFormat], assemblyAIOptions?: AssemblyAIOptions);
82
+ constructor(transcriptId: string, subtitleFormat: SubtitleFormat, assemblyAIOptions?: AssemblyAIOptions);
101
83
  /**
102
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
103
- * and loads subtitles for the transcript as `srt` or `vtt` format.
84
+ * Creates a transcript and loads subtitles for the transcript as `srt` or `vtt` format.
104
85
  * @returns A promise that resolves a document containing the subtitles as the page content.
105
86
  */
106
87
  load(): Promise<Document[]>;
@@ -1,10 +1,7 @@
1
- import { promises as fs } from "node:fs";
1
+ import { AssemblyAI, } from "assemblyai";
2
2
  import { Document } from "../../document.js";
3
3
  import { BaseDocumentLoader } from "../base.js";
4
4
  import { getEnvironmentVariable } from "../../util/env.js";
5
- import { AssemblyAIClient } from "../../util/assemblyai-client.js";
6
- import { SubtitleFormat, } from "../../types/assemblyai-types.js";
7
- export * from "../../types/assemblyai-types.js";
8
5
  /**
9
6
  * Base class for AssemblyAI loaders.
10
7
  */
@@ -22,57 +19,67 @@ class AssemblyAILoader extends BaseDocumentLoader {
22
19
  writable: true,
23
20
  value: void 0
24
21
  });
25
- const apiKey = assemblyAIOptions?.apiKey ?? getEnvironmentVariable("ASSEMBLYAI_API_KEY");
26
- this.client = new AssemblyAIClient(apiKey);
27
- }
28
- /**
29
- * Attempts to upload the file to AssemblyAI if it is a local file.
30
- * If `audio_url` starts with `http://` or `https://`, it is assumed to be a remote file.
31
- * Otherwise, it is assumed to be a local file and is uploaded to AssemblyAI.
32
- * @param createTranscriptOptions
33
- */
34
- async uploadFile(createTranscriptOptions) {
35
- let path = createTranscriptOptions.audio_url;
36
- if (path.startsWith("http://") || path.startsWith("https://"))
37
- return;
38
- if (path.startsWith("file://"))
39
- path = path.slice("file://".length);
40
- const file = await fs.readFile(path);
41
- const uploadUrl = await this.client.uploadFile(file);
42
- // eslint-disable-next-line no-param-reassign
43
- createTranscriptOptions.audio_url = uploadUrl;
22
+ let options = assemblyAIOptions;
23
+ if (!options) {
24
+ options = {};
25
+ }
26
+ if (!options.apiKey) {
27
+ options.apiKey = getEnvironmentVariable("ASSEMBLYAI_API_KEY");
28
+ }
29
+ if (!options.apiKey) {
30
+ throw new Error("No AssemblyAI API key provided");
31
+ }
32
+ this.client = new AssemblyAI(options);
44
33
  }
45
34
  }
46
- /**
47
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
48
- * and loads the transcript as a document using AssemblyAI.
49
- */
50
- export class AudioTranscriptLoader extends AssemblyAILoader {
35
+ class CreateTranscriptLoader extends AssemblyAILoader {
51
36
  /**
52
- * Creates a new AudioTranscriptLoader.
53
- * @param createTranscriptParams The parameters to create the transcript.
37
+ * Retrevies an existing transcript by its ID.
38
+ * @param params The parameters to create the transcript, or the ID of the transcript to retrieve.
54
39
  * @param assemblyAIOptions The options to configure the AssemblyAI loader.
55
40
  * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
56
41
  */
57
- constructor(createTranscriptParams, assemblyAIOptions) {
42
+ constructor(params, assemblyAIOptions) {
58
43
  super(assemblyAIOptions);
59
- Object.defineProperty(this, "createTranscriptParams", {
44
+ Object.defineProperty(this, "CreateTranscriptParameters", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: void 0
49
+ });
50
+ Object.defineProperty(this, "transcriptId", {
60
51
  enumerable: true,
61
52
  configurable: true,
62
53
  writable: true,
63
- value: createTranscriptParams
54
+ value: void 0
64
55
  });
56
+ if (typeof params === "string") {
57
+ this.transcriptId = params;
58
+ }
59
+ else {
60
+ this.CreateTranscriptParameters = params;
61
+ }
62
+ }
63
+ async getOrCreateTranscript() {
64
+ if (this.transcriptId) {
65
+ return await this.client.transcripts.get(this.transcriptId);
66
+ }
67
+ if (this.CreateTranscriptParameters) {
68
+ return await this.client.transcripts.create(this.CreateTranscriptParameters);
69
+ }
65
70
  }
71
+ }
72
+ /**
73
+ * Creates and loads the transcript as a document using AssemblyAI.
74
+ */
75
+ export class AudioTranscriptLoader extends CreateTranscriptLoader {
66
76
  /**
67
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
68
- * and loads the transcript as a document using AssemblyAI.
77
+ * Creates a transcript and loads the transcript as a document using AssemblyAI.
69
78
  * @returns A promise that resolves to a single document containing the transcript text
70
79
  * as the page content, and the transcript object as the metadata.
71
80
  */
72
81
  async load() {
73
- await this.uploadFile(this.createTranscriptParams);
74
- let transcript = await this.client.createTranscript(this.createTranscriptParams);
75
- transcript = await this.client.waitForTranscriptToComplete(transcript.id);
82
+ const transcript = await this.getOrCreateTranscript();
76
83
  return [
77
84
  new Document({
78
85
  pageContent: transcript.text,
@@ -82,34 +89,16 @@ export class AudioTranscriptLoader extends AssemblyAILoader {
82
89
  }
83
90
  }
84
91
  /**
85
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
86
- * and loads the paragraphs of the transcript, creating a document for each paragraph.
92
+ * Creates a transcript and loads the paragraphs of the transcript, creating a document for each paragraph.
87
93
  */
88
- export class AudioTranscriptParagraphsLoader extends AssemblyAILoader {
89
- /**
90
- * Creates a new AudioTranscriptParagraphsLoader.
91
- * @param createTranscriptParams The parameters to create the transcript.
92
- * @param assemblyAIOptions The options to configure the AssemblyAI loader.
93
- * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
94
- */
95
- constructor(createTranscriptParams, assemblyAIOptions) {
96
- super(assemblyAIOptions);
97
- Object.defineProperty(this, "createTranscriptParams", {
98
- enumerable: true,
99
- configurable: true,
100
- writable: true,
101
- value: createTranscriptParams
102
- });
103
- }
94
+ export class AudioTranscriptParagraphsLoader extends CreateTranscriptLoader {
104
95
  /**
105
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
106
- * and loads the paragraphs of the transcript, creating a document for each paragraph.
96
+ * Creates a transcript and loads the paragraphs of the transcript, creating a document for each paragraph.
107
97
  * @returns A promise that resolves to an array of documents, each containing a paragraph of the transcript.
108
98
  */
109
99
  async load() {
110
- let transcript = await this.client.createTranscript(this.createTranscriptParams);
111
- transcript = await this.client.waitForTranscriptToComplete(transcript.id);
112
- const paragraphsResponse = await this.client.getParagraphs(transcript.id);
100
+ const transcript = await this.getOrCreateTranscript();
101
+ const paragraphsResponse = await this.client.transcripts.paragraphs(transcript.id);
113
102
  return paragraphsResponse.paragraphs.map((p) => new Document({
114
103
  pageContent: p.text,
115
104
  metadata: p,
@@ -117,34 +106,17 @@ export class AudioTranscriptParagraphsLoader extends AssemblyAILoader {
117
106
  }
118
107
  }
119
108
  /**
120
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
109
+ * Creates a transcript for the given `CreateTranscriptParameters.audio_url`,
121
110
  * and loads the sentences of the transcript, creating a document for each sentence.
122
111
  */
123
- export class AudioTranscriptSentencesLoader extends AssemblyAILoader {
124
- /**
125
- * Creates a new AudioTranscriptSentencesLoader.
126
- * @param createTranscriptParams The parameters to create the transcript.
127
- * @param assemblyAIOptions The options to configure the AssemblyAI loader.
128
- * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
129
- */
130
- constructor(createTranscriptParams, assemblyAIOptions) {
131
- super(assemblyAIOptions);
132
- Object.defineProperty(this, "createTranscriptParams", {
133
- enumerable: true,
134
- configurable: true,
135
- writable: true,
136
- value: createTranscriptParams
137
- });
138
- }
112
+ export class AudioTranscriptSentencesLoader extends CreateTranscriptLoader {
139
113
  /**
140
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
141
- * and loads the sentences of the transcript, creating a document for each sentence.
114
+ * Creates a transcript and loads the sentences of the transcript, creating a document for each sentence.
142
115
  * @returns A promise that resolves to an array of documents, each containing a sentence of the transcript.
143
116
  */
144
117
  async load() {
145
- let transcript = await this.client.createTranscript(this.createTranscriptParams);
146
- transcript = await this.client.waitForTranscriptToComplete(transcript.id);
147
- const sentencesResponse = await this.client.getSentences(transcript.id);
118
+ const transcript = await this.getOrCreateTranscript();
119
+ const sentencesResponse = await this.client.transcripts.sentences(transcript.id);
148
120
  return sentencesResponse.sentences.map((p) => new Document({
149
121
  pageContent: p.text,
150
122
  metadata: p,
@@ -152,25 +124,18 @@ export class AudioTranscriptSentencesLoader extends AssemblyAILoader {
152
124
  }
153
125
  }
154
126
  /**
155
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
156
- * and loads subtitles for the transcript as `srt` or `vtt` format.
127
+ * Creates a transcript and loads subtitles for the transcript as `srt` or `vtt` format.
157
128
  */
158
- export class AudioSubtitleLoader extends AssemblyAILoader {
129
+ export class AudioSubtitleLoader extends CreateTranscriptLoader {
159
130
  /**
160
131
  * Creates a new AudioSubtitleLoader.
161
- * @param createTranscriptParams The parameters to create the transcript.
132
+ * @param params The parameters to create the transcript, or the ID of the transcript to retrieve.
162
133
  * @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.
163
134
  * @param assemblyAIOptions The options to configure the AssemblyAI loader.
164
135
  * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
165
136
  */
166
- constructor(createTranscriptParams, subtitleFormat = SubtitleFormat.Srt, assemblyAIOptions) {
167
- super(assemblyAIOptions);
168
- Object.defineProperty(this, "createTranscriptParams", {
169
- enumerable: true,
170
- configurable: true,
171
- writable: true,
172
- value: createTranscriptParams
173
- });
137
+ constructor(params, subtitleFormat = "srt", assemblyAIOptions) {
138
+ super(params, assemblyAIOptions);
174
139
  Object.defineProperty(this, "subtitleFormat", {
175
140
  enumerable: true,
176
141
  configurable: true,
@@ -180,14 +145,12 @@ export class AudioSubtitleLoader extends AssemblyAILoader {
180
145
  this.subtitleFormat = subtitleFormat;
181
146
  }
182
147
  /**
183
- * Creates a transcript for the given `CreateTranscriptParams.audio_url`,
184
- * and loads subtitles for the transcript as `srt` or `vtt` format.
148
+ * Creates a transcript and loads subtitles for the transcript as `srt` or `vtt` format.
185
149
  * @returns A promise that resolves a document containing the subtitles as the page content.
186
150
  */
187
151
  async load() {
188
- let transcript = await this.client.createTranscript(this.createTranscriptParams);
189
- transcript = await this.client.waitForTranscriptToComplete(transcript.id);
190
- const subtitles = await this.client.getSubtitles(transcript.id, this.subtitleFormat);
152
+ const transcript = await this.getOrCreateTranscript();
153
+ const subtitles = await this.client.transcripts.subtitles(transcript.id, this.subtitleFormat);
191
154
  return [
192
155
  new Document({
193
156
  pageContent: subtitles,