';
let aiName = '
bigshift.bot:
';
let sysName = 'System:';
let env = 'chatbot';
let apiKey = '';
let session = 'N/A';
let mode = 'chat';
let model = 'text-davinci-003';
let context = isCasuallyFineTuned ? null : 'Converse as if you were an AI assistant. Be friendly, creative.';
let embeddingsIndex = '';
let promptEnding = '';
let stop = '';
let startSentence = 'Was kann ich Gutes für Dich tun?';
let maxSentences = 15;
let memorizeChat = false;
let maxTokens = 1024;
let maxResults = 1;
let temperature = 0;
let memorizedChat = [];
let typewriter = false;
let copyButton = true;
if (isDebugMode) {
window.mwai_66d178e1043c2 = {
memorizedChat: memorizedChat,
parameters: { mode: mode, model, temperature, maxTokens, context: context, startSentence,
isMobile, isWindow, isFullscreen, isCasuallyFineTuned, memorizeChat, maxSentences,
rawUserName, rawAiName, embeddingsIndex, typewriter, maxResults, userName, aiName, env, apiKey, session
}
};
}
// Set button text
function setButtonText() {
let input = document.querySelector('#mwai-chat-66d178e1043c2 .mwai-input textarea');
let button = document.querySelector('#mwai-chat-66d178e1043c2 .mwai-input button');
let buttonSpan = button.querySelector('span');
if (memorizedChat.length 0.5) {
timerElement = document.createElement('div');
timerElement.classList.add('mwai-timer');
element.appendChild(timerElement);
}
}
if (timerElement) {
let minutes = Math.floor(timer / 60);
let seconds = timer - (minutes * 60);
seconds = seconds ';
for (var i = 0; i
';
}
text = newText + '
';
}
var mwaiClasses = ['mwai-reply'];
if (role === 'assistant') {
mwaiClasses.push('mwai-ai');
}
else if (role === 'system') {
mwaiClasses.push('mwai-system');
}
else {
mwaiClasses.push('mwai-user');
}
var div = document.createElement('div');
div.classList.add(...mwaiClasses);
var nameSpan = document.createElement('span');
nameSpan.classList.add('mwai-name');
if (role === 'assistant') {
nameSpan.innerHTML = aiName;
}
else if (role === 'system') {
nameSpan.innerHTML = sysName;
}
else {
nameSpan.innerHTML = userName;
}
var textSpan = document.createElement('span');
textSpan.classList.add('mwai-text');
textSpan.innerHTML = text;
div.appendChild(nameSpan);
div.appendChild(textSpan);
// Copy Button
if (copyButton && role === 'assistant') {
var button = document.createElement('div');
button.classList.add('mwai-copy-button');
var firstElement = document.createElement('div');
firstElement.classList.add('mwai-copy-button-one');
var secondElement = document.createElement('div');
secondElement.classList.add('mwai-copy-button-two');
button.appendChild(firstElement);
button.appendChild(secondElement);
div.appendChild(button);
button.addEventListener('click', function () {
try {
var content = textSpan.textContent;
navigator.clipboard.writeText(content);
button.classList.add('mwai-animate');
setTimeout(function () {
button.classList.remove('mwai-animate');
}, 1000);
}
catch (err) {
console.warn('Not allowed to copy to clipboard. Make sure your website uses HTTPS.');
}
});
}
conversation.appendChild(div);
if (typewriter) {
if (role === 'assistant' && text !== startSentence && !replay) {
let typewriter = new Typewriter(textSpan, {
deleteSpeed: 50, delay: 25, loop: false, cursor: '', autoStart: true,
wrapperClassName: 'mwai-typewriter',
});
typewriter.typeString(text).start().callFunction((state) => {
state.elements.cursor.setAttribute('hidden', 'hidden');
typewriter.stop();
});
}
}
conversation.scrollTop = conversation.scrollHeight;
setButtonText();
// Syntax coloring
if (typeof hljs !== 'undefined') {
document.querySelectorAll('pre code').forEach((el) => {
hljs.highlightElement(el);
});
}
}
function buildPrompt(last = 15) {
let prompt = context ? (context + '\n\n') : '';
memorizedChat = memorizedChat.slice(-last);
// Casually fine tuned, let's use the last question
if (isCasuallyFineTuned) {
let lastLine = memorizedChat[memorizedChat.length - 1];
prompt = lastLine.content + promptEnding;
return prompt;
}
// Otherwise let's compile the latest conversation
let conversation = memorizedChat.map(x => x.who + x.content);
prompt += conversation.join('\n');
prompt += '\n' + rawAiName;
return prompt;
}
// Function to request the completion
function onSendClick() {
let input = document.querySelector('#mwai-chat-66d178e1043c2 .mwai-input textarea');
let inputText = input.value.trim();
// Reset the conversation if empty
if (inputText === '') {
document.querySelector('#mwai-chat-66d178e1043c2 .mwai-conversation').innerHTML = '';
localStorage.removeItem('mwai-chat-66d178e1043c2');
memorizedChat = [];
memorizedChat.push({ role: 'assistant', content: startSentence, who: rawAiName, html: startSentence });
addReply(startSentence, 'assistant');
return;
}
// Disable the button
var button = document.querySelector('#mwai-chat-66d178e1043c2 .mwai-input button');
button.disabled = true;
// Add the user reply
memorizedChat.push({ role: 'user', content: inputText, who: rawUserName, html: inputText });
addReply(inputText, 'user');
input.value = '';
input.setAttribute('rows', 1);
input.disabled = true;
let prompt = buildPrompt(maxSentences);
const data = mode === 'images' ? {
env, session: session,
prompt: inputText, rawInput: inputText,
model: model, maxResults, apiKey: apiKey,
} : {
env, session: session,
prompt: prompt, context: context,
messages: memorizedChat, rawInput: inputText,
userName: userName, aiName: aiName,
model: model, temperature: temperature, maxTokens: maxTokens, maxResults: 1, apiKey: apiKey,
embeddingsIndex: embeddingsIndex, stop: stop,
};
// Start the timer
const stopTimer = injectTimer(button);
// Send the request
if (isDebugMode) {
console.log('[BOT] Sent: ', data);
}
fetch(apiURL, { method: 'POST', headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': restNonce,
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
if (isDebugMode) {
console.log('[BOT] Recv: ', data);
}
if (!data.success) {
addReply(data.message, 'system');
}
else {
let html = data.images ? data.images : data.html;
memorizedChat.push({ role: 'assistant', content: data.answer, who: rawAiName, html: html });
addReply(html, 'assistant');
}
button.disabled = false;
input.disabled = false;
stopTimer();
// Only focus only on desktop (to avoid the mobile keyboard to kick-in)
if (!isMobile) {
input.focus();
}
})
.catch(error => {
console.error(error);
button.disabled = false;
input.disabled = false;
stopTimer();
});
}
// Keep the textarea height in sync with the content
function resizeTextArea(ev) {
ev.target.style.height = 'auto';
ev.target.style.height = ev.target.scrollHeight + 'px';
}
// Keep the textarea height in sync with the content
function delayedResizeTextArea(ev) {
window.setTimeout(resizeTextArea, 0, event);
}
// Init the chatbot
function initMeowChatbot() {
var input = document.querySelector('#mwai-chat-66d178e1043c2 .mwai-input textarea');
var button = document.querySelector('#mwai-chat-66d178e1043c2 .mwai-input button');
input.addEventListener('keypress', (event) => {
let text = event.target.value;
if (event.keyCode === 13 && !text.length && !event.shiftKey) {
event.preventDefault();
return;
}
if (event.keyCode === 13 && text.length && !event.shiftKey) {
onSendClick();
}
});
input.addEventListener('keydown', (event) => {
var rows = input.getAttribute('rows');
if (event.keyCode === 13 && event.shiftKey) {
var lines = input.value.split('\n').length + 1;
//mwaiSetTextAreaHeight(input, lines);
}
});
input.addEventListener('keyup', (event) => {
var rows = input.getAttribute('rows');
var lines = input.value.split('\n').length ;
//mwaiSetTextAreaHeight(input, lines);
setButtonText();
});
input.addEventListener('change', resizeTextArea, false);
input.addEventListener('cut', delayedResizeTextArea, false);
input.addEventListener('paste', delayedResizeTextArea, false);
input.addEventListener('drop', delayedResizeTextArea, false);
input.addEventListener('keydown', delayedResizeTextArea, false);
button.addEventListener('click', (event) => {
onSendClick();
});
// If window, add event listener to mwai-open-button and mwai-close-button
if ( isWindow ) {
var openButton = document.querySelector('#mwai-chat-66d178e1043c2 .mwai-open-button');
openButton.addEventListener('click', (event) => {
var chat = document.querySelector('#mwai-chat-66d178e1043c2');
chat.classList.add('mwai-open');
// Only focus only on desktop (to avoid the mobile keyboard to kick-in)
if (!isMobile) {
input.focus();
}
});
var closeButton = document.querySelector('#mwai-chat-66d178e1043c2 .mwai-close-button');
closeButton.addEventListener('click', (event) => {
var chat = document.querySelector('#mwai-chat-66d178e1043c2');
chat.classList.remove('mwai-open');
});
if (isFullscreen) {
var resizeButton = document.querySelector('#mwai-chat-66d178e1043c2 .mwai-resize-button');
resizeButton.addEventListener('click', (event) => {
var chat = document.querySelector('#mwai-chat-66d178e1043c2');
chat.classList.toggle('mwai-fullscreen');
});
}
}
// Get back the previous chat if any for the same ID
var chatHistory = [];
if (memorizeChat) {
chatHistory = localStorage.getItem('mwai-chat-66d178e1043c2');
if (chatHistory) {
memorizedChat = JSON.parse(chatHistory);
memorizedChat = memorizedChat.filter(x => x && x.html && x.role);
memorizedChat.forEach(x => {
addReply(x.html, x.role, true);
});
}
else {
memorizedChat = [];
}
}
if (memorizedChat.length === 0) {
memorizedChat.push({ role: 'assistant', content: startSentence, who: rawAiName, html: startSentence });
addReply(startSentence, 'assistant');
}
}
// Let's go totally meoooow on this!
initMeowChatbot();
})();