From f8d4f001fb990167f9673967c94404a5d071d78f Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Fri, 14 Apr 2023 19:54:56 +0200 Subject: [PATCH] [mirotalksfu] - add chatGPT-OpenAI --- app/src/Server.js | 46 ++++++++++++++++++++++++++++++++++++++ app/src/config.template.js | 14 ++++++++++++ package.json | 1 + public/js/Room.js | 7 ++++++ public/js/RoomClient.js | 43 +++++++++++++++++++++++++++-------- public/js/Rules.js | 1 + public/js/SpeechRec.js | 10 +++++++++ public/views/Room.html | 5 ++++- 8 files changed, 117 insertions(+), 10 deletions(-) diff --git a/app/src/Server.js b/app/src/Server.js index 5beef782..74640c84 100644 --- a/app/src/Server.js +++ b/app/src/Server.js @@ -121,6 +121,20 @@ if (sentryEnabled) { */ } +// OpenAI/ChatGPT +let chatGPT; +if (config.chatGPT.enabled) { + if (config.chatGPT.apiKey) { + const { Configuration, OpenAIApi } = require('openai'); + const configuration = new Configuration({ + apiKey: config.chatGPT.apiKey, + }); + chatGPT = new OpenAIApi(configuration); + } else { + log.warning('ChatGPT seems enabled, but you missing the apiKey!'); + } +} + // directory const dir = { public: path.join(__dirname, '../../', 'public'), @@ -404,6 +418,8 @@ function startServer() { mediasoup_server_version: mediasoup.version, mediasoup_client_version: mediasoupClient.version, sentry_enabled: sentryEnabled, + slack_enabled: slackEnabled, + chatGPT_enabled: config.chatGPT.enabled, }); } catch (err) { log.error('Ngrok Start error: ', err.body); @@ -442,6 +458,8 @@ function startServer() { mediasoup_server_version: mediasoup.version, mediasoup_client_version: mediasoupClient.version, sentry_enabled: sentryEnabled, + slack_enabled: slackEnabled, + chatGPT_enabled: config.chatGPT.enabled, }); }); @@ -924,6 +942,34 @@ function startServer() { } }); + socket.on('getChatGPT', async ({ prompt }, cb) => { + if (!roomList.has(socket.room_id)) return; + if (!config.chatGPT.enabled) return cb('ChatGPT seems disabled, try later!'); + try { + // https://platform.openai.com/docs/api-reference/completions/create + const completion = await chatGPT.createCompletion({ + model: config.chatGPT.model || 'text-davinci-003', + prompt: prompt, + max_tokens: config.chatGPT.max_tokens, + temperature: config.chatGPT.temperature, + }); + const response = completion.data.choices[0].text; + log.debug('ChatGPT', { + prompt: prompt, + response: response, + }); + cb(response); + } catch (error) { + if (error.response) { + log.error('ChatGPT', error.response); + cb(error.response.data.error.message); + } else { + log.error('ChatGPT', error.message); + cb(error.message); + } + } + }); + socket.on('disconnect', () => { if (!roomList.has(socket.room_id)) return; diff --git a/app/src/config.template.js b/app/src/config.template.js index edd92110..c0e13eb8 100644 --- a/app/src/config.template.js +++ b/app/src/config.template.js @@ -70,6 +70,7 @@ module.exports = { }, slack: { /* + Slack 1. Goto https://api.slack.com/apps/ 2. Create your app 3. On Settings - Basic Information - App Credentials, chose your Signing Secret @@ -78,6 +79,19 @@ module.exports = { enabled: false, signingSecret: '', }, + chatGPT: { + /** + ChatGPT + 1. Goto https://platform.openai.com/ + 2. Create your account + 3. Generate your APIKey https://platform.openai.com/account/api-keys + */ + enabled: false, + apiKey: '', + model: 'text-davinci-003', + max_tokens: 1000, + temperature: 0, + }, mediasoup: { // Worker settings numWorkers: Object.keys(os.cpus()).length, diff --git a/package.json b/package.json index ceedd8cc..343c803d 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "mediasoup": "3.11.22", "mediasoup-client": "3.6.84", "ngrok": "^4.3.3", + "openai": "^3.2.1", "qs": "6.11.1", "socket.io": "4.6.1", "swagger-ui-express": "4.6.2", diff --git a/public/js/Room.js b/public/js/Room.js index f17853d4..80ea796f 100644 --- a/public/js/Room.js +++ b/public/js/Room.js @@ -93,6 +93,7 @@ let isParticipantsListOpen = false; let isVideoControlsOn = false; let isChatPasteTxt = false; let isChatMarkdownOn = false; +let isChatGPTOn = false; let joinRoomWithoutAudioVideo = true; let joinRoomWithScreen = false; let initAudioButton = null; @@ -181,6 +182,7 @@ function initClient() { setTippy('chatSpeechStopButton', 'Stop speech recognition', 'top'); setTippy('chatEmojiButton', 'Emoji', 'top'); setTippy('chatMarkdownButton', 'Markdown', 'top'); + setTippy('chatGPTButton', 'ChatGPT', 'top'); setTippy('chatShareFileButton', 'Share file', 'top'); setTippy('chatCleanButton', 'Clean', 'bottom'); setTippy('chatSaveButton', 'Save', 'bottom'); @@ -743,6 +745,7 @@ function roomIsReady() { !BUTTONS.chat.chatSaveButton && hide(chatSaveButton); BUTTONS.chat.chatEmojiButton && show(chatEmojiButton); BUTTONS.chat.chatMarkdownButton && show(chatMarkdownButton); + BUTTONS.chat.chatGPTButton && show(chatGPTButton); BUTTONS.chat.chatShareFileButton && show(chatShareFileButton); if (isWebkitSpeechRecognitionSupported && BUTTONS.chat.chatSpeechStartButton) { show(chatSpeechStartButton); @@ -953,6 +956,10 @@ function handleButtons() { isChatMarkdownOn = !isChatMarkdownOn; setColor(chatMarkdownButton, isChatMarkdownOn ? 'lime' : 'white'); }; + chatGPTButton.onclick = () => { + isChatGPTOn = !isChatGPTOn; + setColor(chatGPTButton, isChatGPTOn ? 'lime' : 'white'); + }; chatShareFileButton.onclick = () => { fileShareButton.click(); }; diff --git a/public/js/RoomClient.js b/public/js/RoomClient.js index cba65151..0c1d4ac3 100644 --- a/public/js/RoomClient.js +++ b/public/js/RoomClient.js @@ -2701,7 +2701,7 @@ class RoomClient { } sendMessage() { - if (!this.thereIsParticipants()) { + if (!this.thereIsParticipants() && !isChatGPTOn) { this.cleanMessage(); isChatPasteTxt = false; return this.userLog('info', 'No participants in the room', 'top-end'); @@ -2711,14 +2711,39 @@ class RoomClient { return this.cleanMessage(); } this.peer_name = filterXSS(this.peer_name); - let data = { - peer_name: this.peer_name, - peer_id: this.peer_id, - to_peer_id: 'all', - peer_msg: peer_msg, - }; - console.log('Send message:', data); - this.socket.emit('message', data); + if (isChatGPTOn) { + this.socket + .request('getChatGPT', { prompt: peer_msg }) + .then( + function (completion) { + console.log('Receive message:', completion); + this.setMsgAvatar('left', 'ChatGPT'); + this.appendMessage( + 'left', + this.leftMsgAvatar, + 'ChatGPT', + this.peer_id, + completion, + this.peer_id, + this.peer_name, + ); + this.cleanMessage(); + this.sound('message'); + }.bind(this), + ) + .catch((err) => { + console.log('ChatGPT error:', err); + }); + } else { + let data = { + peer_name: this.peer_name, + peer_id: this.peer_id, + to_peer_id: 'all', + peer_msg: peer_msg, + }; + console.log('Send message:', data); + this.socket.emit('message', data); + } this.setMsgAvatar('right', this.peer_name); this.appendMessage('right', this.rightMsgAvatar, this.peer_name, this.peer_id, peer_msg, 'all', 'all'); this.cleanMessage(); diff --git a/public/js/Rules.js b/public/js/Rules.js index a339dfaf..c94fd527 100644 --- a/public/js/Rules.js +++ b/public/js/Rules.js @@ -58,6 +58,7 @@ const BUTTONS = { chatSaveButton: true, chatEmojiButton: true, chatMarkdownButton: true, + chatGPTButton: true, chatShareFileButton: true, chatSpeechStartButton: true, }, diff --git a/public/js/SpeechRec.js b/public/js/SpeechRec.js index 96037200..938e6d85 100644 --- a/public/js/SpeechRec.js +++ b/public/js/SpeechRec.js @@ -23,6 +23,8 @@ const commands = { chatOn: 'open the chat', chatSend: 'send', chatOff: 'close the chat', + chatGPTOn: 'open chatGPT', + chatGPTOff: 'close chatGPT', whiteboardOn: 'open the whiteboard', whiteboardOff: 'close the whiteboard', recordingOn: 'start the recording', @@ -189,6 +191,14 @@ function execVoiceCommands(transcript) { printCommand(commands.whiteboardOn); whiteboardButton.click(); break; + case commands.chatGPTOn: + printCommand(commands.chatGPTOn); + chatGPTButton.click(); + break; + case commands.chatGPTOff: + printCommand(commands.chatGPTOff); + chatGPTButton.click(); + break; case commands.whiteboardOff: printCommand(commands.whiteboardOff); whiteboardCloseBtn.click(); diff --git a/public/views/Room.html b/public/views/Room.html index 1c1654bd..9fe46f2c 100644 --- a/public/views/Room.html +++ b/public/views/Room.html @@ -26,7 +26,7 @@ /> @@ -477,6 +477,9 @@ access to use this app. +