-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
212 lines (175 loc) · 5.35 KB
/
app.js
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
210
211
212
/*
* Dumbar - a simple menu bar for LLM.
*
* Copyright 2023 Jerry Sievert
* https://github.com/JerrySievert/Dumbar
*/
const Parser = require('jsonparse');
/* Our error message, in case anything goes wrong. */
const error_message = `<div class="error">Unable to connect to Ollama.<br><br>Is it running?</div>`;
/*
* read_chunks
*
* Reads streaming data from a reader, returning an iterator.
*/
const read_chunks = (reader) => {
return {
async *[Symbol.asyncIterator]() {
let readResult = await reader.read();
while (!readResult.done) {
yield readResult.value;
readResult = await reader.read();
}
}
};
};
/*
* get_pref
*
* Reads a preference item from localStorage, returning null if it is
* either unset or blank.
*/
const get_pref = (which) => {
const pref = localStorage.getItem(which);
if (pref == '') {
return null;
}
return pref;
};
/*
* get_models
*
* Retrieve all known models from the Ollama API endpoint. On error,
* write the standard error message into the results.
*/
const get_models = async () => {
const host =
get_pref('prefs:host') !== null ? get_pref('prefs:host') : '127.0.0.1';
const port = get_pref('prefs:port') !== null ? get_pref(prefs.port) : 11434;
const url = `http://${host}:${port}`;
try {
fetch(`${url}/api/tags`)
.then(async (response) => {
const models = await response.json();
const eModel = document.getElementById('model');
eModel.innerHTML = '';
for (let model of models.models) {
const option = document.createElement('option');
option.text = model.name;
option.value = model.name;
eModel.add(option);
}
})
.catch((err) => {
document.getElementById('spinner').classList.add('hidden');
resultsWindow.innerHTML = error_message;
});
} catch (err) {
console.log(err);
}
};
/*
* query
*
* Read the model and prompt from the UI and make a query, writing the results
* into the results element as they arrive. On error, write the standard error
* message.
*/
const query = async () => {
const eModel = document.getElementById('model');
const model = eModel.value;
const ePrompt = document.getElementById('prompt');
const prompt = ePrompt.value;
document.getElementById('spinner').classList.remove('hidden');
const body = {
model,
prompt
};
const resultsWindow = document.getElementById('results');
resultsWindow.innerText = '';
let response;
const host = get_pref('prefs:host') ? get_pref('prefs:host') : '127.0.0.1';
const port = get_pref('prefs:port') ? get_pref(prefs.port) : 11434;
const url = `http://${host}:${port}`;
try {
let utf8decoder = new TextDecoder();
fetch(`${url}/api/generate`, {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' }
})
.then(async (response) => {
document.getElementById('spinner').classList.add('hidden');
const reader = response.body.getReader();
const parser = new Parser();
parser.onValue = (value) => {
if (value.response) {
resultsWindow.innerText += value.response;
}
};
for await (const chunk of read_chunks(reader)) {
let str = utf8decoder.decode(chunk.buffer);
parser.write(str);
}
})
.catch((err) => {
document.getElementById('spinner').classList.add('hidden');
resultsWindow.innerHTML = `<div class="error">Unable to connect to Ollama.<br><br>Is it running?</div>`;
});
} catch (err) {
console.log(err);
}
};
/*
* show_preferences
*
* Show the preferences dialog pane, set the current values.
*/
const show_preferences = () => {
const host = document.getElementById('host');
host.value = localStorage.getItem('prefs:host');
const port = document.getElementById('port');
port.value = localStorage.getItem('prefs:port');
const prefPane = document.getElementById('preferences');
prefPane.classList.remove('hidden');
};
/*
* prefs_cancel
*
* Cancel out of the preferences pane, setting things back to normal.
*/
const prefs_cancel = () => {
const host = document.getElementById('host');
host.value = '';
const port = document.getElementById('port');
port.value = '';
const prefPane = document.getElementById('preferences');
prefPane.classList.add('hidden');
};
/*
* prefs_save
*
* Save the new preferences, then call for the models again from Ollama.
*/
const prefs_save = () => {
const host = document.getElementById('host');
localStorage.setItem('prefs:host', host.value);
const port = document.getElementById('port');
localStorage.setItem('prefs:port', port.value);
const prefPane = document.getElementById('preferences');
prefPane.classList.add('hidden');
const resultsWindow = document.getElementById('results');
resultsWindow.innerText = '';
get_models();
};
/* Set up click handlers for all of the buttons. */
const searchButton = document.getElementById('query');
searchButton.onclick = query;
const prefsButton = document.getElementById('prefs');
prefsButton.onclick = show_preferences;
const saveButton = document.getElementById('save');
saveButton.onclick = prefs_save;
const cancelButton = document.getElementById('cancel');
cancelButton.onclick = prefs_cancel;
/* Start off by retrieving the models. */
get_models();