summaryrefslogtreecommitdiff
path: root/index.html
blob: ad1c5403feda681140a771501fcc25006d0e3ea4 (plain)
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
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>A(I)MA - A(I)sk me anything!</title>
  <style>
    div#chat>p {
      width: 100%;
    }
  </style>
</head>

<body style="margin: 0;">
  <main
    style="display: flex; flex-direction: column; justify-content: space-between; height: 100vh; align-items: center;">
    <h1 style="text-align: center;">A(I)MA - A(I)sk me anything!</h1>
    <div id="chat" style="padding: 20px; width: 80%; justify-content: center;">
      <p id="placeholder">Ask below :-)</p>
    </div>
    <form style="display: flex; justify-content: center; align-items: center; padding: 20px; width: 80%">
      <input id="prompt" type="text" placeholder="Enter your message here"
        style="padding: 10px; width: 100%; margin-right: 10px;">
      <button id="send" type="submit" style="padding: 10px;">Send</button>
    </form>
  </main>
  <script>
    var firstCall = true;
    const chat = document.getElementById('chat');
    const prompt = document.getElementById('prompt');
    const send = document.getElementById('send');
    send.addEventListener('click', e => {
      e.preventDefault();

      // clear chat on first call
      if (firstCall) {
        firstCall = false;
        const placeholder = document.getElementById('placeholder');
        placeholder.parentNode.removeChild(placeholder);
      }

      // clear prompt on submit
      const promptValue = prompt.value;
      prompt.value = '';

      // append request to the chat
      appendChat("> " + promptValue);

      // call the api
      fetch('http://localhost:11434/api/generate', {
        method: 'POST',
        body: JSON.stringify({
          model: "codegemma:7b",
          prompt: promptValue,
          stream: false
        })
      })
        .then(response => response.json())
        .then(data => appendChat(data.response))
        .catch(error => console.error(error));
    });
    function appendChat(message) {
      const messageDisplay = document.createElement('p');
      messageDisplay.textContent = message;
      chat.appendChild(messageDisplay);
    }
  </script>
</body>

</html>