-
Notifications
You must be signed in to change notification settings - Fork 0
/
playground.html
563 lines (505 loc) · 18.2 KB
/
playground.html
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<!--
SPDX-License-Identifier: AGPL-3.0-only
🄯 2022, Alexey Parfenov <zxed@alkatrazstudio.net>
-->
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Neodim Playground</title>
<script>
const PARAMS_KEY = 'neodim-params'
async function generate(endpoint, request)
{
const body = JSON.stringify(request)
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body
})
const responseData = await response.json()
return responseData
}
function saveSettings(request)
{
const rawVal = JSON.stringify(request)
localStorage.setItem(PARAMS_KEY, rawVal)
}
function loadSettings()
{
try {
const rawVal = localStorage.getItem(PARAMS_KEY)
const request = JSON.parse(rawVal) ?? {}
return request
} catch(e) {
return {}
}
}
function resetSettings()
{
const curSettings = loadSettings()
const preamble = curSettings.preamble
const prompt = curSettings.prompt
localStorage.removeItem(PARAMS_KEY)
saveSettings({preamble, prompt})
location.reload()
}
function fromJSON(s, def)
{
try {
return JSON.parse(s.trim())
} catch(e) {
return def
}
}
function main()
{
const preambleEl = document.querySelector('#preamble textarea')
const promptEl = document.querySelector('#prompt textarea')
const optEndpointEl = document.querySelector('#option-endpoint input')
const optGeneratedTokensCountEl = document.querySelector('#option-generated-tokens-count input')
const optMaxTotalTokensEl = document.querySelector('#option-max-total-tokens input')
const optTemperatureEl = document.querySelector('#option-temperature input')
const optTopKEl = document.querySelector('#option-top-k input')
const optTopPEl = document.querySelector('#option-top-p input')
const optTfsEl = document.querySelector('#option-tfs input')
const optTypicalEl = document.querySelector('#option-typical input')
const optTopAEl = document.querySelector('#option-top-a input')
const optPenaltyAlphaEl = document.querySelector('#option-penalty-alpha input')
const optWarpersOrderEl = document.querySelector('#option-warpers-order input')
const optRepetitionPenaltyEl = document.querySelector('#option-repetition-penalty input')
const optRepetitionPenaltyRangeEl = document.querySelector('#option-repetition-penalty-range input')
const optRepetitionPenaltySlopeEl = document.querySelector('#option-repetition-penalty-slope input')
const optRepetitionPenaltyIncludePreambleEl = document.querySelector('#option-repetition-penalty-include-preamble input')
const optRepetitionPenaltyIncludeGeneratedEl = document.querySelector('#option-repetition-penalty-include-generated select')
const optRepetitionPenaltyTruncateToInputEl = document.querySelector('#option-repetition-penalty-truncate-to-input input')
const optNoRepeatNGramSizeEl = document.querySelector('#option-no-repeat-ngram-size input')
const optTruncatePromptUntilEl = document.querySelector('#option-truncate-prompt-until input')
const optStopStringsEl = document.querySelector('#option-stop-strings input')
const optStopStringsTypeEl = document.querySelector('#option-stop-strings-type select')
const optStopStringsRequiredMatchesCountEl = document.querySelector('#option-stop-strings-required-matches-count input')
const optWordsWhitelistEl = document.querySelector('#option-words-whitelist textarea')
const optWordsBlacklistEl = document.querySelector('#option-words-blacklist textarea')
const optWordsBlacklistAtStartEl = document.querySelector('#option-words-blacklist-at-start textarea')
const optCanStopEarlyEl = document.querySelector('#option-can-stop-early input')
const btnGenerate = document.querySelector('#submit')
const spinnerEl = document.querySelector('.spinner')
const responseTimeEl = document.querySelector('#response h3 span')
const requestArea = document.querySelector('#request output')
const responseArea = document.querySelector('#response output')
const errorArea = document.querySelector('#error output')
const storedRequest = loadSettings()
preambleEl.value = (storedRequest.preamble ?? '').trim()
promptEl.value = (storedRequest.prompt ?? '').trim()
optEndpointEl.value = storedRequest.endpoint ?? 'http://127.0.0.1:8787/generate'
optGeneratedTokensCountEl.value = storedRequest.generated_tokens_count ?? 32
optMaxTotalTokensEl.value = storedRequest.max_total_tokens ?? 1024
optTemperatureEl.value = storedRequest.temperature ?? 0.6
optTopKEl.value = storedRequest.top_k ?? 0
optTopPEl.value = storedRequest.top_p ?? 0
optTfsEl.value = storedRequest.tfs ?? 0.95
optTypicalEl.value = storedRequest.typical ?? 0
optTopAEl.value = storedRequest.top_a ?? 0
optPenaltyAlphaEl.value = storedRequest.penalty_alpha ?? 0
optWarpersOrderEl.value = JSON.stringify(storedRequest.warpers_order ?? [])
optRepetitionPenaltyEl.value = storedRequest.repetition_penalty ?? 1.25
optRepetitionPenaltyRangeEl.value = storedRequest.repetition_penalty_range ?? 512
optRepetitionPenaltySlopeEl.value = storedRequest.repetition_penalty_slope ?? 0
optRepetitionPenaltyIncludePreambleEl.value = storedRequest.repetition_penalty_include_preamble ?? false
optRepetitionPenaltyIncludeGeneratedEl.value = storedRequest.repetition_penalty_include_generated ?? 'slide'
optRepetitionPenaltyTruncateToInputEl.value = storedRequest.repetition_penalty_truncate_to_input ?? false
optNoRepeatNGramSizeEl.value = storedRequest.no_repeat_ngram_size ?? 0
optTruncatePromptUntilEl.value = JSON.stringify(storedRequest.truncate_prompt_until ?? [' '])
optStopStringsEl.value = JSON.stringify(storedRequest.stop_strings ?? [])
optStopStringsTypeEl.value = storedRequest.stop_strings_type ?? 'string'
optStopStringsRequiredMatchesCountEl.value = storedRequest.stop_strings_required_matches_count ?? 1
optWordsWhitelistEl.value = (storedRequest.words_whitelist ?? []).join('\n').trim()
optWordsBlacklistEl.value = (storedRequest.words_blacklist ?? []).join('\n').trim()
optWordsBlacklistAtStartEl.value = (storedRequest.words_blacklist_at_start ?? []).join('\n').trim()
optCanStopEarlyEl.value = storedRequest.can_stop_early ?? false
const locale = navigator.languages[0] ?? 'en-US'
const decimalFormat = new Intl.NumberFormat(locale, {
maximumFractionDigits: 1,
useGrouping: false
})
btnGenerate.addEventListener('click', async () => {
let preamble = preambleEl.value.trim()
if(preamble.length)
preamble += '\n\n'
const prompt = promptEl.value.replace(/[ ]+$/, '')
const endpoint = optEndpointEl.value.trim()
errorArea.textContent = ''
responseArea.textContent = ''
responseTimeEl.textContent = ''
btnGenerate.disabled = true
spinnerEl.classList.add('running')
const request = {
preamble,
prompt,
temperature: parseFloat(optTemperatureEl.value),
top_k: parseInt(optTopKEl.value),
top_p: parseFloat(optTopPEl.value),
tfs: parseFloat(optTfsEl.value),
typical: parseFloat(optTypicalEl.value),
top_a: parseFloat(optTopAEl.value),
penalty_alpha: parseFloat(optPenaltyAlphaEl.value),
warpers_order: fromJSON(optWarpersOrderEl.value, []),
repetition_penalty: parseFloat(optRepetitionPenaltyEl.value),
repetition_penalty_range: parseFloat(optRepetitionPenaltyRangeEl.value),
repetition_penalty_slope: parseFloat(optRepetitionPenaltySlopeEl.value),
repetition_penalty_include_preamble: Boolean(optRepetitionPenaltyIncludePreambleEl.checked),
repetition_penalty_include_generated: optRepetitionPenaltyIncludeGeneratedEl.value,
repetition_penalty_truncate_to_input: Boolean(optRepetitionPenaltyTruncateToInputEl.checked),
no_repeat_ngram_size: parseInt(optNoRepeatNGramSizeEl.value),
generated_tokens_count: parseInt(optGeneratedTokensCountEl.value),
max_total_tokens: parseInt(optMaxTotalTokensEl.value),
sequences_count: 1,
truncate_prompt_until: fromJSON(optTruncatePromptUntilEl.value, []),
stop_strings: fromJSON(optStopStringsEl.value, []),
stop_strings_type: optStopStringsTypeEl.value,
stop_strings_required_matches_count: parseInt(optStopStringsRequiredMatchesCountEl.value),
words_whitelist: optWordsWhitelistEl.value ? optWordsWhitelistEl.value.split('\n') : null,
words_blacklist: optWordsBlacklistEl.value ? optWordsBlacklistEl.value.split('\n') : null,
words_blacklist_at_start: optWordsBlacklistAtStartEl.value ? optWordsBlacklistAtStartEl.value.split('\n') : null,
can_stop_early: Boolean(optCanStopEarlyEl.checked),
}
saveSettings({
...request,
endpoint
})
try {
const requestJson = JSON.stringify(request, null, 4)
requestArea.textContent = requestJson
const timeStart = performance.now()
const response = await generate(endpoint, request)
const timeElapsed = (performance.now() - timeStart) / 1000
const responseJson = JSON.stringify(response, null, 4)
const elapsedTimeStr = decimalFormat.format(timeElapsed)
responseArea.textContent = responseJson
if(response.error)
{
responseTimeEl.textContent = `(${elapsedTimeStr}s)`
throw new Error(response.error)
}
const tokensPerSec = response.generated_tokens_count / timeElapsed
const tokensPerSecStr = decimalFormat.format(tokensPerSec)
responseTimeEl.textContent = `(${elapsedTimeStr}s, ${tokensPerSecStr} tokens per second)`
const seq = response.sequences[0]
promptEl.value += seq.generated_text + seq.stop_string
promptEl.focus()
promptEl.setSelectionRange(promptEl.value.length, promptEl.value.length)
} catch(e) {
errorArea.textContent = e.toString()
}
saveSettings({
...request,
endpoint,
prompt: promptEl.value
})
btnGenerate.disabled = false
spinnerEl.classList.remove('running')
})
}
window.addEventListener('DOMContentLoaded', main)
</script>
<style>
* {
box-sizing: border-box;
}
h3 {
margin: 0;
}
main {
display: flex;
gap: 10px;
}
#options {
width: 300px;
display: flex;
flex-direction: column;
}
#options .spacer {
flex: 1;
}
#options #reset-settings {
margin-top: 50px;
text-align: center;
}
main aside,
main article {
border: 1px solid gray;
padding: 0 10px;
box-shadow: 3px 3px 2px 0 rgba(0, 0, 0, 0.25);
}
main article {
flex: 1;
}
main article textarea {
width: 100%;
}
#preamble textarea {
height: 100px;
}
#prompt textarea {
min-height: 300px;
}
#traffic {
display:flex;
flex-direction: column;
width: 500px;
}
#traffic section {
margin-bottom: 10px;
}
#traffic output {
background:rgba(0, 0, 0, 0.15);
padding: 10px;
width: 100%;
display: block;
white-space: break-spaces;
font-size: smaller;
font-family: monospace;
}
#request,
#response,
#error {
flex: 1
}
#controls {
text-align: right;
margin-bottom: 10px;
}
#controls button {
font-size: large;
}
@keyframes rotate {
100% {
transform: rotate(1turn);
}
}
.spinner {
animation: rotate 5s linear infinite;
display: none;
}
.spinner.running {
display: inline-block;
}
#options section {
margin-bottom: 10px;
}
#options section label {
font-size: smaller;
}
input,
select {
width: 100%;
font-family: monospace;
}
</style>
</head>
<body>
<main>
<aside id="options">
<h3>Options</h3>
<section id="option-endpoint">
<label>
Endpoint
<input type="url"/>
</label>
</section>
<section id="option-generated-tokens-count">
<label>
Generated tokens count
<input type="number" min="1" max="2048"/>
</label>
</section>
<section id="option-max-total-tokens">
<label>
Max. total tokens
<input type="number" min="1" max="2048"/>
</label>
</section>
<section id="option-temperature">
<label>
Temperature
<input type="number" min="0" max="2" step="0.01"/>
</label>
</section>
<section id="option-top-k">
<label>
Top k
<input type="number" min="0" max="1024"/>
</label>
</section>
<section id="option-top-p">
<label>
Top p (nucleus sampling)
<input type="number" min="0" max="1" step="0.01"/>
</label>
</section>
<section id="option-tfs">
<label>
Tail-free sampling
<input type="number" min="0" max="1" step="0.01"/>
</label>
</section>
<section id="option-typical">
<label>
Typical sampling
<input type="number" min="0" max="1" step="0.01"/>
</label>
</section>
<section id="option-top-a">
<label>
Top a
<input type="number" min="0" max="1" step="0.01"/>
</label>
</section>
<section id="option-penalty-alpha">
<label>
Penalty alpha
<input type="number" min="0" max="1" step="0.01"/>
</label>
</section>
<section id="option-warpers-order">
<label>
Warpers order (JSON)
<input type="text"/>
</label>
</section>
<section id="option-repetition-penalty">
<label>
Repetition penalty
<input type="number" min="0" max="10" step="0.01"/>
</label>
</section>
<section id="option-repetition-penalty-range">
<label>
Repetition penalty range
<input type="number" min="0" max="2048" step="1"/>
</label>
</section>
<section id="option-repetition-penalty-slope">
<label>
Repetition penalty slope
<input type="number" min="0" max="10" step="0.01"/>
</label>
</section>
<section id="option-no-repeat-ngram-size">
<label>
No repeat N-gram size
<input type="number" min="0" max="2048" step="1"/>
</label>
</section>
<section id="option-repetition-penalty-include-preamble">
<label>
Include preamble in repetition penalty range
<input type="checkbox"/>
</label>
</section>
<section id="option-repetition-penalty-include-generated">
<label>
Include generated text in repetition penalty range
<select>
<option>ignore</option>
<option>allow</option>
<option>expand</option>
<option>slide</option>
</select>
</label>
</section>
<section id="option-repetition-penalty-truncate-to-input">
<label>
Truncate repetition penalty range to input
<input type="checkbox"/>
</label>
</section>
<section id="option-truncate-prompt-until">
<label>
Truncate prompt until (JSON)
<input type="text"/>
</label>
</section>
<section id="option-stop-strings">
<label>
Stop strings (JSON)
<input type="text"/>
</label>
</section>
<section id="option-stop-strings-type">
<label>
Stop strings type
<select>
<option>string</option>
<option>regex</option>
</select>
</label>
</section>
<section id="option-stop-strings-required-matches-count">
<label>
Stop strings required matches count
<input type="number" min="1" max="99"/>
</label>
</section>
<section id="option-words-whitelist">
<label>
Words whitelist
<textarea></textarea>
</label>
</section>
<section id="option-words-blacklist">
<label>
Words blacklist
<textarea></textarea>
</label>
</section>
<section id="option-words-blacklist-at-start">
<label>
Words blacklist at the start
<textarea></textarea>
</label>
</section>
<section id="option-can-stop-early">
<label>
Can stop early
<input type="checkbox"/>
</label>
</section>
<div class="spacer"></div>
<section id="reset-settings">
<button onclick="resetSettings()">Reset to default</button>
</section>
</aside>
<article>
<section id="preamble">
<h3>Preamble</h3>
<textarea></textarea>
</section>
<section id="prompt">
<h3>Prompt</h3>
<textarea></textarea>
</section>
<section id="controls">
<span class="spinner">⏳</span>
<button id="submit">Generate</button>
</section>
</article>
<aside id="traffic">
<section id="request">
<h3>Request</h3>
<output></output>
</section>
<section id="response">
<h3>Response <span></span></h3>
<output></output>
</section>
<section id="error">
<h3>Error</h3>
<output></output>
</section>
</aside>
</main>
</body>
</html>