-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpt_js.js
446 lines (436 loc) · 19.5 KB
/
gpt_js.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
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
var lang = app.LoadText('lang','en','gptnoads');
const db = app.OpenDatabase('dbgptnoads');
const dbFields = 'id INTEGER NOT NULL primary key,'+
'model TEXT,'+
'question TEXT,'+
'answer TEXT,'+
'dt TEXT,'+
'tokens INTEGER,'+
'temperature REAL,'+
'top_p REAL,'+
'presence_penalty REAL,'+
'frequency_penalty REAL';
db.ExecuteSql('CREATE TABLE IF NOT EXISTS history ('+dbFields+')',null,exeSqlCreateOk,exeSqlCreateErr);
const modelsFields = 'idn INTEGER NOT NULL primary key,'+
'id TEXT,'+
'object TEXT,'+
'created INTEGER,'+
'owned_by TEXT,'+
'p_id TEXT,'+
'p_object TEXT,'+
'p_created INTEGER,'+
'p_allow_create_engine INTEGER,'+
'p_allow_sampling INTEGER,'+
'p_allow_logprobs INTEGER,'+
'p_allow_search_indices INTEGER,'+
'p_allow_view INTEGER,'+
'p_allow_fine_tuning INTEGER,'+
'p_organization TEXT,'+
'p_group TEXT,'+
'p_is_blocking INTEGER,'+
'root TEXT,'+
'parent TEXT';
db.ExecuteSql('CREATE TABLE IF NOT EXISTS models ('+modelsFields+')',null,exeSqlCreateOk,exeSqlCreateErr);
db.ExecuteSql('SELECT idn FROM models;', [], exeSqlCountModelsOK,exeSqlCountModelsKO);
const chatUrl = 'https://api.openai.com/v1/chat/completions';
const modelsUrl = 'https://api.openai.com/v1/models';
const sub = document.querySelector('#submit-box');
const bum = document.querySelector('#menu-box');
const txt = document.querySelector('#input-box textarea');
const main = document.querySelector('#main');
const down = document.querySelector('#down');
const waitCur = document.querySelector('#wait-cursor');
const spanModel = document.querySelector('#span_model');
const menuLabel = document.querySelector('#menu-box>center>p');
var model = app.LoadText('model','gpt-3.5-turbo','gptnoads');
var max_tokens = app.LoadNumber('max_tokens',0,'gptnoads');
var temperature = app.LoadNumber('temperature',-10,'gptnoads');
var top_p = app.LoadNumber('top_p',-10,'gptnoads');
var presence_penalty = app.LoadNumber('presence_penalty',-10,'gptnoads');
var frequency_penalty = app.LoadNumber('frequency_penalty',-10,'gptnoads');
var theme = app.LoadText('theme','dark','gptnoads');
main.className = theme;
bum.addEventListener('click', clickMenu);
sub.addEventListener('click', getResponse);
spanModel.innerHTML = model;
changeLang(lang);
//------------------------------------------------------------------------------
function OnMenu(item) {
switch (item) {
case t_history[lang]:
reset();
db.ExecuteSql('select * from history order by dt desc;', [], exeSqlSelectAll);
break;
case t_reset[lang]:
reset();
txt.value = '';
break;
case t_param[lang]:
reset();
db.ExecuteSql('SELECT * FROM models;', [], exeSqlSelectModelsOK, exeSqlSelectModelsKO);
break;
case t_language[lang]:
reset();
lang_flags.forEach(function (e, i) {
main.innerHTML += '<button '+
'style="height:6vh;font-size:4vw;margin:8px;border-radius:20%;box-shadow:4px 4px gray;" '+
'onclick="changeLang(\''+
e[0]+'\')"> '+e[1]+' '+e[2]+
'</button>';
});
break;
case t_theme[lang]:
if (main.className == 'dark') {theme='bright';} else {theme='dark';}
app.SaveText('theme',theme,'gptnoads');
main.className = theme;
break;
case t_key[lang]:
reset();
load_html('apikey.html');
break;
case t_help[lang]:
reset();
load_html('help_'+lang+'.html');
break;
default:
}
}
//------------------------------------------------------------------------------
function load_html(fh) {
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
modifiedContent = replaceVars(xhr.responseText);
main.innerHTML = modifiedContent;
}
}
xhr.open('GET', 'Html/'+fh, true);
xhr.setRequestHeader('Content-type', 'text/html');
xhr.send();
}
//------------------------------------------------------------------------------
function replaceVars(htmlContent) {
data = ['t_menu','t_history','t_reset','t_param','t_language','t_key','t_help','t_write','t_getapi','t_date','t_question','t_answer','t_yourkey','t_apinotvalid','t_dbaserr','t_apisaved','t_ok','t_cancel','t_norisp'];
var modifiedContent = htmlContent;
data.forEach(function(k, i) {
var regex = new RegExp(k, "g");
modifiedContent = modifiedContent.replace(regex, eval(k+'[lang]'));
});
return modifiedContent;
}
//------------------------------------------------------------------------------
function reset() {
main.innerHTML = '';
}
//------------------------------------------------------------------------------
function clickMenu() {
app.ShowMenu();
}
//------------------------------------------------------------------------------
function getTemperature(o) {
var t = document.querySelector('#dia-top_p').value;
var v = o.value;
if (t!=-10) {o.value=-10}
}
//------------------------------------------------------------------------------
function getTop_t(o) {
var t = document.querySelector('#dia-temperature').value;
var v = o.value;
if (t!=-10) {o.value=-10}
}
//------------------------------------------------------------------------------
function apiBuOkFun(o) {
apiKey = o.previousElementSibling.value;
if (apiKey.length<50) {
app.ShowPopup(t_apinotvalid[lang],'Long');
} else {
app.SaveText('apiKey',apiKey,'gptnoads')
app.ShowPopup(t_apisaved[lang],'Long');
}
reset();
}
//------------------------------------------------------------------------------
function paramBuOkFun(o) {
var oModel = o.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.children[0];
var model = oModel.value;
var oMaxTokens = o.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.children[0];
var max_tokens = parseInt(oMaxTokens.value);
var oTemperature = o.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.children[0];
var temperature = Number(oTemperature.value);
var oTopp = o.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.children[0];
var top_p = Number(oTopp.value);
var oPresencePenalty = o.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.children[0];
var presence_penalty = Number(oPresencePenalty.value);
var oFrequencyPenalty = o.parentElement.previousElementSibling.children[0];
var frequency_penalty = Number(oFrequencyPenalty.value);
app.SaveText('model',model,'gptnoads');
app.SaveNumber('max_tokens',max_tokens,'gptnoads');
app.SaveNumber('temperature',temperature,'gptnoads');
app.SaveNumber('top_p',top_p,'gptnoads');
app.SaveNumber('presence_penalty',presence_penalty,'gptnoads');
app.SaveNumber('frequency_penalty',frequency_penalty,'gptnoads');
spanModel.innerHTML = model;
reset();
}
//------------------------------------------------------------------------------
function buShareFun(el,pm) {
var strModel = el.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.textContent;
var strDate = el.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.textContent;
var strQuestion = el.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.textContent;
var strAnswer = el.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.textContent;
var strToken = el.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.textContent;
var strTemperature = el.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.textContent;
var strTop_p = el.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.textContent;
var strPresencePenalty = el.parentElement.previousElementSibling.previousElementSibling.textContent;
var strFrequencyPenalty = el.parentElement.previousElementSibling.textContent;
var txt = strModel+'\n'+strDate+'\n'+strQuestion+'\n'+strAnswer+'\n'+strToken+'\n'+strTemperature+'\n'+strTop_p+'\n'+strPresencePenalty+'\n'+strFrequencyPenalty;
switch (pm) {
case 'whatsapp':
app.OpenUrl('whatsapp://send?text='+txt );
break;
case 'telegram':
app.OpenUrl('tg://msg?text='+txt );
break;
default:
}
}
//------------------------------------------------------------------------------
function changeLang(l) {
app.SaveText('lang',l,'gptnoads');
menuStr = t_history[l]+','+t_reset[l]+','+t_param[l]+','+t_language[l]+','+t_theme[l]+','+t_key[l]+','+t_help[l];
app.SetMenu(menuStr);
menuLabel.innerText = t_menu[l];
// app.ShowPopup(l,'Short');
lang = l;
reset();
}
//------------------------------------------------------------------------------
function OnEntryError(e) {
app.ShowPopup(t_dbaserr[lang]+':\n'+e,'Long');
}
//------------------------------------------------------------------------------
function exeSqlCreateOk(s) {
app.Debug('exeSqlCreateOk = '+JSON.stringify(s, null, 2));
}
//------------------------------------------------------------------------------
function exeSqlCreateErr(e) {
app.Debug('exeSqlCreateErr = '+JSON.stringify(e, null, 2));
}
//------------------------------------------------------------------------------
function escapeQuotes(str) {
return str.replace(/['"]/g, '\\$&');
}
//------------------------------------------------------------------------------
function exeSqlSelectAll(res) {
var len = res.rows.length;
for(var i = 0; i < len; i++ ) {
var item = res.rows.item(i);
main.innerHTML += '<div><b>Model:</b> '+item.model+'</div>';
main.innerHTML += '<div><b>'+t_date[lang]+':</b> '+item.dt+'</div>';
main.innerHTML += '<div><b>'+t_question[lang]+'</b></div>';
main.innerHTML += '<div>'+item.question+'</div>';
main.innerHTML += '<div><b>'+t_answer[lang]+'</b></div>';
main.innerHTML += '<div>'+item.answer+'</div>';
main.innerHTML += '<div><b>Tokens:</b> '+item.tokens+'</div>';
main.innerHTML += '<div><b>Temperature:</b> '+item.temperature+'</div>';
main.innerHTML += '<div><b>top_p:</b> '+item.top_p+'</div>';
main.innerHTML += '<div><b>Presence penalty:</b> '+item.presence_penalty+'</div>';
main.innerHTML += '<div><b>Frequency penalty:</b> '+item.frequency_penalty+'</div>';
main.innerHTML += '<div>';
main.innerHTML += '<button class="bu_share bu_whatsapp" onclick="buShareFun(this,\'whatsapp\');"></button>';
main.innerHTML += '<button class="bu_share bu_telegram" onclick="buShareFun(this,\'telegram\');"></button>';
main.innerHTML += '</div>';
main.innerHTML += '<hr />';
}
}
//------------------------------------------------------------------------------
function exeSqlCountModelsOK(s) {
var len_s = s.rows.length;
if (len_s < 10) {
getModels();
}
}
//------------------------------------------------------------------------------
function exeSqlCountModelsKO(e) {
main.innerHTML = '<div>'+
t_norisp[lang]+
'<br />exeSqlCountModelsKO(e)<br />'+
e+
'</div>';
console.log('-'.repeat(40));
console.log('exeSqlCountModelsKO(e)');
console.log('e = '+JSON.stringify(e, null, 2));
console.log('-'.repeat(40));
}
//------------------------------------------------------------------------------
async function getResponse() {
waitCur.style.display = 'block';
reset();
var quest = txt.value;
if (quest == '') {
app.ShowPopup(t_write[lang],'Long');
waitCur.style.display = 'none';
return;
}
var apiKey = app.LoadText('apiKey','','gptnoads');
if (apiKey.length<50) {
app.ShowPopup(t_getapi[lang],'Long');
waitCur.style.display = 'none';
main.innerHTML = quest;
waitCur.style.display = 'none';
return;
}
var nowDateTime = new Date().toISOString().slice(0, 19).replace('T', ' ');
model = app.LoadText('model','gpt-3.5-turbo','gptnoads');
max_tokens = app.LoadNumber('max_tokens',0,'gptnoads');
temperature = app.LoadNumber('temperature',0,'gptnoads');
top_p = app.LoadNumber('top_p',0,'gptnoads');
presence_penalty = app.LoadNumber('presence_penalty',0,'gptnoads');
frequency_penalty = app.LoadNumber('frequency_penalty',0,'gptnoads');
var body = {};
body.model = model;
if (max_tokens!=0 ) { body.max_tokens = max_tokens};
if (temperature!=-10) { body.temperature = temperature};
if (top_p!=-10) { body.top_p = top_p};
if (presence_penalty!=-10) { body.presence_penalty = presence_penalty};
if (frequency_penalty!=-10) { body.frequency_penalty = frequency_penalty};
body.messages = [];
var objMessages = {role:'user',content:quest};
body.messages.push(objMessages);
const options = {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + apiKey,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}
try {
const response = await fetch(chatUrl, options)
const data = await response.json()
var respText = data.choices[0].message.content;
var tokens = data.usage.total_tokens;
if(respText && quest) {
main.innerHTML = '<hr /><p style="border-bottom:solid yellow 1px">Q. - '+
txt.value+
'</p><p style="border-bottom:solid yellow 1px">A. - '+
respText+'</p><p>Tokens: '+tokens+
'</p>'+
'<hr />';
txt.value = '';
db.ExecuteSql( 'INSERT INTO history (model,question,answer,dt,tokens,temperature,top_p,presence_penalty,frequency_penalty) VALUES (?,?,?,?,?,?,?,?,?)', [model,quest,respText,nowDateTime,tokens,temperature,top_p,presence_penalty,frequency_penalty], null, OnEntryError );
}
} catch(e) {
main.innerHTML = '<div>'+t_norisp[lang]+'</div>';
console.log('e = '+e);
}
waitCur.style.display = 'none';
}
//------------------------------------------------------------------------------
async function getModels() {
var a_model_keys = [];
var a_model_vals = [];
var temp_val;
var sqlite = '';
var apiKey = app.LoadText('apiKey','','gptnoads');
fetch(modelsUrl, {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + apiKey
}
})
.then(response => response.json())
.then(data => {
data.data.forEach((e) => {
for (k in e) {
if (k == 'permission') {
var permis = e[k][0];
for (h in permis) {
a_model_keys.push('p_' + h);
temp_val = val_sqlite_compatible(permis[h]);
a_model_vals.push(temp_val);
}
} else {
a_model_keys.push(k);
temp_val = val_sqlite_compatible(e[k]);
a_model_vals.push(temp_val);
}
}
sqlite = 'INSERT INTO models ('+a_model_keys.join(',')+') VALUES ('+a_model_vals.join(',')+');';
a_model_keys = [];
a_model_vals = [];
db.ExecuteSql( sqlite, null, OnEntryModelOK, OnEntryModelKO );
})
})
.catch(e => {
app.ShowPopup(t_norisp[lang],'Long');
console.log('e = '+e);
});
}
//------------------------------------------------------------------------------
function val_sqlite_compatible(v) {
if(typeof(v)=='boolean' && (v=='false' || v==false)) { return 0; }
if(typeof(v)=='boolean' && (v=='true' || v==true)) { return 1; }
if(v==null) { return 'NULL'; }
if(typeof(v)=='string') { return "'"+v+"'"; }
if(typeof(v)=='number') { return v; }
return v;
}
//------------------------------------------------------------------------------
function OnEntryModelOK(s) {
app.ShowPopup(t_ok[lang],'Short');
console.log('OnEntryModelOK('+JSON.stringify(s, null, 2)+')');
}
//------------------------------------------------------------------------------
function OnEntryModelKO(e) {
app.ShowPopup(t_dbaserr[lang],'Short');
console.log('OnEntryModelKO('+JSON.stringify(e, null, 2)+')');
}
//------------------------------------------------------------------------------
function exeSqlSelectModelsOK(res) {
var xhrParam = new XMLHttpRequest();
xhrParam.onreadystatechange = function() {
if (xhrParam.readyState == 4 && xhrParam.status == 200) {
modifiedContent = replaceVars(xhrParam.responseText);
main.innerHTML = modifiedContent;
model = app.LoadText('model','gpt-3.5-turbo','gptnoads');
max_tokens = app.LoadNumber('max_tokens',0,'gptnoads');
temperature = app.LoadNumber('temperature',-10,'gptnoads');
top_p = app.LoadNumber('top_p',-10,'gptnoads');
presence_penalty = app.LoadNumber('presence_penalty',-10,'gptnoads');
frequency_penalty = app.LoadNumber('frequency_penalty',-10,'gptnoads');
var oDiv = main.children[0]
var oModel = oDiv.children[1].children[0];
var lenRes = res.rows.length;
for(var i = 0; i < lenRes; i++ ) {
var item = res.rows.item(i)
let opt = document.createElement('option');
opt.value = item.id;
opt.innerHTML = item.id;
if (item.owned_by!='openai') { continue; }
if (item.id==model) { opt.setAttribute('selected', 'selected'); }
oModel.append(opt);
}
var oMaxTokens = oDiv.children[3].children[0];
oMaxTokens.value = max_tokens;
var oTemperature = oDiv.children[5].children[0];
oTemperature.value = temperature;
var oTopp = oDiv.children[7].children[0];
oTopp.value = top_p;
var oPresencePenalty = oDiv.children[9].children[0];
oPresencePenalty.value = presence_penalty;
var oFrequencyPenalty = oDiv.children[11].children[0];
oFrequencyPenalty.value = frequency_penalty;
}
}
xhrParam.open('GET', 'Html/parameters.html', true);
xhrParam.setRequestHeader('Content-type', 'text/html');
xhrParam.send();
console.log('exeSqlSelectModelsOK()');
}
//------------------------------------------------------------------------------
function exeSqlSelectModelsKO(e) {
main.innerHTML = '<hr /><p style="color:red;font-size:x-large;">'+t_dbaserr[lang]+' '+t_param[lang]+'</p><hr />';
console.log('exeSqlSelectModelsKO()');
}
//------------------------------------------------------------------------------