[mirotalk] - #219 keep chatGPT context

This commit is contained in:
Miroslav Pejic
2024-03-11 00:13:08 +01:00
parent 9ae149f344
commit 1c657de2a0
4 changed files with 35 additions and 21 deletions
+1 -1
View File
@@ -131,7 +131,7 @@ SLACK_SIGNING_SECRET=YourSlackSigningSecret
CHATGPT_ENABLED=false
CHATGPT_BASE_PATH=https://api.openai.com/v1/
CHATGTP_APIKEY=YourOpenAiApiKey
CHATGPT_MODEL=gpt-3.5-turbo-instruct
CHATGPT_MODEL=gpt-3.5-turbo
CHATGPT_MAX_TOKENS=1000
CHATGPT_TEMPERATURE=0
+21 -13
View File
@@ -39,7 +39,7 @@ dependencies: {
* @license For commercial use or closed source, contact us at license.mirotalk@gmail.com or purchase directly from CodeCanyon
* @license CodeCanyon: https://codecanyon.net/item/mirotalk-p2p-webrtc-realtime-video-conferences/38376661
* @author Miroslav Pejic - miroslav.pejic.85@gmail.com
* @version 1.2.97
* @version 1.2.98
*
*/
@@ -762,24 +762,32 @@ io.sockets.on('connect', async (socket) => {
break;
case 'getChatGPT':
// https://platform.openai.com/docs/introduction
if (!configChatGPT.enabled) return cb('ChatGPT seems disabled, try later!');
if (!configChatGPT.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: configChatGPT.model || 'gpt-3.5-turbo-instruct',
prompt: params.prompt,
const { time, prompt, context } = params;
// 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: configChatGPT.model || 'gpt-3.5-turbo',
messages: context,
max_tokens: configChatGPT.max_tokens || 1000,
temperature: configChatGPT.temperature || 0,
});
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: params.time,
time: time,
room: room_id,
name: peer_name,
prompt: params.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', {
@@ -789,11 +797,11 @@ io.sockets.on('connect', async (socket) => {
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 });
}
}
break;
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "mirotalk",
"version": "1.2.97",
"version": "1.2.98",
"description": "A free WebRTC browser-based video call",
"main": "server.js",
"scripts": {
@@ -37,8 +37,8 @@
"license": "AGPL-3.0",
"homepage": "https://github.com/miroslavpejic85/mirotalk",
"dependencies": {
"@sentry/integrations": "^7.105.0",
"@sentry/node": "^7.105.0",
"@sentry/integrations": "^7.106.0",
"@sentry/node": "^7.106.0",
"axios": "^1.6.7",
"body-parser": "^1.20.2",
"colors": "^1.4.0",
+10 -4
View File
@@ -15,7 +15,7 @@
* @license For commercial use or closed source, contact us at license.mirotalk@gmail.com or purchase directly from CodeCanyon
* @license CodeCanyon: https://codecanyon.net/item/mirotalk-p2p-webrtc-realtime-video-conferences/38376661
* @author Miroslav Pejic - miroslav.pejic.85@gmail.com
* @version 1.2.97
* @version 1.2.98
*
*/
@@ -647,6 +647,7 @@ let speechInMessages = false;
let isSpeechSynthesisSupported = 'speechSynthesis' in window;
let transcripts = []; // collect all the transcripts to save it later if you need
let chatMessages = []; // collect chat messages to save it later if want
let chatGPTcontext = []; // keep chatGPT messages context
// settings
let videoMaxFrameRate = 30;
@@ -6653,8 +6654,10 @@ function cleanMessages() {
msgerChat.removeChild(msgs);
msgs = msgerChat.firstChild;
}
// clean object
// clean chat messages
chatMessages = [];
// clean chatGPT context
chatGPTcontext = [];
playSound('delete');
}
});
@@ -7364,15 +7367,18 @@ async function getChatGPTmessage(msg) {
params: {
time: getDataTimeString(),
prompt: msg,
context: chatGPTcontext,
},
})
.then(
function (completion) {
if (!completion) return;
const { message, context } = completion;
chatGPTcontext = context ? context : [];
setPeerChatAvatarImgName('left', 'ChatGPT');
appendMessage('ChatGPT', leftChatAvatar, 'left', completion, true);
appendMessage('ChatGPT', leftChatAvatar, 'left', message, true);
cleanMessageInput();
speechInMessages ? speechMessage(true, 'ChatGPT', completion) : playSound('message');
speechInMessages ? speechMessage(true, 'ChatGPT', message) : playSound('message');
}.bind(this),
)
.catch((err) => {