Slight refactor Kobold version flags

This commit is contained in:
Cohee 2023-09-01 01:07:04 +03:00
parent e9de47615a
commit 1f10acdf17
3 changed files with 188 additions and 25 deletions

View File

@ -1018,11 +1018,67 @@
Single-line mode</span> Single-line mode</span>
</label> </label>
</div> </div>
<div class="range-block">
<label class="checkbox_label" for="use_default_badwordids">
<input id="use_default_badwordids" type="checkbox" />
<span data-i18n="Ban EOS Token">
Ban EOS Token
</span>
</label>
</div>
<hr>
<h4 data-i18n="Mirostat">Mirostat</h4>
<div class="range-block">
<div class="range-block-title" data-i18n="Mirostat Mode">
Mirostat Mode
</div>
<div class="range-block-range-and-counter">
<div class="range-block-range">
<input type="range" id="mirostat_mode_kobold" name="volume" min="0" max="2" step="1" />
</div>
<div class="range-block-counter">
<div contenteditable="true" data-for="mirostat_mode_kobold" id="mirostat_mode_counter_kobold">
select
</div>
</div>
</div>
</div>
<div class="range-block">
<div class="range-block-title" data-i18n="Mirostat Tau">
Mirostat Tau
</div>
<div class="range-block-range-and-counter">
<div class="range-block-range">
<input type="range" id="mirostat_tau_kobold" name="volume" min="0" max="10" step="0.01" />
</div>
<div class="range-block-counter">
<div contenteditable="true" data-for="mirostat_tau_kobold" id="mirostat_tau_counter_kobold">
select
</div>
</div>
</div>
</div>
<div class="range-block">
<div class="range-block-title" data-i18n="Mirostat Eta">
Mirostat Eta
</div>
<div class="range-block-range-and-counter">
<div class="range-block-range">
<input type="range" id="mirostat_eta_kobold" name="volume" min="0" max="1" step="0.01" />
</div>
<div class="range-block-counter">
<div contenteditable="true" data-for="mirostat_eta_kobold" id="mirostat_eta_counter_kobold">
select
</div>
</div>
</div>
</div>
<hr>
<div class="range-block flexFlowColumn"> <div class="range-block flexFlowColumn">
<div class="range-block-title"> <div class="range-block-title">
<span data-i18n="Samplers Order">Samplers Order</span> <span data-i18n="Samplers Order">Samplers Order</span>
</div> </div>
<div class="toggle-description" data-i18n="Samplers will be applied in a top-down order. Use with caution."> <div class="toggle-description widthUnset" data-i18n="Samplers will be applied in a top-down order. Use with caution.">
Samplers will be applied in a top-down order. Samplers will be applied in a top-down order.
Use with caution. Use with caution.
</div> </div>
@ -1240,7 +1296,7 @@
<div class="range-block-title"> <div class="range-block-title">
<span data-i18n="Samplers Order">Samplers Order</span> <span data-i18n="Samplers Order">Samplers Order</span>
</div> </div>
<div class="toggle-description" data-i18n="Samplers will be applied in a top-down order. Use with caution."> <div class="toggle-description widthUnset" data-i18n="Samplers will be applied in a top-down order. Use with caution.">
Samplers will be applied in a top-down order. Use with caution. Samplers will be applied in a top-down order. Use with caution.
</div> </div>
<div id="novel_order" class="prompt_order"> <div id="novel_order" class="prompt_order">
@ -2134,7 +2190,45 @@
</div> </div>
</div> </div>
</div> </div>
<div data-newbie-hidden class="inline-drawer wide100p flexFlowColumn margin-bot-10px" style="display:none;">
<div class="inline-drawer-toggle inline-drawer-header">
<b><span data-i18n="Context Order">Context Order</span></b>
<div class="fa-solid fa-circle-chevron-down inline-drawer-icon down"></div>
</div>
<div class="inline-drawer-content">
<div id="context_order" class="prompt_order">
<div data-id="0">
<span data-i18n="Story String">Story String</span>
<small>0</small>
</div>
<div data-id="1">
<span data-i18n="Summary">Summary</span>
<small>1</small>
</div>
<div data-id="2">
<span data-i18n="Author's Note">Author's Note</span>
<small>2</small>
</div>
<div data-id="3">
<span data-i18n="Example Dialogues">Example Dialogues</span>
<small>3</small>
</div>
<div data-id="4">
<span data-i18n="Chat Start">Chat History</span>
<small>4</small>
</div>
</div>
<small>
<b data-i18n="Hint">Hint:</b>
<span data-i18n="In-Chat Position not affected">
Summary and Author's Note orders are only affected when they don't have an In-Chat position set.
</span>
</small>
</div>
</div>
</div> </div>
<div> <div>
<h4 data-i18n="Instruct Mode">Instruct Mode <h4 data-i18n="Instruct Mode">Instruct Mode
<a href="https://docs.sillytavern.app/usage/core-concepts/instructmode/" class="notes-link" target="_blank"> <a href="https://docs.sillytavern.app/usage/core-concepts/instructmode/" class="notes-link" target="_blank">

