File size: 1,049 Bytes
4e81ac5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>LLaMA2 聊天机器人</title>
</head>
<body>
  <h2>🤖 LLaMA2 Chat</h2>
  <div id="chat-box" style="border:1px solid #ccc;padding:10px;height:300px;overflow-y:auto;"></div>
  <input id="user-input" type="text" placeholder="请输入..." style="width:80%;">
  <button onclick="sendMessage()">发送</button>

  <script>

    async function sendMessage() {

      const input = document.getElementById("user-input").value;

      const chat = document.getElementById("chat-box");

      chat.innerHTML += `<div><b>你:</b> ${input}</div>`;

      const res = await fetch("http://localhost:8000/generate", {

        method: "POST",

        headers: {"Content-Type": "application/json"},

        body: JSON.stringify({prompt: input, token: "YOUR_SECURE_TOKEN"})

      });

      const data = await res.json();

      chat.innerHTML += `<div><b>助手:</b> ${data.response}</div>`;

      chat.scrollTop = chat.scrollHeight;

    }

  </script>
</body>
</html>