summaryrefslogtreecommitdiff
path: root/index.html
blob: 484353cb32e967705798de7528dc7e6c8ba01ed4 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!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="generate" type="submit" style="padding: 10px;">Generate</button>
    </form>
  </main>
  <script>
    // global variables
    var firstCall = true;
    const chat = document.getElementById('chat');
    const prompt = document.getElementById('prompt');
    const generate = document.getElementById('generate');

    // generate button
    generate.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(markdownParser(data.response)))
        .catch(error => console.error(error));
    });
    function appendChat(message) {
      const messageDisplay = document.createElement('p');
      messageDisplay.textContent = message;
      chat.appendChild(messageDisplay);
    }

    // simple markdown parser
    function escapeHtml(html) {
      var line = html.split('<').join('&lt;');
      line = line.split('>').join('&gt;');
      return line;
    }
    function markdownParser(text) {
      // Regular expressions for different markdown elements
      const headingRegex = /^#+ (.+)$/;
      const boldRegex = /\*\*(.+?)\*\*/;
      const italicRegex = /\_\_(.+?)\_\_/;
      const listRegex = /^\* (.+)$/;
      const blockPreRegex = /```.*/;
      const inlinePreRegex = /`(.+?)`/;

      // Split the text into lines
      const lines = text.split('\n');

      // Initialize the parsed output
      const output = [];

      var preMode = false;
      var listMode = false;
      for (var line of lines) {
        // Check for pre-formatted block mode
        const preMatch = line.match(blockPreRegex);
        if (preMatch) {
          if (preMode)
            output.push('</pre>');
          else
            output.push('<pre>');
          preMode = !preMode;
          continue;
        }
        if (preMode) {
          output.push(escapeHtml(line));
          continue;
        }

        // Check for inline pre-formatted mode
        var inlinePreMatch = line.match(inlinePreRegex);
        while (inlinePreMatch) {
          line = line.replace(inlinePreMatch[0], `<span style="font-family:monospace">${escapeHtml(inlinePreMatch[1])}</span>`);
          inlinePreMatch = line.match(inlinePreRegex);
        }

        // handle bold text
        var boldMatch = line.match(boldRegex);
        while (boldMatch) {
          line = line.replace(boldMatch[0], `<strong>${boldMatch[1]}</strong>`);
          boldMatch = line.match(boldRegex);
        }

        // handle italic text
        var italicMatch = line.match(italicRegex);
        while (italicMatch) {
          line = line.replace(italicMatch[0], `<strong>${italicMatch[1]}</strong>`);
          italicMatch = line.match(italicRegex);
        }

        // List
        const listMatch = line.match(listRegex);
        if (listMatch) {
          if (!listMode)
            output.push('<ul>');
          listMode = true;
          output.push(`<li>${listMatch[1]}</li>`);
          continue;
        }
        else {
          listMode = false;
          output.push('</ul>');
        }

        // Check for headings
        const headingMatch = line.match(headingRegex);
        if (headingMatch) {
          output.push(`<h1>${headingMatch[1]}</h1>`);
          continue;
        }

        // Check for paragraphs
        output.push(`<p>${line}</p>`);
      }

      // Join the parsed elements into HTML
      return output.join('\n');
    }
  </script>
</body>

</html>