Skip to content

Custom Realtime Model Selection #92

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
4 changes: 1 addition & 3 deletions dist/lib/api.d.ts
Original file line number Diff line number Diff line change
@@ -31,9 +31,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
* @param {{model?: string}} [settings]
* @returns {Promise<true>}
*/
connect({ model }?: {
model?: string;
}): Promise<true>;
connect(model?: string): Promise<true>;
/**
* Disconnects from Realtime API server
* @param {WebSocket} [ws]
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.

49 changes: 39 additions & 10 deletions dist/lib/client.d.ts
Original file line number Diff line number Diff line change
@@ -153,21 +153,25 @@
* @property {ItemType[]} output
* @property {UsageType|null} usage
*/
/**
* RealtimeClient Settings
* @typedef {Object} RealtimeClientSettings
* @property {string} [url] - The URL for the realtime client
* @property {string} [apiKey] - The API key
* @property {string} [model] - The model name to use
* @property {boolean} [dangerouslyAllowAPIKeyInBrowser] - Whether to allow API key in browser
* @property {boolean} [debug] - Enable debug mode
*/
/**
* RealtimeClient Class
* @class
*/
export class RealtimeClient extends RealtimeEventHandler {
/**
* Create a new RealtimeClient instance
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
* @param {RealtimeClientSettings} [settings]
*/
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug }?: {
url?: string;
apiKey?: string;
dangerouslyAllowAPIKeyInBrowser?: boolean;
debug?: boolean;
});
constructor({ url, apiKey, model, dangerouslyAllowAPIKeyInBrowser, debug, }?: RealtimeClientSettings);
defaultSessionConfig: {
modalities: string[];
instructions: string;
@@ -181,6 +185,7 @@ export class RealtimeClient extends RealtimeEventHandler {
temperature: number;
max_response_output_tokens: number;
};
realtimeModel: string;
sessionConfig: {};
transcriptionModels: {
model: string;
@@ -336,8 +341,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;
@@ -454,7 +458,32 @@ export type ResponseResourceType = {
output: ItemType[];
usage: UsageType | null;
};
/**
* RealtimeClient Settings
*/
export type RealtimeClientSettings = {
/**
* - The URL for the realtime client
*/
url?: string;
/**
* - The API key
*/
apiKey?: string;
/**
* - The model name to use
*/
model?: string;
/**
* - Whether to allow API key in browser
*/
dangerouslyAllowAPIKeyInBrowser?: boolean;
/**
* - Enable debug mode
*/
debug?: boolean;
};
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.

4 changes: 2 additions & 2 deletions dist/lib/conversation.d.ts
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ export class RealtimeConversation {
'response.audio.delta': (event: any) => {
item: any;
delta: {
audio: Int16Array;
audio: Int16Array<ArrayBuffer>;
};
};
'response.text.delta': (event: any) => {
@@ -82,7 +82,7 @@ export class RealtimeConversation {
};
};
};
queuedInputAudio: Int16Array;
queuedInputAudio: Int16Array<ArrayBufferLike>;
/**
* Clears the conversation history and resets to default
* @returns {true}
2 changes: 1 addition & 1 deletion dist/lib/conversation.d.ts.map

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

3 changes: 2 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
@@ -56,13 +56,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 = 'gpt-4o-realtime-preview-2024-10-01') {
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`);
}

if (globalThis.WebSocket) {
/**
* Web browser
27 changes: 24 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -182,16 +182,32 @@ import { RealtimeUtils } from './utils.js';
* @property {UsageType|null} usage
*/

/**
* RealtimeClient Settings
* @typedef {Object} RealtimeClientSettings
* @property {string} [url] - The URL for the realtime client
* @property {string} [apiKey] - The API key
* @property {string} [model] - The model name to use
* @property {boolean} [dangerouslyAllowAPIKeyInBrowser] - Whether to allow API key in browser
* @property {boolean} [debug] - Enable debug mode
*/

/**
* RealtimeClient Class
* @class
*/
export class RealtimeClient extends RealtimeEventHandler {
/**
* Create a new RealtimeClient instance
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
* @param {RealtimeClientSettings} [settings]
*/
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
constructor({
url,
apiKey,
model,
dangerouslyAllowAPIKeyInBrowser,
debug,
} = {}) {
super();
this.defaultSessionConfig = {
modalities: ['text', 'audio'],
@@ -206,6 +222,11 @@ export class RealtimeClient extends RealtimeEventHandler {
temperature: 0.8,
max_response_output_tokens: 4096,
};
if (!model) {
this.realtimeModel = 'gpt-4o-realtime-preview-2024-10-01';
} else {
this.realtimeModel = model;
}
Comment on lines +225 to +229

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I'm not wrong, it can be done like this? this.realtimeModel = model || 'gpt-4o-realtime-preview-2024-10-01';

this.sessionConfig = {};
this.transcriptionModels = [
{
@@ -393,7 +414,7 @@ export class RealtimeClient extends RealtimeEventHandler {
if (this.isConnected()) {
throw new Error(`Already connected, use .disconnect() first`);
}
await this.realtime.connect();
await this.realtime.connect(this.realtimeModel);
this.updateSession();
return true;
}