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.
Files changed (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +192 -83
  3. package/dist/index.d.ts +4 -0
  4. package/dist/index.esm.js +473 -0
  5. package/dist/index.js +482 -0
  6. package/dist/services/base.d.ts +13 -0
  7. package/dist/services/files/index.d.ts +9 -0
  8. package/dist/services/index.d.ts +29 -0
  9. package/dist/services/lemur/index.d.ts +8 -0
  10. package/dist/services/realtime/factory.d.ts +10 -0
  11. package/dist/services/realtime/index.d.ts +2 -0
  12. package/dist/services/realtime/service.d.ts +22 -0
  13. package/dist/services/transcripts/index.d.ts +59 -0
  14. package/dist/types/asyncapi.generated.d.ts +87 -0
  15. package/dist/types/index.d.ts +5 -0
  16. package/dist/types/openapi.generated.d.ts +685 -0
  17. package/dist/types/realtime/index.d.ts +36 -0
  18. package/dist/types/services/abstractions.d.ts +52 -0
  19. package/dist/types/services/index.d.ts +6 -0
  20. package/dist/types/transcripts/index.d.ts +5 -0
  21. package/dist/utils/axios.d.ts +3 -0
  22. package/dist/utils/errors/index.d.ts +1 -0
  23. package/dist/utils/errors/realtime.d.ts +23 -0
  24. package/package.json +58 -21
  25. package/src/index.ts +5 -0
  26. package/src/services/base.ts +14 -0
  27. package/src/services/files/index.ts +22 -0
  28. package/src/services/index.ts +49 -0
  29. package/src/services/lemur/index.ts +49 -0
  30. package/src/services/realtime/factory.ts +32 -0
  31. package/src/services/realtime/index.ts +2 -0
  32. package/src/services/realtime/service.ts +184 -0
  33. package/src/services/transcripts/index.ts +178 -0
  34. package/src/types/asyncapi.generated.ts +124 -0
  35. package/src/types/index.ts +5 -0
  36. package/src/types/openapi.generated.ts +834 -0
  37. package/src/types/realtime/index.ts +68 -0
  38. package/src/types/services/abstractions.ts +56 -0
  39. package/src/types/services/index.ts +7 -0
  40. package/src/types/transcripts/index.ts +5 -0
  41. package/src/utils/.gitkeep +0 -0
  42. package/src/utils/axios.ts +19 -0
  43. package/src/utils/errors/index.ts +5 -0
  44. package/src/utils/errors/realtime.ts +45 -0
  45. package/.eslintrc.json +0 -3
  46. package/index.js +0 -15
  47. package/src/Client.js +0 -28
  48. package/src/api/Http/Request.js +0 -108
  49. package/src/api/Http/Response.js +0 -23
  50. package/src/api/Model.js +0 -17
  51. package/src/api/Transcript.js +0 -16
  52. package/src/api/Upload.js +0 -41
  53. 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
- ## Installing the module:
1
+ <img src="https://github.com/AssemblyAI/assemblyai-typescript-sdk/blob/master/assemblyai.png?raw=true" width="500"/>
2
2
 
3
- `npm i assemblyai`
3
+ ---
4
4
 
5
- ## Authenticating with the API
5
+ # AssemblyAI Node.js SDK
6
6
 
7
- ### Using Environment Variables
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
- If you have the `ASSEMBLYAI_API_KEY` environment variable set, then the application
10
- will attempt to read it and use this value to authenticate with the API.
10
+ ## Installation
11
11
 
12
- ### Setting the value manually
12
+ You can install the AssemblyAI SDK by running:
13
13
 
14
- Here is what the code would look like if you were to set the API token manually.
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
- const assemblyai = require('assemblyai')
18
- assemblyai.setAPIKey("ENTER YOUR KEY HERE")
35
+ import AssemblyAI from "assemblyai";
36
+
37
+ const client = new AssemblyAI({
38
+ apiKey: process.env.ASSEMBLYAI_API_KEY,
39
+ })
19
40
  ```
20
41
 
21
- ## Usage
42
+ You can now use the `client` object to interact with the AssemblyAI API.
22
43
 
23
- ### Initialization
44
+ ## Create a transcript
24
45
 
25
- The initialization of the module of course has to be at the beginning of your project.
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
- const assemblyai = require('assemblyai')
29
- assemblyai.setAPIKey("ENTER YOUR KEY HERE")
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
- ### Upload an audio file for transcription
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
- async function upload () {
36
- try {
37
- const transcript = new assemblyai.Upload('/path/to/audiofile.wav')
38
- const response = await transcript.create()
39
- const data = response.get()
40
-
41
- // do something with the JSON response
42
- console.log(data);
43
-
44
- } catch (e) {
45
- // Do some error handling here
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
- ### Transcribe audio from a URL
79
+ ## Get a transcript
51
80
 
52
- The only required parameter is the `audio_src_url` parameter. For more information about transcribing audio, please see the full API documentation [here](https://docs.assemblyai.com/api/#posttranscript).
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
- async function transcribe () {
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
- ### Create a custom model
87
+ ## List transcripts
82
88
 
83
- Boost accuracy for keywords/phrases, and add custom terms to the vocabulary with a custom model. For more information, please see the full API documentation [here](https://docs.assemblyai.com/guides/custom_models_101/).
89
+ This will return a paged list of transcripts that you have transcript.
84
90
 
85
91
  ```javascript
86
- async function model() {
87
- try {
88
- const instance = new assemblyai.Model()
89
- const response = await instance.create({
90
- phrases: ['foo', 'bar']
91
- })
92
- const { id } = response.get()
93
- const data = await instance.poll(id)
94
-
95
- // do something with the response data.
96
- // `data` is a wrapper of the API's JSON
97
- // response. `data.get()` returns the JSON
98
- // response of the API
99
- var responseJson = data.get();
100
- console.log(responseJson);
101
-
102
- } catch (e) {
103
- // Do some error handling
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
- When using the `Response` object, you will find a couple of methods:
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
- - `get()`
113
- - `toString()`
114
- - `stringify()`
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
- The method that you will most likely want to use will be `get()` which returns the full JSON object from the API, one level down.
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
+ ```
@@ -0,0 +1,4 @@
1
+ import AssemblyAI from "./services";
2
+ export * from "./services";
3
+ export type * from "./types";
4
+ export default AssemblyAI;