|
| 1 | +## Realtime API beta |
| 2 | + |
| 3 | +The Realtime API enables you to build low-latency, multi-modal conversational experiences. It currently supports text and audio as both input and output, as well as [function calling](https://platform.openai.com/docs/guides/function-calling) through a `WebSocket` connection. |
| 4 | + |
| 5 | +The Realtime API works through a combination of client-sent events and server-sent events. Clients can send events to do things like update session configuration or send text and audio inputs. Server events confirm when audio responses have completed, or when a text response from the model has been received. A full event reference can be found [here](https://platform.openai.com/docs/api-reference/realtime-client-events) and a guide can be found [here](https://platform.openai.com/docs/guides/realtime). |
| 6 | + |
| 7 | +This SDK supports accessing the Realtime API through the [WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) or with [ws](https://github.com/websockets/ws). |
| 8 | + |
| 9 | +Basic text based example with `ws`: |
| 10 | + |
| 11 | +```ts |
| 12 | +// requires `yarn add ws @types/ws` |
| 13 | +import { OpenAIRealtimeWS } from 'openai/beta/realtime/ws'; |
| 14 | + |
| 15 | +const rt = new OpenAIRealtimeWS({ model: 'gpt-4o-realtime-preview-2024-12-17' }); |
| 16 | + |
| 17 | +// access the underlying `ws.WebSocket` instance |
| 18 | +rt.socket.on('open', () => { |
| 19 | + console.log('Connection opened!'); |
| 20 | + rt.send({ |
| 21 | + type: 'session.update', |
| 22 | + session: { |
| 23 | + modalities: ['text'], |
| 24 | + model: 'gpt-4o-realtime-preview', |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + rt.send({ |
| 29 | + type: 'conversation.item.create', |
| 30 | + item: { |
| 31 | + type: 'message', |
| 32 | + role: 'user', |
| 33 | + content: [{ type: 'input_text', text: 'Say a couple paragraphs!' }], |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + rt.send({ type: 'response.create' }); |
| 38 | +}); |
| 39 | + |
| 40 | +rt.on('error', (err) => { |
| 41 | + // in a real world scenario this should be logged somewhere as you |
| 42 | + // likely want to continue procesing events regardless of any errors |
| 43 | + throw err; |
| 44 | +}); |
| 45 | + |
| 46 | +rt.on('session.created', (event) => { |
| 47 | + console.log('session created!', event.session); |
| 48 | + console.log(); |
| 49 | +}); |
| 50 | + |
| 51 | +rt.on('response.text.delta', (event) => process.stdout.write(event.delta)); |
| 52 | +rt.on('response.text.done', () => console.log()); |
| 53 | + |
| 54 | +rt.on('response.done', () => rt.close()); |
| 55 | + |
| 56 | +rt.socket.on('close', () => console.log('\nConnection closed!')); |
| 57 | +``` |
| 58 | + |
| 59 | +To use the web API `WebSocket` implementation, replace `OpenAIRealtimeWS` with `OpenAIRealtimeWebSocket` and adjust any `rt.socket` access: |
| 60 | + |
| 61 | +```ts |
| 62 | +import { OpenAIRealtimeWebSocket } from 'openai/beta/realtime/websocket'; |
| 63 | + |
| 64 | +const rt = new OpenAIRealtimeWebSocket({ model: 'gpt-4o-realtime-preview-2024-12-17' }); |
| 65 | +// ... |
| 66 | +rt.socket.addEventListener('open', () => { |
| 67 | + // ... |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +A full example can be found [here](https://github.com/openai/openai-node/blob/master/examples/realtime/websocket.ts). |
| 72 | + |
| 73 | +### Realtime error handling |
| 74 | + |
| 75 | +When an error is encountered, either on the client side or returned from the server through the [`error` event](https://platform.openai.com/docs/guides/realtime-model-capabilities#error-handling), the `error` event listener will be fired. However, if you haven't registered an `error` event listener then an `unhandled Promise rejection` error will be thrown. |
| 76 | + |
| 77 | +It is **highly recommended** that you register an `error` event listener and handle errors approriately as typically the underlying connection is still usable. |
| 78 | + |
| 79 | +```ts |
| 80 | +const rt = new OpenAIRealtimeWS({ model: 'gpt-4o-realtime-preview-2024-12-17' }); |
| 81 | +rt.on('error', (err) => { |
| 82 | + // in a real world scenario this should be logged somewhere as you |
| 83 | + // likely want to continue procesing events regardless of any errors |
| 84 | + throw err; |
| 85 | +}); |
| 86 | +``` |
0 commit comments