assemblyai 1.0.1 → 2.0.1-beta
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/LICENSE +21 -0
- package/README.md +192 -83
- package/dist/index.d.ts +4 -0
- package/dist/index.esm.js +473 -0
- package/dist/index.js +482 -0
- package/dist/services/base.d.ts +13 -0
- package/dist/services/files/index.d.ts +9 -0
- package/dist/services/index.d.ts +29 -0
- package/dist/services/lemur/index.d.ts +8 -0
- package/dist/services/realtime/factory.d.ts +10 -0
- package/dist/services/realtime/index.d.ts +2 -0
- package/dist/services/realtime/service.d.ts +22 -0
- package/dist/services/transcripts/index.d.ts +59 -0
- package/dist/types/asyncapi.generated.d.ts +87 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/openapi.generated.d.ts +685 -0
- package/dist/types/realtime/index.d.ts +36 -0
- package/dist/types/services/abstractions.d.ts +52 -0
- package/dist/types/services/index.d.ts +6 -0
- package/dist/types/transcripts/index.d.ts +5 -0
- package/dist/utils/axios.d.ts +3 -0
- package/dist/utils/errors/index.d.ts +1 -0
- package/dist/utils/errors/realtime.d.ts +23 -0
- package/package.json +58 -21
- package/src/index.ts +5 -0
- package/src/services/base.ts +14 -0
- package/src/services/files/index.ts +22 -0
- package/src/services/index.ts +49 -0
- package/src/services/lemur/index.ts +49 -0
- package/src/services/realtime/factory.ts +32 -0
- package/src/services/realtime/index.ts +2 -0
- package/src/services/realtime/service.ts +184 -0
- package/src/services/transcripts/index.ts +178 -0
- package/src/types/asyncapi.generated.ts +124 -0
- package/src/types/index.ts +5 -0
- package/src/types/openapi.generated.ts +834 -0
- package/src/types/realtime/index.ts +68 -0
- package/src/types/services/abstractions.ts +56 -0
- package/src/types/services/index.ts +7 -0
- package/src/types/transcripts/index.ts +5 -0
- package/src/utils/.gitkeep +0 -0
- package/src/utils/axios.ts +19 -0
- package/src/utils/errors/index.ts +5 -0
- package/src/utils/errors/realtime.ts +45 -0
- package/.eslintrc.json +0 -3
- package/index.js +0 -15
- package/src/Client.js +0 -28
- package/src/api/Http/Request.js +0 -108
- package/src/api/Http/Response.js +0 -23
- package/src/api/Model.js +0 -17
- package/src/api/Transcript.js +0 -16
- package/src/api/Upload.js +0 -41
- package/src/api/util.js +0 -48
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 AssemblyAI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,116 +1,225 @@
|
|
|
1
|
-
|
|
1
|
+
<img src="https://github.com/AssemblyAI/assemblyai-typescript-sdk/blob/master/assemblyai.png?raw=true" width="500"/>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
---
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
# AssemblyAI Node.js SDK
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
The AssemblyAI Node.js SDK provides an easy-to-use interface for interacting with the AssemblyAI API,
|
|
8
|
+
which supports async and real-time transcription, as well as the latest LeMUR models.
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
will attempt to read it and use this value to authenticate with the API.
|
|
10
|
+
## Installation
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
You can install the AssemblyAI SDK by running:
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
```bash
|
|
15
|
+
npm install assemblyai
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
yarn add assemblyai
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add assemblyai
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
bun add assemblyai
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
# Usage
|
|
31
|
+
|
|
32
|
+
Import the AssemblyAI package and create an AssemblyAI object with your API key:
|
|
15
33
|
|
|
16
34
|
```javascript
|
|
17
|
-
|
|
18
|
-
|
|
35
|
+
import AssemblyAI from "assemblyai";
|
|
36
|
+
|
|
37
|
+
const client = new AssemblyAI({
|
|
38
|
+
apiKey: process.env.ASSEMBLYAI_API_KEY,
|
|
39
|
+
})
|
|
19
40
|
```
|
|
20
41
|
|
|
21
|
-
|
|
42
|
+
You can now use the `client` object to interact with the AssemblyAI API.
|
|
22
43
|
|
|
23
|
-
|
|
44
|
+
## Create a transcript
|
|
24
45
|
|
|
25
|
-
|
|
46
|
+
When you create a transcript, you can either pass in a URL to an audio file, or upload a file directly.
|
|
26
47
|
|
|
27
48
|
```javascript
|
|
28
|
-
|
|
29
|
-
|
|
49
|
+
// Using a remote URL
|
|
50
|
+
const transcript = await client.transcripts.create({
|
|
51
|
+
audio_url: 'https://storage.googleapis.com/aai-web-samples/espn-bears.m4a',
|
|
52
|
+
})
|
|
30
53
|
```
|
|
31
54
|
|
|
32
|
-
|
|
55
|
+
```javascript
|
|
56
|
+
// Uploading a file
|
|
57
|
+
const transcript = await client.transcripts.create({
|
|
58
|
+
audio_url: './news.mp4',
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
By default, when you create a transcript, it'll be polled until the status is `completed` or `error`.
|
|
63
|
+
You can configure whether to poll, the polling interval, and polling timeout using these options:
|
|
33
64
|
|
|
34
65
|
```javascript
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
}
|
|
66
|
+
const transcript = await client.transcripts.create({
|
|
67
|
+
audio_url: 'https://storage.googleapis.com/aai-web-samples/espn-bears.m4a',
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
// Enable or disable polling. Defaults to true.
|
|
71
|
+
poll: true,
|
|
72
|
+
// How frequently the transcript is polled in ms. Defaults to 3000.
|
|
73
|
+
pollingInterval: 1000,
|
|
74
|
+
// How long to wait in ms until the "Polling timeout" error is thrown. Defaults to 180000.
|
|
75
|
+
pollingTimeout: 5000,
|
|
76
|
+
})
|
|
48
77
|
```
|
|
49
78
|
|
|
50
|
-
|
|
79
|
+
## Get a transcript
|
|
51
80
|
|
|
52
|
-
|
|
81
|
+
This will return the transcript object in its current state. If the transcript is still processing, the `status` field will be `queued` or `processing`. Once the transcript is complete, the `status` field will be `completed`.
|
|
53
82
|
|
|
54
83
|
```javascript
|
|
55
|
-
|
|
56
|
-
try {
|
|
57
|
-
const transcript = new assemblyai.Transcript()
|
|
58
|
-
const response = await transcript.create({
|
|
59
|
-
audio_src_url: "https://example.com/example.wav",
|
|
60
|
-
model_id: 123,
|
|
61
|
-
options: {
|
|
62
|
-
format_text: true || false
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
const { id } = response.get()
|
|
66
|
-
const data = await transcript.poll(id)
|
|
67
|
-
|
|
68
|
-
// do something with the response data.
|
|
69
|
-
// `data` is a wrapper of the API's JSON
|
|
70
|
-
// response. `data.get()` returns the JSON
|
|
71
|
-
// response of the API
|
|
72
|
-
var responseJson = data.get();
|
|
73
|
-
console.log(responseJson);
|
|
74
|
-
|
|
75
|
-
} catch (e) {
|
|
76
|
-
// Do some error handling here
|
|
77
|
-
}
|
|
78
|
-
}
|
|
84
|
+
const transcript = await client.transcripts.get(transcript.id)
|
|
79
85
|
```
|
|
80
86
|
|
|
81
|
-
|
|
87
|
+
## List transcripts
|
|
82
88
|
|
|
83
|
-
|
|
89
|
+
This will return a paged list of transcripts that you have transcript.
|
|
84
90
|
|
|
85
91
|
```javascript
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
92
|
+
const page = await client.transcripts.list()
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
You can also paginate over all pages.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
let nextPageUrl: string | null = null;
|
|
99
|
+
do {
|
|
100
|
+
const page = await client.transcripts.list(nextPageUrl)
|
|
101
|
+
nextPageUrl = page.page_details.next_url
|
|
102
|
+
} while(nextPageUrl !== null)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Delete a transcript
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
const res = await client.transcripts.delete(transcript.id)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Use LeMUR
|
|
112
|
+
|
|
113
|
+
Call [LeMUR endpoints](https://www.assemblyai.com/docs/API%20reference/lemur) to summarize, ask questions, generate action items, or run a custom task.
|
|
114
|
+
|
|
115
|
+
Custom Summary:
|
|
116
|
+
```javascript
|
|
117
|
+
const { response } = await client.lemur.summary({
|
|
118
|
+
transcript_ids: ['0d295578-8c75-421a-885a-2c487f188927'],
|
|
119
|
+
answer_format: 'one sentence',
|
|
120
|
+
context: {
|
|
121
|
+
speakers: ['Alex', 'Bob'],
|
|
104
122
|
}
|
|
105
|
-
}
|
|
123
|
+
})
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Question & Answer:
|
|
127
|
+
```javascript
|
|
128
|
+
const { response } = await client.lemur.questionAnswer({
|
|
129
|
+
transcript_ids: ['0d295578-8c75-421a-885a-2c487f188927'],
|
|
130
|
+
questions: [
|
|
131
|
+
{
|
|
132
|
+
question: 'What are they discussing?',
|
|
133
|
+
answer_format: 'text',
|
|
134
|
+
}
|
|
135
|
+
]
|
|
136
|
+
})
|
|
106
137
|
```
|
|
107
|
-
|
|
108
|
-
### The Response Object
|
|
109
138
|
|
|
110
|
-
|
|
139
|
+
Action Items:
|
|
140
|
+
```javascript
|
|
141
|
+
const { response } = await client.lemur.actionItems({
|
|
142
|
+
transcript_ids: ['0d295578-8c75-421a-885a-2c487f188927'],
|
|
143
|
+
})
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Custom Task:
|
|
147
|
+
```javascript
|
|
148
|
+
const { response } = await client.lemur.task({
|
|
149
|
+
transcript_ids: ['0d295578-8c75-421a-885a-2c487f188927'],
|
|
150
|
+
prompt: 'Write a haiku about this conversation.',
|
|
151
|
+
})
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Transcribe in real time
|
|
155
|
+
|
|
156
|
+
Create the real-time service.
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const service = client.realtime.createService();
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
You can also pass in the following options.
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
const service = client.realtime.createService({
|
|
166
|
+
realtimeUrl: 'wss://localhost/override',
|
|
167
|
+
apiKey: process.env.ASSEMBLYAI_API_KEY // The API key passed to `AssemblyAI` will be used by default,
|
|
168
|
+
sampleRate: 16_000,
|
|
169
|
+
wordBoost: ['foo', 'bar']
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
You can also generate a temporary auth token for real-time.
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
const token = await client.realtime.createTemporaryToken({expires_in = 60});
|
|
177
|
+
const rt = client.realtime.createService({
|
|
178
|
+
token: token
|
|
179
|
+
});
|
|
180
|
+
```
|
|
111
181
|
|
|
112
|
-
|
|
113
|
-
-
|
|
114
|
-
|
|
182
|
+
> [!WARNING]
|
|
183
|
+
> Storing your API key in client-facing applications exposes your API key.
|
|
184
|
+
> Generate a temporary auth token on the server and pass it to your client.
|
|
115
185
|
|
|
116
|
-
|
|
186
|
+
You can configure the following events.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
rt.on("open", ({ sessionId, expiresAt }) => console.log('Session ID:', sessionId, 'Expires at:', expiresAt));
|
|
190
|
+
rt.on("close", (code: number, reason: string) => console.log('Closed', code, reason));
|
|
191
|
+
rt.on("transcript", (transcript: TranscriptMessage) => console.log('Transcript:', transcript));
|
|
192
|
+
rt.on("transcript.partial", (transcript: PartialTranscriptMessage) => console.log('Partial transcript:', transcript));
|
|
193
|
+
rt.on("transcript.final", (transcript: FinalTranscriptMessage) => console.log('Final transcript:', transcript));
|
|
194
|
+
rt.on("error", (error: Error) => console.error('Error', error));
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
After configuring your events, connect to the server.
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
await rt.connect();
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Send audio data.
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
// Pseudo code for getting audio
|
|
207
|
+
getAudio((chunk) => {
|
|
208
|
+
rt.sendAudio(chunk);
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Close the connection when you're finished.
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
rt.close();
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
# Tests
|
|
219
|
+
|
|
220
|
+
To run the test suite, first install the dependencies, then run `pnpm test`:
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
pnpm install
|
|
224
|
+
pnpm test
|
|
225
|
+
```
|
package/dist/index.d.ts
ADDED