Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/aimogen-pro/scripts/batch.js
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
"use strict";
2
+
function aiomaticTimeConverter(UNIX_timestamp){
3
+
var a = new Date(UNIX_timestamp * 1000);
4
+
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
5
+
var year = a.getFullYear();
6
+
var month = months[a.getMonth()];
7
+
var date = a.getDate();
8
+
var hour = a.getHours() < 10 ? '0' + a.getHours() : a.getHours();
9
+
var min = a.getMinutes() < 10 ? '0' + a.getMinutes() : a.getMinutes();
10
+
var sec = a.getSeconds() < 10 ? '0' + a.getSeconds() : a.getSeconds();
11
+
var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
12
+
return time;
13
+
}
14
+
function aiomaticLoading2(btn){
15
+
btn.attr('disabled','disabled');
16
+
if(!btn.find('spinner').length){
17
+
btn.append('<span class="spinner"></span>');
18
+
}
19
+
btn.find('.spinner').css('visibility','unset');
20
+
}
21
+
function aiomaticRmLoading(btn)
22
+
{
23
+
btn.removeAttr('disabled');
24
+
btn.find('.spinner').remove();
25
+
}
26
+
jQuery(document).ready(function()
27
+
{
28
+
function initializeCarousel() {
29
+
var currentIndex = 0;
30
+
var $items = jQuery('.aiomatic-carousel-item');
31
+
var itemCount = $items.length;
32
+
33
+
function showItem(index) {
34
+
$items.hide();
35
+
$items.eq(index).show();
36
+
jQuery('#aiomatic-paging-holder').html((index + 1) + '/' + itemCount);
37
+
}
38
+
39
+
jQuery('.aiomatic-carousel-prev').on('click', function() {
40
+
currentIndex = (currentIndex > 0) ? currentIndex - 1 : itemCount - 1;
41
+
showItem(currentIndex);
42
+
});
43
+
44
+
jQuery('.aiomatic-carousel-next').on('click', function() {
45
+
currentIndex = (currentIndex < itemCount - 1) ? currentIndex + 1 : 0;
46
+
showItem(currentIndex);
47
+
});
48
+
showItem(currentIndex);
49
+
}
50
+
51
+
jQuery('body').on('click', '.aiomatic_download_file', function() {
52
+
var dataID = jQuery(this).attr('data-id');
53
+
if(dataID != '')
54
+
{
55
+
var btn = jQuery(this);
56
+
jQuery.ajax({
57
+
url: aiomatic_object.ajax_url,
58
+
data: {action: 'aiomatic_download_file', id: dataID, nonce: aiomatic_object.nonce},
59
+
type: 'POST',
60
+
beforeSend: function () {
61
+
aiomaticAjaxRunning = true;
62
+
aiomaticLoading2(btn);
63
+
},
64
+
success: function (res) {
65
+
aiomaticAjaxRunning = false;
66
+
aiomaticRmLoading(btn);
67
+
if (res.status !== 'success')
68
+
{
69
+
alert('Error in processing: ' + JSON.stringify(res));
70
+
}
71
+
else
72
+
{
73
+
var hiddenElement = document.createElement('a');
74
+
hiddenElement.href = 'data:attachment/text,' + encodeURI(res.data);
75
+
hiddenElement.target = '_blank';
76
+
hiddenElement.download = res.filename;
77
+
hiddenElement.click();
78
+
}
79
+
},
80
+
error: function (r, s, error) {
81
+
aiomaticAjaxRunning = false;
82
+
aiomaticRmLoading(btn);
83
+
alert('Error in processing file download: ' + error);
84
+
}
85
+
});
86
+
}
87
+
else
88
+
{
89
+
alert('Empty data-id provided');
90
+
}
91
+
});
92
+
jQuery('body').on('click', '.aiomatic_parse_output', function() {
93
+
var dataID = jQuery(this).attr('data-id');
94
+
var dataInID = jQuery(this).attr('data-in-id');
95
+
var endpoint = jQuery(this).attr('endpoint');
96
+
if(dataID != '' && dataInID != '')
97
+
{
98
+
document.getElementById('mymodalfzr-parse').style.display = "block";
99
+
var btn = jQuery(this);
100
+
jQuery.ajax({
101
+
url: aiomatic_object.ajax_url,
102
+
data: {action: 'aiomatic_parse_output', id: dataID, idin: dataInID, endpoint: endpoint, nonce: aiomatic_object.nonce},
103
+
type: 'POST',
104
+
beforeSend: function () {
105
+
jQuery('#aiomatic-batch-result-parsed').html(aiomatic_object.loadingstr);
106
+
aiomaticAjaxRunning = true;
107
+
aiomaticLoading2(btn);
108
+
},
109
+
success: function (res) {
110
+
aiomaticAjaxRunning = false;
111
+
aiomaticRmLoading(btn);
112
+
if (res.status !== 'success')
113
+
{
114
+
alert('Error in processing: ' + JSON.stringify(res));
115
+
}
116
+
else
117
+
{
118
+
jQuery('#aiomatic-batch-result-parsed').html(res.data);
119
+
initializeCarousel();
120
+
}
121
+
},
122
+
error: function (r, s, error) {
123
+
aiomaticAjaxRunning = false;
124
+
aiomaticRmLoading(btn);
125
+
alert('Error in processing file parsing: ' + error);
126
+
}
127
+
});
128
+
}
129
+
else
130
+
{
131
+
alert('Empty data-id or data-in-id provided');
132
+
}
133
+
});
134
+
jQuery('body').on('click', '.aiomatic_delete_file', function() {
135
+
var dataID = jQuery(this).attr('data-id');
136
+
if(dataID != '')
137
+
{
138
+
var btn = jQuery(this);
139
+
jQuery.ajax({
140
+
url: aiomatic_object.ajax_url,
141
+
data: {action: 'aiomatic_delete_assistant_file', id: dataID, nonce: aiomatic_object.nonce},
142
+
type: 'POST',
143
+
beforeSend: function () {
144
+
aiomaticAjaxRunning = true;
145
+
aiomaticLoading2(btn);
146
+
},
147
+
success: function (res) {
148
+
aiomaticAjaxRunning = false;
149
+
aiomaticRmLoading(btn);
150
+
if (res.status !== 'success')
151
+
{
152
+
alert('Error in processing: ' + JSON.stringify(res));
153
+
}
154
+
else
155
+
{
156
+
location.reload();
157
+
}
158
+
},
159
+
error: function (r, s, error) {
160
+
aiomaticAjaxRunning = false;
161
+
aiomaticRmLoading(btn);
162
+
alert('Error in processing file delete: ' + error);
163
+
}
164
+
});
165
+
}
166
+
else
167
+
{
168
+
alert('Empty data-id provided');
169
+
}
170
+
});
171
+
jQuery('body').on('click', '#aiomatic_file_button', function() {
172
+
var aiomatic_file_upload = jQuery('#aiomatic_batch_file_upload');
173
+
if(aiomatic_file_upload[0].files.length === 0){
174
+
alert('Please select a file!');
175
+
}
176
+
else{
177
+
var aiomatic_max_file_size = aiomatic_object.maxfilesize;
178
+
var aiomatic_max_size_in_mb = aiomatic_object.maxfilesize / (1024 ** 2);
179
+
var aiomatic_progress = jQuery('.aiomatic_progress');
180
+
var aiomatic_file_button = jQuery('#aiomatic_file_button');
181
+
var aiomatic_file = aiomatic_file_upload[0].files[0];
182
+
var aiomatic_upload_success = jQuery('.aiomatic_upload_success');
183
+
var aiomatic_error_message = jQuery('.aiomatic-error-msg');
184
+
if(aiomatic_file.size > aiomatic_max_file_size){
185
+
aiomatic_file_upload.val('');
186
+
alert('Dataset allowed maximum size (MB): '+ aiomatic_max_size_in_mb)
187
+
}
188
+
else{
189
+
var formData = new FormData();
190
+
formData.append('action', 'aiomatic_batch_file_upload');
191
+
formData.append('file', aiomatic_file);
192
+
formData.append('nonce', aiomatic_object.nonce);
193
+
jQuery.ajax({
194
+
url: aiomatic_object.ajax_url,
195
+
type: 'POST',
196
+
dataType: 'JSON',
197
+
data: formData,
198
+
beforeSend: function (){
199
+
aiomatic_progress.find('span').css('width','0');
200
+
aiomatic_progress.show();
201
+
aiomaticLoading2(aiomatic_file_button);
202
+
aiomatic_error_message.hide();
203
+
aiomatic_upload_success.hide();
204
+
},
205
+
xhr: function() {
206
+
var xhr = jQuery.ajaxSettings.xhr();
207
+
xhr.upload.addEventListener("progress", function(evt) {
208
+
if (evt.lengthComputable) {
209
+
var percentComplete = evt.loaded / evt.total;
210
+
aiomatic_progress.find('span').css('width',(Math.round(percentComplete * 100))+'%');
211
+
}
212
+
}, false);
213
+
return xhr;
214
+
},
215
+
success: function(res) {
216
+
if(res.status === 'success'){
217
+
aiomaticRmLoading(aiomatic_file_button);
218
+
aiomatic_progress.hide();
219
+
aiomatic_file_upload.val('');
220
+
aiomatic_upload_success.show();
221
+
location.reload();
222
+
}
223
+
else{
224
+
aiomaticRmLoading(aiomatic_file_button);
225
+
aiomatic_progress.find('small').html('Error');
226
+
aiomatic_progress.addClass('aiomatic_error');
227
+
aiomatic_error_message.html(res.msg);
228
+
aiomatic_error_message.show();
229
+
}
230
+
},
231
+
cache: false,
232
+
contentType: false,
233
+
processData: false,
234
+
error: function (r, s, error){
235
+
aiomatic_file_upload.val('');
236
+
aiomaticRmLoading(aiomatic_file_button);
237
+
aiomatic_progress.addClass('aiomatic_error');
238
+
aiomatic_progress.find('small').html('Error');
239
+
alert('Error in processing batch file uploading: ' + error);
240
+
aiomatic_error_message.show();
241
+
}
242
+
});
243
+
}
244
+
}
245
+
});
246
+
var assistent_files = [];
247
+
var assistent_files_input = [];
248
+
var aiomaticAjaxRunning = false;
249
+
jQuery('#aiomatic_sync_batch_files').on('click', function (e){
250
+
e.preventDefault();
251
+
jQuery("#aiomatic-batch-files > tbody").empty();
252
+
var btn = jQuery(this);
253
+
if(!aiomaticAjaxRunning) {
254
+
jQuery.ajax({
255
+
url: aiomatic_object.ajax_url,
256
+
data: {action: 'aiomatic_list_batch_files', nonce: aiomatic_object.nonce},
257
+
dataType: 'JSON',
258
+
type: 'POST',
259
+
beforeSend: function () {
260
+
aiomaticAjaxRunning = true;
261
+
aiomaticLoading2(btn);
262
+
},
263
+
success: function (res) {
264
+
aiomaticAjaxRunning = false;
265
+
aiomaticRmLoading(btn);
266
+
if (res.status === 'success')
267
+
{
268
+
assistent_files = Object.values(res.data);
269
+
assistent_files.forEach(async (tfile) =>
270
+
{
271
+
if(tfile.purpose == 'batch')
272
+
{
273
+
assistent_files_input.push(tfile);
274
+
}
275
+
var appendme = '<tr><td>' + tfile.id + '</td><td>' + tfile.bytes + '</td><td>' + tfile.purpose + '</td><td>' + aiomaticTimeConverter(tfile.created_at) + '</td><td>' + tfile.filename + '</td><td>' + tfile.status + '</td><td><button data-id="' + tfile.id + '" class="button button-small aiomatic_download_file"';
276
+
appendme += '>Download</button><button data-id="' + tfile.id + '" class="button button-small button-link-delete aiomatic_delete_file">Delete</button></td></tr>';
277
+
jQuery('#aiomatic-batch-files > tbody:last-child').append(appendme);
278
+
});
279
+
aiomatic_refresh_options();
280
+
} else {
281
+
alert(res.msg);
282
+
}
283
+
},
284
+
error: function (r, s, error) {
285
+
aiomaticAjaxRunning = false;
286
+
aiomaticRmLoading(btn);
287
+
alert('Error in processing file sync: ' + error);
288
+
}
289
+
});
290
+
}
291
+
});
292
+
jQuery( "#aiomatic_sync_batch_files" ).trigger( "click" );
293
+
function aiomatic_refresh_options()
294
+
{
295
+
jQuery('#aiomatic-batch-file').find('option').remove();
296
+
assistent_files_input.forEach(async (tfile) => {
297
+
jQuery("#aiomatic-batch-file").append(jQuery('<option>', {
298
+
value: tfile.id,
299
+
text: tfile.filename + ' (' + tfile.id + ')'
300
+
}));
301
+
});
302
+
if(assistent_files_input.length == 0)
303
+
{
304
+
jQuery("#aiomatic-batch-file").append(jQuery('<option>', {
305
+
value: '',
306
+
disabled: 'disabled',
307
+
text: "Please upload files in the 'Manage AI Batch Requests Files' tab to use this option"
308
+
}));
309
+
}
310
+
}
311
+
jQuery(".aiomatic_cancel_batch").on('click', function(e) {
312
+
e.preventDefault();
313
+
var batchid = jQuery(this).attr("edit-id");
314
+
if(batchid == '')
315
+
{
316
+
alert('Incorrect edit id submitted');
317
+
}
318
+
else
319
+
{
320
+
var data = {
321
+
action: 'aiomatic_cancel_batch',
322
+
batchid: batchid,
323
+
nonce: aiomatic_object.nonce,
324
+
};
325
+
jQuery.ajax({
326
+
url: aiomatic_object.ajax_url,
327
+
data: data,
328
+
dataType: 'JSON',
329
+
type: 'POST',
330
+
success: function (res){
331
+
if(res.status === 'success'){
332
+
location.reload();
333
+
}
334
+
else{
335
+
alert(res.msg);
336
+
}
337
+
},
338
+
error: function (r, s, error){
339
+
alert('Error in processing batch viewing: ' + error);
340
+
}
341
+
});
342
+
}
343
+
});
344
+
jQuery(".aiomatic_view_batch").on('click', function(e) {
345
+
e.preventDefault();
346
+
var batchid = jQuery(this).attr("edit-id");
347
+
if(batchid == '')
348
+
{
349
+
alert('Incorrect edit id submitted');
350
+
}
351
+
else
352
+
{
353
+
jQuery('#batch-window').html(aiomatic_object.loadingstr);
354
+
jQuery('#batch-id').html(aiomatic_object.loadingstr);
355
+
jQuery('#batch-status').html(aiomatic_object.loadingstr);
356
+
jQuery('#batch-endpoint').html(aiomatic_object.loadingstr);
357
+
jQuery('#batch-counts').html(aiomatic_object.loadingstr);
358
+
jQuery('#batch-created').html(aiomatic_object.loadingstr);
359
+
jQuery('#batch-input-file').html(aiomatic_object.loadingstr);
360
+
jQuery('#batch-output-file').html(aiomatic_object.loadingstr);
361
+
jQuery('#batch-error-file').html(aiomatic_object.loadingstr);
362
+
jQuery('#batch-timeline-wrapper').html(aiomatic_object.loadingstr);
363
+
jQuery('#batch-failed-report').html('');
364
+
365
+
document.getElementById('mymodalfzr-edit').style.display = "block";
366
+
var data = {
367
+
action: 'aiomatic_get_batch',
368
+
batchid: batchid,
369
+
nonce: aiomatic_object.nonce,
370
+
};
371
+
jQuery.ajax({
372
+
url: aiomatic_object.ajax_url,
373
+
data: data,
374
+
dataType: 'JSON',
375
+
type: 'POST',
376
+
success: function (res){
377
+
if(res.status === 'success'){
378
+
if(res.data !== undefined)
379
+
{
380
+
jQuery('#batch-window').html(res.data['batch_completion_window']);
381
+
jQuery('#batch-id').html('<a href="https://platform.openai.com/batches/' + res.data['batch_id'] + '" target="_blank">' + res.data['batch_id'] + '</a>');
382
+
if(res.data['batch_status'] == 'failed')
383
+
{
384
+
var failed_report = '<hr/>';
385
+
if (res.data['batch_errors'])
386
+
{
387
+
res.data['batch_errors'].data.forEach(error =>
388
+
{
389
+
failed_report += `<span class="cr_red">Line: ${error.line}</span> ${error.message}<br/>`;
390
+
});
391
+
}
392
+
jQuery('#batch-failed-report').html(failed_report);
393
+
jQuery('#batch-status').html(res.data['batch_status']);
394
+
}
395
+
else
396
+
{
397
+
jQuery('#batch-status').html(res.data['batch_status']);
398
+
}
399
+
jQuery('#batch-endpoint').html(res.data['batch_endpoint']);
400
+
jQuery('#batch-counts').html('<span>Completed:</span> ' + res.data['batch_request_completed'] + ' <span>Failed:</span> ' + res.data['batch_request_failed'] + ' <span>Total:</span> ' + res.data['batch_request_count'] + ' ');
401
+
jQuery('#batch-created').html(aiomaticTimeConverter(res.data['batch_created_at']));
402
+
jQuery('#batch-input-file').html('<a href="https://platform.openai.com/storage/files/' + res.data['batch_input_file_id'] + '" target="_blank">' + res.data['batch_input_file_id'] + '</a> <button data-id="' + res.data['batch_input_file_id'] + '" class="button button-small aiomatic_download_file">Download</button>');
403
+
if (res.data['batch_output_file_id'])
404
+
{
405
+
jQuery('#batch-output-file').html('<a href="https://platform.openai.com/storage/files/' + res.data['batch_output_file_id'] + '" target="_blank">' + res.data['batch_output_file_id'] + '</a> <button data-id="' + res.data['batch_output_file_id'] + '" class="button button-small aiomatic_download_file">Download</button> <button data-id="' + res.data['batch_output_file_id'] + '" data-in-id="' + res.data['batch_input_file_id'] + '" endpoint="' + res.data['batch_endpoint'] + '" class="button button-small aiomatic_parse_output">Parse Output</button>');
406
+
}
407
+
else
408
+
{
409
+
jQuery('#batch-output-file').html('');
410
+
}
411
+
if (res.data['batch_error_file_id'])
412
+
{
413
+
jQuery('#batch-error-file').html('<a href="https://platform.openai.com/storage/files/' + res.data['batch_error_file_id'] + '" target="_blank">' + res.data['batch_error_file_id'] + '</a> <button data-id="' + res.data['batch_error_file_id'] + '" class="button button-small aiomatic_download_file">Download</button>');
414
+
}
415
+
else
416
+
{
417
+
jQuery('#batch-error-file').html('');
418
+
}
419
+
var timestr = aiomatic_object.createdstr + ' ' + aiomaticTimeConverter(res.data['batch_created_at']);
420
+
if (res.data['batch_in_progress_at'])
421
+
{
422
+
timestr += '<br/>' + aiomatic_object.progressstr + ' ' + aiomaticTimeConverter(res.data['batch_in_progress_at']);
423
+
}
424
+
if (res.data['batch_cancelling_at'])
425
+
{
426
+
timestr += '<br/>' + aiomatic_object.cancellingstr + ' ' + aiomaticTimeConverter(res.data['batch_cancelling_at']);
427
+
}
428
+
if (res.data['batch_cancelled_at'])
429
+
{
430
+
timestr += '<br/>' + aiomatic_object.cancelledstr + ' ' + aiomaticTimeConverter(res.data['batch_cancelled_at']);
431
+
}
432
+
if (res.data['batch_finalizing_at'])
433
+
{
434
+
timestr += '<br/>' + aiomatic_object.finalizingstr + ' ' + aiomaticTimeConverter(res.data['batch_finalizing_at']);
435
+
}
436
+
if (res.data['batch_completed_at'])
437
+
{
438
+
timestr += '<br/>' + aiomatic_object.completedstr + ' ' + aiomaticTimeConverter(res.data['batch_completed_at']);
439
+
const date = new Date(null);
440
+
date.setSeconds(res.data['batch_completed_at'] - res.data['batch_created_at']);
441
+
const resulttime = date.toISOString().slice(11, 19);
442
+
timestr += '<br/>' + aiomatic_object.completedinstr + ' ' + resulttime;
443
+
}
444
+
if (res.data['batch_failed_at'])
445
+
{
446
+
timestr += '<br/>' + aiomatic_object.failedstr + ' ' + aiomaticTimeConverter(res.data['batch_failed_at']);
447
+
}
448
+
if (res.data['batch_expired_at'])
449
+
{
450
+
timestr += '<br/>' + aiomatic_object.expiredstr + ' ' + aiomaticTimeConverter(res.data['batch_expired_at']);
451
+
}
452
+
jQuery('#batch-timeline-wrapper').html(timestr);
453
+
}
454
+
else
455
+
{
456
+
alert('Incorrect response from the back end!');
457
+
}
458
+
}
459
+
else{
460
+
alert(res.msg);
461
+
}
462
+
},
463
+
error: function (r, s, error){
464
+
alert('Error in processing batch viewing: ' + error);
465
+
}
466
+
});
467
+
}
468
+
});
469
+
jQuery('#aiomatic_sync_batches').on('click', function (e)
470
+
{
471
+
e.preventDefault();
472
+
var btn = jQuery(this);
473
+
aiomaticLoading2(btn);
474
+
var data = {
475
+
action: 'aiomatic_sync_batches',
476
+
nonce: aiomatic_object.nonce,
477
+
};
478
+
jQuery.ajax({
479
+
url: aiomatic_object.ajax_url,
480
+
data: data,
481
+
dataType: 'JSON',
482
+
type: 'POST',
483
+
success: function (res){
484
+
aiomaticRmLoading(btn);
485
+
if(res.status === 'success'){
486
+
jQuery('.aiomatic-batch-success').show();
487
+
setTimeout(function (){
488
+
jQuery('.aiomatic-batch-success').hide();
489
+
},2000);
490
+
location.reload();
491
+
}
492
+
else{
493
+
alert(res.msg);
494
+
}
495
+
},
496
+
error: function (r, s, error){
497
+
aiomaticRmLoading(btn);
498
+
alert('Error in processing batch requests sync: ' + error);
499
+
}
500
+
});
501
+
});
502
+
jQuery('#aiomatic_delete_all_batches').on('click', function (e)
503
+
{
504
+
e.preventDefault();
505
+
if(confirm('Are you sure you want to delete all locally stored AI Batch Requests?'))
506
+
{
507
+
var btn = jQuery(this);
508
+
aiomaticLoading2(btn);
509
+
var data = {
510
+
action: 'aiomatic_delete_all_batches',
511
+
nonce: aiomatic_object.nonce,
512
+
};
513
+
jQuery.ajax({
514
+
url: aiomatic_object.ajax_url,
515
+
data: data,
516
+
dataType: 'JSON',
517
+
type: 'POST',
518
+
success: function (res){
519
+
aiomaticRmLoading(btn);
520
+
if(res.status === 'success'){
521
+
jQuery('.aiomatic-batch-success').show();
522
+
setTimeout(function (){
523
+
jQuery('.aiomatic-batch-success').hide();
524
+
},2000);
525
+
location.reload();
526
+
}
527
+
else{
528
+
alert(res.msg);
529
+
}
530
+
},
531
+
error: function (r, s, error){
532
+
aiomaticRmLoading(btn);
533
+
alert('Error in processing batch requests sync: ' + error);
534
+
}
535
+
});
536
+
}
537
+
});
538
+
jQuery(".aiomatic_sync_batch").on('click', function(e) {
539
+
e.preventDefault();
540
+
var batchid = jQuery(this).attr("sync-id");
541
+
if(batchid == '')
542
+
{
543
+
alert('Incorrect sync id submitted');
544
+
}
545
+
else
546
+
{
547
+
var data = {
548
+
action: 'aiomatic_sync_batch',
549
+
batchid: batchid,
550
+
nonce: aiomatic_object.nonce,
551
+
};
552
+
jQuery.ajax({
553
+
url: aiomatic_object.ajax_url,
554
+
data: data,
555
+
dataType: 'JSON',
556
+
type: 'POST',
557
+
beforeSend: function (){
558
+
aiomaticLoading2(jQuery('#aiomatic_sync_batch_' + batchid));
559
+
},
560
+
success: function (res){
561
+
if(res.status === 'success'){
562
+
location.reload();
563
+
}
564
+
else{
565
+
alert(res.msg);
566
+
aiomaticRmLoading(jQuery('#aiomatic_sync_batch_' + batchid));
567
+
}
568
+
},
569
+
error: function (r, s, error){
570
+
alert('Error in processing batch sync by id: ' + error);
571
+
aiomaticRmLoading(jQuery('#aiomatic_sync_batch_' + batchid));
572
+
}
573
+
});
574
+
}
575
+
});
576
+
jQuery('#aiomatic_batches_form').on('submit', function (e)
577
+
{
578
+
e.preventDefault();
579
+
var form = jQuery('#aiomatic_batches_form');
580
+
var btn = form.find('#aiomatic-batch-save-button');
581
+
var data = form.serialize();
582
+
jQuery.ajax({
583
+
url: aiomatic_object.ajax_url,
584
+
data: data,
585
+
dataType: 'JSON',
586
+
type: 'POST',
587
+
beforeSend: function (){
588
+
aiomaticLoading2(btn);
589
+
},
590
+
success: function (res){
591
+
aiomaticRmLoading(btn);
592
+
if(res.status === 'success'){
593
+
jQuery('.aiomatic-batch-success').html("AI Batch Request saved successfully!");
594
+
jQuery('.aiomatic-batch-success').show();
595
+
location.reload();
596
+
}
597
+
else{
598
+
alert(res.msg);
599
+
}
600
+
},
601
+
error: function (r, s, error){
602
+
aiomaticRmLoading(btn);
603
+
alert('Error in processing AI Batch Request saving: ' + error);
604
+
}
605
+
});
606
+
return false;
607
+
});
608
+
var codemodalfzr = document.getElementById('mymodalfzr');
609
+
var btn = document.getElementById("aiomatic_manage_batches");
610
+
var span = document.getElementById("aiomatic_close");
611
+
if(btn != null)
612
+
{
613
+
btn.onclick = function(e) {
614
+
e.preventDefault();
615
+
codemodalfzr.style.display = "block";
616
+
}
617
+
}
618
+
if(span != null)
619
+
{
620
+
span.onclick = function() {
621
+
codemodalfzr.style.display = "none";
622
+
}
623
+
}
624
+
625
+
var codemodalfzr_edit = document.getElementById('mymodalfzr-edit');
626
+
var span_edit = document.getElementById("aiomatic_close-edit");
627
+
if(span_edit != null)
628
+
{
629
+
span_edit.onclick = function() {
630
+
codemodalfzr_edit.style.display = "none";
631
+
}
632
+
}
633
+
634
+
var codemodalfzr_parse = document.getElementById('mymodalfzr-parse');
635
+
var span_parse = document.getElementById("aiomatic_close-parse");
636
+
if(span_parse != null)
637
+
{
638
+
span_parse.onclick = function() {
639
+
codemodalfzr_parse.style.display = "none";
640
+
}
641
+
}
642
+
643
+
window.onclick = function(event)
644
+
{
645
+
if (event.target == codemodalfzr_parse) {
646
+
codemodalfzr_parse.style.display = "none";
647
+
}
648
+
if (event.target == codemodalfzr_edit) {
649
+
codemodalfzr_edit.style.display = "none";
650
+
}
651
+
if (event.target == codemodalfzr) {
652
+
codemodalfzr.style.display = "none";
653
+
}
654
+
}
655
+
});
656
+
657
+
//https://help.openai.com/en/articles/9197833-batch-api-faq
658
+
function aiomatic_batch_data_changed()
659
+
{
660
+
var select = jQuery('#model_selector_data_batch').val();
661
+
if(aiomatic_object.moder_gpt_models_aiomatic.includes(select))
662
+
{
663
+
var aiomatic_legacy_data = jQuery('#aiomatic_legacy_data');
664
+
var aiomatic_gpt_data = jQuery('#aiomatic_gpt_data');
665
+
aiomatic_legacy_data.hide();
666
+
aiomatic_gpt_data.show();
667
+
}
668
+
else
669
+
{
670
+
var aiomatic_legacy_data = jQuery('#aiomatic_legacy_data');
671
+
var aiomatic_gpt_data = jQuery('#aiomatic_gpt_data');
672
+
aiomatic_legacy_data.show();
673
+
aiomatic_gpt_data.hide();
674
+
}
675
+
}
676
+
jQuery(document).ready(function ($){
677
+
function aiomatic_stripslashes (str)
678
+
{
679
+
return (str + '').replace(/\\(.?)/g, function (s, n1) {
680
+
switch (n1) {
681
+
case '\\':
682
+
return '\\';
683
+
case '0':
684
+
return '\u0000';
685
+
case '':
686
+
return '';
687
+
default:
688
+
return n1;
689
+
}
690
+
});
691
+
}
692
+
$('.aiomatic_modal_close').on('click', function (){
693
+
$('.aiomatic_modal_close').closest('.aiomatic_modal').hide();
694
+
$('.aiomatic_modal_close').closest('.aiomatic_modal').removeClass('aiomatic-small-modal');
695
+
$('.aiomatic-overlay').hide();
696
+
});
697
+
function aiomaticLoading(btn){
698
+
btn.attr('disabled','disabled');
699
+
if(!btn.find('spinner').length){
700
+
btn.append('<span class="spinner"></span>');
701
+
}
702
+
btn.find('.spinner').css('visibility','unset');
703
+
}
704
+
function aiomaticRmLoading(btn){
705
+
btn.removeAttr('disabled');
706
+
btn.find('.spinner').remove();
707
+
}
708
+
var aiomatic_max_file_size = aiomatic_object.maxfilesize;
709
+
var aiomatic_max_size_in_mb = aiomatic_object.maxfilesize / (1024 ** 2);
710
+
var aiomatic_delete_file = $('.aiomatic_delete_file');
711
+
var aiomatic_ajax_url = aiomatic_object.ajax_url;
712
+
function aiomaticSortData(){
713
+
$('.aiomatic_data').each(function (idx, item){
714
+
$(item).find('.aiomatic_data_prompt').attr('name','data_batch['+idx+'][prompt]');
715
+
});
716
+
$('.aiomatic_new_data_batch').each(function (idx, item){
717
+
$(item).find('.aiomatic_new_data_batch_system').attr('name','new_data_batch['+idx+'][system]');
718
+
$(item).find('.aiomatic_new_data_batch_prompt').attr('name','new_data_batch['+idx+'][prompt]');
719
+
});
720
+
}
721
+
var aiomatic_item = '<div class="aiomatic_data_item_single aiomatic_data"><div><textarea rows="1" name="data_batch[0][prompt]" class="regular-text aiomatic_data_prompt aiomatic_height" placeholder="Prompt"></textarea><span class="button button-link-delete">×</span></div></div>';
722
+
var aiomatic_new_item = '<div class="aiomatic_data_item aiomatic_new_data_batch"><div><textarea rows="1" name="new_data_batch[0][system]" class="regular-text aiomatic_new_data_batch_system aiomatic_height" placeholder="System"></textarea> </div><div><textarea rows="1" name="new_data_batch[0][prompt]" class="regular-text aiomatic_new_data_batch_prompt aiomatic_height" placeholder="User"></textarea><span class="button button-link-delete">×</span></div></div>';
723
+
var aiomatic_data_restore = window.localStorage.getItem('aiomatic_data_list_batch');
724
+
if(aiomatic_data_restore !== null && aiomatic_data_restore !== "")
725
+
{
726
+
var appendData = '';
727
+
var oldobj = '';
728
+
try{
729
+
oldobj = JSON.parse(aiomatic_data_restore);
730
+
oldobj.forEach(function (element){if(element.prompt !== null && element.prompt !== undefined){appendData += '<div class="aiomatic_data_item_single aiomatic_data"><div><textarea rows="1" name="data_batch[0][prompt]" class="regular-text aiomatic_data_prompt aiomatic_height" placeholder="Prompt">' + element.prompt + '</textarea><span class="button button-link-delete">×</span></div></div>';}});
731
+
appendData += aiomatic_item;
732
+
$('#aiomatic_data_list_batch').html(appendData);
733
+
}
734
+
catch(e)
735
+
{
736
+
alert(e);
737
+
}
738
+
}
739
+
var aiomatic_new_data_batch_restore = window.localStorage.getItem('aiomatic_new_data_batch_list');
740
+
if(aiomatic_new_data_batch_restore !== null && aiomatic_new_data_batch_restore !== "")
741
+
{
742
+
var appendData = '';
743
+
var oldobj = '';
744
+
try{
745
+
oldobj = JSON.parse(aiomatic_new_data_batch_restore);
746
+
oldobj.forEach(function (element){if(element.prompt !== null && element.prompt !== undefined){appendData += '<div class="aiomatic_data_item aiomatic_new_data_batch"><div><textarea rows="1" name="new_data_batch[0][system]" class="regular-text aiomatic_new_data_batch_system aiomatic_height" placeholder="System">' + element.system + '</textarea> </div><div><textarea rows="1" name="new_data_batch[0][prompt]" class="regular-text aiomatic_new_data_batch_prompt aiomatic_height" placeholder="User">' + element.prompt + '</textarea><span class="button button-link-delete">×</span></div></div>';}});
747
+
appendData += aiomatic_new_item;
748
+
$('#aiomatic_new_data_batch_list').html(appendData);
749
+
}
750
+
catch(e)
751
+
{
752
+
alert(e);
753
+
}
754
+
}
755
+
var progressBar = $('.aiomatic-convert-bar');
756
+
var aiomatic_add_data = $('.aiomatic_add_data');
757
+
var aiomatic_clear_data = $('.aiomatic_clear_data');
758
+
var aiomatic_download_data = $('.aiomatic_download_data');
759
+
var aiomatic_load_data = $('.aiomatic_load_data');
760
+
var form = $('#aiomatic_form_data');
761
+
aiomatic_add_data.on('click', function (){
762
+
var select = jQuery('#model_selector_data_batch').val();
763
+
if(aiomatic_object.moder_gpt_models_aiomatic.includes(select))
764
+
{
765
+
$('#aiomatic_new_data_batch_list').append(aiomatic_new_item);
766
+
}
767
+
else
768
+
{
769
+
$('#aiomatic_data_list_batch').append(aiomatic_item);
770
+
}
771
+
aiomaticSortData();
772
+
var total = 0;
773
+
var lists = [];
774
+
$('.aiomatic_data').each(function (idx, item){
775
+
var item_prompt = $(item).find('.aiomatic_data_prompt').val();
776
+
if(item_prompt !== ''){
777
+
total += 1;
778
+
lists.push({prompt: item_prompt});
779
+
}
780
+
});
781
+
if(total > 0){
782
+
try
783
+
{
784
+
var jsonstr = JSON.stringify(lists);
785
+
window.localStorage.setItem('aiomatic_data_list_batch', jsonstr);
786
+
}
787
+
catch(e)
788
+
{
789
+
alert(e);
790
+
}
791
+
}
792
+
var new_total = 0;
793
+
var new_lists = [];
794
+
$('.aiomatic_new_data_batch').each(function (idx, item){
795
+
var item_system = $(item).find('.aiomatic_new_data_batch_system').val();
796
+
var item_prompt = $(item).find('.aiomatic_new_data_batch_prompt').val();
797
+
if(item_prompt !== ''){
798
+
new_total += 1;
799
+
new_lists.push({system: item_system, prompt: item_prompt});
800
+
}
801
+
});
802
+
if(new_total > 0){
803
+
try
804
+
{
805
+
var jsonstr = JSON.stringify(new_lists);
806
+
window.localStorage.setItem('aiomatic_new_data_batch_list', jsonstr);
807
+
}
808
+
catch(e)
809
+
{
810
+
alert(e);
811
+
}
812
+
}
813
+
});
814
+
aiomatic_clear_data.on('click', function ()
815
+
{
816
+
var select = jQuery('#model_selector_data_batch').val();
817
+
if(aiomatic_object.moder_gpt_models_aiomatic.includes(select))
818
+
{
819
+
$('#aiomatic_new_data_batch_list').html('<div class="aiomatic_data_item aiomatic_new_data_batch"><div><textarea rows="1" name="new_data_batch[0][system]" class="regular-text aiomatic_new_data_batch_system aiomatic_height" placeholder="System"></textarea></div><div><textarea rows="1" name="new_data_batch[0][prompt]" class="regular-text aiomatic_new_data_batch_prompt aiomatic_height" placeholder="User"></textarea><span class="button button-link-delete">×</span></div></div>');
820
+
window.localStorage.removeItem('aiomatic_new_data_batch_list');
821
+
}
822
+
else
823
+
{
824
+
$('#aiomatic_data_list_batch').html('<div class="aiomatic_data_item_single aiomatic_data"><div><textarea rows="1" name="data_batch[0][prompt]" class="regular-text aiomatic_data_prompt aiomatic_height" placeholder="Prompt"></textarea><span class="button button-link-delete">×</span></div></div>');
825
+
window.localStorage.removeItem('aiomatic_data_list_batch');
826
+
}
827
+
});
828
+
var Download =
829
+
{
830
+
click : function(node) {
831
+
var ev = new MouseEvent("click", {
832
+
bubbles: true,
833
+
cancelable: false,
834
+
view: window,
835
+
detail: 0,
836
+
screenX: 0,
837
+
screenY: 0,
838
+
clientX: 0,
839
+
clientY: 0,
840
+
ctrlKey: false,
841
+
altKey: false,
842
+
shiftKey: false,
843
+
metaKey: false,
844
+
button: 0,
845
+
relatedTarget: null
846
+
});
847
+
return node.dispatchEvent(ev);
848
+
},
849
+
encode : function(data) {
850
+
return 'data:application/octet-stream;base64,' + btoa( data );
851
+
},
852
+
link : function(data, name){
853
+
var a = document.createElement('a');
854
+
a.download = name || self.location.pathname.slice(self.location.pathname.lastIndexOf('/')+1);
855
+
a.href = data || self.location.href;
856
+
return a;
857
+
}
858
+
};
859
+
Download.save = function(data, name)
860
+
{
861
+
this.click(
862
+
this.link(
863
+
this.encode( data ),
864
+
name
865
+
)
866
+
);
867
+
};
868
+
aiomatic_download_data.on('click', function ()
869
+
{
870
+
var select = jQuery('#model_selector_data_batch').val();
871
+
var model_max_tokens = jQuery('#model_max_tokens').val();
872
+
if(aiomatic_object.moder_gpt_models_aiomatic.includes(select))
873
+
{
874
+
var total = 0;
875
+
var lists = '';
876
+
$('.aiomatic_new_data_batch').each(function (idx, item){
877
+
var item_system = $(item).find('.aiomatic_new_data_batch_system').val();
878
+
var item_prompt = $(item).find('.aiomatic_new_data_batch_prompt').val();
879
+
if(item_prompt !== '')
880
+
{
881
+
total += 1;
882
+
var messages = [
883
+
{
884
+
role: "system",
885
+
content: item_system
886
+
},
887
+
{
888
+
role: "user",
889
+
content: item_prompt
890
+
}
891
+
];
892
+
var json_arr =
893
+
{
894
+
custom_id: "request-" + total,
895
+
method: "POST",
896
+
url: "/v1/chat/completions",
897
+
body:
898
+
{
899
+
model: select,
900
+
messages: messages
901
+
}
902
+
};
903
+
if (typeof model_max_tokens !== 'undefined' && model_max_tokens !== null && model_max_tokens != '')
904
+
{
905
+
json_arr.body.max_tokens = model_max_tokens;
906
+
}
907
+
try
908
+
{
909
+
var myJsonString = JSON.stringify(json_arr);
910
+
lists += myJsonString + '\n';
911
+
}
912
+
catch(e)
913
+
{
914
+
alert(e);
915
+
}
916
+
}
917
+
});
918
+
lists = lists.trim();
919
+
if(total > 0){
920
+
try
921
+
{
922
+
Download.save(lists, "new_data_batch.jsonl");
923
+
}
924
+
catch(e)
925
+
{
926
+
alert(e);
927
+
}
928
+
}
929
+
else
930
+
{
931
+
alert('No data to download!');
932
+
}
933
+
}
934
+
else
935
+
{
936
+
var total = 0;
937
+
var lists = '';
938
+
$('.aiomatic_data').each(function (idx, item){
939
+
var item_prompt = $(item).find('.aiomatic_data_prompt').val();
940
+
if(item_prompt !== ''){
941
+
total += 1;
942
+
var json_arr =
943
+
{
944
+
custom_id: "request-" + total,
945
+
method: "POST",
946
+
url: "/v1/embeddings",
947
+
body:
948
+
{
949
+
model: select,
950
+
input: item_prompt
951
+
}
952
+
};
953
+
try
954
+
{
955
+
var myJsonString = JSON.stringify(json_arr);
956
+
lists += myJsonString + '\n';
957
+
}
958
+
catch(e)
959
+
{
960
+
alert(e);
961
+
}
962
+
}
963
+
});
964
+
lists = lists.trim();
965
+
if(total > 0){
966
+
try
967
+
{
968
+
Download.save(lists, "data.jsonl");
969
+
}
970
+
catch(e)
971
+
{
972
+
alert(e);
973
+
}
974
+
}
975
+
else
976
+
{
977
+
alert('No data to download!');
978
+
}
979
+
}
980
+
});
981
+
aiomatic_load_data.on('click', function (event){
982
+
event.preventDefault();
983
+
var aiomatic_file_load = $('#aiomatic_file_load');
984
+
if(aiomatic_file_load[0].files.length === 0){
985
+
alert('Please select a file first!');
986
+
}
987
+
else
988
+
{
989
+
var aiomatic_file = aiomatic_file_load[0].files[0];
990
+
var aiomatic_file_extension = aiomatic_file.name.substr( (aiomatic_file.name.lastIndexOf('.') +1) );
991
+
if(aiomatic_file_extension !== 'jsonl' && aiomatic_file_extension !== 'csv')
992
+
{
993
+
aiomatic_file_load.val('');
994
+
alert('This feature only accepts JSONL or CSV file types!');
995
+
}
996
+
else if(aiomatic_file.size > aiomatic_max_file_size)
997
+
{
998
+
aiomatic_file_load.val('');
999
+
alert('Dataset allowed maximum size (MB): '+ aiomatic_max_size_in_mb)
1000
+
}
1001
+
else
1002
+
{
1003
+
var select = jQuery('#model_selector_data_batch').val();
1004
+
if(aiomatic_object.moder_gpt_models_aiomatic.includes(select))
1005
+
{
1006
+
var reader = new FileReader();
1007
+
reader.readAsText(aiomatic_file, "UTF-8");
1008
+
var thehtml = '';
1009
+
reader.onload = function (evt) {
1010
+
if(aiomatic_file_extension == 'jsonl')
1011
+
{
1012
+
var explodefile = evt.target.result.split(/\r?\n/);
1013
+
explodefile.forEach(function (element){if(element.trim() !== ''){var oldobj = '';try{oldobj = JSON.parse(element.trim());}catch(e) {alert(e);}if(oldobj.hasOwnProperty("messages")){if(oldobj.messages[0].role !== null && oldobj.messages[1].role !== null && oldobj.messages[2].role !== null && oldobj.messages[0].role !== undefined && oldobj.messages[1].role !== undefined && oldobj.messages[2].role !== undefined){thehtml += '<div class="aiomatic_data_item aiomatic_new_data_batch"><div><textarea rows="1" name="new_data_batch[0][prompt]" class="regular-text aiomatic_new_data_batch_system aiomatic_height" placeholder="System">' + oldobj.messages[0].content + '</textarea> </div><div><textarea rows="1" name="new_data_batch[0][prompt]" class="regular-text aiomatic_new_data_batch_prompt aiomatic_height" placeholder="User">' + oldobj.messages[1].content + '</textarea><span class="button button-link-delete">×</span></div></div>';}}}});
1014
+
if(thehtml !== '')
1015
+
{
1016
+
thehtml += '<div class="aiomatic_data_item aiomatic_new_data_batch"><div><textarea rows="1" name="new_data_batch[0][system]" class="regular-text aiomatic_new_data_batch_system aiomatic_height" placeholder="System"></textarea> </div><div><textarea rows="1" name="new_data_batch[0][prompt]" class="regular-text aiomatic_new_data_batch_prompt aiomatic_height" placeholder="User"></textarea><span class="button button-link-delete">×</span></div></div>';
1017
+
$('#aiomatic_new_data_batch_list').html(thehtml);
1018
+
var total = 0;
1019
+
var lists = [];
1020
+
$('.aiomatic_new_data_batch').each(function (idx, item){
1021
+
var item_system = $(item).find('.aiomatic_new_data_batch_system').val();
1022
+
var item_prompt = $(item).find('.aiomatic_new_data_batch_prompt').val();
1023
+
if(item_prompt !== ''){
1024
+
total += 1;
1025
+
lists.push({system: item_system, prompt: item_prompt});
1026
+
}
1027
+
});
1028
+
if(total > 0){
1029
+
try
1030
+
{
1031
+
var jsonstr = JSON.stringify(lists);
1032
+
window.localStorage.setItem('aiomatic_new_data_batch_list', jsonstr);
1033
+
alert("Data loaded successfully!");
1034
+
}
1035
+
catch(e)
1036
+
{
1037
+
alert(e);
1038
+
}
1039
+
}
1040
+
}
1041
+
else
1042
+
{
1043
+
alert("Invalid file submitted: " + aiomatic_file.name);
1044
+
}
1045
+
}
1046
+
else
1047
+
{
1048
+
let data = evt.target.result.split("\r\n");
1049
+
for (let i in data) {
1050
+
data[i] = data[i].split(",");
1051
+
}
1052
+
data.forEach(function (element){if(element[0] !== null && element[0] != '' && element[1] != '' && element[1] !== null){thehtml += '<div class="aiomatic_data_item aiomatic_new_data_batch"><div><textarea rows="1" name="new_data_batch[0][system]" class="regular-text aiomatic_new_data_batch_system aiomatic_height" placeholder="System">' + element[0] + '</textarea> </div><div><textarea rows="1" name="new_data_batch[0][prompt]" class="regular-text aiomatic_new_data_batch_prompt aiomatic_height" placeholder="User">' + element[1] + '</textarea><span class="button button-link-delete">×</span></div></div>';}});
1053
+
if(thehtml !== '')
1054
+
{
1055
+
thehtml += '<div class="aiomatic_data_item aiomatic_new_data_batch"><div><textarea rows="1" name="new_data_batch[0][system]" class="regular-text aiomatic_new_data_batch_system aiomatic_height" placeholder="System"></textarea> </div><div><textarea rows="1" name="new_data_batch[0][prompt]" class="regular-text aiomatic_new_data_batch_prompt aiomatic_height" placeholder="User"></textarea><span class="button button-link-delete">×</span></div></div>';
1056
+
$('#aiomatic_new_data_batch_list').html(thehtml);
1057
+
var total = 0;
1058
+
var lists = [];
1059
+
$('.aiomatic_new_data_batch').each(function (idx, item){
1060
+
var item_system = $(item).find('.aiomatic_new_data_batch_system').val();
1061
+
var item_prompt = $(item).find('.aiomatic_new_data_batch_prompt').val();
1062
+
if(item_prompt !== ''){
1063
+
total += 1;
1064
+
lists.push({system: item_system, prompt: item_prompt});
1065
+
}
1066
+
});
1067
+
if(total > 0){
1068
+
try
1069
+
{
1070
+
var jsonstr = JSON.stringify(lists);
1071
+
window.localStorage.setItem('aiomatic_new_data_batch_list', jsonstr);
1072
+
alert("Data loaded successfully!");
1073
+
}
1074
+
catch(e)
1075
+
{
1076
+
alert(e);
1077
+
}
1078
+
}
1079
+
}
1080
+
else
1081
+
{
1082
+
alert("Invalid file submitted: " + aiomatic_file.name);
1083
+
}
1084
+
}
1085
+
}
1086
+
reader.onerror = function (evt) {
1087
+
alert("Error reading file: " + aiomatic_file.name + ' - ' + reader.error);
1088
+
}
1089
+
}
1090
+
else
1091
+
{
1092
+
var reader = new FileReader();
1093
+
reader.readAsText(aiomatic_file, "utf-8");
1094
+
var thehtml = '';
1095
+
reader.onload = function (evt) {
1096
+
if(aiomatic_file_extension == 'jsonl')
1097
+
{
1098
+
var explodefile = evt.target.result.split(/\r?\n/);
1099
+
explodefile.forEach(function (element){if(element.trim() !== ''){var oldobj = '';try{oldobj = JSON.parse(element.trim());}catch(e) {alert(e);}if(oldobj.hasOwnProperty("prompt")){if(oldobj.prompt !== null){thehtml += '<div class="aiomatic_data_item_single aiomatic_data"><div><textarea rows="1" name="data_batch[0][prompt]" class="regular-text aiomatic_data_prompt aiomatic_height" placeholder="Prompt">' + oldobj.prompt + '</textarea><span class="button button-link-delete">×</span></div></div>';}}}});
1100
+
if(thehtml !== '')
1101
+
{
1102
+
thehtml += '<div class="aiomatic_data_item_single aiomatic_data"><div><textarea rows="1" name="data_batch[0][prompt]" class="regular-text aiomatic_data_prompt aiomatic_height" placeholder="Prompt"></textarea><span class="button button-link-delete">×</span></div></div>';
1103
+
$('#aiomatic_data_list_batch').html(thehtml);
1104
+
var total = 0;
1105
+
var lists = [];
1106
+
$('.aiomatic_data').each(function (idx, item){
1107
+
var item_prompt = $(item).find('.aiomatic_data_prompt').val();
1108
+
if(item_prompt !== ''){
1109
+
total += 1;
1110
+
lists.push({prompt: item_prompt});
1111
+
}
1112
+
});
1113
+
if(total > 0){
1114
+
try
1115
+
{
1116
+
var jsonstr = JSON.stringify(lists);
1117
+
window.localStorage.setItem('aiomatic_data_list_batch', jsonstr);
1118
+
alert("Data loaded successfully!");
1119
+
}
1120
+
catch(e)
1121
+
{
1122
+
alert(e);
1123
+
}
1124
+
}
1125
+
}
1126
+
else
1127
+
{
1128
+
alert("Invalid file submitted: " + aiomatic_file.name);
1129
+
}
1130
+
}
1131
+
else
1132
+
{
1133
+
let data = evt.target.result.split("\r\n");
1134
+
for (let i in data) {
1135
+
data[i] = data[i].split(",");
1136
+
}
1137
+
data.forEach(function (element){if(element[0] !== null && element[0] != ''){thehtml += '<div class="aiomatic_data_item_single aiomatic_data"><div><textarea rows="1" name="data_batch[0][prompt]" class="regular-text aiomatic_data_prompt aiomatic_height" placeholder="Prompt">' + element[0] + '</textarea><span class="button button-link-delete">×</span></div></div>';}});
1138
+
if(thehtml !== '')
1139
+
{
1140
+
thehtml += '<div class="aiomatic_data_item_single aiomatic_data"><div><textarea rows="1" name="data_batch[0][prompt]" class="regular-text aiomatic_data_prompt aiomatic_height" placeholder="Prompt"></textarea><span class="button button-link-delete">×</span></div></div>';
1141
+
$('#aiomatic_data_list_batch').html(thehtml);
1142
+
var total = 0;
1143
+
var lists = [];
1144
+
$('.aiomatic_data').each(function (idx, item){
1145
+
var item_prompt = $(item).find('.aiomatic_data_prompt').val();
1146
+
if(item_prompt !== ''){
1147
+
total += 1;
1148
+
lists.push({prompt: item_prompt});
1149
+
}
1150
+
});
1151
+
if(total > 0){
1152
+
try
1153
+
{
1154
+
var jsonstr = JSON.stringify(lists);
1155
+
window.localStorage.setItem('aiomatic_data_list_batch', jsonstr);
1156
+
alert("Data loaded successfully!");
1157
+
}
1158
+
catch(e)
1159
+
{
1160
+
alert(e);
1161
+
}
1162
+
}
1163
+
}
1164
+
else
1165
+
{
1166
+
alert("Invalid file submitted: " + aiomatic_file.name);
1167
+
}
1168
+
}
1169
+
}
1170
+
reader.onerror = function (evt) {
1171
+
alert("Error reading file: " + aiomatic_file.name + ' - ' + reader.error);
1172
+
}
1173
+
}
1174
+
}
1175
+
}
1176
+
});
1177
+
$(document).on('click','.aiomatic_data span', function (e){
1178
+
$(e.currentTarget).parent().parent().remove();
1179
+
var total = 0;
1180
+
var lists = [];
1181
+
$('.aiomatic_data').each(function (idx, item){
1182
+
var item_prompt = $(item).find('.aiomatic_data_prompt').val();
1183
+
if(item_prompt !== ''){
1184
+
total += 1;
1185
+
lists.push({prompt: item_prompt});
1186
+
}
1187
+
});
1188
+
if(total > 0){
1189
+
try
1190
+
{
1191
+
var jsonstr = JSON.stringify(lists);
1192
+
window.localStorage.setItem('aiomatic_data_list_batch', jsonstr);
1193
+
}
1194
+
catch(e)
1195
+
{
1196
+
alert(e);
1197
+
}
1198
+
}
1199
+
else
1200
+
{
1201
+
window.localStorage.removeItem('aiomatic_data_list_batch');
1202
+
}
1203
+
aiomaticSortData();
1204
+
});
1205
+
1206
+
$(document).on('click','.aiomatic_new_data_batch span', function (e){
1207
+
$(e.currentTarget).parent().parent().remove();
1208
+
var total = 0;
1209
+
var lists = [];
1210
+
$('.aiomatic_new_data_batch').each(function (idx, item){
1211
+
var item_system = $(item).find('.aiomatic_new_data_batch_system').val();
1212
+
var item_prompt = $(item).find('.aiomatic_new_data_batch_prompt').val();
1213
+
if(item_prompt !== ''){
1214
+
total += 1;
1215
+
lists.push({system: item_system, prompt: item_prompt});
1216
+
}
1217
+
});
1218
+
if(total > 0){
1219
+
try
1220
+
{
1221
+
var jsonstr = JSON.stringify(lists);
1222
+
window.localStorage.setItem('aiomatic_new_data_batch_list', jsonstr);
1223
+
}
1224
+
catch(e)
1225
+
{
1226
+
alert(e);
1227
+
}
1228
+
}
1229
+
else
1230
+
{
1231
+
window.localStorage.removeItem('aiomatic_new_data_batch_list');
1232
+
}
1233
+
aiomaticSortData();
1234
+
});
1235
+
1236
+
function aiomaticFileUpload(data, btn){
1237
+
var aiomatic_upload_convert_index = parseInt($('#aiomatic_upload_convert_index').val());
1238
+
$.ajax({
1239
+
url: aiomatic_ajax_url,
1240
+
data: data,
1241
+
type: 'POST',
1242
+
dataType: 'JSON',
1243
+
success: function (res){
1244
+
if(res.status === 'success'){
1245
+
if(res.next === 'DONE'){
1246
+
var select = jQuery('#model_selector_data_batch').val();
1247
+
if(aiomatic_object.moder_gpt_models_aiomatic.includes(select))
1248
+
{
1249
+
$('#aiomatic_new_data_batch_list').html(aiomatic_new_item);
1250
+
}
1251
+
else
1252
+
{
1253
+
$('#aiomatic_data_list_batch').html(aiomatic_item);
1254
+
}
1255
+
$('.aiomatic-upload-message').html('The upload was successfully completed!');
1256
+
progressBar.find('small').html('100%');
1257
+
progressBar.find('span').css('width','100%');
1258
+
aiomaticRmLoading(btn);
1259
+
setTimeout(function (){
1260
+
$('#aiomatic_upload_convert_line').val('0');
1261
+
$('#aiomatic_upload_convert_index').val('1');
1262
+
progressBar.hide();
1263
+
progressBar.removeClass('aiomatic_error')
1264
+
progressBar.find('span').css('width',0);
1265
+
progressBar.find('small').html('0%');
1266
+
},2000);
1267
+
1268
+
}
1269
+
else{
1270
+
$('#aiomatic_upload_convert_line').val(res.next);
1271
+
$('#aiomatic_upload_convert_index').val(aiomatic_upload_convert_index+1);
1272
+
var data = $('#aiomatic_upload_convert').serialize();
1273
+
aiomaticFileUpload(data, btn);
1274
+
}
1275
+
}
1276
+
else{
1277
+
progressBar.addClass('aiomatic_error');
1278
+
aiomaticRmLoading(btn);
1279
+
alert(res.msg);
1280
+
}
1281
+
},
1282
+
error: function (r, s, error){
1283
+
progressBar.addClass('aiomatic_error');
1284
+
aiomaticRmLoading(btn);
1285
+
alert('Error in processing upload: ' + error);
1286
+
}
1287
+
})
1288
+
}
1289
+
1290
+
function aiomaticProcessData(lists, start, file, btn){
1291
+
var purpose = $('select[name=purpose]').val();
1292
+
var model = $('select[name=model]').val();
1293
+
var name = $('#file-name-holder').val();
1294
+
var model_max_tokens = jQuery('#model_max_tokens').val();
1295
+
var total = 0;
1296
+
var js_list = '';
1297
+
if(file == '' && name != '')
1298
+
{
1299
+
file = name;
1300
+
}
1301
+
lists.forEach((item) =>
1302
+
{
1303
+
total += 1;
1304
+
var json_arr =
1305
+
{
1306
+
custom_id: "request-" + total,
1307
+
method: "POST",
1308
+
url: "/v1/embeddings",
1309
+
body:
1310
+
{
1311
+
model: model,
1312
+
input: item.prompt
1313
+
}
1314
+
};
1315
+
if (typeof model_max_tokens !== 'undefined' && model_max_tokens !== null && model_max_tokens != '')
1316
+
{
1317
+
json_arr.body.max_tokens = model_max_tokens;
1318
+
}
1319
+
try
1320
+
{
1321
+
var myJsonString = JSON.stringify(json_arr);
1322
+
js_list += myJsonString + '\n';
1323
+
}
1324
+
catch(e)
1325
+
{
1326
+
alert(e);
1327
+
}
1328
+
});
1329
+
if(js_list == '')
1330
+
{
1331
+
alert('Processing failed, please try again later');
1332
+
return;
1333
+
}
1334
+
var data = {
1335
+
action: 'aiomatic_data_insert_batch',
1336
+
js_list: js_list,
1337
+
file: file,
1338
+
nonce: aiomatic_object.nonce
1339
+
};
1340
+
$.ajax({
1341
+
url: aiomatic_ajax_url,
1342
+
data: data,
1343
+
dataType: 'JSON',
1344
+
type: 'POST',
1345
+
success: function (res){
1346
+
if(res.status === 'success'){
1347
+
var percent = '100';
1348
+
progressBar.find('small').html(percent+'%');
1349
+
progressBar.find('span').css('width',percent+'%');
1350
+
$('#aiomatic_upload_convert input[name=model]').val(model);
1351
+
$('#aiomatic_upload_convert input[name=purpose]').val(purpose);
1352
+
$('#aiomatic_upload_convert input[name=custom]').val(name);
1353
+
$('#aiomatic_upload_convert input[name=file]').val(res.file);
1354
+
var data = $('#aiomatic_upload_convert').serialize();
1355
+
aiomaticFileUpload(data, btn);
1356
+
}
1357
+
else{
1358
+
progressBar.addClass('aiomatic_error');
1359
+
aiomaticRmLoading(btn);
1360
+
alert(res.msg);
1361
+
}
1362
+
},
1363
+
error: function (r, s, error){
1364
+
progressBar.addClass('aiomatic_error');
1365
+
aiomaticRmLoading(btn);
1366
+
alert('Error in processing data: ' + error);
1367
+
}
1368
+
});
1369
+
}
1370
+
function aiomaticNewProcessData(lists, start, file, btn){
1371
+
var purpose = $('select[name=purpose]').val();
1372
+
var model = $('select[name=model]').val();
1373
+
var name = $('#file-name-holder').val();
1374
+
var model_max_tokens = jQuery('#model_max_tokens').val();
1375
+
if(file == '' && name != '')
1376
+
{
1377
+
file = name;
1378
+
}
1379
+
var js_list = '';
1380
+
var total = 0;
1381
+
lists.forEach((item) =>
1382
+
{
1383
+
total += 1;
1384
+
var messages = [
1385
+
{
1386
+
role: "system",
1387
+
content: item.system
1388
+
},
1389
+
{
1390
+
role: "user",
1391
+
content: item.prompt
1392
+
}
1393
+
];
1394
+
var json_arr =
1395
+
{
1396
+
custom_id: "request-" + total,
1397
+
method: "POST",
1398
+
url: "/v1/chat/completions",
1399
+
body:
1400
+
{
1401
+
model: model,
1402
+
messages: messages
1403
+
}
1404
+
};
1405
+
if (typeof model_max_tokens !== 'undefined' && model_max_tokens !== null && model_max_tokens != '')
1406
+
{
1407
+
json_arr.body.max_tokens = model_max_tokens;
1408
+
}
1409
+
try
1410
+
{
1411
+
var myJsonString = JSON.stringify(json_arr);
1412
+
js_list += myJsonString + '\n';
1413
+
}
1414
+
catch(e)
1415
+
{
1416
+
alert(e);
1417
+
}
1418
+
});
1419
+
if(js_list == '')
1420
+
{
1421
+
alert('Processing failed, please try again later');
1422
+
return;
1423
+
}
1424
+
var data = {
1425
+
action: 'aiomatic_new_data_batch_insert',
1426
+
js_list: js_list,
1427
+
file: file,
1428
+
nonce: aiomatic_object.nonce
1429
+
};
1430
+
$.ajax({
1431
+
url: aiomatic_ajax_url,
1432
+
data: data,
1433
+
dataType: 'JSON',
1434
+
type: 'POST',
1435
+
success: function (res){
1436
+
if(res.status === 'success'){
1437
+
var percent = 100;
1438
+
progressBar.find('small').html(percent+'%');
1439
+
progressBar.find('span').css('width',percent+'%');
1440
+
$('#aiomatic_upload_convert input[name=model]').val(model);
1441
+
$('#aiomatic_upload_convert input[name=purpose]').val(purpose);
1442
+
$('#aiomatic_upload_convert input[name=custom]').val(name);
1443
+
$('#aiomatic_upload_convert input[name=file]').val(res.file);
1444
+
var data = $('#aiomatic_upload_convert').serialize();
1445
+
aiomaticFileUpload(data, btn);
1446
+
}
1447
+
else{
1448
+
progressBar.addClass('aiomatic_error');
1449
+
aiomaticRmLoading(btn);
1450
+
alert(res.msg);
1451
+
}
1452
+
},
1453
+
error: function (r, s, error){
1454
+
progressBar.addClass('aiomatic_error');
1455
+
aiomaticRmLoading(btn);
1456
+
alert('Error in processing data: ' + error);
1457
+
}
1458
+
});
1459
+
}
1460
+
form.on('submit', function (e){
1461
+
e.preventDefault();
1462
+
var select = jQuery('#model_selector_data_batch').val();
1463
+
if(aiomatic_object.moder_gpt_models_aiomatic.includes(select))
1464
+
{
1465
+
var total = 0;
1466
+
var lists = [];
1467
+
var btn = form.find('.aiomatic_submit');
1468
+
$('.aiomatic_new_data_batch').each(function (idx, item){
1469
+
var item_system = $(item).find('.aiomatic_new_data_batch_system').val();
1470
+
var item_prompt = $(item).find('.aiomatic_new_data_batch_prompt').val();
1471
+
if(item_prompt !== ''){
1472
+
total += 1;
1473
+
lists.push({system: item_system, prompt: item_prompt })
1474
+
}
1475
+
});
1476
+
if(total > 0){
1477
+
$('#aiomatic_upload_convert_line').val('0');
1478
+
$('#aiomatic_upload_convert_index').val('1');
1479
+
$('.aiomatic-upload-message').empty();
1480
+
progressBar.show();
1481
+
progressBar.removeClass('aiomatic_error')
1482
+
progressBar.find('span').css('width',0);
1483
+
progressBar.find('small').html('0%');
1484
+
aiomaticLoading(btn);
1485
+
aiomaticNewProcessData(lists, 0, '', btn);
1486
+
}
1487
+
else{
1488
+
alert('Please insert at least one row');
1489
+
}
1490
+
}
1491
+
else
1492
+
{
1493
+
var total = 0;
1494
+
var lists = [];
1495
+
var btn = form.find('.aiomatic_submit');
1496
+
$('.aiomatic_data').each(function (idx, item){
1497
+
var item_prompt = $(item).find('.aiomatic_data_prompt').val();
1498
+
if(item_prompt !== ''){
1499
+
total += 1;
1500
+
lists.push({prompt: item_prompt})
1501
+
}
1502
+
});
1503
+
if(total > 0){
1504
+
$('#aiomatic_upload_convert_line').val('0');
1505
+
$('#aiomatic_upload_convert_index').val('1');
1506
+
$('.aiomatic-upload-message').empty();
1507
+
progressBar.show();
1508
+
progressBar.removeClass('aiomatic_error')
1509
+
progressBar.find('span').css('width',0);
1510
+
progressBar.find('small').html('0%');
1511
+
aiomaticLoading(btn);
1512
+
aiomaticProcessData(lists, 0, '', btn);
1513
+
}
1514
+
else{
1515
+
alert('Please insert at least one row');
1516
+
}
1517
+
}
1518
+
return false;
1519
+
});
1520
+
$('.aiomatic_modal_close').on('click', function (){
1521
+
$('.aiomatic_modal_close').closest('.aiomatic_modal').hide();
1522
+
$('.aiomatic_modal_close').closest('.aiomatic_modal').removeClass('aiomatic-small-modal');
1523
+
$('.aiomatic-overlay').hide();
1524
+
});
1525
+
function aiomaticFileUpload(data, btn){
1526
+
var aiomatic_upload_convert_index = parseInt($('#aiomatic_upload_convert_index').val());
1527
+
var total_lines = parseInt($('#aiomatic_upload_convert_lines').val());
1528
+
if(total_lines === 0)
1529
+
{
1530
+
total_lines = 1;
1531
+
}
1532
+
var aiomatic_upload_bar = $('.aiomatic-convert-bar');
1533
+
$.ajax({
1534
+
url: aiomatic_ajax_url,
1535
+
data: data,
1536
+
type: 'POST',
1537
+
dataType: 'JSON',
1538
+
success: function (res){
1539
+
if(res.status === 'success'){
1540
+
if(res.next === 'DONE'){
1541
+
$('.aiomatic-upload-message').html('Upload was successful!');
1542
+
res.next = total_lines;
1543
+
var percent = Math.ceil(res.next*100/total_lines);
1544
+
aiomatic_upload_bar.find('small').html(percent+'%');
1545
+
aiomatic_upload_bar.find('span').css('width',percent+'%');
1546
+
aiomaticRmLoading(btn);
1547
+
}
1548
+
else{
1549
+
var percent = Math.ceil(res.next*100/total_lines);
1550
+
aiomatic_upload_bar.find('small').html(percent+'%');
1551
+
aiomatic_upload_bar.find('span').css('width',percent+'%');
1552
+
$('#aiomatic_upload_convert_line').val(res.next);
1553
+
$('#aiomatic_upload_convert_index').val(aiomatic_upload_convert_index+1);
1554
+
var data = $('#aiomatic_upload_convert').serialize();
1555
+
aiomaticFileUpload(data,btn);
1556
+
}
1557
+
}
1558
+
else{
1559
+
aiomatic_upload_bar.addClass('aiomatic_error');
1560
+
aiomaticRmLoading(btn);
1561
+
alert(res.msg);
1562
+
}
1563
+
},
1564
+
error: function (r, s, error){
1565
+
aiomatic_upload_bar.addClass('aiomatic_error');
1566
+
aiomaticRmLoading(btn);
1567
+
alert('Error in processing file upload: ' + error);
1568
+
}
1569
+
});
1570
+
}
1571
+
$(document).on('submit','#aiomatic_upload_convert', function (e){
1572
+
$('#aiomatic_upload_convert_index').val(1);
1573
+
$('#aiomatic_upload_convert_line').val(0);
1574
+
$('.aiomatic-upload-message').empty();
1575
+
var form = $(e.currentTarget);
1576
+
var data = form.serialize();
1577
+
var btn = form.find('button');
1578
+
aiomaticLoading(btn);
1579
+
var aiomatic_upload_bar = $('.aiomatic-upload-bar');
1580
+
aiomatic_upload_bar.show();
1581
+
aiomatic_upload_bar.removeClass('aiomatic_error')
1582
+
aiomatic_upload_bar.find('span').css('width',0);
1583
+
aiomatic_upload_bar.find('small').html('0%');
1584
+
aiomaticFileUpload(data,btn);
1585
+
return false;
1586
+
});
1587
+
$('.aiomatic_modal_close').on('click', function (){
1588
+
$('.aiomatic_modal_close').closest('.aiomatic_modal').hide();
1589
+
$('.aiomatic_modal_close').closest('.aiomatic_modal').removeClass('aiomatic-small-modal');
1590
+
$('.aiomatic-overlay').hide();
1591
+
})
1592
+
var aiomaticAjaxRunning = false;
1593
+
aiomatic_delete_file.on('click', function (){
1594
+
if(!aiomaticAjaxRunning) {
1595
+
var conf = confirm('Are you sure that you want to delete this file?');
1596
+
if (conf) {
1597
+
var btn = $(this);
1598
+
var id = btn.attr('data-id');
1599
+
$.ajax({
1600
+
url: aiomatic_ajax_url,
1601
+
data: {action: 'aiomatic_delete_finetune_file', id: id, nonce: aiomatic_object.nonce},
1602
+
dataType: 'JSON',
1603
+
type: 'POST',
1604
+
beforeSend: function () {
1605
+
aiomaticAjaxRunning = true;
1606
+
aiomaticLoading(btn);
1607
+
},
1608
+
success: function (res) {
1609
+
aiomaticAjaxRunning = false;
1610
+
aiomaticRmLoading(btn);
1611
+
if (res.status === 'success') {
1612
+
window.location.reload();
1613
+
} else {
1614
+
alert(res.msg);
1615
+
}
1616
+
},
1617
+
error: function (r, s, error) {
1618
+
aiomaticAjaxRunning = false;
1619
+
aiomaticRmLoading(btn);
1620
+
alert('Error in processing finetune removal: ' + error);
1621
+
}
1622
+
});
1623
+
}
1624
+
else{
1625
+
aiomaticAjaxRunning = false;
1626
+
}
1627
+
}
1628
+
});
1629
+
var aiomaticAjaxRunning = false;
1630
+
$('.aiomatic_modal_close').on('click', function (){
1631
+
$('.aiomatic_modal_close').closest('.aiomatic_modal').hide();
1632
+
$('.aiomatic-overlay').hide();
1633
+
})
1634
+
function aiomaticLoading(btn){
1635
+
btn.attr('disabled','disabled');
1636
+
if(btn.find('.spinner').length === 0){
1637
+
btn.append('<span class="aiomatic-spinner spinner"></span>');
1638
+
}
1639
+
btn.find('.spinner').css('visibility','unset');
1640
+
}
1641
+
function aiomaticRmLoading(btn){
1642
+
btn.removeAttr('disabled');
1643
+
btn.find('.spinner').remove();
1644
+
}
1645
+
});