Merge branch 'staging' of https://github.com/Cohee1207/SillyTavern into staging

This commit is contained in:
RossAscends 2023-10-05 20:10:19 +09:00
commit 2befd69c31
3 changed files with 51 additions and 7 deletions

View File

@ -20,6 +20,7 @@ import {
isMancer, isMancer,
isAphrodite, isAphrodite,
textgen_types, textgen_types,
textgenerationwebui_banned_in_macros,
} from "./scripts/textgen-settings.js"; } from "./scripts/textgen-settings.js";
import { import {
@ -1757,9 +1758,37 @@ function substituteParams(content, _name1, _name2, _original, _group) {
}); });
content = randomReplace(content); content = randomReplace(content);
content = diceRollReplace(content); content = diceRollReplace(content);
content = bannedWordsReplace(content);
return content; return content;
} }
/**
* Replaces banned words in macros with an empty string.
* Adds them to textgenerationwebui ban list.
* @param {string} inText Text to replace banned words in
* @returns {string} Text without the "banned" macro
*/
function bannedWordsReplace(inText) {
if (!inText) {
return '';
}
const banPattern = /{{banned "(.*)"}}/gi;
if (main_api == 'textgenerationwebui') {
const bans = inText.matchAll(banPattern);
if (bans) {
for (const banCase of bans) {
console.log("Found banned words in macros: " + banCase[1]);
textgenerationwebui_banned_in_macros.push(banCase[1]);
}
}
}
inText = inText.replaceAll(banPattern, "");
return inText;
}
function getTimeSinceLastMessage() { function getTimeSinceLastMessage() {
const now = moment(); const now = moment();

View File

@ -6,6 +6,7 @@ System-wide Replacement Macros:
<li><tt>&lcub;&lcub;time&rcub;&rcub;</tt> - the current time</li> <li><tt>&lcub;&lcub;time&rcub;&rcub;</tt> - the current time</li>
<li><tt>&lcub;&lcub;date&rcub;&rcub;</tt> - the current date</li> <li><tt>&lcub;&lcub;date&rcub;&rcub;</tt> - the current date</li>
<li><tt>&lcub;&lcub;bias "text here"&rcub;&rcub;</tt> - sets a behavioral bias for the AI until the next user input. Quotes around the text are important.</li> <li><tt>&lcub;&lcub;bias "text here"&rcub;&rcub;</tt> - sets a behavioral bias for the AI until the next user input. Quotes around the text are important.</li>
<li><tt>&lcub;&lcub;banned "text here"&rcub;&rcub;</tt> - dynamically add text in the quotes to banned words sequences, if Text Generation WebUI backend used. Do nothing for others backends. Can be used anywhere (Character description, WI, AN, etc.) Quotes around the text are important.</li>
<li><tt>&lcub;&lcub;idle_duration&rcub;&rcub;</tt> - the time since the last user message was sent</li> <li><tt>&lcub;&lcub;idle_duration&rcub;&rcub;</tt> - the time since the last user message was sent</li>
<li><tt>&lcub;&lcub;random:(args)&rcub;&rcub;</tt> - returns a random item from the list. (ex: &lcub;&lcub;random:1,2,3,4&rcub;&rcub; will return 1 of the 4 numbers at random. Works with text lists too.</li> <li><tt>&lcub;&lcub;random:(args)&rcub;&rcub;</tt> - returns a random item from the list. (ex: &lcub;&lcub;random:1,2,3,4&rcub;&rcub; will return 1 of the 4 numbers at random. Works with text lists too.</li>
<li><tt>&lcub;&lcub;roll:(formula)&rcub;&rcub;</tt> - rolls a dice. (ex: &lcub;&lcub;roll:1d6&rcub;&rcub; will roll a 6-sided dice and return a number between 1 and 6)</li> <li><tt>&lcub;&lcub;roll:(formula)&rcub;&rcub;</tt> - rolls a dice. (ex: &lcub;&lcub;roll:1d6&rcub;&rcub; will roll a 6-sided dice and return a number between 1 and 6)</li>

View File

@ -65,6 +65,8 @@ const textgenerationwebui_settings = {
type: textgen_types.OOBA, type: textgen_types.OOBA,
}; };
export let textgenerationwebui_banned_in_macros = [];
export let textgenerationwebui_presets = []; export let textgenerationwebui_presets = [];
export let textgenerationwebui_preset_names = []; export let textgenerationwebui_preset_names = [];
@ -136,19 +138,31 @@ function formatTextGenURL(value, use_mancer) {
} }
function convertPresets(presets) { function convertPresets(presets) {
return Array.isArray(presets) ? presets.map(JSON.parse) : []; return Array.isArray(presets) ? presets.map((p) => JSON.parse(p)) : [];
} }
/** /**
* @returns {string} String with comma-separated banned token IDs * @returns {string} String with comma-separated banned token IDs
*/ */
function getCustomTokenBans() { function getCustomTokenBans() {
if (!textgenerationwebui_settings.banned_tokens) { if (!textgenerationwebui_settings.banned_tokens && !textgenerationwebui_banned_in_macros.length) {
return ''; return '';
} }
const sequences = textgenerationwebui_settings.banned_tokens.split('\n');
const result = []; const result = [];
const sequences = textgenerationwebui_settings.banned_tokens
.split('\n')
.concat(textgenerationwebui_banned_in_macros)
.filter(x => x.length > 0)
.filter(onlyUnique);
//debug
if (textgenerationwebui_banned_in_macros.length) {
console.log("=== Found banned word sequences in the macros:", textgenerationwebui_banned_in_macros, "Resulting array of banned sequences (will be used this generation turn):", sequences);
}
//clean old temporary bans found in macros before, for the next generation turn.
textgenerationwebui_banned_in_macros = [];
for (const line of sequences) { for (const line of sequences) {
// Raw token ids, JSON serialized // Raw token ids, JSON serialized