|
|
|
let conversationHistory = {}; |
|
|
|
|
|
const userId = generateUniqueId(); |
|
|
|
|
|
function generateUniqueId() { |
|
return "user_" + Math.random().toString(36).substr(2, 9); |
|
} |
|
|
|
async function sendMessage() { |
|
const userInput = document.getElementById("userInput"); |
|
const messages = document.getElementById("messages"); |
|
const typingIndicator = document.getElementById("typingIndicator"); |
|
|
|
if (userInput.value.trim() === "") { |
|
return; |
|
} |
|
|
|
|
|
const userMessage = document.createElement("div"); |
|
userMessage.className = "message user-message"; |
|
userMessage.textContent = userInput.value; |
|
messages.appendChild(userMessage); |
|
|
|
|
|
if (!conversationHistory[userId]) { |
|
conversationHistory[userId] = []; |
|
} |
|
conversationHistory[userId].push({ |
|
role: "user", |
|
content: userInput.value |
|
}); |
|
|
|
|
|
messages.scrollTop = messages.scrollHeight; |
|
|
|
|
|
userInput.value = ""; |
|
|
|
|
|
typingIndicator.style.display = "flex"; |
|
|
|
|
|
const botResponse = await generate(userMessage.textContent); |
|
const botMessage = document.createElement("div"); |
|
botMessage.className = "message bot-message"; |
|
let ee = 0; |
|
botMessage.textContent = botResponse; |
|
|
|
|
|
conversationHistory[userId].push({ |
|
role: "assistant", |
|
content: botResponse |
|
}); |
|
|
|
|
|
typingIndicator.style.display = "none"; |
|
messages.appendChild(botMessage); |
|
|
|
|
|
messages.scrollTop = messages.scrollHeight; |
|
} |
|
|
|
async function generate(q) { |
|
|
|
const userConversation = conversationHistory[userId] || []; |
|
|
|
const bot = await fetch("https://chatbot-ji1z.onrender.com/chatbot-ji1z", { |
|
method: "post", |
|
body: JSON.stringify({ |
|
messages: [ |
|
{ |
|
role: "system", |
|
content: |
|
"Kamu adalah asisten virtual dengan nama ikyy yang dibuat oleh ikyyofc. Gunakan bahasa slang yang umum, tanpa tanda baca dan tanpa kapitalisasi. Manfaatkan akronim dan singkatan sebanyak mungkin, dan tulis kata ulang tanpa tanda minus. Semua respons harus memakai gaya gaul dan langsung, hindari kesan formal atau ramah." |
|
}, |
|
...userConversation |
|
] |
|
}) |
|
}); |
|
let tes = await bot.json(); |
|
|
|
|
|
return tes.choices[0].messages.content; |
|
} |
|
|