forked from simliai/create-simli-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
263 lines (223 loc) · 8.31 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const express = require("express");
const http = require("http");
const WebSocket = require("ws");
const dotenv = require("dotenv");
const axios = require("axios");
const url = require('url');
dotenv.config();
dotenv.config({ path: `.env.local`, override: true });
const app = express();
const cors = require('cors');
app.use(cors());
app.use(express.json());
const server = http.createServer(app);
const { createClient, LiveTranscriptionEvents } = require("@deepgram/sdk");
const deepgramClient = createClient(process.env.NEXT_PUBLIC_DEEPGRAM_API_KEY);
const Groq = require('groq-sdk');
const groq = new Groq(process.env.GROQ_API_KEY);
console.log("Deepgram API key:", process.env.NEXT_PUBLIC_DEEPGRAM_API_KEY ? `Set: ${process.env.NEXT_PUBLIC_DEEPGRAM_API_KEY}` : "Not set");
console.log("Groq API key:", process.env.GROQ_API_KEY ? `Set: ${process.env.GROQ_API_KEY}` : "Not set");
console.log("ElevenLabs API key:", process.env.NEXT_PUBLIC_ELEVENLABS_API_KEY ? `Set: ${process.env.NEXT_PUBLIC_ELEVENLABS_API_KEY}` : "Not set");
console.log("Simli API key:", process.env.NEXT_PUBLIC_SIMLI_API_KEY ? `Set: ${process.env.NEXT_PUBLIC_SIMLI_API_KEY}` : "Not set");
// Connection manager to keep track of active connections
const connections = new Map();
app.post('/start-conversation', (req, res) => {
const { prompt, voiceId } = req.body;
if (!prompt || !voiceId) {
return res.status(400).json({ error: 'Prompt and voiceId are required' });
}
const connectionId = Date.now().toString();
connections.set(connectionId, { prompt, voiceId });
res.json({ connectionId, message: 'Conversation started. Connect to WebSocket to continue.' });
});
const wss = new WebSocket.Server({ noServer: true });
server.on('upgrade', (request, socket, head) => {
const { pathname, query } = url.parse(request.url, true);
if (pathname === '/ws') {
const connectionId = query.connectionId;
if (!connectionId || !connections.has(connectionId)) {
socket.destroy();
return;
}
wss.handleUpgrade(request, socket, head, (ws) => {
const connection = connections.get(connectionId);
console.log(`WebSocket: Client connected (ID: ${connectionId})`);
setupWebSocket(ws, connection.prompt, connection.voiceId, connectionId);
});
} else {
socket.destroy();
}
});
function setupWebSocket(ws, initialPrompt, voiceId, connectionId) {
let is_finals = [];
let audioQueue = [];
let keepAlive;
const deepgram = deepgramClient.listen.live({
model: "nova-2",
language: "en",
smart_format: true,
no_delay: true,
interim_results: true,
endpointing: 300,
utterance_end_ms: 1000
});
deepgram.addListener(LiveTranscriptionEvents.Open, () => {
console.log(`Deepgram STT: Connected (ID: ${connectionId})`);
while (audioQueue.length > 0) {
const audioData = audioQueue.shift();
deepgram.send(audioData);
}
});
deepgram.addListener(LiveTranscriptionEvents.Transcript, (data) => {
const transcript = data.channel.alternatives[0].transcript;
if (transcript !== "") {
if (data.is_final) {
is_finals.push(transcript);
if (data.speech_final) {
const utterance = is_finals.join(" ");
is_finals = [];
console.log(`Deepgram STT: [Speech Final] ${utterance} (ID: ${connectionId})`);
promptLLM(ws, initialPrompt, utterance, voiceId, connectionId);
} else {
console.log(`Deepgram STT: [Is Final] ${transcript} (ID: ${connectionId})`);
}
} else {
console.log(`Deepgram STT: [Interim Result] ${transcript} (ID: ${connectionId})`);
}
}
});
deepgram.addListener(LiveTranscriptionEvents.UtteranceEnd, () => {
if (is_finals.length > 0) {
const utterance = is_finals.join(" ");
is_finals = [];
console.log(`Deepgram STT: [Speech Final] ${utterance} (ID: ${connectionId})`);
promptLLM(ws, utterance, voiceId, connectionId);
}
});
deepgram.addListener(LiveTranscriptionEvents.Close, () => {
console.log(`Deepgram STT: Disconnected (ID: ${connectionId})`);
clearInterval(keepAlive);
deepgram.removeAllListeners();
});
deepgram.addListener(LiveTranscriptionEvents.Error, (error) => {
console.error(`Deepgram STT error (ID: ${connectionId}):`, error);
});
ws.on("message", (message) => {
console.log(`WebSocket: Client data received (ID: ${connectionId})`, typeof message, message.length, "bytes");
if (deepgram.getReadyState() === 1) {
deepgram.send(message);
} else {
console.log(`WebSocket: Data queued for Deepgram. Current state: ${deepgram.getReadyState()} (ID: ${connectionId})`);
audioQueue.push(message);
}
});
ws.on("close", () => {
console.log(`WebSocket: Client disconnected (ID: ${connectionId})`);
clearInterval(keepAlive);
deepgram.removeAllListeners();
connections.delete(connectionId);
});
keepAlive = setInterval(() => {
deepgram.keepAlive();
}, 10 * 1000);
connections.set(connectionId, { ...connections.get(connectionId), ws, deepgram });
}
async function promptLLM(ws,initialPrompt, prompt, voiceId, connectionId) {
try {
const chatCompletion = await groq.chat.completions.create({
messages: [
{
role: 'assistant',
content: initialPrompt
},
{
role: 'user',
content: prompt
}
],
model: "llama3-8b-8192",
temperature: 1,
max_tokens: 50,
top_p: 1,
stream: true,
stop: null
});
let fullResponse = '';
let elevenLabsWs = null;
for await (const chunk of chatCompletion) {
if (!connections.has(connectionId)) {
console.log(`LLM process stopped: Connection ${connectionId} no longer exists`);
break;
}
const chunkMessage = chunk.choices[0]?.delta?.content || '';
fullResponse += chunkMessage;
ws.send(JSON.stringify({ type: 'text', content: chunkMessage }));
if (!elevenLabsWs && fullResponse.length > 0) {
elevenLabsWs = await startElevenLabsStreaming(ws, voiceId, connectionId);
}
if (elevenLabsWs && chunkMessage) {
const contentMessage = {
text: chunkMessage,
try_trigger_generation: true,
};
elevenLabsWs.send(JSON.stringify(contentMessage));
}
}
if (elevenLabsWs) {
elevenLabsWs.send(JSON.stringify({ text: "", try_trigger_generation: true }));
}
} catch (error) {
console.error(`Error in promptLLM (ID: ${connectionId}):`, error);
}
}
async function startElevenLabsStreaming(ws, voiceId, connectionId) {
return new Promise((resolve, reject) => {
const elevenLabsWs = new WebSocket(`wss://api.elevenlabs.io/v1/text-to-speech/${voiceId}/stream-input?model_id=eleven_turbo_v2_5&output_format=pcm_16000`);
elevenLabsWs.on('open', () => {
console.log(`Connected to ElevenLabs WebSocket (ID: ${connectionId})`);
const initialMessage = {
text: " ",
voice_settings: {
stability: 0.5,
similarity_boost: 0.5
},
xi_api_key: process.env.NEXT_PUBLIC_ELEVENLABS_API_KEY,
};
elevenLabsWs.send(JSON.stringify(initialMessage));
resolve(elevenLabsWs);
});
elevenLabsWs.on('message', (data) => {
if (!connections.has(connectionId)) {
console.log(`ElevenLabs process stopped: Connection ${connectionId} no longer exists`);
elevenLabsWs.close();
return;
}
const message = JSON.parse(data);
if (message.audio) {
const audioData = Buffer.from(message.audio, 'base64');
const chunkSize = 5 * 1024; // 5KB
let i = 0;
while (i < audioData.length) {
const end = Math.min(i + chunkSize, audioData.length);
const chunk = audioData.slice(i, end);
ws.send(chunk);
i += chunkSize;
}
} else if (message.isFinal) {
console.log(`ElevenLabs streaming completed (ID: ${connectionId})`);
elevenLabsWs.close();
}
});
elevenLabsWs.on('error', (error) => {
console.error(`ElevenLabs WebSocket error (ID: ${connectionId}):`, error);
reject(error);
});
elevenLabsWs.on('close', () => {
console.log(`ElevenLabs WebSocket closed (ID: ${connectionId})`);
});
});
}
const port = 8080;
server.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});