forked from AwaisKamran/openai-realtime-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
68 lines (59 loc) · 1.75 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import dotenv from 'dotenv';
import WebSocket from "ws";
import readline from 'readline';
dotenv.config();
const RESPONSE_TYPE_DELTA = "response.text.delta"
const RESPONSE_TYPE_DONE = "response.text.done"
let data = ""
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
/* Callback method */
const startConversation = () => {
rl.question('Enter your prompt here: ', (prompt) => {
/* Send Client Event */
ws.send(JSON.stringify({
type: "response.create",
response: {
modalities: ["text"],
instructions: `You are a high school math tutor, help the user with this questions - ${prompt}.`,
}
}));
});
}
const incomingMessage = (message) => {
try{
const response = JSON.parse(message.toString());
if(response.type === RESPONSE_TYPE_DELTA) {
const { delta } = response
data += delta;
}
else if(response.type === RESPONSE_TYPE_DONE) {
console.log(data);
console.log("\n")
data = ""
startConversation()
}
}
catch(ex){
console.error(ex.toString)
}
}
/* Socket Initialization */
const url = "wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01";
const ws = new WebSocket(url, {
headers: {
"Authorization": "Bearer " + process.env.OPENAI_API_KEY,
"OpenAI-Beta": "realtime=v1",
"OpenAI-Project": process.env.PROJECT
},
});
ws.onerror = function (error) {
console.error('WebSocket Error: ', error.message);
};
ws.on("open", function open() {
console.log("Connected to server.");
startConversation()
});
ws.on("message", incomingMessage);