File size: 1,780 Bytes
9417734
 
 
 
59da560
 
9417734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59da560
9417734
 
 
 
59da560
9417734
59da560
 
 
 
9417734
 
 
59da560
9417734
59da560
 
 
 
 
9417734
59da560
 
9417734
 
 
 
 
 
 
 
 
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
const chatBox = document.querySelector(".chat-box");
const sendButton = document.querySelector(".send-btn");
const input = document.querySelector("#userInput");

const MODEL = "mistralai/Mixtral-8x7B-Instruct-v0.1";
const API_URL = `https://huggingface.co/api/inference-proxy/tokens/${MODEL}`;

function addMessage(sender, text) {
  const msg = document.createElement("div");
  msg.classList.add("message", sender);
  msg.innerHTML = `<strong>${sender === "user" ? "🧍‍♂️ Siz:" : "🤖 ZenkaMind:"}</strong> ${text}`;
  chatBox.appendChild(msg);
  chatBox.scrollTop = chatBox.scrollHeight;
}

async function sendMessage() {
  const userInput = input.value.trim();
  if (!userInput) return;
  addMessage("user", userInput);
  input.value = "";

  addMessage("bot", "💭 Düşünüyorum...");

  try {
    const res = await fetch(API_URL, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        inputs: userInput,
        options: { wait_for_model: true },
        parameters: { temperature: 0.7, max_new_tokens: 300 }
      }),
    });

    const data = await res.json();
    document.querySelector(".message.bot:last-child").remove();

    if (data?.error) {
      addMessage("bot", "⚠️ Model şu anda meşgul veya hatalı: " + data.error);
    } else {
      addMessage("bot", data?.[0]?.generated_text || "Yanıt alınamadı.");
    }
  } catch (err) {
    document.querySelector(".message.bot:last-child").remove();
    addMessage("bot", "❌ Bağlantı hatası oluştu.");
  }
}

sendButton.addEventListener("click", sendMessage);
input.addEventListener("keypress", e => {
  if (e.key === "Enter") sendMessage();
});

addMessage("bot", "Merhaba 👋 Ben ZenkaMind! Size nasıl yardımcı olabilirim?");