@@ -4,15 +4,16 @@ import { RealtimeUtils } from './utils.js';
4
4
export class RealtimeAPI extends RealtimeEventHandler {
5
5
/**
6
6
* 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]
8
8
* @returns {RealtimeAPI }
9
9
*/
10
- constructor ( { url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = { } ) {
10
+ constructor ( { url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug, model } = { } ) {
11
11
super ( ) ;
12
12
this . defaultUrl = 'wss://api.openai.com/v1/realtime' ;
13
13
this . url = url || this . defaultUrl ;
14
14
this . apiKey = apiKey || null ;
15
15
this . debug = ! ! debug ;
16
+ this . model = model || 'gpt-4o-realtime-preview' ;
16
17
this . ws = null ;
17
18
if ( globalThis . document && this . apiKey ) {
18
19
if ( ! dangerouslyAllowAPIKeyInBrowser ) {
@@ -56,13 +57,15 @@ export class RealtimeAPI extends RealtimeEventHandler {
56
57
* @param {{model?: string} } [settings]
57
58
* @returns {Promise<true> }
58
59
*/
59
- async connect ( { model } = { model : 'gpt-4o-realtime-preview-2024-10-01' } ) {
60
+ async connect ( { model } = { } ) {
60
61
if ( ! this . apiKey && this . url === this . defaultUrl ) {
61
62
console . warn ( `No apiKey provided for connection to "${ this . url } "` ) ;
62
63
}
63
64
if ( this . isConnected ( ) ) {
64
65
throw new Error ( `Already connected` ) ;
65
66
}
67
+ const useModel = model || this . model ;
68
+
66
69
if ( globalThis . WebSocket ) {
67
70
/**
68
71
* Web browser
@@ -73,7 +76,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
73
76
) ;
74
77
}
75
78
const WebSocket = globalThis . WebSocket ;
76
- const ws = new WebSocket ( `${ this . url } ${ model ? `?model=${ model } ` : '' } ` , [
79
+ const ws = new WebSocket ( `${ this . url } ${ useModel ? `?model=${ useModel } ` : '' } ` , [
77
80
'realtime' ,
78
81
`openai-insecure-api-key.${ this . apiKey } ` ,
79
82
'openai-beta.realtime-v1' ,
@@ -113,7 +116,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
113
116
const wsModule = await import ( /* webpackIgnore: true */ moduleName ) ;
114
117
const WebSocket = wsModule . default ;
115
118
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 } ` : '' } ` ,
117
120
[ ] ,
118
121
{
119
122
finishRequest : ( request ) => {
0 commit comments