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

Support Azure OpenAI #60

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
20 changes: 15 additions & 5 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ export class RealtimeAPI extends RealtimeEventHandler {
return true;
}

/**
* Determines if the URL is an Azure OpenAI URL
* @returns {boolean}
*/
isAzureOpenai() {
return this.url && this.url.indexOf('openai.azure.com') !== -1;
}

/**
* Connects to Realtime API Websocket Server
* @param {{model?: string}} [settings]
Expand All @@ -73,11 +81,13 @@ export class RealtimeAPI extends RealtimeEventHandler {
);
}
const WebSocket = globalThis.WebSocket;
const ws = new WebSocket(`${this.url}${model ? `?model=${model}` : ''}`, [
'realtime',
`openai-insecure-api-key.${this.apiKey}`,
'openai-beta.realtime-v1',
]);
const ws = this.isAzureOpenai()
? new WebSocket(`${this.url.replace('https://','wss://')}${model ? `&api-key=${this.apiKey}` : ''}`)
: new WebSocket(`${this.url}${model ? `?model=${model}` : ''}`, [
'realtime',
`openai-insecure-api-key.${this.apiKey}`,
'openai-beta.realtime-v1',
]);
ws.addEventListener('message', (event) => {
const message = JSON.parse(event.data);
this.receive(message.type, message);
Expand Down