View File

@ -6,9 +6,8 @@ import {
loadKoboldSettings, loadKoboldSettings,
formatKoboldUrl, formatKoboldUrl,
getKoboldGenerationData, getKoboldGenerationData,
canUseKoboldStopSequence, kai_flags,
canUseKoboldStreaming, setKoboldFlags,
canUseKoboldTokenization,
} from "./scripts/kai-settings.js"; } from "./scripts/kai-settings.js";
import { import {
@ -808,9 +807,7 @@ async function getStatus() {
// determine if we can use stop sequence and streaming // determine if we can use stop sequence and streaming
if (main_api === "kobold" || main_api === "koboldhorde") { if (main_api === "kobold" || main_api === "koboldhorde") {
kai_settings.use_stop_sequence = canUseKoboldStopSequence(data.version); setKoboldFlags(data.version, data.koboldVersion);
kai_settings.can_use_streaming = canUseKoboldStreaming(data.koboldVersion);
kai_settings.can_use_tokenization = canUseKoboldTokenization(data.koboldVersion);
} }
// We didn't get a 200 status code, but the endpoint has an explanation. Which means it DID connect, but I digress. // We didn't get a 200 status code, but the endpoint has an explanation. Which means it DID connect, but I digress.
@ -2026,7 +2023,7 @@ function baseChatReplace(value, name1, name2) {
function isStreamingEnabled() { function isStreamingEnabled() {
return ((main_api == 'openai' && oai_settings.stream_openai && oai_settings.chat_completion_source !== chat_completion_sources.SCALE && oai_settings.chat_completion_source !== chat_completion_sources.AI21) return ((main_api == 'openai' && oai_settings.stream_openai && oai_settings.chat_completion_source !== chat_completion_sources.SCALE && oai_settings.chat_completion_source !== chat_completion_sources.AI21)
|| (main_api == 'kobold' && kai_settings.streaming_kobold && kai_settings.can_use_streaming) || (main_api == 'kobold' && kai_settings.streaming_kobold && kai_flags.can_use_streaming)
|| (main_api == 'novel' && nai_settings.streaming_novel) || (main_api == 'novel' && nai_settings.streaming_novel)
|| (main_api == 'textgenerationwebui' && textgenerationwebui_settings.streaming)) || (main_api == 'textgenerationwebui' && textgenerationwebui_settings.streaming))
&& !isMultigenEnabled(); // Multigen has a quasi-streaming mode which breaks the real streaming && !isMultigenEnabled(); // Multigen has a quasi-streaming mode which breaks the real streaming
@ -2318,7 +2315,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
return; return;
} }
if (main_api == 'kobold' && kai_settings.streaming_kobold && !kai_settings.can_use_streaming) { if (main_api == 'kobold' && kai_settings.streaming_kobold && !kai_flags.can_use_streaming) {
toastr.error('Streaming is enabled, but the version of Kobold used does not support token streaming.', undefined, { timeOut: 10000, preventDuplicates: true, }); toastr.error('Streaming is enabled, but the version of Kobold used does not support token streaming.', undefined, { timeOut: 10000, preventDuplicates: true, });
is_send_press = false; is_send_press = false;
return; return;

View File

@ -20,15 +20,29 @@ export const kai_settings = {
tfs: 1, tfs: 1,
rep_pen_slope: 0.9, rep_pen_slope: 0.9,
single_line: false, single_line: false,
use_stop_sequence: false,
can_use_tokenization: false,
streaming_kobold: false, streaming_kobold: false,
sampler_order: [0, 1, 2, 3, 4, 5, 6], sampler_order: [0, 1, 2, 3, 4, 5, 6],
mirostat: 0,
mirostat_tau: 5.0,
mirostat_eta: 0.1,
use_default_badwordids: true,
}; };
export const kai_flags = {
can_use_tokenization: false,
can_use_stop_sequence: false,
can_use_streaming: false,
can_use_default_badwordids: false,
can_use_mirostat: false,
};
const defaultValues = Object.freeze(structuredClone(kai_settings));
const MIN_STOP_SEQUENCE_VERSION = '1.2.2'; const MIN_STOP_SEQUENCE_VERSION = '1.2.2';
const MIN_UNBAN_VERSION = '1.2.4';
const MIN_STREAMING_KCPPVERSION = '1.30'; const MIN_STREAMING_KCPPVERSION = '1.30';
const MIN_TOKENIZATION_KCPPVERSION = '1.41'; const MIN_TOKENIZATION_KCPPVERSION = '1.41';
const MIN_MIROSTAT_KCPPVERSION = '1.35';
const KOBOLDCPP_ORDER = [6, 0, 1, 3, 4, 2, 5]; const KOBOLDCPP_ORDER = [6, 0, 1, 3, 4, 2, 5];
export function formatKoboldUrl(value) { export function formatKoboldUrl(value) {
@ -44,16 +58,16 @@ export function formatKoboldUrl(value) {
export function loadKoboldSettings(preset) { export function loadKoboldSettings(preset) {
for (const name of Object.keys(kai_settings)) { for (const name of Object.keys(kai_settings)) {
const value = preset[name]; const value = preset[name] ?? defaultValues[name];
const slider = sliders.find(x => x.name === name); const slider = sliders.find(x => x.name === name);
if (value === undefined || !slider) { if (!slider) {
continue; continue;
} }
const formattedValue = slider.format(value); const formattedValue = slider.format(value);
slider.setValue(preset[name]); slider.setValue(value);
$(slider.sliderId).val(preset[name]); $(slider.sliderId).val(value);
$(slider.counterId).text(formattedValue); $(slider.counterId).text(formattedValue);
} }
@ -66,6 +80,10 @@ export function loadKoboldSettings(preset) {
kai_settings.streaming_kobold = preset.streaming_kobold; kai_settings.streaming_kobold = preset.streaming_kobold;
$('#streaming_kobold').prop('checked', kai_settings.streaming_kobold); $('#streaming_kobold').prop('checked', kai_settings.streaming_kobold);
} }
if (preset.hasOwnProperty('use_default_badwordids')) {
kai_settings.use_default_badwordids = preset.use_default_badwordids;
$('#use_default_badwordids').prop('checked', kai_settings.use_default_badwordids);
}
} }
export function getKoboldGenerationData(finalPrompt, this_settings, this_amount_gen, this_max_context, isImpersonate, type) { export function getKoboldGenerationData(finalPrompt, this_settings, this_amount_gen, this_max_context, isImpersonate, type) {
@ -94,9 +112,13 @@ export function getKoboldGenerationData(finalPrompt, this_settings, this_amount_
s7: sampler_order[6], s7: sampler_order[6],
use_world_info: false, use_world_info: false,
singleline: kai_settings.single_line, singleline: kai_settings.single_line,
stop_sequence: kai_settings.use_stop_sequence ? getStoppingStrings(isImpersonate, false) : undefined, stop_sequence: kai_flags.can_use_stop_sequence ? getStoppingStrings(isImpersonate, false) : undefined,
streaming: kai_settings.streaming_kobold && kai_settings.can_use_streaming && type !== 'quiet', streaming: kai_settings.streaming_kobold && kai_flags.can_use_streaming && type !== 'quiet',
can_abort: kai_settings.can_use_streaming, can_abort: kai_flags.can_use_streaming,
mirostat: kai_flags.can_use_mirostat ? kai_settings.mirostat : undefined,
mirostat_tau: kai_flags.can_use_mirostat ? kai_settings.mirostat_tau : undefined,
mirostat_eta: kai_flags.can_use_mirostat ? kai_settings.mirostat_eta : undefined,
use_default_badwordids: kai_flags.can_use_default_badwordids ? kai_settings.use_default_badwordids : undefined,
}; };
return generate_data; return generate_data;
} }
@ -213,24 +235,62 @@ const sliders = [
counterId: "#no_op_selector", counterId: "#no_op_selector",
format: (val) => val, format: (val) => val,
setValue: (val) => { sortItemsByOrder(val); kai_settings.sampler_order = val; }, setValue: (val) => { sortItemsByOrder(val); kai_settings.sampler_order = val; },
} },
{
name: "mirostat",
sliderId: "#mirostat_mode_kobold",
counterId: "#mirostat_mode_counter_kobold",
format: (val) => val,
setValue: (val) => { kai_settings.mirostat = Number(val); },
},
{
name: "mirostat_tau",
sliderId: "#mirostat_tau_kobold",
counterId: "#mirostat_tau_counter_kobold",
format: (val) => val,
setValue: (val) => { kai_settings.mirostat_tau = Number(val); },
},
{
name: "mirostat_eta",
sliderId: "#mirostat_eta_kobold",
counterId: "#mirostat_eta_counter_kobold",
format: (val) => val,
setValue: (val) => { kai_settings.mirostat_eta = Number(val); },
},
]; ];
export function setKoboldFlags(version, koboldVersion) {
kai_flags.can_use_stop_sequence = canUseKoboldStopSequence(version);
kai_flags.can_use_streaming = canUseKoboldStreaming(koboldVersion);
kai_flags.can_use_tokenization = canUseKoboldTokenization(koboldVersion);
kai_flags.can_use_default_badwordids = canUseDefaultBadwordIds(version);
kai_flags.can_use_mirostat = canUseMirostat(koboldVersion);
}
/** /**
* Determines if the Kobold stop sequence can be used with the given version. * Determines if the Kobold stop sequence can be used with the given version.
* @param {string} version KoboldAI version to check. * @param {string} version KoboldAI version to check.
* @returns {boolean} True if the Kobold stop sequence can be used, false otherwise. * @returns {boolean} True if the Kobold stop sequence can be used, false otherwise.
*/ */
export function canUseKoboldStopSequence(version) { function canUseKoboldStopSequence(version) {
return (version || '0.0.0').localeCompare(MIN_STOP_SEQUENCE_VERSION, undefined, { numeric: true, sensitivity: 'base' }) > -1; return (version || '0.0.0').localeCompare(MIN_STOP_SEQUENCE_VERSION, undefined, { numeric: true, sensitivity: 'base' }) > -1;
} }
/**
* Determines if the Kobold default badword ids can be used with the given version.
* @param {string} version KoboldAI version to check.
* @returns {boolean} True if the Kobold default badword ids can be used, false otherwise.
*/
function canUseDefaultBadwordIds(version) {
return (version || '0.0.0').localeCompare(MIN_UNBAN_VERSION, undefined, { numeric: true, sensitivity: 'base' }) > -1;
}
/** /**
* Determines if the Kobold streaming API can be used with the given version. * Determines if the Kobold streaming API can be used with the given version.
* @param {{ result: string; version: string; }} koboldVersion KoboldAI version object. * @param {{ result: string; version: string; }} koboldVersion KoboldAI version object.
* @returns {boolean} True if the Kobold streaming API can be used, false otherwise. * @returns {boolean} True if the Kobold streaming API can be used, false otherwise.
*/ */
export function canUseKoboldStreaming(koboldVersion) { function canUseKoboldStreaming(koboldVersion) {
if (koboldVersion && koboldVersion.result == 'KoboldCpp') { if (koboldVersion && koboldVersion.result == 'KoboldCpp') {
return (koboldVersion.version || '0.0').localeCompare(MIN_STREAMING_KCPPVERSION, undefined, { numeric: true, sensitivity: 'base' }) > -1; return (koboldVersion.version || '0.0').localeCompare(MIN_STREAMING_KCPPVERSION, undefined, { numeric: true, sensitivity: 'base' }) > -1;
} else return false; } else return false;
@ -241,12 +301,18 @@ export function canUseKoboldStreaming(koboldVersion) {
* @param {{ result: string; version: string; }} koboldVersion KoboldAI version object. * @param {{ result: string; version: string; }} koboldVersion KoboldAI version object.
* @returns {boolean} True if the Kobold tokenization API can be used, false otherwise. * @returns {boolean} True if the Kobold tokenization API can be used, false otherwise.
*/ */
export function canUseKoboldTokenization(koboldVersion) { function canUseKoboldTokenization(koboldVersion) {
if (koboldVersion && koboldVersion.result == 'KoboldCpp') { if (koboldVersion && koboldVersion.result == 'KoboldCpp') {
return (koboldVersion.version || '0.0').localeCompare(MIN_TOKENIZATION_KCPPVERSION, undefined, { numeric: true, sensitivity: 'base' }) > -1; return (koboldVersion.version || '0.0').localeCompare(MIN_TOKENIZATION_KCPPVERSION, undefined, { numeric: true, sensitivity: 'base' }) > -1;
} else return false; } else return false;
} }
function canUseMirostat(koboldVersion) {
if (koboldVersion && koboldVersion.result == 'KoboldCpp') {
return (koboldVersion.version || '0.0').localeCompare(MIN_MIROSTAT_KCPPVERSION, undefined, { numeric: true, sensitivity: 'base' }) > -1;
} else return false;
}
/** /**
* Sorts the sampler items by the given order. * Sorts the sampler items by the given order.
* @param {any[]} orderArray Sampler order array. * @param {any[]} orderArray Sampler order array.
@ -274,17 +340,23 @@ jQuery(function () {
}); });
$('#single_line').on("input", function () { $('#single_line').on("input", function () {
const value = $(this).prop('checked'); const value = !!$(this).prop('checked');
kai_settings.single_line = value; kai_settings.single_line = value;
saveSettingsDebounced(); saveSettingsDebounced();
}); });
$('#streaming_kobold').on("input", function () { $('#streaming_kobold').on("input", function () {
const value = $(this).prop('checked'); const value = !!$(this).prop('checked');
kai_settings.streaming_kobold = value; kai_settings.streaming_kobold = value;
saveSettingsDebounced(); saveSettingsDebounced();
}); });
$('#use_default_badwordids').on("input", function () {
const value = !!$(this).prop('checked');
kai_settings.use_default_badwordids = value;
saveSettingsDebounced();
});
$('#kobold_order').sortable({ $('#kobold_order').sortable({
delay: getSortableDelay(), delay: getSortableDelay(),
stop: function () { stop: function () {