Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify constructor to allow specifying model to use #90

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -56,13 +57,15 @@ export class RealtimeAPI extends RealtimeEventHandler {
* @param {{model?: string}} [settings]
* @returns {Promise<true>}
*/
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
Expand All @@ -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',
Expand Down Expand Up @@ -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) => {
Expand Down
6 changes: 4 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down Expand Up @@ -223,6 +224,7 @@ export class RealtimeClient extends RealtimeEventHandler {
apiKey,
dangerouslyAllowAPIKeyInBrowser,
debug,
model: this.defaultModel,
});
this.conversation = new RealtimeConversation();
this._resetConfig();
Expand Down