This is what I tried to implement but wasn’t able to use it:
const peerConnection = new RTCPeerConnection({});
const dataChannel = peerConnection.createDataChannel('oai-events', {
ordered: true,
});
const offer = await peerConnection.createOffer({});
await peerConnection.setLocalDescription(offer);
const sdpResponse = await fetch(`${OPENAI_BASE_URL}?model=${OPEN_AI_REALTIME_MODEL}`, {
method: 'POST',
body: offer.sdp,
headers: {
Authorization: `Bearer ${EPHEMERAL_KEY}`,
'Content-Type': 'application/sdp',
},
});
const answer = {
type: 'answer',
sdp: await sdpResponse.text(),
};
await peerConnection.setRemoteDescription(answer);
peerConnectionRef.current = peerConnection;
But what I get from the OpenAI Server is this error/response:
{
"type": "answer",
"sdp": "{\"error\":{\"message\":\"Invalid SDP offer. Offer did not have an audio media section.\",\"type\":\"invalid_request_error\",\"param\":null,\"code\":\"invalid_offer\"}}"
}
Only thing that I need is a text communication through the WebRTC protocol.
Sending a message like the below code:
const event = {
type: 'conversation.item.create',
item: {
type: 'message',
role: 'user',
content: [
{
type: 'input_text',
text: testCounter.current.toString(),
},
],
},
};
dataChannel?.send(JSON.stringify(event));
and parsing the response like this:
dataChannel.addEventListener('message', (event) => {
try {
const message = JSON.parse(event.data);
if (message.type === 'response.done' && message?.response?.output[0]?.content[0]?.text) {
logger(message?.response?.output[0]?.content[0]?.text);
}
} catch (error) {
logger(error, 'Error in data channel message');
}
});
But I can not when I don’t put the audio in the connection flow.