-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
382 lines (300 loc) · 14 KB
/
index.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
<html data-theme="dark">
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css" />
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
<!-- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/darcula.min.css -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark" />
<title>NotTavern</title>
<link rel="stylesheet" href="./style.css" />
<script src="script.js"></script>
<script>
// Messages
function scrollDown() {
setTimeout(() => window.scrollTo(0, document.body.scrollHeight), 1)
}
function streamScrollStep() {
const diff = window.scrollMaxY - window.scrollY
if (diff < 60) scrollDown()
}
function loadMessages(data) {
data.messages = Messages.list()
}
function addMessage(data, message) {
if (!message.content) return
let addedMessage = Messages.add(message)
data.messages.push(addedMessage)
scrollDown()
}
async function addMessageStream(data, stream) {
var message = {
role: 'assistant',
content: ''
}
data.messages.push(message)
scrollDown()
for await (const value of stream) {
data.messages[data.messages.length - 1].content += value
streamScrollStep()
}
data.reader = undefined
data.busy = false
const content = data.messages[data.messages.length - 1].content
if (!content) return
data.messages[data.messages.length - 1] = Messages.add(message)
scrollDown()
}
async function sendMessage(data, message) {
data.content = ''
data.busy = true
if (!!message.content) addMessage(data, message)
const response = Completions.send()
data.reader = (await response.next()).value
if (!!data.reader) addMessageStream(data, response)
}
function updateMessage(data, message, updated) {
const index = data.messages.indexOf(message)
data.messages[index] = { ...message, ...updated }
Messages.update(message.id, updated)
}
function deleteMessage(data, message, drop = false) {
const index = data.messages.indexOf(message)
if (drop) data.messages.splice(index)
else data.messages.splice(index, 1)
Messages.remove(message.id, drop)
}
//
function alterTextArea(textArea) {
textArea.style.height = 'auto';
textArea.style.height = (textArea.scrollHeight + 2) + 'px';
}
function checkSubmit(e, data) {
if (e.key == 'Enter' && !e.shiftKey && !data.busy) {
e.preventDefault()
sendMessage(data, { role: 'user', content: data.content })
}
}
// Profile
function loadProfile(data) {
const profile = Profile.get()
data.base_url = profile.base_url
data.api_key = profile.api_key
data.model = profile.model
data.model_override = profile.model_override
data.main = profile.main
data.jailbreak = profile.jailbreak
data.max_messages = parseInt(profile.max_messages)
data.max_tokens = parseInt(profile.max_tokens)
data.temperature = parseFloat(profile.temperature)
}
function updateProfile(data) {
const profile = Profile.get()
updated = {
base_url: data.base_url,
api_key: data.api_key,
model: data.model,
model_override: data.model_override,
main: data.main,
jailbreak: data.jailbreak,
max_messages: parseInt(data.max_messages),
max_tokens: parseInt(data.max_tokens),
temperature: parseFloat(data.temperature)
}
Object.keys(updated).forEach(key => updated[key] === undefined && delete updated[key]);
console.log({ ...profile, ...updated })
Profile.update({ ...profile, ...updated })
}
async function loadModels(data) {
data.models = await Completions.models()
}
document.addEventListener('alpine:init', () => {
Alpine.store('modalConnection', false)
Alpine.store('modalCharacter', false)
Alpine.store('modalTune', false)
Alpine.store('modalNotes', false)
})
</script>
</head>
<body x-data="{messages: []}" x-init="loadMessages($data)">
<header class="container-fluid">
<nav>
<ul>
<li> <strong>This is not tavern</strong> </li>
</ul>
<ul>
<!-- <li>
<button class="button-icon" x-on:click="$store.modalNotes=true">
<span class="material-symbols-outlined"> note_stack </span>
</button>
</li> -->
<li>
<button class="button-icon" x-on:click="$store.modalConnection=true">
<span class="material-symbols-outlined"> link </span>
</button>
</li>
<li>
<button class="button-icon" x-on:click="$store.modalCharacter=true">
<span class="material-symbols-outlined"> person </span>
</button>
</li>
<li>
<button class="button-icon" x-on:click="$store.modalTune=true">
<span class="material-symbols-outlined"> tune </span>
</button>
</li>
</ul>
</nav>
</header>
<main x-ref="messages" class="container-fluid">
<template x-for="message in messages">
<article>
<header>
<strong x-text="message.role">Assistant</strong>
<span x-show="message.pinned === true"
style="font-size: 18; flex-grow: 1; user-select: none; padding-left: 4px; color: var(--pico-muted-color)"
class="material-symbols-outlined">
keep
</span>
<details class="dropdown dropdown-icon">
<summary role="button" class="button-icon small"> <span
class="material-symbols-outlined">more_vert</span> </summary>
<ul style="right: -16px;">
<li> <a x-on:click="updateMessage($data, message, {pinned: !message.pinned} )"
x-text="message.pinned ? 'Unpin' : 'Pin' ">Pin</a> </li>
<li> <a x-on:click="deleteMessage($data, message)">Delete</a> </li>
<li> <a x-on:click="deleteMessage($data, message, true)">Delete all</a> </li>
</ul>
</details>
</header>
<main x-bind:aria-busy="!message.content" x-html="marked.parse(message.content)">lol</main>
</article>
</template>
</main>
<footer x-data="{content: '', reader: undefined, busy: false}" class="container-fluid"
style="display: flex; padding-right: 0px;">
<textarea x-init="$watch('content', value => alterTextArea($el))" x-on:keydown="checkSubmit($event, $data)"
x-bind:disabled="busy" x-ref="messageInput" x-model="content" placeholder="Enter your message" rows="1"
cols="50" spellcheck="false" style="resize: none; overflow: hidden; margin-bottom: 0px;"></textarea>
<div style="width: 64px; display: flex; align-items: center; justify-content: center;">
<button x-bind:disabled="busy" x-show="!reader"
x-on:click="sendMessage($data, {role: 'user', content: content} );" class="button-icon">
<span class="material-symbols-outlined"> send </span>
</button>
<button x-show="!!reader" x-on:click="reader.cancel()" class="cancel button-icon">
<span class="material-symbols-outlined"> cancel </span>
</button>
</div>
</footer>
<dialog x-bind:open="$store.modalConnection">
<article>
<header>
<strong>OpenAI compaitable API</strong>
<button x-on:click="$store.modalConnection=false" class="button-icon small">
<span class="material-symbols-outlined"> close </span>
</button>
</header>
<main>
<form x-data="{base_url: '', api_key: '', model: '', model_override: '', models: []}"
x-init="loadProfile($data)">
<label>Base URL</label>
<input x-model="base_url">
<small>Should end with / </small>
<label>API key</label>
<input x-model="api_key">
<label>Model</label>
<select x-model="model">
<template x-for="model in models">
<option x-text="model">gpt-4o</option>
</template>
</select>
<label>Model override</label>
<input x-model="model_override">
<hr>
<input x-on:click="loadModels($data)" type="button" value="Get models" class="outline">
<input x-on:click="$store.modalConnection=false; updateProfile($data)" type="button" value="Apply">
</form>
</main>
</article>
</dialog>
<dialog x-bind:open="$store.modalCharacter">
<article>
<header>
<strong>Character definitions</strong>
<button x-on:click="$store.modalCharacter=false" class="button-icon small">
<span class="material-symbols-outlined"> close </span>
</button>
</header>
<main>
<form x-data="{main: '', jailbreak: ''}" x-init="loadProfile($data)">
<label>Main prompt</label>
<textarea rows="4" x-model="main"></textarea>
<small>Inserted before first message</small>
<label>Jailbreak</label>
<textarea rows="4" x-model="jailbreak"></textarea>
<small>Inserted after last message</small>
<hr>
<input x-on:click="$store.modalCharacter=false; updateProfile($data)" type="button" value="Apply">
</form>
</main>
</article>
</dialog>
<dialog x-bind:open="$store.modalTune">
<article>
<header>
<strong>Request settings</strong>
<button x-on:click="$store.modalTune=false" class="button-icon small">
<span class="material-symbols-outlined"> close </span>
</button>
</header>
<main>
<form x-data="{max_messages: '', max_tokens: '', temperature: 1.0}" x-init="loadProfile($data)">
<label>Messages limit </label>
<input x-model="max_messages" type="range" min="1" max="100" />
<small>Max messages to send: <span x-text="max_messages">12</span> </small>
<label>Max response</label>
<input x-model="max_tokens" type="range" min="1" max="2048" />
<small>Max response size: <span x-text="max_tokens">12</span> </small>
<label>Temperature</label>
<input x-model="temperature" type="range" min="0.1" max="2.0" step="0.1" />
<small>Temperature: <span x-text="temperature">1.0</span> </small>
<hr>
<input x-on:click="$store.modalTune=false; updateProfile($data)" type="button" value="Apply">
<!-- Blur for range not work on mobile -->
</form>
</main>
</article>
</dialog>
<dialog x-bind:open="$store.modalNotes">
<article>
<header>
<strong>Notes</strong>
<button x-on:click="$store.modalNotes=false" class="button-icon small">
<span class="material-symbols-outlined"> close </span>
</button>
</header>
<main>
<template x-for="message in messages.filter(msg => !!msg.pinned) ">
<article style="width: auto;">
<header>
<strong x-text="message.role">Assistant</strong>
<button x-on:click="updateMessage($data, message, {pinned: false} )"
class="button-icon small cancel">
<span class="material-symbols-outlined"> delete </span>
</button>
</header>
<main x-bind:aria-busy="!message.content" x-html="marked.parse(message.content)">lol</main>
</article>
</template>
<hr>
</main>
</article>
</dialog>
</body>
</html>
<script>hljs.highlightAll();</script>