Skip to content

Commit

Permalink
Merge pull request #67 from AssemblyAI/E07417BDFEA3614F5967B1520F8B2F61
Browse files Browse the repository at this point in the history
Sync from internal repo (2024/08/26)
  • Loading branch information
Swimburger authored Aug 26, 2024
2 parents 18a2b66 + 6f23ed3 commit c126ce0
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 35 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
11 changes: 4 additions & 7 deletions src/services/realtime/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 19 additions & 3 deletions src/types/openapi.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,16 @@ export type Transcript = {
* The default value is 'en_us'.
*/
language_code?: LiteralUnion<TranscriptLanguageCode, string>;
/**
* 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
*/
Expand Down Expand Up @@ -2542,7 +2552,7 @@ export type Transcript = {
};

/**
* The word boost parameter value
* How much to boost specified words
*/
export type TranscriptBoostParam = "low" | "default" | "high";

Expand Down Expand Up @@ -2823,7 +2833,7 @@ export type TranscriptOptionalParams = {
*/
auto_highlights?: boolean;
/**
* The word boost parameter value
* How much to boost specified words
*/
boost_param?: TranscriptBoostParam;
/**
Expand Down Expand Up @@ -2871,6 +2881,12 @@ export type TranscriptOptionalParams = {
* The default value is 'en_us'.
*/
language_code?: LiteralUnion<TranscriptLanguageCode, string> | 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.
*/
Expand Down Expand Up @@ -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;
Expand Down
73 changes: 49 additions & 24 deletions src/utils/errors/realtime.ts
Original file line number Diff line number Diff line change
@@ -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<RealtimeErrorType, string> = {
type RealtimeErrorTypeCodes =
(typeof RealtimeErrorType)[keyof typeof RealtimeErrorType];

const RealtimeErrorMessages: Record<RealtimeErrorTypeCodes, string> = {
[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",
Expand All @@ -31,14 +47,23 @@ const RealtimeErrorMessages: Record<RealtimeErrorType, string> = {
[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,
};

0 comments on commit c126ce0

Please sign in to comment.