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.
- package/README.md +2 -2
- package/agents/format_scratchpad.cjs +1 -0
- package/agents/format_scratchpad.d.ts +1 -0
- package/agents/format_scratchpad.js +1 -0
- package/chat_models/llama_cpp.cjs +1 -0
- package/chat_models/llama_cpp.d.ts +1 -0
- package/chat_models/llama_cpp.js +1 -0
- package/dist/agents/agent.cjs +49 -1
- package/dist/agents/agent.d.ts +19 -1
- package/dist/agents/agent.js +47 -0
- package/dist/agents/executor.cjs +10 -1
- package/dist/agents/executor.d.ts +22 -8
- package/dist/agents/executor.js +11 -2
- package/dist/agents/format_scratchpad.cjs +25 -0
- package/dist/agents/format_scratchpad.d.ts +10 -0
- package/dist/agents/format_scratchpad.js +21 -0
- package/dist/agents/toolkits/aws_sfn.d.ts +4 -1
- package/dist/agents/toolkits/conversational_retrieval/openai_functions.d.ts +1 -1
- package/dist/agents/toolkits/json/json.d.ts +4 -1
- package/dist/agents/toolkits/openapi/openapi.cjs +8 -0
- package/dist/agents/toolkits/openapi/openapi.d.ts +12 -1
- package/dist/agents/toolkits/openapi/openapi.js +8 -0
- package/dist/agents/toolkits/sql/sql.d.ts +4 -1
- package/dist/agents/toolkits/vectorstore/vectorstore.d.ts +8 -2
- package/dist/agents/types.d.ts +13 -1
- package/dist/chains/sql_db/sql_db_chain.cjs +11 -0
- package/dist/chains/sql_db/sql_db_chain.d.ts +11 -0
- package/dist/chains/sql_db/sql_db_chain.js +11 -0
- package/dist/chat_models/baiduwenxin.cjs +12 -1
- package/dist/chat_models/baiduwenxin.d.ts +3 -1
- package/dist/chat_models/baiduwenxin.js +12 -1
- package/dist/chat_models/llama_cpp.cjs +243 -0
- package/dist/chat_models/llama_cpp.d.ts +94 -0
- package/dist/chat_models/llama_cpp.js +239 -0
- package/dist/document_loaders/web/assemblyai.cjs +63 -114
- package/dist/document_loaders/web/assemblyai.d.ts +38 -57
- package/dist/document_loaders/web/assemblyai.js +63 -100
- package/dist/document_loaders/web/pdf.cjs +23 -5
- package/dist/document_loaders/web/pdf.d.ts +9 -1
- package/dist/document_loaders/web/pdf.js +20 -2
- package/dist/graphs/neo4j_graph.cjs +14 -0
- package/dist/graphs/neo4j_graph.d.ts +14 -0
- package/dist/graphs/neo4j_graph.js +14 -0
- package/dist/llms/googlepalm.cjs +3 -0
- package/dist/llms/googlepalm.js +3 -0
- package/dist/load/import_constants.cjs +2 -0
- package/dist/load/import_constants.js +2 -0
- package/dist/load/import_map.cjs +3 -2
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/memory/index.d.ts +1 -1
- package/dist/memory/index.js +1 -1
- package/dist/retrievers/parent_document.cjs +22 -2
- package/dist/retrievers/parent_document.d.ts +8 -1
- package/dist/retrievers/parent_document.js +22 -2
- package/dist/retrievers/time_weighted.cjs +1 -1
- package/dist/retrievers/time_weighted.d.ts +1 -1
- package/dist/retrievers/time_weighted.js +1 -1
- package/dist/schema/runnable/base.cjs +4 -1
- package/dist/schema/runnable/base.d.ts +1 -0
- package/dist/schema/runnable/base.js +4 -1
- package/dist/schema/runnable/passthrough.cjs +35 -1
- package/dist/schema/runnable/passthrough.d.ts +11 -1
- package/dist/schema/runnable/passthrough.js +34 -1
- package/dist/sql_db.cjs +14 -0
- package/dist/sql_db.d.ts +14 -0
- package/dist/sql_db.js +14 -0
- package/dist/storage/ioredis.cjs +2 -1
- package/dist/storage/ioredis.js +2 -1
- package/dist/storage/upstash_redis.cjs +155 -0
- package/dist/storage/upstash_redis.d.ts +59 -0
- package/dist/storage/upstash_redis.js +151 -0
- package/dist/storage/vercel_kv.cjs +2 -1
- package/dist/storage/vercel_kv.js +2 -1
- package/dist/types/assemblyai-types.cjs +0 -150
- package/dist/types/assemblyai-types.d.ts +4 -670
- package/dist/types/assemblyai-types.js +1 -149
- package/dist/util/stream.cjs +3 -0
- package/dist/util/stream.js +3 -0
- package/dist/vectorstores/cassandra.cjs +25 -4
- package/dist/vectorstores/cassandra.d.ts +11 -1
- package/dist/vectorstores/cassandra.js +25 -4
- package/dist/vectorstores/momento_vector_index.cjs +3 -15
- package/dist/vectorstores/momento_vector_index.d.ts +0 -8
- package/dist/vectorstores/momento_vector_index.js +3 -15
- package/dist/vectorstores/neo4j_vector.cjs +14 -0
- package/dist/vectorstores/neo4j_vector.d.ts +14 -0
- package/dist/vectorstores/neo4j_vector.js +14 -0
- package/dist/vectorstores/pgvector.cjs +1 -1
- package/dist/vectorstores/pgvector.js +1 -1
- package/package.json +37 -8
- package/storage/upstash_redis.cjs +1 -0
- package/storage/upstash_redis.d.ts +1 -0
- package/storage/upstash_redis.js +1 -0
- package/dist/util/assemblyai-client.cjs +0 -173
- package/dist/util/assemblyai-client.d.ts +0 -63
- package/dist/util/assemblyai-client.js +0 -170
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "langchain",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.171",
|
|
4
4
|
"description": "Typescript bindings for langchain",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -31,6 +31,9 @@
|
|
|
31
31
|
"agents/toolkits/sql.cjs",
|
|
32
32
|
"agents/toolkits/sql.js",
|
|
33
33
|
"agents/toolkits/sql.d.ts",
|
|
34
|
+
"agents/format_scratchpad.cjs",
|
|
35
|
+
"agents/format_scratchpad.js",
|
|
36
|
+
"agents/format_scratchpad.d.ts",
|
|
34
37
|
"base_language.cjs",
|
|
35
38
|
"base_language.js",
|
|
36
39
|
"base_language.d.ts",
|
|
@@ -472,6 +475,9 @@
|
|
|
472
475
|
"chat_models/minimax.cjs",
|
|
473
476
|
"chat_models/minimax.js",
|
|
474
477
|
"chat_models/minimax.d.ts",
|
|
478
|
+
"chat_models/llama_cpp.cjs",
|
|
479
|
+
"chat_models/llama_cpp.js",
|
|
480
|
+
"chat_models/llama_cpp.d.ts",
|
|
475
481
|
"chat_models/yandex.cjs",
|
|
476
482
|
"chat_models/yandex.js",
|
|
477
483
|
"chat_models/yandex.d.ts",
|
|
@@ -664,6 +670,9 @@
|
|
|
664
670
|
"storage/vercel_kv.cjs",
|
|
665
671
|
"storage/vercel_kv.js",
|
|
666
672
|
"storage/vercel_kv.d.ts",
|
|
673
|
+
"storage/upstash_redis.cjs",
|
|
674
|
+
"storage/upstash_redis.js",
|
|
675
|
+
"storage/upstash_redis.d.ts",
|
|
667
676
|
"graphs/neo4j_graph.cjs",
|
|
668
677
|
"graphs/neo4j_graph.js",
|
|
669
678
|
"graphs/neo4j_graph.d.ts",
|
|
@@ -718,7 +727,7 @@
|
|
|
718
727
|
],
|
|
719
728
|
"repository": {
|
|
720
729
|
"type": "git",
|
|
721
|
-
"url": "git@github.com:
|
|
730
|
+
"url": "git@github.com:langchain-ai/langchainjs.git"
|
|
722
731
|
},
|
|
723
732
|
"scripts": {
|
|
724
733
|
"build": "yarn clean && yarn build:esm && yarn build:cjs && node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js",
|
|
@@ -759,8 +768,8 @@
|
|
|
759
768
|
"@faker-js/faker": "^7.6.0",
|
|
760
769
|
"@getmetal/metal-sdk": "^4.0.0",
|
|
761
770
|
"@getzep/zep-js": "^0.8.0",
|
|
762
|
-
"@gomomento/sdk": "^1.
|
|
763
|
-
"@gomomento/sdk-core": "^1.
|
|
771
|
+
"@gomomento/sdk": "^1.44.1",
|
|
772
|
+
"@gomomento/sdk-core": "^1.44.1",
|
|
764
773
|
"@google-ai/generativelanguage": "^0.2.1",
|
|
765
774
|
"@google-cloud/storage": "^6.10.1",
|
|
766
775
|
"@huggingface/inference": "^1.5.1",
|
|
@@ -808,6 +817,7 @@
|
|
|
808
817
|
"@xenova/transformers": "^2.5.4",
|
|
809
818
|
"@zilliz/milvus2-sdk-node": ">=2.2.11",
|
|
810
819
|
"apify-client": "^2.7.1",
|
|
820
|
+
"assemblyai": "^2.0.2",
|
|
811
821
|
"axios": "^0.26.0",
|
|
812
822
|
"cassandra-driver": "^4.6.4",
|
|
813
823
|
"cheerio": "^1.0.0-rc.12",
|
|
@@ -846,7 +856,7 @@
|
|
|
846
856
|
"mongodb": "^5.2.0",
|
|
847
857
|
"mysql2": "^3.3.3",
|
|
848
858
|
"neo4j-driver": "^5.12.0",
|
|
849
|
-
"node-llama-cpp": "
|
|
859
|
+
"node-llama-cpp": "2.7.3",
|
|
850
860
|
"notion-to-md": "^3.1.0",
|
|
851
861
|
"pdf-parse": "1.1.1",
|
|
852
862
|
"peggy": "^3.0.2",
|
|
@@ -893,9 +903,9 @@
|
|
|
893
903
|
"@elastic/elasticsearch": "^8.4.0",
|
|
894
904
|
"@getmetal/metal-sdk": "*",
|
|
895
905
|
"@getzep/zep-js": "^0.8.0",
|
|
896
|
-
"@gomomento/sdk": "^1.
|
|
897
|
-
"@gomomento/sdk-core": "^1.
|
|
898
|
-
"@gomomento/sdk-web": "^1.
|
|
906
|
+
"@gomomento/sdk": "^1.44.1",
|
|
907
|
+
"@gomomento/sdk-core": "^1.44.1",
|
|
908
|
+
"@gomomento/sdk-web": "^1.44.1",
|
|
899
909
|
"@google-ai/generativelanguage": "^0.2.1",
|
|
900
910
|
"@google-cloud/storage": "^6.10.1",
|
|
901
911
|
"@huggingface/inference": "^1.5.1",
|
|
@@ -923,6 +933,7 @@
|
|
|
923
933
|
"@xenova/transformers": "^2.5.4",
|
|
924
934
|
"@zilliz/milvus2-sdk-node": ">=2.2.7",
|
|
925
935
|
"apify-client": "^2.7.1",
|
|
936
|
+
"assemblyai": "^2.0.2",
|
|
926
937
|
"axios": "*",
|
|
927
938
|
"cassandra-driver": "^4.6.4",
|
|
928
939
|
"cheerio": "^1.0.0-rc.12",
|
|
@@ -1109,6 +1120,9 @@
|
|
|
1109
1120
|
"apify-client": {
|
|
1110
1121
|
"optional": true
|
|
1111
1122
|
},
|
|
1123
|
+
"assemblyai": {
|
|
1124
|
+
"optional": true
|
|
1125
|
+
},
|
|
1112
1126
|
"axios": {
|
|
1113
1127
|
"optional": true
|
|
1114
1128
|
},
|
|
@@ -1334,6 +1348,11 @@
|
|
|
1334
1348
|
"import": "./agents/toolkits/sql.js",
|
|
1335
1349
|
"require": "./agents/toolkits/sql.cjs"
|
|
1336
1350
|
},
|
|
1351
|
+
"./agents/format_scratchpad": {
|
|
1352
|
+
"types": "./agents/format_scratchpad.d.ts",
|
|
1353
|
+
"import": "./agents/format_scratchpad.js",
|
|
1354
|
+
"require": "./agents/format_scratchpad.cjs"
|
|
1355
|
+
},
|
|
1337
1356
|
"./base_language": {
|
|
1338
1357
|
"types": "./base_language.d.ts",
|
|
1339
1358
|
"import": "./base_language.js",
|
|
@@ -2069,6 +2088,11 @@
|
|
|
2069
2088
|
"import": "./chat_models/minimax.js",
|
|
2070
2089
|
"require": "./chat_models/minimax.cjs"
|
|
2071
2090
|
},
|
|
2091
|
+
"./chat_models/llama_cpp": {
|
|
2092
|
+
"types": "./chat_models/llama_cpp.d.ts",
|
|
2093
|
+
"import": "./chat_models/llama_cpp.js",
|
|
2094
|
+
"require": "./chat_models/llama_cpp.cjs"
|
|
2095
|
+
},
|
|
2072
2096
|
"./chat_models/yandex": {
|
|
2073
2097
|
"types": "./chat_models/yandex.d.ts",
|
|
2074
2098
|
"import": "./chat_models/yandex.js",
|
|
@@ -2389,6 +2413,11 @@
|
|
|
2389
2413
|
"import": "./storage/vercel_kv.js",
|
|
2390
2414
|
"require": "./storage/vercel_kv.cjs"
|
|
2391
2415
|
},
|
|
2416
|
+
"./storage/upstash_redis": {
|
|
2417
|
+
"types": "./storage/upstash_redis.d.ts",
|
|
2418
|
+
"import": "./storage/upstash_redis.js",
|
|
2419
|
+
"require": "./storage/upstash_redis.cjs"
|
|
2420
|
+
},
|
|
2392
2421
|
"./graphs/neo4j_graph": {
|
|
2393
2422
|
"types": "./graphs/neo4j_graph.d.ts",
|
|
2394
2423
|
"import": "./graphs/neo4j_graph.js",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/storage/upstash_redis.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/storage/upstash_redis.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/storage/upstash_redis.js'
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AssemblyAIClient = void 0;
|
|
4
|
-
const assemblyai_types_js_1 = require("../types/assemblyai-types.cjs");
|
|
5
|
-
/**
|
|
6
|
-
* A client for the AssemblyAI API.
|
|
7
|
-
*/
|
|
8
|
-
class AssemblyAIClient {
|
|
9
|
-
/**
|
|
10
|
-
* @param apiKey The API key for the AssemblyAI API.
|
|
11
|
-
*/
|
|
12
|
-
constructor(apiKey) {
|
|
13
|
-
Object.defineProperty(this, "apiKey", {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
configurable: true,
|
|
16
|
-
writable: true,
|
|
17
|
-
value: apiKey
|
|
18
|
-
});
|
|
19
|
-
if (!apiKey)
|
|
20
|
-
throw new Error("No AssemblyAI API key provided");
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Uploads a file to AssemblyAI CDN.
|
|
24
|
-
* The file will only be accessible to AssemblyAI and be removed after a period of time.
|
|
25
|
-
* @param file Audio or video file to upload.
|
|
26
|
-
* @returns The URL of the uploaded file.
|
|
27
|
-
*/
|
|
28
|
-
async uploadFile(file) {
|
|
29
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/upload`, {
|
|
30
|
-
method: "POST",
|
|
31
|
-
headers: {
|
|
32
|
-
authorization: this.apiKey,
|
|
33
|
-
},
|
|
34
|
-
body: file,
|
|
35
|
-
});
|
|
36
|
-
const json = (await response.json());
|
|
37
|
-
AssemblyAIClient.throwIfError(json);
|
|
38
|
-
return json.upload_url;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Creates a transcript in the AssemblyAI API. The transcript will be queued for processing,
|
|
42
|
-
* but an empty transcript object is returned immediately.
|
|
43
|
-
* @param fileUrl The URL of the audio or video file to transcribe.
|
|
44
|
-
* @returns Empty transcript object
|
|
45
|
-
*/
|
|
46
|
-
async createTranscript(params) {
|
|
47
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript`, {
|
|
48
|
-
method: "POST",
|
|
49
|
-
headers: {
|
|
50
|
-
authorization: this.apiKey,
|
|
51
|
-
"Content-Type": "application/json",
|
|
52
|
-
},
|
|
53
|
-
body: JSON.stringify(params),
|
|
54
|
-
});
|
|
55
|
-
const transcript = (await response.json());
|
|
56
|
-
AssemblyAIClient.throwIfError(transcript);
|
|
57
|
-
return transcript;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Gets the transcript by its ID.
|
|
61
|
-
* @param id The ID of the transcript to retrieve.
|
|
62
|
-
* @returns Transcript object
|
|
63
|
-
*/
|
|
64
|
-
async getTranscript(id) {
|
|
65
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript/${id}`, {
|
|
66
|
-
headers: {
|
|
67
|
-
authorization: this.apiKey,
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
const transcript = (await response.json());
|
|
71
|
-
AssemblyAIClient.throwIfError(transcript);
|
|
72
|
-
return transcript;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Polls the transcript status until it is completed, then returns the completed transcript object.
|
|
76
|
-
* @param id The ID of the transcript to retrieve.
|
|
77
|
-
* @returns Transcript object
|
|
78
|
-
*/
|
|
79
|
-
async waitForTranscriptToComplete(id) {
|
|
80
|
-
const pollingEndpoint = `${AssemblyAIClient.baseUrl}/transcript/${id}`;
|
|
81
|
-
// infinite loop, same as while(true) but linter doesn't like using true constant
|
|
82
|
-
for (;;) {
|
|
83
|
-
const pollingResponse = await fetch(pollingEndpoint, {
|
|
84
|
-
headers: {
|
|
85
|
-
authorization: this.apiKey,
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
const transcript = (await pollingResponse.json());
|
|
89
|
-
AssemblyAIClient.throwIfError(transcript);
|
|
90
|
-
switch (transcript.status) {
|
|
91
|
-
case "queued":
|
|
92
|
-
case "processing":
|
|
93
|
-
await new Promise((resolve) => {
|
|
94
|
-
setTimeout(resolve, 3000);
|
|
95
|
-
});
|
|
96
|
-
break;
|
|
97
|
-
case "completed":
|
|
98
|
-
return transcript;
|
|
99
|
-
default:
|
|
100
|
-
throw new Error(`Unexpected transcript status: ${transcript.status}`);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Gets the paragraphs of the transcript in the specified format.
|
|
106
|
-
* @param id The ID of the transcript to retrieve paragraphs fors.
|
|
107
|
-
* @returns Paragraphs for the transcript.
|
|
108
|
-
*/
|
|
109
|
-
async getParagraphs(id) {
|
|
110
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript/${id}/paragraphs`, {
|
|
111
|
-
headers: {
|
|
112
|
-
authorization: this.apiKey,
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
const paragraphs = (await response.json());
|
|
116
|
-
AssemblyAIClient.throwIfError(paragraphs);
|
|
117
|
-
return paragraphs;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Gets the paragraphs of the transcript in the specified format.
|
|
121
|
-
* @param id The ID of the transcript to retrieve paragraphs fors.
|
|
122
|
-
* @returns Paragraphs for the transcript.
|
|
123
|
-
*/
|
|
124
|
-
async getSentences(id) {
|
|
125
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript/${id}/sentences`, {
|
|
126
|
-
headers: {
|
|
127
|
-
authorization: this.apiKey,
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
const sentences = (await response.json());
|
|
131
|
-
AssemblyAIClient.throwIfError(sentences);
|
|
132
|
-
return sentences;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Gets the subtitles of the transcript in the specified format.
|
|
136
|
-
* @param id The ID of the transcript to retrieve.
|
|
137
|
-
* @param subtitleFormat Format of the subtitles to retrieve.
|
|
138
|
-
* @returns Subtitles in the specified format as text.
|
|
139
|
-
*/
|
|
140
|
-
async getSubtitles(id, subtitleFormat = assemblyai_types_js_1.SubtitleFormat.Srt) {
|
|
141
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript/${id}/${subtitleFormat}`, {
|
|
142
|
-
headers: {
|
|
143
|
-
authorization: this.apiKey,
|
|
144
|
-
},
|
|
145
|
-
});
|
|
146
|
-
if (response.status !== 200) {
|
|
147
|
-
if (response.headers.get("content-type")?.startsWith("application/json")) {
|
|
148
|
-
const errorBody = (await response.json());
|
|
149
|
-
AssemblyAIClient.throwIfError(errorBody);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
throw new Error(`Get Subtitle request returned status ${response.status} ${response.statusText}`);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
const subtitles = await response.text();
|
|
156
|
-
return subtitles;
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Throws an error if the body is an error object.
|
|
160
|
-
* @param body The response object returned by the AssemblyAI API to check.
|
|
161
|
-
*/
|
|
162
|
-
static throwIfError(body) {
|
|
163
|
-
if ("error" in body)
|
|
164
|
-
throw new Error(body.error);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
Object.defineProperty(AssemblyAIClient, "baseUrl", {
|
|
168
|
-
enumerable: true,
|
|
169
|
-
configurable: true,
|
|
170
|
-
writable: true,
|
|
171
|
-
value: "https://api.assemblyai.com/v2"
|
|
172
|
-
});
|
|
173
|
-
exports.AssemblyAIClient = AssemblyAIClient;
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
import { CreateTranscriptParams, ParagraphsResponse, SentencesResponse, SubtitleFormat, Transcript } from "../types/assemblyai-types.js";
|
|
3
|
-
/**
|
|
4
|
-
* A client for the AssemblyAI API.
|
|
5
|
-
*/
|
|
6
|
-
export declare class AssemblyAIClient {
|
|
7
|
-
private readonly apiKey;
|
|
8
|
-
private static readonly baseUrl;
|
|
9
|
-
/**
|
|
10
|
-
* @param apiKey The API key for the AssemblyAI API.
|
|
11
|
-
*/
|
|
12
|
-
constructor(apiKey: string);
|
|
13
|
-
/**
|
|
14
|
-
* Uploads a file to AssemblyAI CDN.
|
|
15
|
-
* The file will only be accessible to AssemblyAI and be removed after a period of time.
|
|
16
|
-
* @param file Audio or video file to upload.
|
|
17
|
-
* @returns The URL of the uploaded file.
|
|
18
|
-
*/
|
|
19
|
-
uploadFile(file: Buffer): Promise<string>;
|
|
20
|
-
/**
|
|
21
|
-
* Creates a transcript in the AssemblyAI API. The transcript will be queued for processing,
|
|
22
|
-
* but an empty transcript object is returned immediately.
|
|
23
|
-
* @param fileUrl The URL of the audio or video file to transcribe.
|
|
24
|
-
* @returns Empty transcript object
|
|
25
|
-
*/
|
|
26
|
-
createTranscript(params: CreateTranscriptParams): Promise<Transcript>;
|
|
27
|
-
/**
|
|
28
|
-
* Gets the transcript by its ID.
|
|
29
|
-
* @param id The ID of the transcript to retrieve.
|
|
30
|
-
* @returns Transcript object
|
|
31
|
-
*/
|
|
32
|
-
getTranscript(id: string): Promise<Transcript>;
|
|
33
|
-
/**
|
|
34
|
-
* Polls the transcript status until it is completed, then returns the completed transcript object.
|
|
35
|
-
* @param id The ID of the transcript to retrieve.
|
|
36
|
-
* @returns Transcript object
|
|
37
|
-
*/
|
|
38
|
-
waitForTranscriptToComplete(id: string): Promise<Transcript>;
|
|
39
|
-
/**
|
|
40
|
-
* Gets the paragraphs of the transcript in the specified format.
|
|
41
|
-
* @param id The ID of the transcript to retrieve paragraphs fors.
|
|
42
|
-
* @returns Paragraphs for the transcript.
|
|
43
|
-
*/
|
|
44
|
-
getParagraphs(id: string): Promise<ParagraphsResponse>;
|
|
45
|
-
/**
|
|
46
|
-
* Gets the paragraphs of the transcript in the specified format.
|
|
47
|
-
* @param id The ID of the transcript to retrieve paragraphs fors.
|
|
48
|
-
* @returns Paragraphs for the transcript.
|
|
49
|
-
*/
|
|
50
|
-
getSentences(id: string): Promise<SentencesResponse>;
|
|
51
|
-
/**
|
|
52
|
-
* Gets the subtitles of the transcript in the specified format.
|
|
53
|
-
* @param id The ID of the transcript to retrieve.
|
|
54
|
-
* @param subtitleFormat Format of the subtitles to retrieve.
|
|
55
|
-
* @returns Subtitles in the specified format as text.
|
|
56
|
-
*/
|
|
57
|
-
getSubtitles(id: string, subtitleFormat?: (typeof SubtitleFormat)[keyof typeof SubtitleFormat]): Promise<string>;
|
|
58
|
-
/**
|
|
59
|
-
* Throws an error if the body is an error object.
|
|
60
|
-
* @param body The response object returned by the AssemblyAI API to check.
|
|
61
|
-
*/
|
|
62
|
-
private static throwIfError;
|
|
63
|
-
}
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { SubtitleFormat, } from "../types/assemblyai-types.js";
|
|
2
|
-
/**
|
|
3
|
-
* A client for the AssemblyAI API.
|
|
4
|
-
*/
|
|
5
|
-
class AssemblyAIClient {
|
|
6
|
-
/**
|
|
7
|
-
* @param apiKey The API key for the AssemblyAI API.
|
|
8
|
-
*/
|
|
9
|
-
constructor(apiKey) {
|
|
10
|
-
Object.defineProperty(this, "apiKey", {
|
|
11
|
-
enumerable: true,
|
|
12
|
-
configurable: true,
|
|
13
|
-
writable: true,
|
|
14
|
-
value: apiKey
|
|
15
|
-
});
|
|
16
|
-
if (!apiKey)
|
|
17
|
-
throw new Error("No AssemblyAI API key provided");
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Uploads a file to AssemblyAI CDN.
|
|
21
|
-
* The file will only be accessible to AssemblyAI and be removed after a period of time.
|
|
22
|
-
* @param file Audio or video file to upload.
|
|
23
|
-
* @returns The URL of the uploaded file.
|
|
24
|
-
*/
|
|
25
|
-
async uploadFile(file) {
|
|
26
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/upload`, {
|
|
27
|
-
method: "POST",
|
|
28
|
-
headers: {
|
|
29
|
-
authorization: this.apiKey,
|
|
30
|
-
},
|
|
31
|
-
body: file,
|
|
32
|
-
});
|
|
33
|
-
const json = (await response.json());
|
|
34
|
-
AssemblyAIClient.throwIfError(json);
|
|
35
|
-
return json.upload_url;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Creates a transcript in the AssemblyAI API. The transcript will be queued for processing,
|
|
39
|
-
* but an empty transcript object is returned immediately.
|
|
40
|
-
* @param fileUrl The URL of the audio or video file to transcribe.
|
|
41
|
-
* @returns Empty transcript object
|
|
42
|
-
*/
|
|
43
|
-
async createTranscript(params) {
|
|
44
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript`, {
|
|
45
|
-
method: "POST",
|
|
46
|
-
headers: {
|
|
47
|
-
authorization: this.apiKey,
|
|
48
|
-
"Content-Type": "application/json",
|
|
49
|
-
},
|
|
50
|
-
body: JSON.stringify(params),
|
|
51
|
-
});
|
|
52
|
-
const transcript = (await response.json());
|
|
53
|
-
AssemblyAIClient.throwIfError(transcript);
|
|
54
|
-
return transcript;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Gets the transcript by its ID.
|
|
58
|
-
* @param id The ID of the transcript to retrieve.
|
|
59
|
-
* @returns Transcript object
|
|
60
|
-
*/
|
|
61
|
-
async getTranscript(id) {
|
|
62
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript/${id}`, {
|
|
63
|
-
headers: {
|
|
64
|
-
authorization: this.apiKey,
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
const transcript = (await response.json());
|
|
68
|
-
AssemblyAIClient.throwIfError(transcript);
|
|
69
|
-
return transcript;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Polls the transcript status until it is completed, then returns the completed transcript object.
|
|
73
|
-
* @param id The ID of the transcript to retrieve.
|
|
74
|
-
* @returns Transcript object
|
|
75
|
-
*/
|
|
76
|
-
async waitForTranscriptToComplete(id) {
|
|
77
|
-
const pollingEndpoint = `${AssemblyAIClient.baseUrl}/transcript/${id}`;
|
|
78
|
-
// infinite loop, same as while(true) but linter doesn't like using true constant
|
|
79
|
-
for (;;) {
|
|
80
|
-
const pollingResponse = await fetch(pollingEndpoint, {
|
|
81
|
-
headers: {
|
|
82
|
-
authorization: this.apiKey,
|
|
83
|
-
},
|
|
84
|
-
});
|
|
85
|
-
const transcript = (await pollingResponse.json());
|
|
86
|
-
AssemblyAIClient.throwIfError(transcript);
|
|
87
|
-
switch (transcript.status) {
|
|
88
|
-
case "queued":
|
|
89
|
-
case "processing":
|
|
90
|
-
await new Promise((resolve) => {
|
|
91
|
-
setTimeout(resolve, 3000);
|
|
92
|
-
});
|
|
93
|
-
break;
|
|
94
|
-
case "completed":
|
|
95
|
-
return transcript;
|
|
96
|
-
default:
|
|
97
|
-
throw new Error(`Unexpected transcript status: ${transcript.status}`);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Gets the paragraphs of the transcript in the specified format.
|
|
103
|
-
* @param id The ID of the transcript to retrieve paragraphs fors.
|
|
104
|
-
* @returns Paragraphs for the transcript.
|
|
105
|
-
*/
|
|
106
|
-
async getParagraphs(id) {
|
|
107
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript/${id}/paragraphs`, {
|
|
108
|
-
headers: {
|
|
109
|
-
authorization: this.apiKey,
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
const paragraphs = (await response.json());
|
|
113
|
-
AssemblyAIClient.throwIfError(paragraphs);
|
|
114
|
-
return paragraphs;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Gets the paragraphs of the transcript in the specified format.
|
|
118
|
-
* @param id The ID of the transcript to retrieve paragraphs fors.
|
|
119
|
-
* @returns Paragraphs for the transcript.
|
|
120
|
-
*/
|
|
121
|
-
async getSentences(id) {
|
|
122
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript/${id}/sentences`, {
|
|
123
|
-
headers: {
|
|
124
|
-
authorization: this.apiKey,
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
const sentences = (await response.json());
|
|
128
|
-
AssemblyAIClient.throwIfError(sentences);
|
|
129
|
-
return sentences;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Gets the subtitles of the transcript in the specified format.
|
|
133
|
-
* @param id The ID of the transcript to retrieve.
|
|
134
|
-
* @param subtitleFormat Format of the subtitles to retrieve.
|
|
135
|
-
* @returns Subtitles in the specified format as text.
|
|
136
|
-
*/
|
|
137
|
-
async getSubtitles(id, subtitleFormat = SubtitleFormat.Srt) {
|
|
138
|
-
const response = await fetch(`${AssemblyAIClient.baseUrl}/transcript/${id}/${subtitleFormat}`, {
|
|
139
|
-
headers: {
|
|
140
|
-
authorization: this.apiKey,
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
if (response.status !== 200) {
|
|
144
|
-
if (response.headers.get("content-type")?.startsWith("application/json")) {
|
|
145
|
-
const errorBody = (await response.json());
|
|
146
|
-
AssemblyAIClient.throwIfError(errorBody);
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
throw new Error(`Get Subtitle request returned status ${response.status} ${response.statusText}`);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
const subtitles = await response.text();
|
|
153
|
-
return subtitles;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Throws an error if the body is an error object.
|
|
157
|
-
* @param body The response object returned by the AssemblyAI API to check.
|
|
158
|
-
*/
|
|
159
|
-
static throwIfError(body) {
|
|
160
|
-
if ("error" in body)
|
|
161
|
-
throw new Error(body.error);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
Object.defineProperty(AssemblyAIClient, "baseUrl", {
|
|
165
|
-
enumerable: true,
|
|
166
|
-
configurable: true,
|
|
167
|
-
writable: true,
|
|
168
|
-
value: "https://api.assemblyai.com/v2"
|
|
169
|
-
});
|
|
170
|
-
export { AssemblyAIClient };
|