This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime.go
558 lines (446 loc) · 13.9 KB
/
realtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package assemblyai
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"sync"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
)
var (
// ErrSessionClosed is returned when attempting to write to a closed
// session.
ErrSessionClosed = errors.New("session closed")
// ErrDisconnected is returned when attempting to write to a disconnected
// client.
ErrDisconnected = errors.New("client is disconnected")
// ErrConnectionNotFound is returned when attempting to disconnect a
// nil connection
ErrConnectionNotFound = errors.New("client connection does not exist")
)
type MessageType string
const (
MessageTypeSessionBegins MessageType = "SessionBegins"
MessageTypeSessionTerminated MessageType = "SessionTerminated"
MessageTypePartialTranscript MessageType = "PartialTranscript"
MessageTypeFinalTranscript MessageType = "FinalTranscript"
MessageTypeSessionInformation MessageType = "SessionInformation"
)
type AudioData struct {
// Base64 encoded raw audio data
AudioData string `json:"audio_data,omitempty"`
}
type TerminateSession struct {
// Set to true to end your real-time session forever
TerminateSession bool `json:"terminate_session"`
}
type endUtteranceSilenceThreshold struct {
// Set to true to configure the silence threshold for ending utterances.
EndUtteranceSilenceThreshold int64 `json:"end_utterance_silence_threshold"`
}
type forceEndUtterance struct {
// Set to true to manually end the current utterance.
ForceEndUtterance bool `json:"force_end_utterance"`
}
type RealTimeBaseMessage struct {
// Describes the type of the message
MessageType MessageType `json:"message_type"`
}
type RealTimeBaseTranscript struct {
// End time of audio sample relative to session start, in milliseconds
AudioEnd int64 `json:"audio_end"`
// Start time of audio sample relative to session start, in milliseconds
AudioStart int64 `json:"audio_start"`
// The confidence score of the entire transcription, between 0 and 1
Confidence float64 `json:"confidence"`
// The timestamp for the partial transcript
Created string `json:"created"`
// The partial transcript for your audio
Text string `json:"text"`
// An array of objects, with the information for each word in the
// transcription text. Includes the start and end time of the word in
// milliseconds, the confidence score of the word, and the text, which is
// the word itself.
Words []Word `json:"words"`
}
type FinalTranscript struct {
RealTimeBaseTranscript
// Describes the type of message
MessageType MessageType `json:"message_type"`
// Whether the text is punctuated and cased
Punctuated bool `json:"punctuated"`
// Whether the text is formatted, for example Dollar -> $
TextFormatted bool `json:"text_formatted"`
}
type PartialTranscript struct {
RealTimeBaseTranscript
// Describes the type of message
MessageType MessageType `json:"message_type"`
}
type Word struct {
// Confidence score of the word
Confidence float64 `json:"confidence"`
// End time of the word in milliseconds
End int64 `json:"end"`
// Start time of the word in milliseconds
Start int64 `json:"start"`
// The word itself
Text string `json:"text"`
}
var DefaultSampleRate = 16_000
type RealTimeClient struct {
baseURL *url.URL
apiKey string
token string
conn *websocket.Conn
httpClient *http.Client
mtx sync.RWMutex
sessionOpen bool
// done is used to clean up resources when the client disconnects.
done chan bool
transcriber *RealTimeTranscriber
sampleRate int
encoding RealTimeEncoding
wordBoost []string
}
func (c *RealTimeClient) isSessionOpen() bool {
c.mtx.RLock()
defer c.mtx.RUnlock()
return c.sessionOpen
}
func (c *RealTimeClient) setSessionOpen(open bool) {
c.mtx.RLock()
defer c.mtx.RUnlock()
c.sessionOpen = open
}
type RealTimeError struct {
Error string `json:"error"`
}
type RealTimeClientOption func(*RealTimeClient)
// WithRealTimeBaseURL sets the API endpoint used by the client. Mainly used for
// testing.
func WithRealTimeBaseURL(rawurl string) RealTimeClientOption {
return func(c *RealTimeClient) {
if u, err := url.Parse(rawurl); err == nil {
c.baseURL = u
}
}
}
// WithRealTimeAuthToken configures the client to authenticate using an
// AssemblyAI API key.
func WithRealTimeAPIKey(apiKey string) RealTimeClientOption {
return func(rtc *RealTimeClient) {
rtc.apiKey = apiKey
}
}
// WithRealTimeAuthToken configures the client to authenticate using a temporary
// token generated using [CreateTemporaryToken].
func WithRealTimeAuthToken(token string) RealTimeClientOption {
return func(rtc *RealTimeClient) {
rtc.token = token
}
}
// WithHandler configures the client to use the provided handler to handle
// real-time events.
//
// Deprecated: WithHandler is deprecated. Use [WithRealTimeTranscriber] instead.
func WithHandler(handler RealTimeHandler) RealTimeClientOption {
return func(rtc *RealTimeClient) {
rtc.transcriber = &RealTimeTranscriber{
OnSessionBegins: handler.SessionBegins,
OnSessionTerminated: handler.SessionTerminated,
OnPartialTranscript: handler.PartialTranscript,
OnFinalTranscript: handler.FinalTranscript,
OnError: handler.Error,
}
}
}
func WithRealTimeTranscriber(transcriber *RealTimeTranscriber) RealTimeClientOption {
return func(rtc *RealTimeClient) {
rtc.transcriber = transcriber
}
}
// WithRealTimeSampleRate sets the sample rate for the audio data. Default is
// 16000.
func WithRealTimeSampleRate(sampleRate int) RealTimeClientOption {
return func(rtc *RealTimeClient) {
rtc.sampleRate = sampleRate
}
}
// WithRealTimeWordBoost sets the word boost for the real-time transcription.
func WithRealTimeWordBoost(wordBoost []string) RealTimeClientOption {
return func(rtc *RealTimeClient) {
rtc.wordBoost = wordBoost
}
}
// RealTimeEncoding is the encoding format for the audio data.
type RealTimeEncoding string
const (
// PCM signed 16-bit little-endian (default)
RealTimeEncodingPCMS16LE RealTimeEncoding = "pcm_s16le"
// PCM Mu-law
RealTimeEncodingPCMMulaw RealTimeEncoding = "pcm_mulaw"
)
// WithRealTimeEncoding specifies the encoding of the audio data.
func WithRealTimeEncoding(encoding RealTimeEncoding) RealTimeClientOption {
return func(rtc *RealTimeClient) {
rtc.encoding = encoding
}
}
// NewRealTimeClientWithOptions returns a new instance of [RealTimeClient].
func NewRealTimeClientWithOptions(options ...RealTimeClientOption) *RealTimeClient {
client := &RealTimeClient{
baseURL: &url.URL{
Scheme: "wss",
Host: "api.assemblyai.com",
Path: "/v2/realtime/ws",
},
httpClient: &http.Client{},
}
for _, option := range options {
option(client)
}
client.baseURL.RawQuery = client.queryFromOptions()
return client
}
type SessionBegins struct {
RealTimeBaseMessage
// Timestamp when this session will expire
ExpiresAt string `json:"expires_at"`
// Describes the type of the message
MessageType string `json:"message_type"`
// Unique identifier for the established session
SessionID string `json:"session_id"`
}
type SessionInformation struct {
RealTimeBaseMessage
// The duration of the audio in seconds.
AudioDurationSeconds float64 `json:"audio_duration_seconds"`
}
type SessionTerminated struct {
// Describes the type of the message
MessageType MessageType `json:"message_type"`
}
// Deprecated.
type RealTimeHandler interface {
SessionBegins(ev SessionBegins)
SessionTerminated(ev SessionTerminated)
FinalTranscript(transcript FinalTranscript)
PartialTranscript(transcript PartialTranscript)
Error(err error)
}
// NewRealTimeClient returns a new instance of [RealTimeClient] with default
// values. Use [NewRealTimeClientWithOptions] for more configuration options.
func NewRealTimeClient(apiKey string, handler RealTimeHandler) *RealTimeClient {
return NewRealTimeClientWithOptions(WithRealTimeAPIKey(apiKey), WithHandler(handler))
}
type RealTimeTranscriber struct {
OnSessionBegins func(event SessionBegins)
OnSessionTerminated func(event SessionTerminated)
OnSessionInformation func(event SessionInformation)
OnPartialTranscript func(event PartialTranscript)
OnFinalTranscript func(event FinalTranscript)
OnError func(err error)
}
// Connects opens a WebSocket connection and waits for a session to begin.
// Closes the any open WebSocket connection in case of errors.
func (c *RealTimeClient) Connect(ctx context.Context) error {
header := make(http.Header)
if c.apiKey != "" {
header.Set("Authorization", c.apiKey)
}
opts := &websocket.DialOptions{
HTTPHeader: header,
HTTPClient: &http.Client{},
}
conn, _, err := websocket.Dial(ctx, c.baseURL.String(), opts)
if err != nil {
return err
}
c.conn = conn
var msg json.RawMessage
if err := wsjson.Read(ctx, c.conn, &msg); err != nil {
return err
}
var realtimeError RealTimeError
if err := json.Unmarshal(msg, &realtimeError); err != nil {
return err
}
if realtimeError.Error != "" {
return errors.New(realtimeError.Error)
}
var session SessionBegins
if err := json.Unmarshal(msg, &session); err != nil {
return err
}
c.setSessionOpen(true)
if c.transcriber.OnSessionBegins != nil {
c.transcriber.OnSessionBegins(session)
}
c.done = make(chan bool)
go func() {
for {
if !c.isSessionOpen() {
return
}
var msg json.RawMessage
if err := wsjson.Read(ctx, c.conn, &msg); err != nil {
if c.transcriber.OnError != nil {
c.transcriber.OnError(err)
}
return
}
var messageType struct {
MessageType MessageType `json:"message_type"`
}
if err := json.Unmarshal(msg, &messageType); err != nil {
if c.transcriber.OnError != nil {
c.transcriber.OnError(err)
}
return
}
switch messageType.MessageType {
case MessageTypeFinalTranscript:
var transcript FinalTranscript
if err := json.Unmarshal(msg, &transcript); err != nil {
if c.transcriber.OnError != nil {
c.transcriber.OnError(err)
}
continue
}
if transcript.Text != "" && c.transcriber.OnFinalTranscript != nil {
c.transcriber.OnFinalTranscript(transcript)
}
case MessageTypePartialTranscript:
var transcript PartialTranscript
if err := json.Unmarshal(msg, &transcript); err != nil {
if c.transcriber.OnError != nil {
c.transcriber.OnError(err)
}
continue
}
if transcript.Text != "" && c.transcriber.OnPartialTranscript != nil {
c.transcriber.OnPartialTranscript(transcript)
}
case MessageTypeSessionTerminated:
var session SessionTerminated
if err := json.Unmarshal(msg, &session); err != nil {
if c.transcriber.OnError != nil {
c.transcriber.OnError(err)
}
continue
}
c.setSessionOpen(false)
if c.transcriber.OnSessionTerminated != nil {
c.transcriber.OnSessionTerminated(session)
}
c.done <- true
case MessageTypeSessionInformation:
var info SessionInformation
if err := json.Unmarshal(msg, &info); err != nil {
if c.transcriber.OnError != nil {
c.transcriber.OnError(err)
}
continue
}
if c.transcriber.OnSessionInformation != nil {
c.transcriber.OnSessionInformation(info)
}
}
}
}()
return nil
}
func (c *RealTimeClient) queryFromOptions() string {
values := url.Values{}
// Temporary token
if c.token != "" {
values.Set("token", c.token)
}
// Sample rate
if c.sampleRate > 0 {
values.Set("sample_rate", strconv.Itoa(c.sampleRate))
}
// Encoding
if c.encoding != "" {
values.Set("encoding", string(c.encoding))
}
// Word boost
if len(c.wordBoost) > 0 {
b, _ := json.Marshal(c.wordBoost)
values.Set("word_boost", string(b))
}
// Disable partial transcripts
if c.transcriber.OnPartialTranscript == nil {
values.Set("disable_partial_transcripts", "true")
}
// Extra session information.
if c.transcriber.OnSessionInformation != nil {
values.Set("enable_extra_session_information", "true")
}
return values.Encode()
}
// Disconnect sends the terminate_session message and waits for the server to
// send a SessionTerminated message before closing the connection.
func (c *RealTimeClient) Disconnect(ctx context.Context, waitForSessionTermination bool) error {
if c.conn == nil {
return ErrConnectionNotFound
}
terminate := TerminateSession{TerminateSession: true}
if err := wsjson.Write(ctx, c.conn, terminate); err != nil {
return err
}
if waitForSessionTermination {
<-c.done
}
return c.conn.Close(websocket.StatusNormalClosure, "")
}
// Send sends audio samples to be transcribed.
//
// Expected audio format:
//
// - 16-bit signed integers
// - PCM-encoded
// - Single-channel
func (c *RealTimeClient) Send(ctx context.Context, samples []byte) error {
if c.conn == nil || !c.isSessionOpen() {
return ErrSessionClosed
}
return c.conn.Write(ctx, websocket.MessageBinary, samples)
}
// ForceEndUtterance manually ends an utterance.
func (c *RealTimeClient) ForceEndUtterance(ctx context.Context) error {
return wsjson.Write(ctx, c.conn, forceEndUtterance{
ForceEndUtterance: true,
})
}
// SetEndUtteranceSilenceThreshold configures the threshold for how long to wait
// before ending an utterance. Default is 700ms.
func (c *RealTimeClient) SetEndUtteranceSilenceThreshold(ctx context.Context, threshold int64) error {
return wsjson.Write(ctx, c.conn, endUtteranceSilenceThreshold{
EndUtteranceSilenceThreshold: threshold,
})
}
// RealTimeService groups operations related to the real-time transcription.
type RealTimeService struct {
client *Client
}
// CreateTemporaryToken creates a temporary token that can be used to
// authenticate a real-time client.
func (svc *RealTimeService) CreateTemporaryToken(ctx context.Context, expiresIn int64) (*RealtimeTemporaryTokenResponse, error) {
params := &CreateRealtimeTemporaryTokenParams{
ExpiresIn: Int64(expiresIn),
}
req, err := svc.client.newJSONRequest(ctx, "POST", "/v2/realtime/token", params)
if err != nil {
return nil, err
}
var tokenResponse RealtimeTemporaryTokenResponse
if err := svc.client.do(req, &tokenResponse); err != nil {
return nil, err
}
return &tokenResponse, nil
}