langchain 0.0.169 → 0.0.170

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 (69) 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/dist/agents/agent.cjs +49 -1
  6. package/dist/agents/agent.d.ts +19 -1
  7. package/dist/agents/agent.js +47 -0
  8. package/dist/agents/executor.cjs +10 -1
  9. package/dist/agents/executor.d.ts +22 -8
  10. package/dist/agents/executor.js +11 -2
  11. package/dist/agents/format_scratchpad.cjs +25 -0
  12. package/dist/agents/format_scratchpad.d.ts +10 -0
  13. package/dist/agents/format_scratchpad.js +21 -0
  14. package/dist/agents/toolkits/aws_sfn.d.ts +4 -1
  15. package/dist/agents/toolkits/conversational_retrieval/openai_functions.d.ts +1 -1
  16. package/dist/agents/toolkits/json/json.d.ts +4 -1
  17. package/dist/agents/toolkits/openapi/openapi.cjs +8 -0
  18. package/dist/agents/toolkits/openapi/openapi.d.ts +12 -1
  19. package/dist/agents/toolkits/openapi/openapi.js +8 -0
  20. package/dist/agents/toolkits/sql/sql.d.ts +4 -1
  21. package/dist/agents/toolkits/vectorstore/vectorstore.d.ts +8 -2
  22. package/dist/agents/types.d.ts +13 -1
  23. package/dist/chains/sql_db/sql_db_chain.cjs +9 -0
  24. package/dist/chains/sql_db/sql_db_chain.d.ts +9 -0
  25. package/dist/chains/sql_db/sql_db_chain.js +9 -0
  26. package/dist/chat_models/baiduwenxin.cjs +12 -1
  27. package/dist/chat_models/baiduwenxin.d.ts +3 -1
  28. package/dist/chat_models/baiduwenxin.js +12 -1
  29. package/dist/document_loaders/web/assemblyai.cjs +63 -114
  30. package/dist/document_loaders/web/assemblyai.d.ts +38 -57
  31. package/dist/document_loaders/web/assemblyai.js +63 -100
  32. package/dist/load/import_constants.cjs +1 -0
  33. package/dist/load/import_constants.js +1 -0
  34. package/dist/load/import_map.cjs +3 -2
  35. package/dist/load/import_map.d.ts +1 -0
  36. package/dist/load/import_map.js +1 -0
  37. package/dist/memory/index.d.ts +1 -1
  38. package/dist/memory/index.js +1 -1
  39. package/dist/retrievers/time_weighted.cjs +1 -1
  40. package/dist/retrievers/time_weighted.d.ts +1 -1
  41. package/dist/retrievers/time_weighted.js +1 -1
  42. package/dist/schema/runnable/base.cjs +4 -1
  43. package/dist/schema/runnable/base.d.ts +1 -0
  44. package/dist/schema/runnable/base.js +4 -1
  45. package/dist/schema/runnable/passthrough.cjs +33 -1
  46. package/dist/schema/runnable/passthrough.d.ts +11 -1
  47. package/dist/schema/runnable/passthrough.js +32 -1
  48. package/dist/sql_db.cjs +12 -0
  49. package/dist/sql_db.d.ts +12 -0
  50. package/dist/sql_db.js +12 -0
  51. package/dist/storage/ioredis.cjs +2 -1
  52. package/dist/storage/ioredis.js +2 -1
  53. package/dist/storage/upstash_redis.cjs +155 -0
  54. package/dist/storage/upstash_redis.d.ts +59 -0
  55. package/dist/storage/upstash_redis.js +151 -0
  56. package/dist/storage/vercel_kv.cjs +2 -1
  57. package/dist/storage/vercel_kv.js +2 -1
  58. package/dist/types/assemblyai-types.cjs +0 -150
  59. package/dist/types/assemblyai-types.d.ts +4 -670
  60. package/dist/types/assemblyai-types.js +1 -149
  61. package/dist/vectorstores/pgvector.cjs +1 -1
  62. package/dist/vectorstores/pgvector.js +1 -1
  63. package/package.json +23 -2
  64. package/storage/upstash_redis.cjs +1 -0
  65. package/storage/upstash_redis.d.ts +1 -0
  66. package/storage/upstash_redis.js +1 -0
  67. package/dist/util/assemblyai-client.cjs +0 -173
  68. package/dist/util/assemblyai-client.d.ts +0 -63
  69. package/dist/util/assemblyai-client.js +0 -170
