@@ -97,52 +97,48 @@ Basic text based example with `ws`:
9797// requires `yarn add ws @types/ws`
9898import { OpenAIRealtimeWS } from ' openai/beta/realtime/ws' ;
9999
100- async function main() {
101- const rt = new OpenAIRealtimeWS ({ model: ' gpt-4o-realtime-preview-2024-12-17' });
102-
103- // access the underlying `ws.WebSocket` instance
104- rt .socket .on (' open' , () => {
105- console .log (' Connection opened!' );
106- rt .send ({
107- type: ' session.update' ,
108- session: {
109- modalities: [' text' ],
110- model: ' gpt-4o-realtime-preview' ,
111- },
112- });
113-
114- rt .send ({
115- type: ' conversation.item.create' ,
116- item: {
117- type: ' message' ,
118- role: ' user' ,
119- content: [{ type: ' input_text' , text: ' Say a couple paragraphs!' }],
120- },
121- });
100+ const rt = new OpenAIRealtimeWS ({ model: ' gpt-4o-realtime-preview-2024-12-17' });
122101
123- rt .send ({ type: ' response.create' });
102+ // access the underlying `ws.WebSocket` instance
103+ rt .socket .on (' open' , () => {
104+ console .log (' Connection opened!' );
105+ rt .send ({
106+ type: ' session.update' ,
107+ session: {
108+ modalities: [' text' ],
109+ model: ' gpt-4o-realtime-preview' ,
110+ },
124111 });
125112
126- rt .on (' error' , (err ) => {
127- // in a real world scenario this should be logged somewhere as you
128- // likely want to continue procesing events regardless of any errors
129- throw err ;
113+ rt .send ({
114+ type: ' conversation.item.create' ,
115+ item: {
116+ type: ' message' ,
117+ role: ' user' ,
118+ content: [{ type: ' input_text' , text: ' Say a couple paragraphs!' }],
119+ },
130120 });
131121
132- rt .on (' session.created' , (event ) => {
133- console .log (' session created!' , event .session );
134- console .log ();
135- });
122+ rt .send ({ type: ' response.create' });
123+ });
136124
137- rt .on (' response.text.delta' , (event ) => process .stdout .write (event .delta ));
138- rt .on (' response.text.done' , () => console .log ());
125+ rt .on (' error' , (err ) => {
126+ // in a real world scenario this should be logged somewhere as you
127+ // likely want to continue procesing events regardless of any errors
128+ throw err ;
129+ });
130+
131+ rt .on (' session.created' , (event ) => {
132+ console .log (' session created!' , event .session );
133+ console .log ();
134+ });
139135
140- rt .on (' response.done' , () => rt .close ());
136+ rt .on (' response.text.delta' , (event ) => process .stdout .write (event .delta ));
137+ rt .on (' response.text.done' , () => console .log ());
141138
142- rt .socket .on (' close' , () => console .log (' \n Connection closed!' ));
143- }
139+ rt .on (' response.done' , () => rt .close ());
144140
145- main ( );
141+ rt . socket . on ( ' close ' , () => console . log ( ' \n Connection closed! ' ) );
146142```
147143
148144To use the web API ` WebSocket ` implementation, replace ` OpenAIRealtimeWS ` with ` OpenAIRealtimeWebSocket ` and adjust any ` rt.socket ` access:
0 commit comments