Feature, ability to add banned words sequences from anywhere through {{banned "..."}} macro. (#1202)

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Fix constant assignment, reformat code

---------

Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
This commit is contained in:
valden80 2023-10-05 13:10:41 +03:00 committed by GitHub
parent 99a89a7329
commit 788bbe969f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 7 deletions

View File

@ -20,6 +20,7 @@ import {
isMancer,
isAphrodite,
textgen_types,
textgenerationwebui_banned_in_macros,
} from "./scripts/textgen-settings.js";
import {
@ -1757,9 +1758,37 @@ function substituteParams(content, _name1, _name2, _original, _group) {
});
content = randomReplace(content);
content = diceRollReplace(content);
content = bannedWordsReplace(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() {
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;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;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;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>

View File

@ -65,6 +65,8 @@ const textgenerationwebui_settings = {
type: textgen_types.OOBA,
};
export let textgenerationwebui_banned_in_macros = [];
export let textgenerationwebui_presets = [];
export let textgenerationwebui_preset_names = [];
@ -136,19 +138,31 @@ function formatTextGenURL(value, use_mancer) {
}
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
*/
function getCustomTokenBans() {
if (!textgenerationwebui_settings.banned_tokens) {
if (!textgenerationwebui_settings.banned_tokens && !textgenerationwebui_banned_in_macros.length) {
return '';
}
const sequences = textgenerationwebui_settings.banned_tokens.split('\n');
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) {
// Raw token ids, JSON serialized
@ -309,7 +323,7 @@ async function generateTextGenWithStreaming(generate_data, signal) {
streamingUrl = api_server_textgenerationwebui.replace("http", "ws") + "/v1/stream";
}
if (isAphrodite()){
if (isAphrodite()) {
streamingUrl = api_server_textgenerationwebui;
}
@ -342,7 +356,7 @@ async function generateTextGenWithStreaming(generate_data, signal) {
try {
const { results } = JSON.parse(event);
if (Array.isArray(results) && results.length > 0) {
getMessage = results[0].text;
yield getMessage;
@ -361,11 +375,11 @@ async function generateTextGenWithStreaming(generate_data, signal) {
} else {
getMessage += response;
if (done) {
return;
}
yield getMessage;
}
}