Add author's note to floating prompt extension

This commit is contained in:
SillyLossy
2023-03-27 01:43:46 +03:00
parent 96faff5b2e
commit 9b3613c63c
5 changed files with 110 additions and 22 deletions

View File

@ -101,6 +101,7 @@ export {
openCharacterChat,
saveChat,
messageFormating,
getExtensionPrompt,
chat,
this_chid,
settings,
@ -120,6 +121,7 @@ export {
system_message_types,
talkativeness_default,
default_ch_mes,
extension_prompt_types,
}
// API OBJECT FOR EXTERNAL WIRING
@ -183,6 +185,11 @@ const system_message_types = {
BOOKMARK_BACK: "bookmark_back",
};
const extension_prompt_types = {
AFTER_SCENARIO: 0,
IN_CHAT: 1
};
const system_messages = {
help: {
name: systemUserName,
@ -195,6 +202,7 @@ const system_messages = {
'<ol>',
'<li><tt>*text*</tt> format the actions that your character does</li>',
'<li><tt>{*text*}</tt> set the behavioral bias for your character</li>',
'<li><tt>{}</tt> cancel a previously set bias</li>',
'</ol>',
'Need more help? Visit our wiki <a href=\"https://github.com/TavernAI/TavernAI/wiki\">TavernAI Wiki</a>!'
].join('')
@ -767,7 +775,7 @@ function messageFormating(mes, ch_name, isSystem, forceAvatar) {
.replace(/\n/g, "<br/>");
} else if (!isSystem) {
mes = converter.makeHtml(mes);
mes = mes.replace(/{([^}]+)}/g, "");
mes = mes.replace(/{.*}/g, "");
mes = mes.replace(/\n/g, "<br/>");
mes = mes.trim();
}
@ -945,7 +953,11 @@ function extractMessageBias(message) {
}
if (!found.length) {
return "";
// cancels a bias
if (message.includes('{') && message.includes('}')) {
return '';
}
return null;
}
return ` ${found.join(" ")} `;
@ -970,11 +982,12 @@ function cleanGroupMessage(getMessage) {
return getMessage;
}
function getExtensionPrompt() {
function getExtensionPrompt(position = 0, depth) {
let extension_prompt = Object.keys(extension_prompts)
.sort()
.map((x) => extension_prompts[x])
.filter(x => x)
.filter(x => x.position == position && x.value && (depth === undefined || x.depth == depth))
.map(x => x.value)
.join("\n");
if (extension_prompt.length && !extension_prompt.startsWith("\n")) {
extension_prompt = "\n" + extension_prompt;
@ -1058,10 +1071,13 @@ async function Generate(type, automatic_trigger, force_name2) {//encode("dsfs").
let messageBias = extractMessageBias(textareaText);
// gets bias of the latest message where it was applied
for (let mes of chat) {
for (let mes of chat.slice().reverse()) {
if (mes && mes.is_user && mes.extra && mes.extra.bias) {
if (mes.extra.bias.trim().length > 0) {
promptBias = mes.extra.bias;
}
break;
}
}
// bias from the latest message is top priority//
@ -1220,7 +1236,7 @@ async function Generate(type, automatic_trigger, force_name2) {//encode("dsfs").
}
// replace bias markup
chat2[i] = (chat2[i] ?? '').replace(/{([^}]+)}/g, '');
chat2[i] = (chat2[i] ?? '').replace(/{.*}/g, '');
//console.log('replacing chat2 {}s');
j++;
}
@ -1246,7 +1262,7 @@ async function Generate(type, automatic_trigger, force_name2) {//encode("dsfs").
}
let { worldInfoString, worldInfoBefore, worldInfoAfter } = getWorldInfoPrompt(chat2);
let extension_prompt = getExtensionPrompt();
let extension_prompt = getExtensionPrompt(extension_prompt_types.AFTER_SCENARIO);
/////////////////////// swipecode
if (type == 'swipe') {
@ -1356,6 +1372,24 @@ async function Generate(type, automatic_trigger, force_name2) {//encode("dsfs").
item = item.replace(name1 + ':', 'You:');
}
}
if (i === 0) {
// Process those that couldn't get that far
for (let upperDepth = 100; upperDepth >= arrMes.length; upperDepth--) {
const upperAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, upperDepth);
if (upperAnchor && upperAnchor.length) {
item = upperAnchor + item;
}
}
}
const anchorDepth = Math.abs(i - arrMes.length + 1);
const extensionAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, anchorDepth);
if (extensionAnchor && extensionAnchor.length) {
item += extensionAnchor;
}
mesSend[mesSend.length] = item;
});
@ -2619,8 +2653,8 @@ function select_rm_characters() {
setRightTabSelectedClass('rm_button_characters');
}
function setExtensionPrompt(key, value) {
extension_prompts[key] = value;
function setExtensionPrompt(key, value, position, depth) {
extension_prompts[key] = { value, position, depth };
}
function callPopup(text, type) {

View File

@ -25,17 +25,25 @@
padding-left: 0;
margin-top: 0;
margin-bottom: 3px;
overflow: hidden;
background-color: black;
border: 1px solid #666;
border-radius: 15px;
box-shadow: 0 0 5px black;
text-shadow: 0 0 3px black;
}
.list-group-item:hover {
background-color: rgba(255, 255, 255, 0.3);
}
.list-group-item {
color: rgba(229, 224, 216, 1);
position: relative;
display: block;
padding: 0.75rem 1.25rem;
margin-bottom: -1px;
background-color: rgba(0,0,0,0.5);
border: 1px solid rgba(0,0,0,0.7);
box-sizing: border-box;
user-select: none;
cursor: pointer;
backdrop-filter: blur(10px);
}

View File

@ -6,6 +6,8 @@ const UPDATE_INTERVAL = 1000;
let lastMessageNumber = null;
let promptInsertionInterval = 0;
let promptInsertionPosition = 0;
let promptInsertionDepth = 0;
function onExtensionFloatingPromptInput() {
saveSettings();
@ -16,24 +18,45 @@ function onExtensionFloatingIntervalInput() {
saveSettings();
}
function onExtensionFloatingDepthInput() {
promptInsertionDepth = Number($(this).val());
saveSettings();
}
function onExtensionFloatingPositionInput(e) {
promptInsertionPosition = e.target.value;
saveSettings();
}
function getLocalStorageKeys() {
const context = getContext();
const keySuffix = context.groupId ? context.groupId : `${context.characters[context.characterId].name}_${context.chatId}`;
return { prompt: `extensions_floating_prompt_${keySuffix}`, interval: `extensions_floating_interval_${keySuffix}` };
return {
prompt: `extensions_floating_prompt_${keySuffix}`,
interval: `extensions_floating_interval_${keySuffix}`,
depth: `extensions_floating_depth_${keySuffix}`,
position: `extensions_floating_position_${keySuffix}`,
};
}
function loadSettings() {
const keys = getLocalStorageKeys();
const prompt = localStorage.getItem(keys.prompt) ?? '';
const interval = localStorage.getItem(keys.interval) ?? 0;
const position = localStorage.getItem(keys.position) ?? 0;
const depth = localStorage.getItem(keys.depth) ?? 0;
$('#extension_floating_prompt').val(prompt).trigger('input');
$('#extension_floating_interval').val(interval).trigger('input');
$('#extension_floating_depth').val(depth).trigger('input');
$(`input[name="extension_floating_position"][value="${position}"]`).prop('checked', true).trigger('change');
}
function saveSettings() {
const keys = getLocalStorageKeys();
localStorage.setItem(keys.prompt, $('#extension_floating_prompt').val());
localStorage.setItem(keys.interval, $('#extension_floating_interval').val());
localStorage.setItem(keys.depth, $('#extension_floating_depth').val());
localStorage.setItem(keys.position, $('input:radio[name="extension_floating_position"]:checked').val());
}
async function moduleWorker() {
@ -61,21 +84,31 @@ async function moduleWorker() {
const messagesTillInsertion = lastMessageNumber >= promptInsertionInterval
? (lastMessageNumber % promptInsertionInterval)
: (promptInsertionInterval - lastMessageNumber);
const shouldAddPrompt = messagesTillInsertion == 0;
const shouldAddPrompt = messagesTillInsertion == 0 && (promptInsertionPosition == 0 || promptInsertionDepth > 0);
const prompt = shouldAddPrompt ? $('#extension_floating_prompt').val() : '';
context.setExtensionPrompt(MODULE_NAME, prompt);
context.setExtensionPrompt(MODULE_NAME, prompt, promptInsertionPosition, promptInsertionDepth);
$('#extension_floating_counter').text(shouldAddPrompt ? 'This' : messagesTillInsertion);
}
(function() {
function addExtensionsSettings() {
const settingsHtml = `
<h4>Floating Prompt</h4>
<h4>Floating Prompt / Author's Note</h4>
<div class="floating_prompt_settings">
<label for="extension_floating_prompt">Append the following text to the scenario:</label>
<label for="extension_floating_prompt">Append the following text:</label>
<textarea id="extension_floating_prompt" class="text_pole" rows="2"></textarea>
<label>
<input type="radio" name="extension_floating_position" value="0" />
After scenario
</label>
<label>
<input type="radio" name="extension_floating_position" value="1" />
In-chat
</label>
<label for="extension_floating_interval">Every N messages <b>you</b> send (set to 0 to disable):</label>
<input id="extension_floating_interval" class="text_pole" type="number" value="0" min="0" max="999" />
<label for="extension_floating_interval">Insertion depth (for in-chat positioning, set to 0 to disable):</label>
<input id="extension_floating_depth" class="text_pole" type="number" value="0" min="0" max="99" />
<span>Appending the prompt in next: <span id="extension_floating_counter">No</span> message(s)</span>
</div>
`;
@ -83,6 +116,8 @@ async function moduleWorker() {
$('#extensions_settings').append(settingsHtml);
$('#extension_floating_prompt').on('input', onExtensionFloatingPromptInput);
$('#extension_floating_interval').on('input', onExtensionFloatingIntervalInput);
$('#extension_floating_depth').on('input', onExtensionFloatingDepthInput);
$('input[name="extension_floating_position"]').on('change', onExtensionFloatingPositionInput);
}
addExtensionsSettings();

View File

@ -1,5 +1,5 @@
{
"display_name": "Floating Prompt",
"display_name": "Floating Prompt / Author's Note",
"loading_order": 1,
"requires": [],
"js": "index.js",

View File

@ -13,9 +13,11 @@ import {
saveChat,
checkOnlineStatus,
setOnlineStatus,
getExtensionPrompt,
token,
name1,
name2,
extension_prompt_types,
} from "../script.js";
import { groups, selected_group } from "./group-chats.js";
@ -94,13 +96,21 @@ function setOpenAIMessages(chat) {
}
// replace bias markup
content = (content ?? '').replace(/{([^}]+)}/g, '');
content = (content ?? '').replace(/{.*}/g, '');
// Apply the "wrap in quotes" option
if (role == 'user' && oai_settings.wrap_in_quotes) content = `"${content}"`;
openai_msgs[i] = { "role": role, "content": content };
j++;
}
for (let i = 1; i < 100; i++) {
const anchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, i);
if (anchor && anchor.length) {
openai_msgs.splice(i, 0, { "role": 'system', 'content': anchor.trim() })
}
}
}
function setOpenAIMessageExamples(mesExamplesArray) {
@ -130,6 +140,7 @@ function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, a
if (i >= openai_msgs.length - 1 && count_view_mes > 8 && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//For add anchor in end
item = anchorBottom + "\n" + item;
}
msg["content"] = item;
openai_msgs[i] = msg;
});
@ -232,8 +243,8 @@ function prepareOpenAIMessages(name2, storyString, worldInfoBefore, worldInfoAft
let start_chat_count = countTokens([new_chat_msg]);
let total_count = countTokens([prompt_msg], true) + start_chat_count;
if (bias) {
let bias_msg = { "role": "system", "content": bias };
if (bias && bias.trim().length) {
let bias_msg = { "role": "system", "content": bias.trim() };
openai_msgs.push(bias_msg);
total_count += countTokens([bias_msg], true);
}