Skip to content

Commit

Permalink
Merge pull request #29 from AssemblyAI/5FC74E4F61B92B08B5515C2BF0B00557
Browse files Browse the repository at this point in the history
Release 4.1.0
  • Loading branch information
Swimburger committed Dec 22, 2023
2 parents ffa9186 + 777f9b6 commit 87c2be2
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 18 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## [4.1.0] - 2023-12-22

### Added

- Add `"anthropic/claude-2-1"` to `LemurModel` type
- Add `encoding` option to the real-time service and factory. `encoding` can be `"pcm_s16le"` or `"pcm_mulaw"`.
- `"pcm_mulaw"` is a newly supported audio encoding for the real-time service.

### Changed

- Allow any string into `final_model` for LeMUR requests

## [4.0.1] - 2023-12-14

### Added
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.0.1",
"version": "4.1.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
7 changes: 7 additions & 0 deletions scripts/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ async function generateTypes(apiSpecPath: string, outputPath: string) {
return schemaObject.nullable ? "Date | null" : "Date";
}
},
postTransform(type) {
if (type === `components["schemas"]["LemurModel"] | string`) {
return `LiteralUnion<components["schemas"]["LemurModel"], string>`;
}
},
});
const schemasPosition = output.indexOf("schemas: {") + 10;
output = output
Expand Down Expand Up @@ -46,6 +51,8 @@ async function generateTypes(apiSpecPath: string, outputPath: string) {
/* tslint:disable */
/* eslint-disable */
import { LiteralUnion } from "./helpers";
/** OneOf type helpers */
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
Expand Down
17 changes: 8 additions & 9 deletions scripts/kitchensink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ import {
} from "../src";

const client = new AssemblyAI({
apiKey: process.env.ASSEMBLYAI_API_KEY || "",
apiKey: process.env.ASSEMBLYAI_API_KEY!,
});

(async function transcribeUsingRealtime() {
const useToken = false;
let token: undefined | string = undefined;
if (useToken) {
token = await client.realtime.createTemporaryToken({
expires_in: 480,
});
}
const serviceParams: CreateRealtimeServiceParams = {
sampleRate: 16_000,
wordBoost: ["gore", "climate"],
token: token,
token: useToken
? await client.realtime.createTemporaryToken({
expires_in: 480,
})
: undefined,
encoding: "pcm_s16le",
};
const rt = client.realtime.createService(serviceParams);

Expand All @@ -51,7 +50,7 @@ const client = new AssemblyAI({
await rt.connect();

const chunkSize = 8 * 1024;
const audio = createReadStream("./tests/static/gore-short.wav", {
const audio = createReadStream("./tests/static/gore.wav", {
highWaterMark: chunkSize,
});
for await (const chunk of audio) {
Expand Down
8 changes: 4 additions & 4 deletions src/services/realtime/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export class RealtimeServiceFactory extends BaseService {
}

createService(params?: CreateRealtimeServiceParams): RealtimeService {
if (!params) params = { apiKey: this.rtFactoryParams.apiKey };
else if (!("token" in params) && !params.apiKey) {
params.apiKey = this.rtFactoryParams.apiKey;
const serviceParams = { ...params } as Record<string, unknown>;
if (!serviceParams.token && !serviceParams.apiKey) {
serviceParams.apiKey = this.rtFactoryParams.apiKey;
}

return new RealtimeService(params as RealtimeServiceParams);
return new RealtimeService(serviceParams as RealtimeServiceParams);
}

async createTemporaryToken(params: RealtimeTokenParams) {
Expand Down
10 changes: 8 additions & 2 deletions src/services/realtime/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PartialTranscript,
FinalTranscript,
SessionBeginsEventData,
AudioEncoding,
} from "../..";
import {
RealtimeError,
Expand All @@ -23,6 +24,7 @@ export class RealtimeService {
private realtimeUrl: string;
private sampleRate: number;
private wordBoost?: string[];
private encoding?: AudioEncoding;
private apiKey?: string;
private token?: string;
private socket?: WebSocket;
Expand All @@ -33,10 +35,11 @@ export class RealtimeService {
this.realtimeUrl = params.realtimeUrl ?? defaultRealtimeUrl;
this.sampleRate = params.sampleRate ?? 16_000;
this.wordBoost = params.wordBoost;
if ("apiKey" in params && params.apiKey) this.apiKey = params.apiKey;
this.encoding = params.encoding;
if ("token" in params && params.token) this.token = params.token;
if ("apiKey" in params && params.apiKey) this.apiKey = params.apiKey;

if (!(this.apiKey || this.token)) {
if (!(this.token || this.apiKey)) {
throw new Error("API key or temporary token is required.");
}
}
Expand All @@ -56,6 +59,9 @@ export class RealtimeService {
if (this.wordBoost && this.wordBoost.length > 0) {
searchParams.set("word_boost", JSON.stringify(this.wordBoost));
}
if (this.encoding) {
searchParams.set("encoding", this.encoding);
}
url.search = searchParams.toString();

return url;
Expand Down
9 changes: 9 additions & 0 deletions src/types/asyncapi.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* tslint:disable */
/* eslint-disable */

import { LiteralUnion } from "./helpers";

/** OneOf type helpers */
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = T | U extends object
Expand All @@ -18,6 +20,13 @@ export type AudioData = {
audio_data: string;
};

/**
* @description The encoding of the audio data
* @default pcm_s16le
* @enum {string}
*/
export type AudioEncoding = "pcm_s16le" | "pcm_mulaw";

export type FinalTranscript = RealtimeBaseTranscript & {
/**
* @description Describes the type of message
Expand Down
4 changes: 4 additions & 0 deletions src/types/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// source: https://github.com/sindresorhus/type-fest/blob/main/source/literal-union.d.ts
export type LiteralUnion<LiteralType, BaseType> =
| LiteralType
| (BaseType & Record<never, never>);
10 changes: 8 additions & 2 deletions src/types/openapi.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* tslint:disable */
/* eslint-disable */

import { LiteralUnion } from "./helpers";

/** OneOf type helpers */
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = T | U extends object
Expand Down Expand Up @@ -512,7 +514,7 @@ export type LemurBaseParams = {
}
]
>;
final_model?: LemurModel;
final_model?: LiteralUnion<LemurModel, string>;
/**
* @description Custom formatted transcript data. Maximum size is the context limit of the selected model, which defaults to 100000.
* Use either transcript_ids or input_text as input into LeMUR.
Expand Down Expand Up @@ -552,7 +554,11 @@ export type LemurBaseResponse = {
*
* @enum {string}
*/
export type LemurModel = "default" | "basic" | "assemblyai/mistral-7b";
export type LemurModel =
| "default"
| "basic"
| "assemblyai/mistral-7b"
| "anthropic/claude-2-1";

/**
* @example {
Expand Down
3 changes: 3 additions & 0 deletions src/types/realtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AudioEncoding,
FinalTranscript,
PartialTranscript,
RealtimeTranscript,
Expand All @@ -9,6 +10,7 @@ type CreateRealtimeServiceParams = {
realtimeUrl?: string;
sampleRate?: number;
wordBoost?: string[];
encoding?: AudioEncoding;
} & (
| {
apiKey?: string;
Expand All @@ -22,6 +24,7 @@ type RealtimeServiceParams = {
realtimeUrl?: string;
sampleRate?: number;
wordBoost?: string[];
encoding?: AudioEncoding;
} & (
| {
apiKey: string;
Expand Down
Binary file added tests/static/gore.mulaw.wav
Binary file not shown.

0 comments on commit 87c2be2

Please sign in to comment.