diff --git a/app/src/Server.js b/app/src/Server.js index 7c61d3ab..64f743cb 100644 --- a/app/src/Server.js +++ b/app/src/Server.js @@ -41,7 +41,7 @@ dependencies: { * @license For commercial or closed source, contact us at license.mirotalk@gmail.com or purchase directly via CodeCanyon * @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970 * @author Miroslav Pejic - miroslav.pejic.85@gmail.com - * @version 1.3.91 + * @version 1.3.92 * */ @@ -1593,26 +1593,33 @@ function startServer() { : room.sendTo(data.to_peer_id, 'message', data); }); - socket.on('getChatGPT', async ({ time, room, name, prompt }, cb) => { + socket.on('getChatGPT', async ({ time, room, name, prompt, context }, cb) => { if (!roomList.has(socket.room_id)) return; - if (!config.chatGPT.enabled) return cb('ChatGPT seems disabled, try later!'); + if (!config.chatGPT.enabled) return cb({ message: 'ChatGPT seems disabled, try later!' }); + // https://platform.openai.com/docs/api-reference/completions/create try { - // https://platform.openai.com/docs/api-reference/completions/create - const completion = await chatGPT.completions.create({ - model: config.chatGPT.model || 'gpt-3.5-turbo-instruct', - prompt: prompt, + // Add the prompt to the context + context.push({ role: 'user', content: prompt }); + // Call OpenAI's API to generate response + const completion = await chatGPT.chat.completions.create({ + model: config.chatGPT.model || 'gpt-3.5-turbo', + messages: context, max_tokens: config.chatGPT.max_tokens, temperature: config.chatGPT.temperature, }); - const response = completion.choices[0].text; + // Extract message from completion + const message = completion.choices[0].message.content.trim(); + // Add response to context + context.push({ role: 'assistant', content: message }); + // Log conversation details log.info('ChatGPT', { time: time, room: room, name: name, - prompt: prompt, - response: response, + context: context, }); - cb(response); + // Callback response to client + cb({ message: message, context: context }); } catch (error) { if (error.name === 'APIError') { log.error('ChatGPT', { @@ -1622,11 +1629,11 @@ function startServer() { code: error.code, type: error.type, }); - cb(error.message); + cb({ message: error.message }); } else { // Non-API error log.error('ChatGPT', error); - cb(error.message); + cb({ message: error.message }); } } }); diff --git a/package.json b/package.json index 7185f61d..1d80c869 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mirotalksfu", - "version": "1.3.91", + "version": "1.3.92", "description": "WebRTC SFU browser-based video calls", "main": "Server.js", "scripts": { diff --git a/public/js/Room.js b/public/js/Room.js index c1dbbfcb..8080ef11 100644 --- a/public/js/Room.js +++ b/public/js/Room.js @@ -11,7 +11,7 @@ if (location.href.substr(0, 5) !== 'https') location.href = 'https' + location.h * @license For commercial or closed source, contact us at license.mirotalk@gmail.com or purchase directly via CodeCanyon * @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970 * @author Miroslav Pejic - miroslav.pejic.85@gmail.com - * @version 1.3.91 + * @version 1.3.92 * */ diff --git a/public/js/RoomClient.js b/public/js/RoomClient.js index 5f3bd2ba..65dde33c 100644 --- a/public/js/RoomClient.js +++ b/public/js/RoomClient.js @@ -9,7 +9,7 @@ * @license For commercial or closed source, contact us at license.mirotalk@gmail.com or purchase directly via CodeCanyon * @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970 * @author Miroslav Pejic - miroslav.pejic.85@gmail.com - * @version 1.3.91 + * @version 1.3.92 * */ @@ -238,6 +238,7 @@ class RoomClient { this.camera = 'user'; this.videoQualitySelectedIndex = 0; + this.chatGPTContext = []; this.chatMessages = []; this.leftMsgAvatar = null; this.rightMsgAvatar = null; @@ -3511,22 +3512,17 @@ class RoomClient { room: this.room_id, name: this.peer_name, prompt: peer_msg, + context: this.chatGPTContext, }) .then((completion) => { if (!completion) return; - console.log('Receive message:', completion); + const { message, context } = completion; + this.chatGPTContext = context ? context : []; + console.log('Receive message:', message); this.setMsgAvatar('right', 'ChatGPT'); - this.appendMessage( - 'right', - image.chatgpt, - 'ChatGPT', - this.peer_id, - completion, - 'ChatGPT', - 'ChatGPT', - ); + this.appendMessage('right', image.chatgpt, 'ChatGPT', this.peer_id, message, 'ChatGPT', 'ChatGPT'); this.cleanMessage(); - this.speechInMessages ? this.speechMessage(true, 'ChatGPT', completion) : this.sound('message'); + this.speechInMessages ? this.speechMessage(true, 'ChatGPT', message) : this.sound('message'); }) .catch((err) => { console.log('ChatGPT error:', err); @@ -3942,6 +3938,7 @@ class RoomClient { removeAllChildNodes(chatPublicMessages); removeAllChildNodes(chatPrivateMessages); this.chatMessages = []; + this.chatGPTContext = []; this.sound('delete'); } });