-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.js
192 lines (162 loc) · 5.66 KB
/
server.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const express = require("express");
const http = require("http");
const {getGroqChat} = require('./models/groq');
const WebSocket = require("ws");
const fs = require('fs');
const { createClient, LiveTranscriptionEvents } = require("@deepgram/sdk");
const dotenv = require("dotenv");
const {play , initialize} = require('./models/playht');
// const {neets} = require('./models/neets');
dotenv.config();
let stack = [{
'role': 'system',
'content': 'Always maintain short and interactive conversations. Always assist with care, respect, and truth. Respond with utmost utility yet securely. Avoid harmful, unethical, prejudiced, or negative content. Ensure replies promote fairness and positivity.'
}];
let keepAlive;
let count=0;
let sid1=0;
let sid2=0;
let pl1=0;
let pl2=0;
if(!process.env.DEEPGRAM_API_KEY && !process.env.GROQ_API_KEY && !process.env.PLAY_API_KEY && !process.env.PLAY_USERID){
console.error('Please provide all the required keys in the .env file')
process.exit(1);
}
const app = express();
const server = http.createServer(app)
const wss = new WebSocket.Server({ server });
const deepgramClient = createClient(process.env.DEEPGRAM_API_KEY);
function log(message) {
let text = new Date().toISOString() + " : " + message;
fs.appendFile('./logs.txt', '\n'+text+'\n', (result)=> { console.log(result)});
}
initialize()
const setupDeepgram = (ws) => {
async function playh(responseText){
//calling playht main fxn
console.time('play_api')
const stream = await play(responseText)
pl2++
sid2++
ws.send(JSON.stringify({'type': 'audio_session', 'sid1': sid1, 'sid2': sid2}));
//added
if( pl1 === pl2){
play_stream(stream)
}
}
function play_stream(stream){
stream.on("data", (chunk) => {
const buffer = Uint8Array.from(chunk).buffer;
ws.send(JSON.stringify({
'type': 'audio',
'output': Array.from(new Uint8Array(buffer)),
'sid1': sid1,
'sid2': sid2
}));
});
console.timeEnd('play_api')
}
const deepgram = deepgramClient.listen.live({
language: "en",
punctuate: true,
smart_format: true,
model: "nova-2-phonecall",
endpointing: 400
});
if (keepAlive) clearInterval(keepAlive);
keepAlive = setInterval(() => {
deepgram.keepAlive();
}, 10 * 1000);
//when deepgram is open
deepgram.addListener(LiveTranscriptionEvents.Open, async () => {
console.log("deepgram: connected");
//deepgram outputs transcripts
deepgram.addListener(LiveTranscriptionEvents.Transcript, async (data) => {
if (data.is_final && data.channel.alternatives[0].transcript !== "") {
if(count>0){
if(sid1 !== sid2){
console.log('stopping the audio')
ws.send(JSON.stringify({'type': 'audio_stop', 'stop': true}));
}}
count++
sid1 = count
pl1++
ws.send(JSON.stringify({'type': 'audio_session', 'sid1': sid1 }));
const words = data.channel.alternatives[0].words;
const caption = words
.map((word) => word.punctuated_word ?? word.word)
.join(" ");
console.log(caption)
log(`deepgram_spoken: ${caption}`)
ws.send(JSON.stringify({'type': 'caption', 'output': JSON.stringify(caption)}));
const regex = /disconnect/i;
if (regex.test(caption)) {
ws.send(JSON.stringify({'type': 'caption', 'output': JSON.stringify('#assistant stopped#')}));
deepgram.finish();
ws.close();
}
else {
const responseText = await getGroqChat(caption, stack);
log(`groq response: ${responseText}`)
await playh(responseText)
// await neets(responseText)
}
}
});
deepgram.addListener(LiveTranscriptionEvents.Close, async () => {
console.log("deepgram: disconnected");
log('deepgram: disconnected')
clearInterval(keepAlive);
deepgram.finish();
});
deepgram.addListener(LiveTranscriptionEvents.Error, async (error) => {
console.log("deepgram: error received");
console.error(error);
});
deepgram.addListener(LiveTranscriptionEvents.Warning, async (warning) => {
console.log("deepgram: warning received");
console.warn(warning);
});
deepgram.addListener(LiveTranscriptionEvents.Metadata, (data) => {
console.log("deepgram: packet received");
console.log("deepgram: metadata received");
console.log("ws: metadata sent to client");
ws.send(JSON.stringify({ metadata: data }));
});
});
return deepgram;
};
wss.on("connection", (ws) => {
console.log("socket: client connected");
log('socket: client connected')
let deepgram = setupDeepgram(ws);
ws.on("message", (message) => {
if (deepgram.getReadyState() === 1 /* OPEN */) {
deepgram.send(message);
} else if (deepgram.getReadyState() >= 2 /* 2 = CLOSING, 3 = CLOSED */) {
console.log("socket: data couldn't be sent to deepgram");
console.log("socket: retrying connection to deepgram");
log('reattempting to send data')
/* Attempt to reopen the Deepgram connection */
deepgram.finish();
deepgram.removeAllListeners();
deepgram = setupDeepgram(socket);
} else {
console.log("socket: data couldn't be sent to deepgram");
}
});
ws.on("close", () => {
console.log("socket: client disconnected");
log('socket: client disconnected')
deepgram.finish();
deepgram.removeAllListeners();
deepgram = null;
});
});
app.use(express.static("public/"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
server.listen(3000, () => {
console.log("Server is listening on port 3000");
});