-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnode_helper.js
148 lines (129 loc) · 4.13 KB
/
node_helper.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
const NodeHelper = require("node_helper");
const { Configuration, OpenAIApi } = require("openai");
const { Deepgram } = require("@deepgram/sdk");
const WebSocket = require("ws");
// const fetch = require("cross-fetch");
// const model = 'general';
// const lang = 'en-US';
// const DG_ENDPOINT = "wss://api.deepgram.com/v1/listen";
const deepgramApiKey = "";
// const deepgram = new Deepgram(deepgramApiKey);
// const deepgramLive = deepgram.transcription.live({
// punctuate: false,
// interim_results: false,
// language: "en-GB"
// });
let stt = "";
module.exports = NodeHelper.create({
start: function () {
console.log("Starting node helper for: " + this.name);
this.configuration = new Configuration({
apiKey: ""
});
this.openai = new OpenAIApi(this.configuration);
//console.log(this.openai);
// console.log("Starting transcription...");
// when the connection is established, send the configuration message
},
// Send a message to the chatGPT API and receive a response
getResponse: function (prompt) {
console.log("start function getREsponse call to chatGPT ",prompt);
return new Promise(async (resolve, reject) => {
try {
const response = await this.openai.createCompletion({
model: "text-davinci-003",
prompt: prompt,
max_tokens: 256,
temperature: 0.1
});
// console.log("Received response from API: ", response);
console.log("Received response from API: ",);
resolve(response);
} catch (error) {
console.log("Error in getResponse: ", error);
reject(error);
}
});
},
// Handle socket notifications
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case "OPENAI_REQUEST":
console.log(payload)
const prompt = payload;
this.getResponse(prompt)
.then(
function (response) {
const message = response.data.choices[0].text;
console.log(message)
this.sendSocketNotification("OPENAI_RESPONSE", { message: message });
}.bind(this)
)
.catch(function (error) {
console.error(error);
});
return;
case "TRANSCRIBE":
// var socket = null;
console.log("Audio", payload.audio_data);
console.log("Media", payload.mediaR)
console.log("Establishing connection.");
// Configure the websocket connection.
// This requires ws installed using 'npm i ws'.
socket = new WebSocket("wss://api.deepgram.com/v1/listen", {
// Replace with your Deepgram project's API Key.
headers: {
Authorization: `Token ${deepgramApiKey}`
}
});
//socket open
socket.onopen = () => {
console.log("Socket opened!");
// Grab an audio file.
var audio = payload.audio_data;
console.log(socket.readyState);
// Send the audio to the Deepgram API all at once (works if audio is relatively short).
// socket.send(contents);
if (socket.readyState == 1) {
console.log("sending Audio Blob to DG");
socket.send(audio);
}
};
socket.onmessage = (m) => {
//console.log({ event: 'onmessage', m });
const received = JSON.parse(m.data);
// Log the received message.
//console.log(m);
if (received.hasOwnProperty("channel") && received.is_final) {
const transcript = received.channel?.alternatives[0]?.transcript;
console.log(transcript);
stt += " " + transcript
// transcript.forEach((t, index) => {
// index !== 0 ? (stt += t.word) : (t += " " + transcript.word);
// });
// console.log(stt)
}
};
// if (typeof stt !== "undefined" && stt.length > 0) {
// this.sendSocketNotification("OPENAI_REQUEST", {
// prompt: stt
// });
// }
socket.onclose = (event) => {
console.log({ event: "onclose", code: event.code });
};
socket.onerror = (error) => {
console.log({ event: "onerror", error });
};
socket.on("upgrade", function message(data) {
console.log(data.headers["dg-request-id"]);
});
if (typeof stt !== "undefined" && stt.length > 0){
this.sendSocketNotification("TEXT_RESPONSE", { transcript: stt });
}
return;
default:
return;
}
}
});