[mirotalk] - add ChatGPT - OpenAI

This commit is contained in:
Miroslav Pejic
2023-04-15 10:36:44 +02:00
parent 4dcca03141
commit 0cd5116a4b
6 changed files with 121 additions and 9 deletions
+11
View File
@@ -64,3 +64,14 @@ SENTRY_TRACES_SAMPLE_RATE=1.0
SLACK_ENABLED=false # true or false
SLACK_SIGNING_SECRET=YourSlackSigningSecret
# ChatGPT/OpenAI
# 1. Goto https://platform.openai.com/
# 2. Create your account
# 3. Generate your APIKey https://platform.openai.com/account/api-keys
CHATGPT_ENABLED=false
CHATGTP_APIKEY=YourOpenAiApiKey
CHATGPT_MODEL=text-davinci-003
CHATGPT_MAX_TOKENS=1000
CHATGPT_TEMPERATURE=0
+1
View File
@@ -38,6 +38,7 @@
- Recording your Screen, Audio and Video
- Snapshot the video frame and save it as image png
- Chat with Emoji Picker to show you feeling, private messages, Markdown support, possibility to Save the conversations, and many more
- ChatGPT (openAI), designed to answer users' questions, provide relevant information, and connect them with relevant resources
- Speech recognition to send the speeches
- Push to talk, like a walkie-talkie.
- Advance collaborative whiteboard for the teachers
+56 -2
View File
@@ -19,6 +19,7 @@ dependencies: {
express : https://www.npmjs.com/package/express
ngrok : https://www.npmjs.com/package/ngrok
qs : https://www.npmjs.com/package/qs
openai : https://www.npmjs.com/package/openai
socket.io : https://www.npmjs.com/package/socket.io
swagger : https://www.npmjs.com/package/swagger-ui-express
uuid : https://www.npmjs.com/package/uuid
@@ -155,6 +156,27 @@ if (sentryEnabled) {
});
}
// OpenAI/ChatGPT
let chatGPT;
const configChatGPT = {
enabled: process.env.CHATGPT_ENABLED == 'true' || false,
apiKey: process.env.CHATGTP_APIKEY,
model: process.env.CHATGTP_MODEL,
max_tokens: parseInt(process.env.CHATGPT_MAX_TOKENS),
temperature: parseInt(process.env.CHATGPT_TEMPERATURE),
};
if (configChatGPT.enabled) {
if (configChatGPT.apiKey) {
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
apiKey: configChatGPT.apiKey,
});
chatGPT = new OpenAIApi(configuration);
} else {
log.warning('ChatGPT seems enabled, but you missing the apiKey!');
}
}
// directory
const dir = {
public: path.join(__dirname, '../../', 'public'),
@@ -465,6 +487,7 @@ async function ngrokStart() {
api_key_secret: api_key_secret,
use_self_signed_certificate: isHttps,
own_turn_enabled: turnEnabled,
chatGPT_enabled: configChatGPT.enabled,
slack_enabled: slackEnabled,
sentry_enabled: sentryEnabled,
survey_enabled: surveyEnabled,
@@ -511,6 +534,7 @@ server.listen(port, null, () => {
api_key_secret: api_key_secret,
use_self_signed_certificate: isHttps,
own_turn_enabled: turnEnabled,
chatGPT_enabled: configChatGPT.enabled,
slack_enabled: slackEnabled,
sentry_enabled: sentryEnabled,
survey_enabled: surveyEnabled,
@@ -569,9 +593,9 @@ io.sockets.on('connect', async (socket) => {
socket.on('data', async (data, cb) => {
log.debug('Socket Promise', data);
//...
const { room_id, peer_id, peer_name, type } = data;
const { room_id, peer_id, peer_name, method, params } = data;
switch (type) {
switch (method) {
case 'checkPeerName':
log.debug('Check if peer name exists', { peer_name: peer_name, room_id: room_id });
for (let id in peers[room_id]) {
@@ -582,6 +606,36 @@ 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!');
try {
// https://platform.openai.com/docs/api-reference/completions/create
const completion = await chatGPT.createCompletion({
model: configChatGPT.model || 'text-davinci-003',
prompt: params.prompt,
max_tokens: configChatGPT.max_tokens || 1000,
temperature: configChatGPT.temperature || 0,
});
const response = completion.data.choices[0].text;
log.debug('ChatGPT', {
time: params.time,
room: room_id,
name: peer_name,
prompt: params.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);
}
}
break;
//....
default:
cb(false);
+1
View File
@@ -43,6 +43,7 @@
"dotenv": "^16.0.3",
"express": "^4.18.2",
"ngrok": "^4.3.3",
"openai": "^3.2.1",
"qs": "^6.11.1",
"socket.io": "^4.6.1",
"swagger-ui-express": "^4.6.2",
+50 -6
View File
@@ -143,6 +143,7 @@ const buttons = {
chat: {
showSaveMessageBtn: true,
showMarkDownBtn: true,
showChatGPTBtn: true,
showFileShareBtn: true,
showShareVideoAudioBtn: true,
showParticipantsBtn: true,
@@ -266,6 +267,7 @@ let isChatRoomVisible = false;
let isCaptionBoxVisible = false;
let isChatEmojiVisible = false;
let isChatMarkdownOn = false;
let isChatGPTOn = false;
let isButtonsVisible = false;
let isButtonsBarOver = false;
let isMySettingsVisible = false;
@@ -331,6 +333,7 @@ let msgerClose;
let msgerChat;
let msgerEmojiBtn;
let msgerMarkdownBtn;
let msgerGPTBtn;
let msgerShareFileBtn;
let msgerInput;
let msgerCleanTextBtn;
@@ -509,6 +512,7 @@ function getHtmlElementsById() {
msgerChat = getId('msgerChat');
msgerEmojiBtn = getId('msgerEmojiBtn');
msgerMarkdownBtn = getId('msgerMarkdownBtn');
msgerGPTBtn = getId('msgerGPTBtn');
msgerShareFileBtn = getId('msgerShareFileBtn');
msgerInput = getId('msgerInput');
msgerCleanTextBtn = getId('msgerCleanTextBtn');
@@ -647,6 +651,7 @@ function setButtonsToolTip() {
setTippy(msgerClose, 'Close', 'right');
setTippy(msgerEmojiBtn, 'Emoji', 'top');
setTippy(msgerMarkdownBtn, 'Markdown', 'top');
setTippy(msgerGPTBtn, 'ChatGPT', 'top');
setTippy(msgerShareFileBtn, 'Share file', 'top');
setTippy(msgerCleanTextBtn, 'Clean', 'top');
setTippy(msgerPasteBtn, 'Paste', 'top');
@@ -1115,6 +1120,7 @@ function handleButtonsRule() {
// chat
elemDisplay(msgerSaveBtn, buttons.chat.showSaveMessageBtn);
elemDisplay(msgerMarkdownBtn, buttons.chat.showMarkDownBtn);
elemDisplay(msgerGPTBtn, buttons.chat.showChatGPTBtn);
elemDisplay(msgerShareFileBtn, buttons.chat.showFileShareBtn);
elemDisplay(msgerVideoUrlBtn, buttons.chat.showShareVideoAudioBtn);
elemDisplay(msgerCPBtn, buttons.chat.showParticipantsBtn);
@@ -1252,10 +1258,11 @@ async function whoAreYou() {
async function checkUserName(peer_name = null) {
return signalingSocket
.request('data', {
type: 'checkPeerName',
room_id: roomId,
peer_id: myPeerId,
peer_name: peer_name ? peer_name : myPeerName,
method: 'checkPeerName',
params: {},
})
.then((response) => response);
}
@@ -3373,6 +3380,12 @@ function setChatRoomBtn() {
setColor(msgerMarkdownBtn, isChatMarkdownOn ? 'lime' : 'white');
});
// ChatGPT/OpenAI
msgerGPTBtn.addEventListener('click', (e) => {
isChatGPTOn = !isChatGPTOn;
setColor(msgerGPTBtn, isChatGPTOn ? 'lime' : 'white');
});
// share file from chat
msgerShareFileBtn.addEventListener('click', (e) => {
e.preventDefault();
@@ -3429,10 +3442,10 @@ function setChatRoomBtn() {
});
// chat send msg
msgerSendBtn.addEventListener('click', (e) => {
msgerSendBtn.addEventListener('click', async (e) => {
// prevent refresh page
e.preventDefault();
sendChatMessage();
await sendChatMessage();
});
// adapt input font size 4 mobile
@@ -5024,8 +5037,8 @@ function hideCaptionBox() {
/**
* Send Chat messages to peers in the room
*/
function sendChatMessage() {
if (!thereIsPeerConnections()) {
async function sendChatMessage() {
if (!thereIsPeerConnections() && !isChatGPTOn) {
cleanMessageInput();
isChatPasteTxt = false;
return userLog('info', "Can't send message, no participants in the room");
@@ -5039,7 +5052,7 @@ function sendChatMessage() {
return cleanMessageInput();
}
emitMsg(myPeerName, 'toAll', msg, false, myPeerId);
isChatGPTOn ? await getChatGPTmessage(msg) : emitMsg(myPeerName, 'toAll', msg, false, myPeerId);
appendMessage(myPeerName, rightChatAvatar, 'right', msg, false);
cleanMessageInput();
}
@@ -5536,6 +5549,37 @@ function emitMsg(from, to, msg, privateMsg, id) {
sendToDataChannel(chatMessage);
}
/**
* Read ChatGPT incoming message
* https://platform.openai.com/docs/introduction
* @param {string} msg
*/
async function getChatGPTmessage(msg) {
console.log('Send ChatGPT message:', msg);
signalingSocket
.request('data', {
room_id: roomId,
peer_id: myPeerId,
peer_name: myPeerName,
method: 'getChatGPT',
params: {
time: getDataTimeString(),
prompt: msg,
},
})
.then(
function (completion) {
if (!completion) return;
appendMessage('ChatGPT', leftChatAvatar, 'left', completion, true);
cleanMessageInput();
playSound('message');
}.bind(this),
)
.catch((err) => {
console.log('ChatGPT error:', err);
});
}
/**
* Hide - Show emoji picker div
*/
+2 -1
View File
@@ -29,7 +29,7 @@
/>
<meta
name="keywords"
content="webrtc, webrtc stun, webrtc turn, video meeting, video chat, multi video chat, peer to peer, p2p, zoom"
content="webrtc, openAI, chatGPT, webrtc stun, webrtc turn, video meeting, video chat, multi video chat, peer to peer, p2p, zoom"
/>
<!-- https://ogp.me -->
@@ -156,6 +156,7 @@ access to use this app.
<div class="msger-input-buttons">
<button id="msgerEmojiBtn" class="fas fa-smile"></button>
<button id="msgerMarkdownBtn" class="fab fa-markdown"></button>
<button id="msgerGPTBtn" class="fas fa-robot"></button>
<button id="msgerShareFileBtn" class="fas fa-paperclip"></button>
<button id="msgerVideoUrlBtn" class="fab fa-youtube"></button>
<button id="msgerCPBtn" class="fas fa-users"></button>