fix: inserting chat history into the answer generation process
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user