Skip to content

Commit 3bc9e26

Browse files
committed
Custom realtime model selection
1 parent a5cb948 commit 3bc9e26

File tree

8 files changed

+71
-22
lines changed

8 files changed

+71
-22
lines changed

dist/lib/api.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
3131
* @param {{model?: string}} [settings]
3232
* @returns {Promise<true>}
3333
*/
34-
connect({ model }?: {
35-
model?: string;
36-
}): Promise<true>;
34+
connect(model?: string): Promise<true>;
3735
/**
3836
* Disconnects from Realtime API server
3937
* @param {WebSocket} [ws]

dist/lib/api.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/client.d.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,25 @@
153153
* @property {ItemType[]} output
154154
* @property {UsageType|null} usage
155155
*/
156+
/**
157+
* RealtimeClient Settings
158+
* @typedef {Object} RealtimeClientSettings
159+
* @property {string} [url] - The URL for the realtime client
160+
* @property {string} [apiKey] - The API key
161+
* @property {string} [model] - The model name to use
162+
* @property {boolean} [dangerouslyAllowAPIKeyInBrowser] - Whether to allow API key in browser
163+
* @property {boolean} [debug] - Enable debug mode
164+
*/
156165
/**
157166
* RealtimeClient Class
158167
* @class
159168
*/
160169
export class RealtimeClient extends RealtimeEventHandler {
161170
/**
162171
* Create a new RealtimeClient instance
163-
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
172+
* @param {RealtimeClientSettings} [settings]
164173
*/
165-
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug }?: {
166-
url?: string;
167-
apiKey?: string;
168-
dangerouslyAllowAPIKeyInBrowser?: boolean;
169-
debug?: boolean;
170-
});
174+
constructor({ url, apiKey, model, dangerouslyAllowAPIKeyInBrowser, debug, }?: RealtimeClientSettings);
171175
defaultSessionConfig: {
172176
modalities: string[];
173177
instructions: string;
@@ -181,6 +185,7 @@ export class RealtimeClient extends RealtimeEventHandler {
181185
temperature: number;
182186
max_response_output_tokens: number;
183187
};
188+
realtimeModel: string;
184189
sessionConfig: {};
185190
transcriptionModels: {
186191
model: string;
@@ -336,8 +341,7 @@ export type SessionResourceType = {
336341
model?: string;
337342
modalities?: string[];
338343
instructions?: string;
339-
voice?: "alloy"|"ash"|"ballad"|"coral"|"echo"|"sage"|"shimmer"|"verse";
340-
344+
voice?: "alloy" | "ash" | "ballad" | "coral" | "echo" | "sage" | "shimmer" | "verse";
341345
input_audio_format?: AudioFormatType;
342346
output_audio_format?: AudioFormatType;
343347
input_audio_transcription?: AudioTranscriptionType | null;
@@ -454,7 +458,32 @@ export type ResponseResourceType = {
454458
output: ItemType[];
455459
usage: UsageType | null;
456460
};
461+
/**
462+
* RealtimeClient Settings
463+
*/
464+
export type RealtimeClientSettings = {
465+
/**
466+
* - The URL for the realtime client
467+
*/
468+
url?: string;
469+
/**
470+
* - The API key
471+
*/
472+
apiKey?: string;
473+
/**
474+
* - The model name to use
475+
*/
476+
model?: string;
477+
/**
478+
* - Whether to allow API key in browser
479+
*/
480+
dangerouslyAllowAPIKeyInBrowser?: boolean;
481+
/**
482+
* - Enable debug mode
483+
*/
484+
debug?: boolean;
485+
};
457486
import { RealtimeEventHandler } from './event_handler.js';
458487
import { RealtimeAPI } from './api.js';
459488
import { RealtimeConversation } from './conversation.js';
460-
//# sourceMappingURL=client.d.ts.map
489+
//# sourceMappingURL=client.d.ts.map

dist/lib/client.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/conversation.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class RealtimeConversation {
6666
'response.audio.delta': (event: any) => {
6767
item: any;
6868
delta: {
69-
audio: Int16Array;
69+
audio: Int16Array<ArrayBuffer>;
7070
};
7171
};
7272
'response.text.delta': (event: any) => {
@@ -82,7 +82,7 @@ export class RealtimeConversation {
8282
};
8383
};
8484
};
85-
queuedInputAudio: Int16Array;
85+
queuedInputAudio: Int16Array<ArrayBufferLike>;
8686
/**
8787
* Clears the conversation history and resets to default
8888
* @returns {true}

dist/lib/conversation.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ export class RealtimeAPI extends RealtimeEventHandler {
5656
* @param {{model?: string}} [settings]
5757
* @returns {Promise<true>}
5858
*/
59-
async connect({ model } = { model: 'gpt-4o-realtime-preview-2024-10-01' }) {
59+
async connect(model = 'gpt-4o-realtime-preview-2024-10-01') {
6060
if (!this.apiKey && this.url === this.defaultUrl) {
6161
console.warn(`No apiKey provided for connection to "${this.url}"`);
6262
}
6363
if (this.isConnected()) {
6464
throw new Error(`Already connected`);
6565
}
66+
6667
if (globalThis.WebSocket) {
6768
/**
6869
* Web browser

lib/client.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,32 @@ import { RealtimeUtils } from './utils.js';
182182
* @property {UsageType|null} usage
183183
*/
184184

185+
/**
186+
* RealtimeClient Settings
187+
* @typedef {Object} RealtimeClientSettings
188+
* @property {string} [url] - The URL for the realtime client
189+
* @property {string} [apiKey] - The API key
190+
* @property {string} [model] - The model name to use
191+
* @property {boolean} [dangerouslyAllowAPIKeyInBrowser] - Whether to allow API key in browser
192+
* @property {boolean} [debug] - Enable debug mode
193+
*/
194+
185195
/**
186196
* RealtimeClient Class
187197
* @class
188198
*/
189199
export class RealtimeClient extends RealtimeEventHandler {
190200
/**
191201
* Create a new RealtimeClient instance
192-
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
202+
* @param {RealtimeClientSettings} [settings]
193203
*/
194-
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
204+
constructor({
205+
url,
206+
apiKey,
207+
model,
208+
dangerouslyAllowAPIKeyInBrowser,
209+
debug,
210+
} = {}) {
195211
super();
196212
this.defaultSessionConfig = {
197213
modalities: ['text', 'audio'],
@@ -206,6 +222,11 @@ export class RealtimeClient extends RealtimeEventHandler {
206222
temperature: 0.8,
207223
max_response_output_tokens: 4096,
208224
};
225+
if (!model) {
226+
this.realtimeModel = 'gpt-4o-realtime-preview-2024-10-01';
227+
} else {
228+
this.realtimeModel = model;
229+
}
209230
this.sessionConfig = {};
210231
this.transcriptionModels = [
211232
{
@@ -393,7 +414,7 @@ export class RealtimeClient extends RealtimeEventHandler {
393414
if (this.isConnected()) {
394415
throw new Error(`Already connected, use .disconnect() first`);
395416
}
396-
await this.realtime.connect();
417+
await this.realtime.connect(this.realtimeModel);
397418
this.updateSession();
398419
return true;
399420
}

0 commit comments

Comments
 (0)