From 6f23ed364480aa6f2621ab408f51919fe9d67275 Mon Sep 17 00:00:00 2001 From: AssemblyAI Date: Mon, 26 Aug 2024 09:07:26 -0400 Subject: [PATCH] Project import generated by Copybara. GitOrigin-RevId: 7a6562608e463ba9c1bea883a2db69152777f069 --- CHANGELOG.md | 17 ++++++++ package.json | 2 +- src/services/realtime/service.ts | 11 ++--- src/types/openapi.generated.ts | 22 ++++++++-- src/utils/errors/realtime.ts | 73 +++++++++++++++++++++----------- 5 files changed, 90 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b5e783..466cbfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # Changelog +## [4.7.0] + +- Add `language_confidence_threshold` to `Transcript`, `TranscriptParams`, and `TranscriptOptionalParams`. + > The confidence threshold for the automatically detected language. + > An error will be returned if the langauge confidence is below this threshold. +- Add `language_confidence` to `Transcript` + > The confidence score for the detected language, between 0.0 (low confidence) and 1.0 (high confidence) + +Using these new fields you can determine the confidence of the language detection model (enable by setting `language_detection` to `true`), and fail the transcript if it doesn't meet your desired threshold. + +[Learn more about the new automatic language detection model and feature improvements on our blog.](https://www.assemblyai.com/blog/ald-improvements) + +## [4.6.2] + +- Change `RealtimeErrorType` from enum to const object. +- Add `RealtimeErrorTypeCodes` which is a union of `RealtimeErrorType` values + ## [4.6.1] - Remove `conformer-2` from `SpeechModel` union type. diff --git a/package.json b/package.json index eb11977..44dfa47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "assemblyai", - "version": "4.6.1", + "version": "4.7.0", "description": "The AssemblyAI JavaScript SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, as well as the latest LeMUR models.", "engines": { "node": ">=18" diff --git a/src/services/realtime/service.ts b/src/services/realtime/service.ts index fc3efe6..8695c01 100644 --- a/src/services/realtime/service.ts +++ b/src/services/realtime/service.ts @@ -17,11 +17,8 @@ import { AudioData, SessionInformation, } from "../.."; -import { - RealtimeError, - RealtimeErrorMessages, - RealtimeErrorType, -} from "../../utils/errors"; +import { RealtimeError, RealtimeErrorMessages } from "../../utils/errors"; +import { RealtimeErrorTypeCodes } from "../../utils/errors/realtime"; const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws"; const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`; @@ -213,8 +210,8 @@ export class RealtimeTranscriber { this.socket!.onclose = ({ code, reason }: CloseEvent) => { if (!reason) { - if (code in RealtimeErrorType) { - reason = RealtimeErrorMessages[code as RealtimeErrorType]; + if (code in RealtimeErrorMessages) { + reason = RealtimeErrorMessages[code as RealtimeErrorTypeCodes]; } } this.listeners.close?.(code, reason); diff --git a/src/types/openapi.generated.ts b/src/types/openapi.generated.ts index b5be12b..a0ca082 100644 --- a/src/types/openapi.generated.ts +++ b/src/types/openapi.generated.ts @@ -2406,6 +2406,16 @@ export type Transcript = { * The default value is 'en_us'. */ language_code?: LiteralUnion; + /** + * The confidence score for the detected language, between 0.0 (low confidence) and 1.0 (high confidence) + */ + language_confidence: number | null; + /** + * The confidence threshold for the automatically detected language. + * An error will be returned if the langauge confidence is below this threshold. + * Defaults to 0. + */ + language_confidence_threshold: number | null; /** * Whether {@link https://www.assemblyai.com/docs/models/speech-recognition#automatic-language-detection | Automatic language detection } is enabled, either true or false */ @@ -2542,7 +2552,7 @@ export type Transcript = { }; /** - * The word boost parameter value + * How much to boost specified words */ export type TranscriptBoostParam = "low" | "default" | "high"; @@ -2823,7 +2833,7 @@ export type TranscriptOptionalParams = { */ auto_highlights?: boolean; /** - * The word boost parameter value + * How much to boost specified words */ boost_param?: TranscriptBoostParam; /** @@ -2871,6 +2881,12 @@ export type TranscriptOptionalParams = { * The default value is 'en_us'. */ language_code?: LiteralUnion | null; + /** + * The confidence threshold for the automatically detected language. + * An error will be returned if the langauge confidence is below this threshold. + * Defaults to 0. + */ + language_confidence_threshold?: number; /** * Enable {@link https://www.assemblyai.com/docs/models/speech-recognition#automatic-language-detection | Automatic language detection }, either true or false. */ @@ -2914,7 +2930,7 @@ export type TranscriptOptionalParams = { */ speakers_expected?: number | null; /** - * The speech model to use for the transcription. When `null`, the default model is used. + * The speech model to use for the transcription. When `null`, the "best" model is used. * @defaultValue null */ speech_model?: SpeechModel | null; diff --git a/src/utils/errors/realtime.ts b/src/utils/errors/realtime.ts index 3377c47..a6a63b8 100644 --- a/src/utils/errors/realtime.ts +++ b/src/utils/errors/realtime.ts @@ -1,28 +1,44 @@ -enum RealtimeErrorType { - BadSampleRate = 4000, - AuthFailed = 4001, - // Both InsufficientFunds and FreeAccount error use 4002 - InsufficientFundsOrFreeAccount = 4002, - NonexistentSessionId = 4004, - SessionExpired = 4008, - ClosedSession = 4010, - RateLimited = 4029, - UniqueSessionViolation = 4030, - SessionTimeout = 4031, - AudioTooShort = 4032, - AudioTooLong = 4033, - BadJson = 4100, - BadSchema = 4101, - TooManyStreams = 4102, - Reconnected = 4103, - ReconnectAttemptsExhausted = 1013, -} +const RealtimeErrorType = { + BadSampleRate: 4000, + AuthFailed: 4001, + /** + * @deprecated Use InsufficientFunds or FreeTierUser instead + */ + InsufficientFundsOrFreeAccount: 4002, + InsufficientFunds: 4002, + FreeTierUser: 4003, + NonexistentSessionId: 4004, + SessionExpired: 4008, + ClosedSession: 4010, + RateLimited: 4029, + UniqueSessionViolation: 4030, + SessionTimeout: 4031, + AudioTooShort: 4032, + AudioTooLong: 4033, + AudioTooSmallToTranscode: 4034, + /** + * @deprecated Don't use + */ + BadJson: 4100, + BadSchema: 4101, + TooManyStreams: 4102, + Reconnected: 4103, + /** + * @deprecated Don't use + */ + ReconnectAttemptsExhausted: 1013, + WordBoostParameterParsingFailed: 4104, +} as const; -const RealtimeErrorMessages: Record = { +type RealtimeErrorTypeCodes = + (typeof RealtimeErrorType)[keyof typeof RealtimeErrorType]; + +const RealtimeErrorMessages: Record = { [RealtimeErrorType.BadSampleRate]: "Sample rate must be a positive integer", [RealtimeErrorType.AuthFailed]: "Not Authorized", - [RealtimeErrorType.InsufficientFundsOrFreeAccount]: - "Insufficient funds or you are using a free account. This feature is paid-only and requires you to add a credit card. Please visit https://assemblyai.com/dashboard/ to add a credit card to your account.", + [RealtimeErrorType.InsufficientFunds]: "Insufficient funds", + [RealtimeErrorType.FreeTierUser]: + "This feature is paid-only and requires you to add a credit card. Please visit https://app.assemblyai.com/ to add a credit card to your account.", [RealtimeErrorType.NonexistentSessionId]: "Session ID does not exist", [RealtimeErrorType.SessionExpired]: "Session has expired", [RealtimeErrorType.ClosedSession]: "Session is closed", @@ -31,14 +47,23 @@ const RealtimeErrorMessages: Record = { [RealtimeErrorType.SessionTimeout]: "Session Timeout", [RealtimeErrorType.AudioTooShort]: "Audio too short", [RealtimeErrorType.AudioTooLong]: "Audio too long", + [RealtimeErrorType.AudioTooSmallToTranscode]: "Audio too small to transcode", [RealtimeErrorType.BadJson]: "Bad JSON", [RealtimeErrorType.BadSchema]: "Bad schema", [RealtimeErrorType.TooManyStreams]: "Too many streams", - [RealtimeErrorType.Reconnected]: "Reconnected", + [RealtimeErrorType.Reconnected]: + "This session has been reconnected. This WebSocket is no longer valid.", [RealtimeErrorType.ReconnectAttemptsExhausted]: "Reconnect attempts exhausted", + [RealtimeErrorType.WordBoostParameterParsingFailed]: + "Could not parse word boost parameter", }; class RealtimeError extends Error {} -export { RealtimeError, RealtimeErrorType, RealtimeErrorMessages }; +export { + RealtimeError, + RealtimeErrorType, + RealtimeErrorTypeCodes, + RealtimeErrorMessages, +};