forked from Barty-Bart/openai-realtime-api-voice-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
309 lines (268 loc) · 11.9 KB
/
index.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import Fastify from 'fastify';
import WebSocket from 'ws';
import fs from 'fs';
import dotenv from 'dotenv';
import fastifyFormBody from '@fastify/formbody';
import fastifyWs from '@fastify/websocket';
import fetch from 'node-fetch';
// Load environment variables from .env file
dotenv.config();
// Retrieve the OpenAI API key from environment variables
const { OPENAI_API_KEY } = process.env;
if (!OPENAI_API_KEY) {
console.error('Missing OpenAI API key. Please set it in the .env file.');
process.exit(1);
}
// Initialize Fastify
const fastify = Fastify();
fastify.register(fastifyFormBody);
fastify.register(fastifyWs);
// Constants
const SYSTEM_MESSAGE = 'You are an AI receptionist for Barts Automotive. Your job is to politely engage with the client and obtain their name, availability, and service/work required. Ask one question at a time. Do not ask for other contact information, and do not check availability, assume we are free. Ensure the conversation remains friendly and professional, and guide the user to provide these details naturally. If necessary, ask follow-up questions to gather the required information.';
const VOICE = 'alloy';
const PORT = process.env.PORT || 5050;
const WEBHOOK_URL = "<input your webhook URL here>";
// Session management
const sessions = new Map();
// List of Event Types to log to the console
const LOG_EVENT_TYPES = [
'response.content.done',
'rate_limits.updated',
'response.done',
'input_audio_buffer.committed',
'input_audio_buffer.speech_stopped',
'input_audio_buffer.speech_started',
'session.created',
'response.text.done',
'conversation.item.input_audio_transcription.completed'
];
// Root Route
fastify.get('/', async (request, reply) => {
reply.send({ message: 'Twilio Media Stream Server is running!' });
});
// Route for Twilio to handle incoming and outgoing calls
fastify.all('/incoming-call', async (request, reply) => {
console.log('Incoming call');
const twimlResponse = `<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Hi, you have called Bart's Automative Centre. How can we help?</Say>
<Connect>
<Stream url="wss://${request.headers.host}/media-stream" />
</Connect>
</Response>`;
reply.type('text/xml').send(twimlResponse);
});
// WebSocket route for media-stream
fastify.register(async (fastify) => {
fastify.get('/media-stream', { websocket: true }, (connection, req) => {
console.log('Client connected');
const sessionId = req.headers['x-twilio-call-sid'] || `session_${Date.now()}`;
let session = sessions.get(sessionId) || { transcript: '', streamSid: null };
sessions.set(sessionId, session);
const openAiWs = new WebSocket('wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01', {
headers: {
Authorization: `Bearer ${OPENAI_API_KEY}`,
"OpenAI-Beta": "realtime=v1"
}
});
const sendSessionUpdate = () => {
const sessionUpdate = {
type: 'session.update',
session: {
turn_detection: { type: 'server_vad' },
input_audio_format: 'g711_ulaw',
output_audio_format: 'g711_ulaw',
voice: VOICE,
instructions: SYSTEM_MESSAGE,
modalities: ["text", "audio"],
temperature: 0.8,
input_audio_transcription: {
"model": "whisper-1"
}
}
};
console.log('Sending session update:', JSON.stringify(sessionUpdate));
openAiWs.send(JSON.stringify(sessionUpdate));
};
// Open event for OpenAI WebSocket
openAiWs.on('open', () => {
console.log('Connected to the OpenAI Realtime API');
setTimeout(sendSessionUpdate, 250);
});
// Listen for messages from the OpenAI WebSocket
openAiWs.on('message', (data) => {
try {
const response = JSON.parse(data);
if (LOG_EVENT_TYPES.includes(response.type)) {
console.log(`Received event: ${response.type}`, response);
}
// User message transcription handling
if (response.type === 'conversation.item.input_audio_transcription.completed') {
const userMessage = response.transcript.trim();
session.transcript += `User: ${userMessage}\n`;
console.log(`User (${sessionId}): ${userMessage}`);
}
// Agent message handling
if (response.type === 'response.done') {
const agentMessage = response.response.output[0]?.content?.find(content => content.transcript)?.transcript || 'Agent message not found';
session.transcript += `Agent: ${agentMessage}\n`;
console.log(`Agent (${sessionId}): ${agentMessage}`);
}
if (response.type === 'session.updated') {
console.log('Session updated successfully:', response);
}
if (response.type === 'response.audio.delta' && response.delta) {
const audioDelta = {
event: 'media',
streamSid: session.streamSid,
media: { payload: Buffer.from(response.delta, 'base64').toString('base64') }
};
connection.send(JSON.stringify(audioDelta));
}
} catch (error) {
console.error('Error processing OpenAI message:', error, 'Raw message:', data);
}
});
// Handle incoming messages from Twilio
connection.on('message', (message) => {
try {
const data = JSON.parse(message);
switch (data.event) {
case 'media':
if (openAiWs.readyState === WebSocket.OPEN) {
const audioAppend = {
type: 'input_audio_buffer.append',
audio: data.media.payload
};
openAiWs.send(JSON.stringify(audioAppend));
}
break;
case 'start':
session.streamSid = data.start.streamSid;
console.log('Incoming stream has started', session.streamSid);
break;
default:
console.log('Received non-media event:', data.event);
break;
}
} catch (error) {
console.error('Error parsing message:', error, 'Message:', message);
}
});
// Handle connection close and log transcript
connection.on('close', async () => {
if (openAiWs.readyState === WebSocket.OPEN) openAiWs.close();
console.log(`Client disconnected (${sessionId}).`);
console.log('Full Transcript:');
console.log(session.transcript);
await processTranscriptAndSend(session.transcript, sessionId);
// Clean up the session
sessions.delete(sessionId);
});
// Handle WebSocket close and errors
openAiWs.on('close', () => {
console.log('Disconnected from the OpenAI Realtime API');
});
openAiWs.on('error', (error) => {
console.error('Error in the OpenAI WebSocket:', error);
});
});
});
fastify.listen({ port: PORT }, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server is listening on port ${PORT}`);
});
// Function to make ChatGPT API completion call with structured outputs
async function makeChatGPTCompletion(transcript) {
console.log('Starting ChatGPT API call...');
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${OPENAI_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: "gpt-4o-2024-08-06",
messages: [
{ "role": "system", "content": "Extract customer details: name, availability, and any special notes from the transcript." },
{ "role": "user", "content": transcript }
],
response_format: {
"type": "json_schema",
"json_schema": {
"name": "customer_details_extraction",
"schema": {
"type": "object",
"properties": {
"customerName": { "type": "string" },
"customerAvailability": { "type": "string" },
"specialNotes": { "type": "string" }
},
"required": ["customerName", "customerAvailability", "specialNotes"]
}
}
}
})
});
console.log('ChatGPT API response status:', response.status);
const data = await response.json();
console.log('Full ChatGPT API response:', JSON.stringify(data, null, 2));
return data;
} catch (error) {
console.error('Error making ChatGPT completion call:', error);
throw error;
}
}
// Function to send data to Make.com webhook
async function sendToWebhook(payload) {
console.log('Sending data to webhook:', JSON.stringify(payload, null, 2));
try {
const response = await fetch(WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
console.log('Webhook response status:', response.status);
if (response.ok) {
console.log('Data successfully sent to webhook.');
} else {
console.error('Failed to send data to webhook:', response.statusText);
}
} catch (error) {
console.error('Error sending data to webhook:', error);
}
}
// Main function to extract and send customer details
async function processTranscriptAndSend(transcript, sessionId = null) {
console.log(`Starting transcript processing for session ${sessionId}...`);
try {
// Make the ChatGPT completion call
const result = await makeChatGPTCompletion(transcript);
console.log('Raw result from ChatGPT:', JSON.stringify(result, null, 2));
if (result.choices && result.choices[0] && result.choices[0].message && result.choices[0].message.content) {
try {
const parsedContent = JSON.parse(result.choices[0].message.content);
console.log('Parsed content:', JSON.stringify(parsedContent, null, 2));
if (parsedContent) {
// Send the parsed content directly to the webhook
await sendToWebhook(parsedContent);
console.log('Extracted and sent customer details:', parsedContent);
} else {
console.error('Unexpected JSON structure in ChatGPT response');
}
} catch (parseError) {
console.error('Error parsing JSON from ChatGPT response:', parseError);
}
} else {
console.error('Unexpected response structure from ChatGPT API');
}
} catch (error) {
console.error('Error in processTranscriptAndSend:', error);
}
}