|
import { saveSettingsDebounced } from '../script.js'; |
|
import { getTextTokens } from './tokenizers.js'; |
|
import { getSortableDelay, uuidv4 } from './utils.js'; |
|
|
|
export const BIAS_CACHE = new Map(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function displayLogitBias(logitBias, containerSelector) { |
|
if (!Array.isArray(logitBias)) { |
|
console.log('Logit bias set not found'); |
|
return; |
|
} |
|
|
|
const list = $(containerSelector).find('.logit_bias_list'); |
|
list.empty(); |
|
|
|
for (const entry of logitBias) { |
|
if (entry) { |
|
createLogitBiasListItem(entry, logitBias, containerSelector); |
|
} |
|
} |
|
|
|
|
|
if (list.sortable('instance') !== undefined) { |
|
|
|
list.sortable('destroy'); |
|
} |
|
|
|
|
|
list.sortable({ |
|
delay: getSortableDelay(), |
|
handle: '.drag-handle', |
|
stop: function () { |
|
const order = []; |
|
list.children().each(function () { |
|
order.unshift($(this).data('id')); |
|
}); |
|
logitBias.sort((a, b) => order.indexOf(a.id) - order.indexOf(b.id)); |
|
console.log('Logit bias reordered:', logitBias); |
|
saveSettingsDebounced(); |
|
}, |
|
}); |
|
|
|
BIAS_CACHE.delete(containerSelector); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
export function createNewLogitBiasEntry(logitBias, containerSelector) { |
|
const entry = { id: uuidv4(), text: '', value: 0 }; |
|
logitBias.push(entry); |
|
BIAS_CACHE.delete(containerSelector); |
|
createLogitBiasListItem(entry, logitBias, containerSelector); |
|
saveSettingsDebounced(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function createLogitBiasListItem(entry, logitBias, containerSelector) { |
|
const id = entry.id; |
|
const template = $('#logit_bias_template .logit_bias_form').clone(); |
|
template.data('id', id); |
|
template.find('.logit_bias_text').val(entry.text).on('input', function () { |
|
entry.text = $(this).val(); |
|
BIAS_CACHE.delete(containerSelector); |
|
saveSettingsDebounced(); |
|
}); |
|
template.find('.logit_bias_value').val(entry.value).on('input', function () { |
|
entry.value = Number($(this).val()); |
|
BIAS_CACHE.delete(containerSelector); |
|
saveSettingsDebounced(); |
|
}); |
|
template.find('.logit_bias_remove').on('click', function () { |
|
$(this).closest('.logit_bias_form').remove(); |
|
const index = logitBias.indexOf(entry); |
|
if (index > -1) { |
|
logitBias.splice(index, 1); |
|
} |
|
BIAS_CACHE.delete(containerSelector); |
|
saveSettingsDebounced(); |
|
}); |
|
$(containerSelector).find('.logit_bias_list').prepend(template); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getLogitBiasListResult(biasPreset, tokenizerType, getBiasObject) { |
|
const result = []; |
|
|
|
for (const entry of biasPreset) { |
|
if (entry.text?.length > 0) { |
|
const text = entry.text.trim(); |
|
|
|
|
|
if (text.length === 0) { |
|
continue; |
|
} |
|
|
|
|
|
if (text.startsWith('{') && text.endsWith('}')) { |
|
const tokens = getTextTokens(tokenizerType, text.slice(1, -1)); |
|
result.push(getBiasObject(entry.value, tokens)); |
|
} |
|
|
|
|
|
|
|
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); |
|
} |
|
} |
|
|
|
|
|
|
|
else { |
|
const biasText = ` ${text}`; |
|
const tokens = getTextTokens(tokenizerType, biasText); |
|
result.push(getBiasObject(entry.value, tokens)); |
|
} |
|
} |
|
} |
|
return result; |
|
} |
|
|