-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
141 lines (120 loc) · 3.63 KB
/
app.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const WebSocketServer = require("ws");
const { createClient, LiveTranscriptionEvents } = require("@deepgram/sdk");
const websocketServer = new WebSocketServer.Server({ server });
require("dotenv").config();
const twilio = require("twilio");
const { OpenAI } = require("openai");
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const deepgramApiKey = process.env.DEEPGRAM_API_KEY;
const VoiceResponse = twilio.twiml.VoiceResponse;
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const openaiApiKey = process.env.OPENAI_API_KEY;
const client = new twilio.Twilio(accountSid, authToken);
let gpt;
const twiml = new VoiceResponse();
websocketServer.on("connection", (ws) => {
console.log("new client connected");
const deepgram = createClient(deepgramApiKey);
const connection = deepgram.listen.live({
model: "nova-2",
smart_format: true,
encoding: "mulaw",
sample_rate: 8000,
channels: 1,
});
connection.on(LiveTranscriptionEvents.Open, () => {
connection.on(LiveTranscriptionEvents.Close, () => {
console.log("Connection closed.");
});
connection.on(LiveTranscriptionEvents.Transcript, async (data) => {
let dpres = data.channel.alternatives[0].transcript;
console.log(dpres);
gpt = dpres
? await gptResponse(data.channel.alternatives[0].transcript)
: null;
});
ws.on("message", (data) => {
const twilioMessage = JSON.parse(data);
if (
twilioMessage["event"] === "connected" ||
twilioMessage["event"] === "start"
) {
console.log("received a twilio connected or start event");
}
if (twilioMessage["event"] === "media") {
const media = twilioMessage["media"];
const audio = Buffer.from(media["payload"], "base64");
connection.send(audio);
}
});
ws.on("close", () => {
console.log("client has disconnected");
if (connection) {
connection.finish();
}
});
ws.onerror = function () {
console.log("some error occurred");
connection.finish();
};
});
});
let systemPrompt = `You're a customer support agent of an Ecommerce business, tone should be empathetic and try resolving the customer issues`;
let gptResponse = async (text) => {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: systemPrompt },
{
role: "user",
content: text,
},
],
temperature: 1,
});
let res = response.choices[0].message.content;
console.log(res);
return res;
};
function twilioSpeech(req, res) {
twiml.say(`${gpt}`);
// twiml.gather({
// input: 'speech',
// timeout: 3,
// action: '/handle-response',
// method: 'POST',
// });
// twiml.record({
// action: '/handle-recording',
// transcribe: true,
// transcribeCallback: '/transcript',
// });
res.type("text/xml");
res.send(twiml.toString());
}
app.use(express.static("public"));
app.post("/", (req, res) => {
res.set("Content-Type", "text/xml");
res.send(`
<Response>
<Start>
<Stream url="wss://${req.headers.host}/"/>
</Start>
<Say>I will stream the next 60 seconds of audio through your websocket</Say>
<Pause length="60" />
</Response>
`);
twiml.gather({
input: "speech",
timeout: 3,
action: "/speech",
method: "POST",
});
});
app.post("/speech", twilioSpeech);
console.log("Listening on Port 8080");
server.listen(8080);