-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
190 lines (144 loc) · 5.16 KB
/
script.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
class Messages {
static #getNextId() {
const idString = localStorage.getItem('messages_last_id')
const lastId = (idString === null) ? 0 : parseInt(idString)
localStorage.setItem('messages_last_id', (lastId + 1).toString())
return lastId
}
static list(notes=false) {
const messagesString = localStorage.getItem('messages')
if (messagesString === null) return []
let messages = JSON.parse(messagesString)
if(notes) messages = messages.filter(msg => !!msg.pinned)
return messages
}
static updateAll(messages) {
localStorage.setItem('messages', JSON.stringify(messages))
}
static add(message) {
const added = { id: Messages.#getNextId(), pinned: false, ...message }
let messages = Messages.list()
messages.push(added)
Messages.updateAll(messages)
return added
}
static update(id, message) {
let messages = Messages.list()
const index = messages.findIndex(item => item.id == id)
if (index == -1) return
messages[index] = { ...messages[index], ...message }
Messages.updateAll(messages)
}
static remove(id, drop=false) {
let messages = Messages.list()
const index = messages.findIndex(item => item.id == id)
if (index == -1) return
if(drop) messages.splice(index)
else messages.splice(index, 1)
Messages.updateAll(messages)
}
}
class Profile {
static #init() {
const profile = {
base_url: '',
api_key: '',
model: 'gpt-4o',
model_override: '',
max_messages: 20,
max_tokens: 1024,
jailbreak: '',
main: '',
temperature: 1.0
}
localStorage.setItem('profile', JSON.stringify(profile))
}
static get() {
let profileString = localStorage.getItem('profile')
if (profileString === null) {
Profile.#init()
profileString = localStorage.getItem('profile')
}
return JSON.parse(profileString)
}
static update(updated) {
let profile = Profile.get()
profile = {
...profile,
...updated
}
localStorage.setItem('profile', JSON.stringify(profile))
}
}
class Completions {
static async * send() {
const profile = Profile.get()
// Messages
let messages = Messages.list()
// messages.unshift({ role: 'system', content: '### Chat history' })
if (!!profile.main) messages.unshift({ role: 'system', content: profile.main })
if (!!profile.jailbreak) messages.push({ role: 'system', content: profile.jailbreak })
messages = messages.slice(-profile.max_messages)
// Notes
let notes = Messages.list(true)
if (notes.length > 0) {
notes.forEach(msg => msg.role = 'system')
messages.unshift(...notes)
messages.unshift({ role: 'system', content: '### Notes (pinned messages containing important information)' })
}
const request = {
messages,
model: !profile.model_override ? profile.model : profile.model_override,
max_tokens: profile.max_tokens,
stream: true,
temperature: (profile.temperature ? profile.temperature : 1.0)
}
console.log(request)
const url = profile.base_url + 'chat/completions'
const headers = {
"Authorization": `Bearer ${profile.api_key}`,
"Content-Type": "application/json"
}
// TODO: throwing errors to messages
let response = undefined
try {
response = await fetch(url, { method: 'POST', headers, body: JSON.stringify(request) })
console.log("Status: " + response.status)
if ( !(response.status == 200 || response.status == 203) ) throw 'err'
} catch {
console.log("Connection error")
return undefined
}
var reader = response.body?.pipeThrough(new TextDecoderStream()).getReader();
let buffer = ''
yield reader
while (true) {
const { value, done } = await reader.read()
if (done) break
buffer += value
let lines = buffer.split('\n\n')
buffer = lines.pop()
for (let i = 0; i < lines.length; i++) {
const line = lines[i].replace('data: ', '')
if (!line || line == '[DONE]') break
else {
try {
const data = JSON.parse(line)
const ch = data?.choices[0]?.delta?.content
if (!!ch) yield ch
} catch { }
}
}
}
}
static async models(){
const profile = Profile.get()
const url = profile.base_url + 'models'
const headers = {
"Authorization": `Bearer ${profile.api_key}`,
}
const response = await fetch(url, {headers})
const data = (await response.json()).data
return data.map( item => item.id )
}
}