fix: inserting chat history into the answer generation process

This commit is contained in:
2025-03-06 11:06:45 +07:00
parent 15210a56ee
commit 3f5d0eb1cd
5 changed files with 198 additions and 150 deletions

View File

@@ -10,6 +10,7 @@ document.addEventListener("DOMContentLoaded", function () {
const textarea = document.getElementById("user-message");
const sendButton = document.getElementById("send");
const conversationArea = document.querySelector(".row.flex-grow");
const chatHistory = [];
// Fungsi untuk mengirim pesan
async function sendMessage() {
@@ -30,7 +31,7 @@ document.addEventListener("DOMContentLoaded", function () {
}
// Panggil API untuk mendapatkan respons dari bot
const botResponse = await getBotResponse(userText);
const botResponse = await getBotResponse(userText, chatHistory);
// Perbarui pesan bot dengan respons yang sebenarnya
if (messageTextContainer) {
@@ -124,12 +125,12 @@ document.addEventListener("DOMContentLoaded", function () {
}
// Fungsi untuk memanggil API
async function getBotResponse(userText) {
async function getBotResponse(userText, historyChat) {
try {
const url = `${GlobalConfig.apiHost}/api/main-generate-text`;
const response = await fetch(url, {
method: "POST",
body: JSON.stringify({prompt: userText }),
body: JSON.stringify({prompt: userText, chatHistory: historyChat}),
headers: {
Authorization: `Bearer ${document
.querySelector('meta[name="api-token"]')
@@ -139,6 +140,12 @@ document.addEventListener("DOMContentLoaded", function () {
});
const data = await response.json();
const rawBotResponse = data.nlpResponse;
// Tambahkan ke chatHistory
chatHistory.push({
user: userText,
rawBotResponse: rawBotResponse,
});
return data.response || "Maaf, saya tidak mengerti.";
} catch (error) {
console.error("Error fetching bot response:", error);