Add import/export/deletion of logit bias presets

This commit is contained in:
SillyLossy
2023-04-24 19:40:02 +03:00
parent 9fc02d53c1
commit 443682a7f3
5 changed files with 105 additions and 4 deletions

View File

@ -721,7 +721,11 @@
<div class="openai_logit_bias_preset_form"> <div class="openai_logit_bias_preset_form">
<select id="openai_logit_bias_preset"> <select id="openai_logit_bias_preset">
</select> </select>
<i id="openai_logit_bias_new_preset" class="menu_button fa-solid fa-plus"></i> <i title="New preset" id="openai_logit_bias_new_preset" class="menu_button fa-solid fa-plus"></i>
<i title="Import preset" id="openai_logit_bias_import_preset" class="menu_button fa-solid fa-upload"></i>
<i title="Export preset" id="openai_logit_bias_export_preset" class="menu_button fa-solid fa-download"></i>
<i title="Delete preset" id="openai_logit_bias_delete_preset" class="menu_button fa-solid fa-trash-can"></i>
<input id="openai_logit_bias_import_file" type="file" accept=".json" hidden />
</div> </div>
<div class="inline-drawer wide100p"> <div class="inline-drawer wide100p">

View File

@ -3237,6 +3237,7 @@ function callPopup(text, type) {
break; break;
case "world_imported": case "world_imported":
case "new_chat": case "new_chat":
case "confirm":
$("#dialogue_popup_ok").text("Yes"); $("#dialogue_popup_ok").text("Yes");
break; break;
case "del_world": case "del_world":

View File

@ -26,7 +26,9 @@ import {
} from "./power-user.js"; } from "./power-user.js";
import { import {
download,
getStringHash, getStringHash,
parseJsonFile,
} from "./utils.js"; } from "./utils.js";
export { export {
@ -67,7 +69,7 @@ const gpt3_max = 4095;
const gpt4_max = 8191; const gpt4_max = 8191;
const gpt4_32k_max = 32767; const gpt4_32k_max = 32767;
let biasCache = null; let biasCache = undefined;
const tokenCache = {}; const tokenCache = {};
const default_settings = { const default_settings = {
@ -888,13 +890,18 @@ async function createNewLogitBiasPreset() {
} }
if (name in oai_settings.bias_presets) { if (name in oai_settings.bias_presets) {
callPopup('Preset name should be unique.', 'input'); callPopup('Preset name should be unique.', 'text');
return; return;
} }
oai_settings.bias_preset_selected = name; oai_settings.bias_preset_selected = name;
oai_settings.bias_presets[name] = []; oai_settings.bias_presets[name] = [];
addLogitBiasPresetOption(name);
saveSettingsDebounced();
}
function addLogitBiasPresetOption(name) {
const option = document.createElement('option'); const option = document.createElement('option');
option.innerText = name; option.innerText = name;
option.value = name; option.value = name;
@ -902,7 +909,78 @@ async function createNewLogitBiasPreset() {
$('#openai_logit_bias_preset').append(option); $('#openai_logit_bias_preset').append(option);
$('#openai_logit_bias_preset').trigger('change'); $('#openai_logit_bias_preset').trigger('change');
}
function onLogitBiasPresetImportClick() {
$('#openai_logit_bias_import_file').click();
}
async function onLogitBiasPresetImportFileChange(e) {
const file = e.target.files[0];
if (!file || file.type !== "application/json") {
return;
}
const name = file.name.replace(/\.[^/.]+$/, "");
const importedFile = await parseJsonFile(file);
e.target.value = '';
if (name in oai_settings.bias_presets) {
callPopup('Preset name should be unique.', 'text');
return;
}
if (!Array.isArray(importedFile)) {
callPopup('Invalid logit bias preset file.', 'text');
return;
}
for (const entry of importedFile) {
if (typeof entry == 'object') {
if (entry.hasOwnProperty('text') && entry.hasOwnProperty('value')) {
continue;
}
}
callPopup('Invalid logit bias preset file.', 'text');
return;
}
oai_settings.bias_presets[name] = importedFile;
oai_settings.bias_preset_selected = name;
addLogitBiasPresetOption(name);
saveSettingsDebounced();
}
function onLogitBiasPresetExportClick() {
if (!oai_settings.bias_preset_selected || Object.keys(oai_settings.bias_presets).length === 0) {
return;
}
const presetJsonString = JSON.stringify(oai_settings.bias_presets[oai_settings.bias_preset_selected]);
download(presetJsonString, oai_settings.bias_preset_selected, 'application/json');
}
async function onLogitBiasPresetDeleteClick() {
const value = await callPopup('Delete the preset?', 'confirm');
if (!value) {
return;
}
$(`#openai_logit_bias_preset option[value="${oai_settings.bias_preset_selected}"]`).remove();
delete oai_settings.bias_presets[oai_settings.bias_preset_selected];
oai_settings.bias_preset_selected = null;
if (Object.keys(oai_settings.bias_presets).length) {
oai_settings.bias_preset_selected = Object.keys(oai_settings.bias_presets)[0];
$(`#openai_logit_bias_preset option[value="${oai_settings.bias_preset_selected}"]`).attr('selected', true);
$('#openai_logit_bias_preset').trigger('change');
}
biasCache = undefined;
saveSettingsDebounced(); saveSettingsDebounced();
} }
@ -1137,4 +1215,8 @@ $(document).ready(function () {
$('#openai_logit_bias_preset').on('change', onLogitBiasPresetChange); $('#openai_logit_bias_preset').on('change', onLogitBiasPresetChange);
$('#openai_logit_bias_new_preset').on('click', createNewLogitBiasPreset); $('#openai_logit_bias_new_preset').on('click', createNewLogitBiasPreset);
$('#openai_logit_bias_new_entry').on('click', createNewLogitBiasEntry); $('#openai_logit_bias_new_entry').on('click', createNewLogitBiasEntry);
$('#openai_logit_bias_import_file').on('input', onLogitBiasPresetImportFileChange);
$('#openai_logit_bias_import_preset').on('click', onLogitBiasPresetImportClick);
$('#openai_logit_bias_export_preset').on('click', onLogitBiasPresetExportClick);
$('#openai_logit_bias_delete_preset').on('click', onLogitBiasPresetDeleteClick);
}); });

View File

@ -10,6 +10,7 @@ export {
isSubsetOf, isSubsetOf,
incrementString, incrementString,
stringFormat, stringFormat,
parseJsonFile,
}; };
/// UTILS /// UTILS
@ -63,6 +64,15 @@ function getBase64Async(file) {
}); });
} }
async function parseJsonFile(file) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = event => resolve(JSON.parse(event.target.result));
fileReader.onerror = error => reject(error);
fileReader.readAsText(file);
});
}
function getStringHash(str, seed = 0) { function getStringHash(str, seed = 0) {
let h1 = 0xdeadbeef ^ seed, let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed; h2 = 0x41c6ce57 ^ seed;

View File

@ -3230,10 +3230,14 @@ toolcool-color-picker {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
width: 100%; width: 100%;
gap: 10px; gap: 5px;
align-items: baseline; align-items: baseline;
} }
#openai_logit_bias_new_entry {
margin-top: 0.5rem;
}
.openai_logit_bias_preset_form select { .openai_logit_bias_preset_form select {
flex: 1; flex: 1;
} }