diff --git a/lib/api.js b/lib/api.js index cc8f904..6407d57 100644 --- a/lib/api.js +++ b/lib/api.js @@ -4,15 +4,16 @@ import { RealtimeUtils } from './utils.js'; export class RealtimeAPI extends RealtimeEventHandler { /** * Create a new RealtimeAPI instance - * @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings] + * @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean, model?: string}} [settings] * @returns {RealtimeAPI} */ - constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) { + constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug, model } = {}) { super(); this.defaultUrl = 'wss://api.openai.com/v1/realtime'; this.url = url || this.defaultUrl; this.apiKey = apiKey || null; this.debug = !!debug; + this.model = model || 'gpt-4o-realtime-preview'; this.ws = null; if (globalThis.document && this.apiKey) { if (!dangerouslyAllowAPIKeyInBrowser) { @@ -56,13 +57,15 @@ export class RealtimeAPI extends RealtimeEventHandler { * @param {{model?: string}} [settings] * @returns {Promise} */ - async connect({ model } = { model: 'gpt-4o-realtime-preview-2024-10-01' }) { + async connect({ model } = {}) { if (!this.apiKey && this.url === this.defaultUrl) { console.warn(`No apiKey provided for connection to "${this.url}"`); } if (this.isConnected()) { throw new Error(`Already connected`); } + const useModel = model || this.model; + if (globalThis.WebSocket) { /** * Web browser @@ -73,7 +76,7 @@ export class RealtimeAPI extends RealtimeEventHandler { ); } const WebSocket = globalThis.WebSocket; - const ws = new WebSocket(`${this.url}${model ? `?model=${model}` : ''}`, [ + const ws = new WebSocket(`${this.url}${useModel ? `?model=${useModel}` : ''}`, [ 'realtime', `openai-insecure-api-key.${this.apiKey}`, 'openai-beta.realtime-v1', @@ -113,7 +116,7 @@ export class RealtimeAPI extends RealtimeEventHandler { const wsModule = await import(/* webpackIgnore: true */ moduleName); const WebSocket = wsModule.default; const ws = new WebSocket( - 'wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01', + `${this.url}${useModel ? `?model=${useModel}` : ''}`, [], { finishRequest: (request) => { diff --git a/lib/client.js b/lib/client.js index 2c48d7f..f101c41 100644 --- a/lib/client.js +++ b/lib/client.js @@ -189,10 +189,11 @@ import { RealtimeUtils } from './utils.js'; export class RealtimeClient extends RealtimeEventHandler { /** * Create a new RealtimeClient instance - * @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings] + * @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean, model?: string}} [settings] */ - constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) { + constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug, model } = {}) { super(); + this.defaultModel = model || 'gpt-4o-realtime-preview'; this.defaultSessionConfig = { modalities: ['text', 'audio'], instructions: '', @@ -223,6 +224,7 @@ export class RealtimeClient extends RealtimeEventHandler { apiKey, dangerouslyAllowAPIKeyInBrowser, debug, + model: this.defaultModel, }); this.conversation = new RealtimeConversation(); this._resetConfig();