Merge pull request #1995 from Wolfsblvt/pick-macro

Add {{pick}} macro replacement
This commit is contained in:
Cohee 2024-04-02 01:17:03 +03:00 committed by GitHub
commit 3b333d3803
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 4 deletions

View File

@ -1,5 +1,5 @@
import { chat, main_api, getMaxContextSize } from '../script.js';
import { timestampToMoment, isDigitsOnly } from './utils.js';
import { chat, main_api, getMaxContextSize, getCurrentChatId } from '../script.js';
import { timestampToMoment, isDigitsOnly, getStringHash } from './utils.js';
import { textgenerationwebui_banned_in_macros } from './textgen-settings.js';
import { replaceInstructMacros } from './instruct-mode.js';
import { replaceVariableMacros } from './variables.js';
@ -190,7 +190,10 @@ function randomReplace(input, emptyListPlaceholder = '') {
input = input.replace(randomPatternNew, (match, listString) => {
//split on double colons instead of commas to allow for commas inside random items
const list = listString.split('::').filter(item => item.length > 0);
const list = listString.includes('::')
? listString.split('::').filter(item => item.length > 0)
: listString.split(',').map(item => item.trim()).filter(item => item.length > 0);
if (list.length === 0) {
return emptyListPlaceholder;
}
@ -211,6 +214,29 @@ function randomReplace(input, emptyListPlaceholder = '') {
return input;
}
function pickReplace(input, rawContent, emptyListPlaceholder = '') {
const pickPattern = /{{pick\s?::\s?([^}]+)}}/gi;
const chatIdHash = getStringHash(getCurrentChatId());
const rawContentHash = getStringHash(rawContent);
return input.replace(pickPattern, (match, listString, offset) => {
const list = listString.includes('::')
? listString.split('::').filter(item => item.length > 0)
: listString.split(',').map(item => item.trim()).filter(item => item.length > 0);
if (list.length === 0) {
return emptyListPlaceholder;
}
const combinedSeedString = `${chatIdHash}-${rawContentHash}-${offset}`;
const finalSeed = getStringHash(combinedSeedString);
const rng = new Math.seedrandom(finalSeed);
const randomIndex = Math.floor(rng() * list.length);
return list[randomIndex].trim();
});
}
function diceRollReplace(input, invalidRollPlaceholder = '') {
const rollPattern = /{{roll[ : ]([^}]+)}}/gi;
@ -245,6 +271,8 @@ export function evaluateMacros(content, env) {
return '';
}
const rawContent = content;
// Legacy non-macro substitutions
content = content.replace(/<USER>/gi, typeof env.user === 'function' ? env.user() : env.user);
content = content.replace(/<BOT>/gi, typeof env.char === 'function' ? env.char() : env.char);
@ -300,5 +328,6 @@ export function evaluateMacros(content, env) {
});
content = bannedWordsReplace(content);
content = randomReplace(content);
content = pickReplace(content, rawContent);
return content;
}

View File

@ -34,8 +34,9 @@
<li><tt>&lcub;&lcub;idle_duration&rcub;&rcub;</tt> the time since the last user message was sent</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;roll:(formula)&rcub;&rcub;</tt> rolls a dice. (ex: <tt>>&lcub;&lcub;roll:1d6&rcub;&rcub;</tt> will roll a 6-sided dice and return a number between 1 and 6)</li>
<li><tt>&lcub;&lcub;random:(args)&rcub;&rcub;</tt> returns a random item from the list. (ex: <tt>&lcub;&lcub;random:1,2,3,4&rcub;&rcub;</tt> 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: <tt>&lcub;&lcub;random::1::2::3::4&rcub;&rcub;</tt> will return 1 of the 4 numbers at random. Works with text lists too.</li>
<li><tt>&lcub;&lcub;random::(arg1)::(arg2)&rcub;&rcub;</tt> alternative syntax for random that allows to use commas in the list items.</li>
<li><tt>&lcub;&lcub;pick::(args)&rcub;&rcub;</tt> picks a random item from the list. Works the same as &lcub;&lcub;random&rcub;&rcub;, with the same possible syntax options, but the pick will stay consistent for this chat once picked and won't be re-rolled on consecutive messages and prompt processing.</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>
</ul>
<div>