@@ -205,51 +205,6 @@ <h1>Chat with GPT-4 Realtime with WebRTC</h1>
205205 ] ;
206206
207207
208- /**
209- * Fetch an ephemeral key from OpenAI to start a Realtime session.
210- */
211- async function fetchEphemeralKey ( apiKey ) {
212- try {
213- const headers = {
214- "Authorization" : `Bearer ${ apiKey } ` ,
215- "Content-Type" : "application/json"
216- } ;
217- const payload = {
218- model : model ,
219- // expires_at: Math.floor(Date.now() / 1000) + 60, // #Fail - unknown parameter!
220- // load these later
221- // voice: voiceEl.value,
222- // input_audio_transcription: {model: "whisper-1"},
223- // instructions: sessionInstructionsEl.value
224- } ;
225-
226- const response = await fetch ( "https://api.openai.com/v1/realtime/sessions" , {
227- method : "POST" ,
228- headers : headers ,
229- body : JSON . stringify ( payload )
230- } ) ;
231-
232- if ( ! response . ok ) {
233- console . error ( "Failed to create ephemeral session:" , await response . text ( ) ) ;
234- return null ;
235- }
236- const data = await response . json ( ) ;
237- const token = data ?. client_secret ?. value ;
238- if ( ! token ) {
239- console . error ( "Invalid response format:" , data ) ;
240- return null ;
241- }
242- // For debugging - don't make this easy to abuse
243- console . log ( `Received ephemeral key: ${ token } will expire at ${ new Date ( data ?. client_secret ?. expires_at * 1000 )
244- . toLocaleString ( ) } `, data ) ;
245- return token ;
246- } catch ( error ) {
247- console . error ( "Error fetching ephemeral key:" , error ) ;
248- return error ;
249- }
250- }
251-
252-
253208 /**
254209 * Send initial session instructions and start message.
255210 * This is called when the data channel is opened.
@@ -353,13 +308,6 @@ <h1>Chat with GPT-4 Realtime with WebRTC</h1>
353308 return ;
354309 }
355310
356- // An ephemeral key is required to start a session
357- // Usually this should be requested from your server to avoid exposing the OPENAI_API_KEY
358- const ephemeralToken = await fetchEphemeralKey ( apiKey ) ;
359- if ( ! ephemeralToken || ephemeralToken === "error" ) {
360- toggleSessionButtons ( false ) ;
361- return ;
362- }
363311
364312 // Capture the local mic
365313 // technically optional for the API, but required in this example
@@ -406,7 +354,7 @@ <h1>Chat with GPT-4 Realtime with WebRTC</h1>
406354 method : "POST" ,
407355 body : pc . localDescription . sdp ,
408356 headers : {
409- Authorization : `Bearer ${ ephemeralToken } ` ,
357+ Authorization : `Bearer ${ apiKey } ` ,
410358 "Content-Type" : "application/sdp"
411359 } ,
412360 } ) ;
0 commit comments