blob: c33a1180407acf09286c34d684762a4ba69c40d3 (
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>A(I)MA - A(I)sk me anything!</title>
<style>
div#chat>p {
width: 100%;
}
.question {
display: flex;
justify-content: right
}
.answer {
background: lightgray;
border-radius: 10px;
display: inline-block;
padding: 1em;
}
.answer p:first-child {
margin-top: 0;
}
.answer p:last-child {
margin-bottom: 0;
}
</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: 70%; justify-content: center;">
<div id="placeholder" class="answer">
<p>Ask below :-)</p>
</div>
</div>
<form style="display: flex; justify-content: center; align-items: center; padding: 20px; width: 70%">
<input id="prompt" type="text" placeholder="Enter your prompt here"
style="padding: 10px; width: 80%; margin-right: 10px;">
<select id="model" type="text" style="padding: 10px; width: 20%; margin-right: 10px;" placeholder="Model to use"
value="qwen2.5-coder:7b"></select>
<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');
const model = document.getElementById('model');
fetch('http://localhost:11434/api/tags')
.then(response => response.json())
.then(data => {
data.models.forEach(m => {
let option = document.createElement('option');
option.value = m.model;
option.text = m.name;
model.appendChild(option);
});
})
.catch(error => console.error(error));
// generate button
generate.addEventListener('click', e => {
e.preventDefault();
const modelValue = model.value;
// 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('<p style="background:lightblue;border-radius:10px;padding:1em">' + promptValue + '</p>', "question");
// call the api
fetch('http://localhost:11434/api/generate', {
method: 'POST',
body: JSON.stringify({
model: modelValue,
prompt: promptValue,
stream: false
})
})
.then(response => response.json())
.then(data => {
appendChat(`<p style="font-size:0.7em;font-style:italic">${modelValue}</p>` + markdownParser(data.response), "answer");
})
.catch(error => console.error(error));
});
// append text to chat
function appendChat(html, className) {
const messageDisplay = document.createElement('div');
messageDisplay.innerHTML = html;
messageDisplay.className = className;
chat.appendChild(messageDisplay);
}
// simple markdown parser
function escapeHtml(html) {
var line = html.split('<').join('<');
line = line.split('>').join('>');
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) {
if (!line)
continue;
// Check for pre-formatted block mode
const preMatch = line.match(blockPreRegex);
if (preMatch) {
if (preMode)
output.push('</pre>');
else
output.push('<pre style="padding-left:3em">');
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], `<em>${italicMatch[1]}</em>`);
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>
|