@@ -1,149 +1 @@
1
- /**
2
- * The list of PII Redaction policies to enable.
3
- * See [PII redaction]{@link https://www.assemblyai.com/docs/Models/pii_redaction} for more details.
4
- */
5
- export const PiiPolicy = {
6
- /**
7
- * Medical process, including treatments, procedures, and tests (e.g., heart surgery, CT scan)
8
- */
9
- MedicalProcess: "medical_process",
10
- /**
11
- * Name of a medical condition, disease, syndrome, deficit, or disorder (e.g., chronic fatigue syndrome, arrhythmia, depression)
12
- */
13
- MedicalCondition: "medical_condition",
14
- /**
15
- * Blood type (e.g., O-, AB positive)
16
- */
17
- BloodType: "blood_type",
18
- /**
19
- * Medications, vitamins, or supplements (e.g., Advil, Acetaminophen, Panadol)
20
- */
21
- Drug: "drug",
22
- /**
23
- * Bodily injury (e.g., I broke my arm, I have a sprained wrist)
24
- */
25
- Injury: "injury",
26
- /**
27
- * A "lazy" rule that will redact any sequence of numbers equal to or greater than 2
28
- */
29
- NumberSequence: "number_sequence",
30
- /**
31
- * Email address (e.g., support@assemblyai.com)
32
- */
33
- EmailAddress: "email_address",
34
- /**
35
- * Date of Birth (e.g., Date of Birth: March 7,1961)
36
- */
37
- DateOfBirth: "date_of_birth",
38
- /**
39
- * Telephone or fax number
40
- */
41
- PhoneNumber: "phone_number",
42
- /**
43
- * Social Security Number or equivalent
44
- */
45
- UsSocialSecurityNumber: "us_social_security_number",
46
- /**
47
- * Credit card number
48
- */
49
- CreditCardNumber: "credit_card_number",
50
- /**
51
- * Expiration date of a credit card
52
- */
53
- CreditCardExpiration: "credit_card_expiration",
54
- /**
55
- * Credit card verification code (e.g., CVV: 080)
56
- */
57
- CreditCardCvv: "credit_card_cvv",
58
- /**
59
- * Specific calendar date (e.g., December 18)
60
- */
61
- Date: "date",
62
- /**
63
- * Terms indicating nationality, ethnicity, or race (e.g., American, Asian, Caucasian)
64
- */
65
- Nationality: "nationality",
66
- /**
67
- * Name of an event or holiday (e.g., Olympics, Yom Kippur)
68
- */
69
- Event: "event",
70
- /**
71
- * Name of a natural language (e.g., Spanish, French)
72
- */
73
- Language: "language",
74
- /**
75
- * Any Location reference including mailing address, postal code, city, state, province, or country
76
- */
77
- Location: "location",
78
- /**
79
- * Name and/or amount of currency (e.g., 15 pesos, $94.50)
80
- */
81
- MoneyAmount: "money_amount",
82
- /**
83
- * Name of a person (e.g., Bob, Doug Jones)
84
- */
85
- PersonName: "person_name",
86
- /**
87
- * Number associated with an age (e.g., 27, 75)
88
- */
89
- PersonAge: "person_age",
90
- /**
91
- * Name of an organization (e.g., CNN, McDonalds, University of Alaska)
92
- */
93
- Organization: "organization",
94
- /**
95
- * Terms referring to a political party, movement, or ideology (e.g., Republican, Liberal)
96
- */
97
- PoliticalAffiliation: "political_affiliation",
98
- /**
99
- * Job title or profession (e.g., professor, actors, engineer, CPA)
100
- */
101
- Occupation: "occupation",
102
- /**
103
- * Terms indicating religious affiliation (e.g., Hindu, Catholic)
104
- */
105
- Religion: "religion",
106
- /**
107
- * Driver’s license number (e.g., DL# 356933-540)
108
- */
109
- DriversLicense: "drivers_license",
110
- /**
111
- * Banking information, including account and routing numbers
112
- */
113
- BankingInformation: "banking_information",
114
- };
115
- export const EntityType = {
116
- BloodType: "Blood type (e.g., O-, AB positive)",
117
- CreditCardCvv: "Credit card verification code (e.g., CVV: 080)",
118
- CreditCardExpiration: "Expiration date of a credit card",
119
- CreditCardNumber: "Credit card number",
120
- Date: "Specific calendar date (e.g., December 18)",
121
- DateOfBirth: "Date of Birth (e.g., Date of Birth: March 7, 1961)",
122
- Drug: "Medications, vitamins, or supplements (e.g., Advil, Acetaminophen, Panadol)",
123
- Event: "Name of an event or holiday (e.g., Olympics, Yom Kippur)",
124
- EmailAddress: "Email address (e.g., support@assemblyai.com)",
125
- Injury: "Bodily injury (e.g., I broke my arm, I have a sprained wrist)",
126
- Language: "Name of a natural language (e.g., Spanish, French)",
127
- Location: "Any location reference including mailing address, postal code, city, state, province, or country",
128
- MedicalCondition: "Name of a medical condition, disease, syndrome, deficit, or disorder (e.g., chronic fatigue syndrome, arrhythmia, depression)",
129
- MedicalProcess: "Medical process, including treatments, procedures, and tests (e.g., heart surgery, CT scan)",
130
- MoneyAmount: "Name and/or amount of currency (e.g., 15 pesos, $94.50)",
131
- Nationality: "Terms indicating nationality, ethnicity, or race (e.g., American, Asian, Caucasian)",
132
- Occupation: "Job title or profession (e.g., professor, actors, engineer, CPA)",
133
- Organization: "Name of an organization (e.g., CNN, McDonalds, University of Alaska)",
134
- PersonAge: "Number associated with an age (e.g., 27, 75)",
135
- PersonName: "Name of a person (e.g., Bob, Doug Jones)",
136
- PhoneNumber: "Telephone or fax number",
137
- PoliticalAffiliation: "Terms referring to a political party, movement, or ideology (e.g., Republican, Liberal)",
138
- Religion: "Terms indicating religious affiliation (e.g., Hindu, Catholic)",
139
- UsSocialSecurityNumber: "Social Security Number or equivalent",
140
- DriversLicense: "Driver's license number (e.g., DL #356933-540)",
141
- BankingInformation: "Banking information, including account and routing numbers",
142
- };
143
- /**
144
- * The format of the subtitles.
145
- */
146
- export const SubtitleFormat = {
147
- Srt: "srt",
148
- Vtt: "vtt",
149
- };
1
+ export {};
@@ -193,7 +193,7 @@ class PGVectorStore extends base_js_1.VectorStore {
193
193
  const queryString = `
194
194
  SELECT *, ${this.vectorColumnName} <=> $1 as "_distance"
195
195
  FROM ${this.tableName}
196
- WHERE ${this.metadataColumnName} @> $2
196
+ WHERE ${this.metadataColumnName}::jsonb @> $2
197
197
  ORDER BY "_distance" ASC
198
198
  LIMIT $3;`;
199
199
  const documents = (await this.pool.query(queryString, [embeddingString, _filter, k])).rows;
@@ -187,7 +187,7 @@ export class PGVectorStore extends VectorStore {
187
187
  const queryString = `
188
188
  SELECT *, ${this.vectorColumnName} <=> $1 as "_distance"
189
189
  FROM ${this.tableName}
190
- WHERE ${this.metadataColumnName} @> $2
190
+ WHERE ${this.metadataColumnName}::jsonb @> $2
191
191
  ORDER BY "_distance" ASC
192
192
  LIMIT $3;`;
193
193
  const documents = (await this.pool.query(queryString, [embeddingString, _filter, k])).rows;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.169",
3
+ "version": "0.0.170",
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",
@@ -664,6 +667,9 @@
664
667
  "storage/vercel_kv.cjs",
665
668
  "storage/vercel_kv.js",
666
669
  "storage/vercel_kv.d.ts",
670
+ "storage/upstash_redis.cjs",
671
+ "storage/upstash_redis.js",
672
+ "storage/upstash_redis.d.ts",
667
673
  "graphs/neo4j_graph.cjs",
668
674
  "graphs/neo4j_graph.js",
669
675
  "graphs/neo4j_graph.d.ts",
@@ -718,7 +724,7 @@
718
724
  ],
719
725
  "repository": {
720
726
  "type": "git",
721
- "url": "git@github.com:hwchase17/langchainjs.git"
727
+ "url": "git@github.com:langchain-ai/langchainjs.git"
722
728
  },
723
729
  "scripts": {
724
730
  "build": "yarn clean && yarn build:esm && yarn build:cjs && node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js",
@@ -808,6 +814,7 @@
808
814
  "@xenova/transformers": "^2.5.4",
809
815
  "@zilliz/milvus2-sdk-node": ">=2.2.11",
810
816
  "apify-client": "^2.7.1",
817
+ "assemblyai": "^2.0.2",
811
818
  "axios": "^0.26.0",
812
819
  "cassandra-driver": "^4.6.4",
813
820
  "cheerio": "^1.0.0-rc.12",
@@ -923,6 +930,7 @@
923
930
  "@xenova/transformers": "^2.5.4",
924
931
  "@zilliz/milvus2-sdk-node": ">=2.2.7",
925
932
  "apify-client": "^2.7.1",
933
+ "assemblyai": "^2.0.2",
926
934
  "axios": "*",
927
935
  "cassandra-driver": "^4.6.4",
928
936
  "cheerio": "^1.0.0-rc.12",
@@ -1109,6 +1117,9 @@
1109
1117
  "apify-client": {
1110
1118
  "optional": true
1111
1119
  },
1120
+ "assemblyai": {
1121
+ "optional": true
1122
+ },
1112
1123
  "axios": {
1113
1124
  "optional": true
1114
1125
  },
@@ -1334,6 +1345,11 @@
1334
1345
  "import": "./agents/toolkits/sql.js",
1335
1346
  "require": "./agents/toolkits/sql.cjs"
1336
1347
  },
1348
+ "./agents/format_scratchpad": {
1349
+ "types": "./agents/format_scratchpad.d.ts",
1350
+ "import": "./agents/format_scratchpad.js",
1351
+ "require": "./agents/format_scratchpad.cjs"
1352
+ },
1337
1353
  "./base_language": {
1338
1354
  "types": "./base_language.d.ts",
1339
1355
  "import": "./base_language.js",
@@ -2389,6 +2405,11 @@
2389
2405
  "import": "./storage/vercel_kv.js",
2390
2406
  "require": "./storage/vercel_kv.cjs"
2391
2407
  },
2408
+ "./storage/upstash_redis": {
2409
+ "types": "./storage/upstash_redis.d.ts",
2410
+ "import": "./storage/upstash_redis.js",
2411
+ "require": "./storage/upstash_redis.cjs"
2412
+ },
2392
2413
  "./graphs/neo4j_graph": {
2393
2414
  "types": "./graphs/neo4j_graph.d.ts",
2394
2415
  "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 };