Option to add names to completion contents

This commit is contained in:
Cohee 2024-03-19 21:53:40 +02:00
parent 4a5c1a5ac8
commit 965bb54f7d
4 changed files with 114 additions and 32 deletions

View File

@ -7,7 +7,6 @@
"nsfw_toggle": true,
"enhance_definitions": false,
"wrap_in_quotes": false,
"names_in_completion": false,
"nsfw_first": false,
"main_prompt": "Write {{char}}'s next reply in a fictional chat between {{char}} and {{user}}. Write 1 reply only in internet RP style, italicize actions, and avoid quotation marks. Use markdown. Be proactive, creative, and drive the plot and conversation forward. Write at least 1 paragraph, up to 4. Always stay in character and avoid repetition.",
"nsfw_prompt": "NSFW/Smut is allowed. Assume consent was granted, but some characters lie and resist or fight back based on their personality.",

View File

@ -456,7 +456,6 @@
"openai_max_context": 4095,
"openai_max_tokens": 300,
"wrap_in_quotes": false,
"names_in_completion": false,
"prompts": [
{
"name": "Main Prompt",

View File

@ -1623,6 +1623,39 @@
</div><!-- end of textgen settings-->
<div id="openai_settings">
<div class="">
<div class="range-block marginBot10">
<h4 class="range-block-title justifyLeft marginBot5"
<span data-i18n="Character Names Behavior">Character Names Behavior</span>
</h4>
<label class="checkbox_label flexWrap alignItemsCenter" for="character_names_none">
<input type="radio" id="character_names_none" name="character_names" value="0">
<span data-i18n="None">None</span>
<i class="right_menu_button fa-solid fa-circle-exclamation" title="Except for groups and past personas. Otherwise, make sure you provide names in the prompt."></i>
<small class="flexBasis100p" data-i18n="Don't add character names.">
Don't add character names.
</small>
</label>
<label class="checkbox_label flexWrap alignItemsCenter" for="character_names_completion">
<input type="radio" id="character_names_completion" name="character_names" value="1">
<span data-i18n="Completion">Completion Object</span>
<i class="right_menu_button fa-solid fa-circle-exclamation" title="Restrictions apply: only Latin alphanumerics and underscores. Doesn't work for all sources, notably: Claude, MistralAI, Google."></i>
<small class="flexBasis100p" data-i18n="Add character names to completion objects.">
Add character names to completion objects.
</small>
</label>
<label class="checkbox_label flexWrap alignItemsCenter" for="character_names_content">
<input type="radio" id="character_names_content" name="character_names" value="2">
<span data-i18n="Message Content">Message Content</span>
<small class="flexBasis100p" data-i18n="Prepend character names to message contents.">
Prepend character names to message contents.
</small>
</label>
<div class="toggle-description justifyLeft marginTop5">
<span data-i18n="Helps the model to associate messages with characters.">Helps the model to associate messages with characters.</span>
</div>
<!-- Hidden input for loading radio buttons from presets. Don't remove! -->
<input type="hidden" id="names_behavior" class="displayNone" />
</div>
<div class="range-block">
<label for="wrap_in_quotes" title="Wrap user messages in quotes before sending" data-i18n="[title]Wrap user messages in quotes before sending" class="checkbox_label widthFreeExpand">
<input id="wrap_in_quotes" type="checkbox" /><span data-i18n="Wrap in Quotes">
@ -1635,14 +1668,6 @@
if you use quotes manually for speech.</span>
</div>
</div>
<div class="range-block">
<label for="names_in_completion" title="Add character names" data-i18n="[title]Add character names" class="checkbox_label widthFreeExpand">
<input id="names_in_completion" type="checkbox" /><span data-i18n="Add character names">Add character names</span>
</label>
<div class="toggle-description justifyLeft">
<span data-i18n="Send names in the message objects. Helps the model to associate messages with characters.">Send names in the message objects. Helps the model to associate messages with characters.</span>
</div>
</div>
<div class="range-block">
<label for="continue_prefill" class="checkbox_label widthFreeExpand">
<input id="continue_prefill" type="checkbox" />

View File

@ -171,6 +171,12 @@ export const chat_completion_sources = {
CUSTOM: 'custom',
};
const character_names_behavior = {
NONE: 0,
COMPLETION: 1,
CONTENT: 2,
};
const prefixMap = selected_group ? {
assistant: '',
user: '',
@ -197,7 +203,6 @@ const default_settings = {
openai_max_context: max_4k,
openai_max_tokens: 300,
wrap_in_quotes: false,
names_in_completion: false,
...chatCompletionDefaultPrompts,
...promptManagerDefaultPromptOrders,
send_if_empty: '',
@ -245,6 +250,7 @@ const default_settings = {
image_inlining: false,
bypass_status_check: false,
continue_prefill: false,
names_behavior: character_names_behavior.NONE,
seed: -1,
n: 1,
};
@ -264,7 +270,6 @@ const oai_settings = {
openai_max_context: max_4k,
openai_max_tokens: 300,
wrap_in_quotes: false,
names_in_completion: false,
...chatCompletionDefaultPrompts,
...promptManagerDefaultPromptOrders,
send_if_empty: '',
@ -312,6 +317,7 @@ const oai_settings = {
image_inlining: false,
bypass_status_check: false,
continue_prefill: false,
names_behavior: character_names_behavior.NONE,
seed: -1,
n: 1,
};
@ -466,11 +472,22 @@ function setOpenAIMessages(chat) {
}
// for groups or sendas command - prepend a character's name
if (!oai_settings.names_in_completion) {
if (selected_group || (chat[j].force_avatar && chat[j].name !== name1 && chat[j].extra?.type !== system_message_types.NARRATOR)) {
content = `${chat[j].name}: ${content}`;
}
switch (oai_settings.names_behavior) {
case character_names_behavior.NONE:
if (selected_group || (chat[j].force_avatar && chat[j].name !== name1 && chat[j].extra?.type !== system_message_types.NARRATOR)) {
content = `${chat[j].name}: ${content}`;
}
break;
case character_names_behavior.CONTENT:
if (chat[j].extra?.type !== system_message_types.NARRATOR) {
content = `${chat[j].name}: ${content}`;
}
break;
default:
// No action for character_names_behavior.COMPLETION
break;
}
// remove caret return (waste of tokens)
content = content.replace(/\r/gm, '');
@ -730,7 +747,7 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
prompt.identifier = `chatHistory-${messages.length - index}`;
const chatMessage = Message.fromPrompt(promptManager.preparePrompt(prompt));
if (true === promptManager.serviceSettings.names_in_completion && prompt.name) {
if (promptManager.serviceSettings.names_behavior === character_names_behavior.COMPLETION && prompt.name) {
const messageName = promptManager.isValidName(prompt.name) ? prompt.name : promptManager.sanitizeName(prompt.name);
chatMessage.setName(messageName);
}
@ -1612,12 +1629,6 @@ async function sendOpenAIRequest(type, messages, signal) {
delete generate_data.stop;
}
// Remove logit bias and stop strings if it's not supported by the model
if (isOAI && oai_settings.openai_model.includes('vision') || isOpenRouter && oai_settings.openrouter_model.includes('vision')) {
delete generate_data.logit_bias;
delete generate_data.stop;
}
// Proxy is only supported for Claude, OpenAI and Mistral
if (oai_settings.reverse_proxy && [chat_completion_sources.CLAUDE, chat_completion_sources.OPENAI, chat_completion_sources.MISTRALAI].includes(oai_settings.chat_completion_source)) {
validateReverseProxy();
@ -1630,6 +1641,13 @@ async function sendOpenAIRequest(type, messages, signal) {
generate_data['logprobs'] = 5;
}
// Remove logit bias, logprobs and stop strings if it's not supported by the model
if (isOAI && oai_settings.openai_model.includes('vision') || isOpenRouter && oai_settings.openrouter_model.includes('vision')) {
delete generate_data.logit_bias;
delete generate_data.stop;
delete generate_data.logprobs;
}
if (isClaude) {
generate_data['top_k'] = Number(oai_settings.top_k_openai);
generate_data['claude_use_sysprompt'] = oai_settings.claude_use_sysprompt;
@ -2554,9 +2572,14 @@ function loadOpenAISettings(data, settings) {
oai_settings.continue_nudge_prompt = settings.continue_nudge_prompt ?? default_settings.continue_nudge_prompt;
oai_settings.squash_system_messages = settings.squash_system_messages ?? default_settings.squash_system_messages;
oai_settings.continue_prefill = settings.continue_prefill ?? default_settings.continue_prefill;
oai_settings.names_behavior = settings.names_behavior ?? default_settings.names_behavior;
// Migrate from old settings
if (settings.names_in_completion === true) {
oai_settings.names_behavior = character_names_behavior.COMPLETION;
}
if (settings.wrap_in_quotes !== undefined) oai_settings.wrap_in_quotes = !!settings.wrap_in_quotes;
if (settings.names_in_completion !== undefined) oai_settings.names_in_completion = !!settings.names_in_completion;
if (settings.openai_model !== undefined) oai_settings.openai_model = settings.openai_model;
if (settings.use_ai21_tokenizer !== undefined) { oai_settings.use_ai21_tokenizer = !!settings.use_ai21_tokenizer; oai_settings.use_ai21_tokenizer ? ai21_max = 8191 : ai21_max = 9200; }
if (settings.use_google_tokenizer !== undefined) oai_settings.use_google_tokenizer = !!settings.use_google_tokenizer;
@ -2592,7 +2615,6 @@ function loadOpenAISettings(data, settings) {
$('#openai_max_tokens').val(oai_settings.openai_max_tokens);
$('#wrap_in_quotes').prop('checked', oai_settings.wrap_in_quotes);
$('#names_in_completion').prop('checked', oai_settings.names_in_completion);
$('#jailbreak_system').prop('checked', oai_settings.jailbreak_system);
$('#openai_show_external_models').prop('checked', oai_settings.show_external_models);
$('#openai_external_category').toggle(oai_settings.show_external_models);
@ -2666,10 +2688,26 @@ function loadOpenAISettings(data, settings) {
oai_settings.chat_completion_source = chat_completion_sources.MAKERSUITE;
}
setNamesBehaviorControls();
$('#chat_completion_source').val(oai_settings.chat_completion_source).trigger('change');
$('#oai_max_context_unlocked').prop('checked', oai_settings.max_context_unlocked);
}
function setNamesBehaviorControls() {
switch (oai_settings.names_behavior) {
case character_names_behavior.NONE:
$('#character_names_none').prop('checked', true);
break;
case character_names_behavior.COMPLETION:
$('#character_names_completion').prop('checked', true);
break;
case character_names_behavior.CONTENT:
$('#character_names_content').prop('checked', true);
break;
}
}
async function getStatusOpen() {
if (oai_settings.chat_completion_source == chat_completion_sources.WINDOWAI) {
let status;
@ -2794,7 +2832,7 @@ async function saveOpenAIPreset(name, settings, triggerUi = true) {
openai_max_context: settings.openai_max_context,
openai_max_tokens: settings.openai_max_tokens,
wrap_in_quotes: settings.wrap_in_quotes,
names_in_completion: settings.names_in_completion,
names_behavior: settings.names_behavior,
send_if_empty: settings.send_if_empty,
jailbreak_prompt: settings.jailbreak_prompt,
jailbreak_system: settings.jailbreak_system,
@ -3172,7 +3210,7 @@ function onSettingsPresetChange() {
openai_max_context: ['#openai_max_context', 'openai_max_context', false],
openai_max_tokens: ['#openai_max_tokens', 'openai_max_tokens', false],
wrap_in_quotes: ['#wrap_in_quotes', 'wrap_in_quotes', true],
names_in_completion: ['#names_in_completion', 'names_in_completion', true],
names_behavior: ['#names_behavior', 'names_behavior', false],
send_if_empty: ['#send_if_empty_textarea', 'send_if_empty', false],
impersonation_prompt: ['#impersonation_prompt_textarea', 'impersonation_prompt', false],
new_chat_prompt: ['#newchat_prompt_textarea', 'new_chat_prompt', false],
@ -3209,6 +3247,11 @@ function onSettingsPresetChange() {
const preset = structuredClone(openai_settings[openai_setting_names[oai_settings.preset_settings_openai]]);
// Migrate old settings
if (preset.names_in_completion === true && preset.names_behavior === undefined) {
preset.names_behavior = character_names_behavior.COMPLETION;
}
const updateInput = (selector, value) => $(selector).val(value).trigger('input');
const updateCheckbox = (selector, value) => $(selector).prop('checked', value).trigger('input');
@ -4077,11 +4120,6 @@ $(document).ready(async function () {
saveSettingsDebounced();
});
$('#names_in_completion').on('change', function () {
oai_settings.names_in_completion = !!$('#names_in_completion').prop('checked');
saveSettingsDebounced();
});
$('#send_if_empty_textarea').on('input', function () {
oai_settings.send_if_empty = String($('#send_if_empty_textarea').val());
saveSettingsDebounced();
@ -4299,6 +4337,27 @@ $(document).ready(async function () {
saveSettingsDebounced();
});
$('#names_behavior').on('input', function () {
oai_settings.names_behavior = Number($(this).val());
setNamesBehaviorControls();
saveSettingsDebounced();
});
$('#character_names_none').on('input', function () {
oai_settings.names_behavior = character_names_behavior.NONE;
saveSettingsDebounced();
});
$('#character_names_completion').on('input', function () {
oai_settings.names_behavior = character_names_behavior.COMPLETION;
saveSettingsDebounced();
});
$('#character_names_content').on('input', function () {
oai_settings.names_behavior = character_names_behavior.CONTENT;
saveSettingsDebounced();
});
$(document).on('input', '#openai_settings .autoSetHeight', function () {
resetScrollHeight($(this));
});