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

Add ability to set realtime model #103

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
7 changes: 5 additions & 2 deletions dist/lib/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
export class RealtimeAPI extends RealtimeEventHandler {
/**
* Create a new RealtimeAPI instance
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
* @param {{url?: string, model?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
* @returns {RealtimeAPI}
*/
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug }?: {
constructor({ url, apiKey, model, dangerouslyAllowAPIKeyInBrowser, debug }?: {
url?: string;
model?: string;
apiKey?: string;
dangerouslyAllowAPIKeyInBrowser?: boolean;
debug?: boolean;
});
defaultUrl: string;
defaultModel: string;
url: string;
apiKey: string;
model: string;
debug: boolean;
ws: any;
/**
Expand Down
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.

12 changes: 6 additions & 6 deletions dist/lib/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@
export class RealtimeClient extends RealtimeEventHandler {
/**
* Create a new RealtimeClient instance
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
* @param {{url?: string, model?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
*/
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug }?: {
constructor({ url, apiKey, model, dangerouslyAllowAPIKeyInBrowser, debug }?: {
url?: string;
model?: string;
apiKey?: string;
dangerouslyAllowAPIKeyInBrowser?: boolean;
debug?: boolean;
Expand Down Expand Up @@ -223,7 +224,7 @@ export class RealtimeClient extends RealtimeEventHandler {
* Updates session config and conversation config
* @returns {Promise<true>}
*/
connect(): Promise<true>;
connect({ model }?: {}): Promise<true>;
/**
* Waits for a session.created event to be executed before proceeding
* @returns {Promise<true>}
Expand Down Expand Up @@ -336,8 +337,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;
Expand Down Expand Up @@ -457,4 +457,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.

12 changes: 7 additions & 5 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +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, model?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
* @returns {RealtimeAPI}
*/
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
constructor({ url, apiKey, model, 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.model = model || this.defaultModel;
this.debug = !!debug;
this.ws = null;
if (globalThis.document && this.apiKey) {
Expand Down Expand Up @@ -56,7 +58,7 @@ 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.model } = {}) {
if (!this.apiKey && this.url === this.defaultUrl) {
console.warn(`No apiKey provided for connection to "${this.url}"`);
}
Expand All @@ -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(`${this.url}?model=${model}`, [
'realtime',
`openai-insecure-api-key.${this.apiKey}`,
'openai-beta.realtime-v1',
Expand Down Expand Up @@ -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',
`wss://api.openai.com/v1/realtime?model=${model}`,
[],
{
finishRequest: (request) => {
Expand Down
9 changes: 5 additions & 4 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ 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, model?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
*/
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
constructor({ url, apiKey, model, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
super();
this.defaultSessionConfig = {
modalities: ['text', 'audio'],
Expand Down Expand Up @@ -221,6 +221,7 @@ export class RealtimeClient extends RealtimeEventHandler {
this.realtime = new RealtimeAPI({
url,
apiKey,
model,
dangerouslyAllowAPIKeyInBrowser,
debug,
});
Expand Down Expand Up @@ -389,11 +390,11 @@ export class RealtimeClient extends RealtimeEventHandler {
* Updates session config and conversation config
* @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;
}
Expand Down