Refactor Novel logit bias

This commit is contained in:
Cohee
2023-12-18 17:32:10 +02:00
parent cc27bcb076
commit 08ea2095f8
4 changed files with 148 additions and 113 deletions

View File

@@ -15,8 +15,8 @@ import {
getSortableDelay,
getStringHash,
onlyUnique,
uuidv4,
} from './utils.js';
import { BIAS_CACHE, createNewLogitBiasEntry, displayLogitBias, getLogitBiasListResult } from './logit-bias.js';
const default_preamble = '[ Style: chat, complex, sensory, visceral ]';
const default_order = [1, 5, 0, 2, 3, 4];
@@ -59,7 +59,7 @@ const nai_tiers = {
let novel_data = null;
let badWordsCache = {};
let biasCache = undefined;
const BIAS_KEY = '#novel_api-settings';
export function setNovelData(data) {
novel_data = data;
@@ -145,7 +145,7 @@ export function loadNovelSettings(settings) {
//load the rest of the Novel settings without any checks
nai_settings.model_novel = settings.model_novel;
$('#model_novel_select').val(nai_settings.model_novel);
$(`#model_novel_select option[value=${nai_settings.model_novel}]`).attr('selected', true);
$(`#model_novel_select option[value=${nai_settings.model_novel}]`).prop('selected', true);
if (settings.nai_preamble !== undefined) {
nai_settings.preamble = settings.nai_preamble;
@@ -217,7 +217,7 @@ function loadNovelSettingsUi(ui_settings) {
$('#streaming_novel').prop('checked', ui_settings.streaming_novel);
sortItemsByOrder(ui_settings.order);
displayLogitBias(ui_settings.logit_bias);
displayLogitBias(ui_settings.logit_bias, BIAS_KEY);
}
const sliders = [
@@ -433,8 +433,8 @@ export function getNovelGenerationData(finalPrompt, settings, maxLength, isImper
let logitBias = [];
if (tokenizerType !== tokenizers.NONE && Array.isArray(nai_settings.logit_bias) && nai_settings.logit_bias.length) {
logitBias = biasCache || calculateLogitBias();
biasCache = logitBias;
logitBias = BIAS_CACHE.get(BIAS_KEY) || calculateLogitBias();
BIAS_CACHE.set(BIAS_KEY, logitBias);
}
return {
@@ -525,65 +525,14 @@ function saveSamplingOrder() {
saveSettingsDebounced();
}
function displayLogitBias(logit_bias) {
if (!Array.isArray(logit_bias)) {
console.log('Logit bias set not found');
return;
}
$('.novelai_logit_bias_list').empty();
for (const entry of logit_bias) {
if (entry) {
createLogitBiasListItem(entry);
}
}
biasCache = undefined;
}
function createNewLogitBiasEntry() {
const entry = { id: uuidv4(), text: '', value: 0 };
nai_settings.logit_bias.push(entry);
biasCache = undefined;
createLogitBiasListItem(entry);
saveSettingsDebounced();
}
function createLogitBiasListItem(entry) {
const id = entry.id;
const template = $('#novelai_logit_bias_template .novelai_logit_bias_form').clone();
template.data('id', id);
template.find('.novelai_logit_bias_text').val(entry.text).on('input', function () {
entry.text = $(this).val();
biasCache = undefined;
saveSettingsDebounced();
});
template.find('.novelai_logit_bias_value').val(entry.value).on('input', function () {
entry.value = Number($(this).val());
biasCache = undefined;
saveSettingsDebounced();
});
template.find('.novelai_logit_bias_remove').on('click', function () {
$(this).closest('.novelai_logit_bias_form').remove();
const index = nai_settings.logit_bias.indexOf(entry);
if (index > -1) {
nai_settings.logit_bias.splice(index, 1);
}
biasCache = undefined;
saveSettingsDebounced();
});
$('.novelai_logit_bias_list').prepend(template);
}
/**
* Calculates logit bias for Novel AI
* @returns {object[]} Array of logit bias objects
*/
function calculateLogitBias() {
const bias_preset = nai_settings.logit_bias;
const biasPreset = nai_settings.logit_bias;
if (!Array.isArray(bias_preset) || bias_preset.length === 0) {
if (!Array.isArray(biasPreset) || biasPreset.length === 0) {
return [];
}
@@ -605,47 +554,7 @@ function calculateLogitBias() {
};
}
const result = [];
for (const entry of bias_preset) {
if (entry.text?.length > 0) {
const text = entry.text.trim();
// Skip empty lines
if (text.length === 0) {
continue;
}
// Verbatim text
if (text.startsWith('{') && text.endsWith('}')) {
const tokens = getTextTokens(tokenizerType, text.slice(1, -1));
result.push(getBiasObject(entry.value, tokens));
}
// Raw token ids, JSON serialized
else if (text.startsWith('[') && text.endsWith(']')) {
try {
const tokens = JSON.parse(text);
if (Array.isArray(tokens) && tokens.every(t => Number.isInteger(t))) {
result.push(getBiasObject(entry.value, tokens));
} else {
throw new Error('Not an array of integers');
}
} catch (err) {
console.log(`Failed to parse logit bias token list: ${text}`, err);
}
}
// Text with a leading space
else {
const biasText = ` ${text}`;
const tokens = getTextTokens(tokenizerType, biasText);
result.push(getBiasObject(entry.value, tokens));
}
}
}
const result = getLogitBiasListResult(biasPreset, tokenizerType, getBiasObject);
return result;
}
@@ -778,5 +687,5 @@ jQuery(function () {
saveSamplingOrder();
});
$('#novelai_logit_bias_new_entry').on('click', createNewLogitBiasEntry);
$('#novelai_logit_bias_new_entry').on('click', () => createNewLogitBiasEntry(nai_settings.logit_bias, BIAS_KEY));
});