Skip to content

remove hardcoded model parameter and make it configurable #108

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions dist/lib/api.d.ts
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
debug?: boolean;
});
defaultUrl: string;
defaultModel: string;
url: string;
apiKey: string;
debug: boolean;
2 changes: 1 addition & 1 deletion dist/lib/api.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions dist/lib/client.d.ts
Original file line number Diff line number Diff line change
@@ -221,9 +221,12 @@ export class RealtimeClient extends RealtimeEventHandler {
/**
* Connects to the Realtime WebSocket API
* Updates session config and conversation config
* @param {{model?: string}} [settings]
* @returns {Promise<true>}
*/
connect(): Promise<true>;
connect({ model }?: {
model?: string;
}): Promise<true>;
/**
* Waits for a session.created event to be executed before proceeding
* @returns {Promise<true>}
@@ -336,8 +339,7 @@ export type SessionResourceType = {
model?: string;
modalities?: string[];
instructions?: string;
voice?: "alloy"|"ash"|"ballad"|"coral"|"echo"|"sage"|"shimmer"|"verse";

voice?: "alloy" | "ash" | "ballad" | "coral" | "echo" | "sage" | "shimmer" | "verse";
input_audio_format?: AudioFormatType;
output_audio_format?: AudioFormatType;
input_audio_transcription?: AudioTranscriptionType | null;
@@ -457,4 +459,4 @@ export type ResponseResourceType = {
import { RealtimeEventHandler } from './event_handler.js';
import { RealtimeAPI } from './api.js';
import { RealtimeConversation } from './conversation.js';
//# sourceMappingURL=client.d.ts.map
//# sourceMappingURL=client.d.ts.map
2 changes: 1 addition & 1 deletion dist/lib/client.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
super();
this.defaultUrl = 'wss://api.openai.com/v1/realtime';
this.defaultModel = 'gpt-4o-realtime-preview-2024-10-01';
this.url = url || this.defaultUrl;
this.apiKey = apiKey || null;
this.debug = !!debug;
@@ -56,13 +57,14 @@ 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 = this.defaultModel } = {}) {
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 url = `${this.url}${model ? `?model=${model}` : ''}`;
if (globalThis.WebSocket) {
/**
* Web browser
@@ -73,7 +75,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
);
}
const WebSocket = globalThis.WebSocket;
const ws = new WebSocket(`${this.url}${model ? `?model=${model}` : ''}`, [
const ws = new WebSocket(url, [
'realtime',
`openai-insecure-api-key.${this.apiKey}`,
'openai-beta.realtime-v1',
@@ -113,7 +115,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',
url,
[],
{
finishRequest: (request) => {
5 changes: 3 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -387,13 +387,14 @@ export class RealtimeClient extends RealtimeEventHandler {
/**
* Connects to the Realtime WebSocket API
* Updates session config and conversation config
* @param {{model?: string}} [settings]
* @returns {Promise<true>}
*/
async connect() {
async connect({ model } = {}) {
if (this.isConnected()) {
throw new Error(`Already connected, use .disconnect() first`);
}
await this.realtime.connect();
await this.realtime.connect({ model });
this.updateSession();
return true;
}
13 changes: 13 additions & 0 deletions test/tests/api.js
Original file line number Diff line number Diff line change
@@ -48,6 +48,19 @@ export async function run() {
expect(realtime.isConnected()).to.equal(false);
});

it('Should connect to the RealtimeAPI with a model', async () => {
const model = 'gpt-4o-mini-realtime-preview-2024-12-17';
realtime = new RealtimeAPI({
apiKey: process.env.OPENAI_API_KEY,
debug,
});
realtime.connect({ model });
const session = await new Promise((resolve) => {
realtime.on('server.session.created', event => resolve(event.session));
});
expect(session.model).to.equal(model);
});

after(() => {
realtime.isConnected() && realtime.disconnect();
});