File size: 2,836 Bytes
c550f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
972a96f
 
c550f7d
972a96f
c550f7d
 
972a96f
 
c550f7d
 
 
 
 
 
 
 
 
 
972a96f
 
c550f7d
972a96f
c550f7d
 
972a96f
c550f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e82bd8
 
 
 
 
 
 
 
 
 
 
 
 
b9fad8d
c550f7d
 
3e82bd8
375f922
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Menyimpan riwayat percakapan per pengguna
let conversationHistory = {};

// Identitas pengguna
const userId = generateUniqueId(); // Fungsi untuk menghasilkan ID unik

// Fungsi untuk menghasilkan ID unik
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; // Don't send empty messages
    }

    // Create user message element
    const userMessage = document.createElement("div");
    userMessage.className = "message user-message";
    userMessage.textContent = userInput.value;
    messages.appendChild(userMessage);

    // Update conversation history
    if (!conversationHistory[userId]) {
        conversationHistory[userId] = [];
    }
    conversationHistory[userId].push({
        role: "user",
        content: userInput.value
    });

    // Scroll to bottom of messages
    messages.scrollTop = messages.scrollHeight;

    // Clear user input field
    userInput.value = "";

    // Show typing indicator
    typingIndicator.style.display = "flex";

    // Create bot message element
    const botResponse = await generate(userMessage.textContent);
    const botMessage = document.createElement("div");
    botMessage.className = "message bot-message";
    let ee = 0;
    botMessage.textContent = botResponse;

    // Update conversation history
    conversationHistory[userId].push({
        role: "assistant",
        content: botResponse
    });

    // Hide typing indicator
    typingIndicator.style.display = "none";
    messages.appendChild(botMessage);

    // Scroll to bottom of messages
    messages.scrollTop = messages.scrollHeight;
}

async function generate(q) {
    // Mengambil riwayat percakapan untuk pengguna tertentu
    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();

    // Mengembalikan respons dari bot
    return tes.choices[0].messages.content;
}