Skip to content

Commit af3baed

Browse files
committed
modify constructor to allow specifying model to use
1 parent a5cb948 commit af3baed

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/api.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { RealtimeUtils } from './utils.js';
44
export class RealtimeAPI extends RealtimeEventHandler {
55
/**
66
* Create a new RealtimeAPI instance
7-
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
7+
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean, model?: string}} [settings]
88
* @returns {RealtimeAPI}
99
*/
10-
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
10+
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug, model } = {}) {
1111
super();
1212
this.defaultUrl = 'wss://api.openai.com/v1/realtime';
1313
this.url = url || this.defaultUrl;
1414
this.apiKey = apiKey || null;
1515
this.debug = !!debug;
16+
this.model = model || 'gpt-4o-realtime-preview';
1617
this.ws = null;
1718
if (globalThis.document && this.apiKey) {
1819
if (!dangerouslyAllowAPIKeyInBrowser) {
@@ -56,13 +57,15 @@ export class RealtimeAPI extends RealtimeEventHandler {
5657
* @param {{model?: string}} [settings]
5758
* @returns {Promise<true>}
5859
*/
59-
async connect({ model } = { model: 'gpt-4o-realtime-preview-2024-10-01' }) {
60+
async connect({ model } = {}) {
6061
if (!this.apiKey && this.url === this.defaultUrl) {
6162
console.warn(`No apiKey provided for connection to "${this.url}"`);
6263
}
6364
if (this.isConnected()) {
6465
throw new Error(`Already connected`);
6566
}
67+
const useModel = model || this.model;
68+
6669
if (globalThis.WebSocket) {
6770
/**
6871
* Web browser
@@ -73,7 +76,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
7376
);
7477
}
7578
const WebSocket = globalThis.WebSocket;
76-
const ws = new WebSocket(`${this.url}${model ? `?model=${model}` : ''}`, [
79+
const ws = new WebSocket(`${this.url}${useModel ? `?model=${useModel}` : ''}`, [
7780
'realtime',
7881
`openai-insecure-api-key.${this.apiKey}`,
7982
'openai-beta.realtime-v1',
@@ -113,7 +116,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
113116
const wsModule = await import(/* webpackIgnore: true */ moduleName);
114117
const WebSocket = wsModule.default;
115118
const ws = new WebSocket(
116-
'wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01',
119+
`${this.url}${useModel ? `?model=${useModel}` : ''}`,
117120
[],
118121
{
119122
finishRequest: (request) => {

lib/client.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,11 @@ import { RealtimeUtils } from './utils.js';
189189
export class RealtimeClient extends RealtimeEventHandler {
190190
/**
191191
* Create a new RealtimeClient instance
192-
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
192+
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean, model?: string}} [settings]
193193
*/
194-
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
194+
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug, model } = {}) {
195195
super();
196+
this.defaultModel = model || 'gpt-4o-realtime-preview';
196197
this.defaultSessionConfig = {
197198
modalities: ['text', 'audio'],
198199
instructions: '',
@@ -223,6 +224,7 @@ export class RealtimeClient extends RealtimeEventHandler {
223224
apiKey,
224225
dangerouslyAllowAPIKeyInBrowser,
225226
debug,
227+
model: this.defaultModel,
226228
});
227229
this.conversation = new RealtimeConversation();
228230
this._resetConfig();

0 commit comments

Comments
 (0)