diff --git a/public/script.js b/public/script.js index 0dbf0b39a..354901156 100644 --- a/public/script.js +++ b/public/script.js @@ -186,6 +186,7 @@ import { getInstructStoppingSequences, autoSelectInstructPreset, formatInstructModeSystemPrompt, + replaceInstructMacros, } from "./scripts/instruct-mode.js"; import { applyLocale } from "./scripts/i18n.js"; import { getFriendlyTokenizerName, getTokenCount, getTokenizerModel, initTokenizers, saveTokenCache } from "./scripts/tokenizers.js"; @@ -2013,6 +2014,7 @@ function substituteParams(content, _name1, _name2, _original, _group, _replaceCh } content = diceRollReplace(content); content = randomReplace(content); + content = replaceInstructMacros(content); content = replaceVariableMacros(content); content = content.replace(/{{newline}}/gi, "\n"); content = content.replace(/{{input}}/gi, String($('#send_textarea').val())); diff --git a/public/scripts/instruct-mode.js b/public/scripts/instruct-mode.js index 60c18f11f..351f3a3b3 100644 --- a/public/scripts/instruct-mode.js +++ b/public/scripts/instruct-mode.js @@ -358,6 +358,31 @@ function selectMatchingContextTemplate(name) { } } +/** + * Replaces instruct mode macros in the given input string. + * @param {string} input Input string. + * @returns {string} String with macros replaced. + */ +export function replaceInstructMacros(input) { + if (!input) { + return ''; + } + + input = input.replace(/{{instructSystem}}/gi, power_user.instruct.enabled ? power_user.instruct.system_prompt : ''); + input = input.replace(/{{instructSystemPrefix}}/gi, power_user.instruct.enabled ? power_user.instruct.system_sequence_prefix : ''); + input = input.replace(/{{instructSystemSuffix}}/gi, power_user.instruct.enabled ? power_user.instruct.system_sequence_suffix : ''); + input = input.replace(/{{instructInput}}/gi, power_user.instruct.enabled ? power_user.instruct.input_sequence : ''); + input = input.replace(/{{instructOutput}}/gi, power_user.instruct.enabled ? power_user.instruct.output_sequence : ''); + input = input.replace(/{{instructFirstOutput}}/gi, power_user.instruct.enabled ? (power_user.instruct.first_output_sequence || power_user.instruct.output_sequence) : ''); + input = input.replace(/{{instructLastOutput}}/gi, power_user.instruct.enabled ? (power_user.instruct.last_output_sequence || power_user.instruct.output_sequence) : ''); + input = input.replace(/{{instructSeparator}}/gi, power_user.instruct.enabled ? power_user.instruct.separator_sequence : ''); + input = input.replace(/{{instructStop}}/gi, power_user.instruct.enabled ? power_user.instruct.stop_sequence : ''); + input = input.replace(/{{exampleSeparator}}/gi, power_user.context.example_separator); + input = input.replace(/{{chatStart}}/gi, power_user.context.chat_start); + + return input; +} + jQuery(() => { $('#instruct_set_default').on('click', function () { if (power_user.instruct.preset === power_user.default_instruct) { diff --git a/public/scripts/templates/macros.html b/public/scripts/templates/macros.html index 1b940e151..5b49fa9f0 100644 --- a/public/scripts/templates/macros.html +++ b/public/scripts/templates/macros.html @@ -32,6 +32,25 @@