From 95e0be7e9e85bc2471b92aeeecedbb217b76c80b Mon Sep 17 00:00:00 2001 From: kingbri <8082010+kingbri1@users.noreply.github.com> Date: Fri, 7 Mar 2025 13:02:20 -0500 Subject: [PATCH 1/3] MesExamples: Make macros consistent with story string mesExamples in the story string is formatted while mesExamplesRaw is unformatted. However mesExamples when used as a normal macro is unformatted. This causes an inconsistency in usage which is now corrected. Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com> --- public/script.js | 55 ++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/public/script.js b/public/script.js index 559b34729..956d445a3 100644 --- a/public/script.js +++ b/public/script.js @@ -2716,7 +2716,13 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re environment.personality = fields.personality || ''; environment.scenario = fields.scenario || ''; environment.persona = fields.persona || ''; - environment.mesExamples = fields.mesExamples || ''; + environment.mesExamples = () => { + const isInstruct = power_user.instruct.enabled && main_api !== 'openai'; + const mesExamplesArray = parseMesExamples(fields.mesExamples, isInstruct); + const instructExamples = formatInstructModeExamples(mesExamplesArray, name1, name2); + return instructExamples.join(''); + }; + environment.mesExamplesRaw = fields.mesExamples || ''; environment.charVersion = fields.version || ''; environment.char_version = fields.version || ''; } @@ -3105,6 +3111,27 @@ export function getCharacterCardFields() { return result; } +/** + * Parses an examples string. + * @param {string} examplesStr + * @returns {string[]} Examples array with block heading + */ +export function parseMesExamples(examplesStr, isInstruct) { + if (!examplesStr || examplesStr.length === 0 || examplesStr === '') { + return []; + } + + if (!examplesStr.startsWith('')) { + examplesStr = '\n' + examplesStr.trim(); + } + + const exampleSeparator = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : ''; + const blockHeading = main_api === 'openai' ? '\n' : (exampleSeparator || (isInstruct ? '\n' : '')); + const splitExamples = examplesStr.split(//gi).slice(1).map(block => `${blockHeading}${block.trim()}\n`); + + return splitExamples; +} + export function isStreamingEnabled() { const noStreamSources = [chat_completion_sources.SCALE]; return ( @@ -3993,29 +4020,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro force_name2 = false; } - // TODO (kingbri): Migrate to a utility function - /** - * Parses an examples string. - * @param {string} examplesStr - * @returns {string[]} Examples array with block heading - */ - function parseMesExamples(examplesStr) { - if (!examplesStr || examplesStr.length === 0 || examplesStr === '') { - return []; - } - - if (!examplesStr.startsWith('')) { - examplesStr = '\n' + examplesStr.trim(); - } - - const exampleSeparator = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : ''; - const blockHeading = main_api === 'openai' ? '\n' : (exampleSeparator || (isInstruct ? '\n' : '')); - const splitExamples = examplesStr.split(//gi).slice(1).map(block => `${blockHeading}${block.trim()}\n`); - - return splitExamples; - } - - let mesExamplesArray = parseMesExamples(mesExamples); + let mesExamplesArray = parseMesExamples(mesExamples, isInstruct); ////////////////////////////////// // Extension added strings @@ -4040,7 +4045,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro } const formattedExample = baseChatReplace(exampleMessage, name1, name2); - const cleanedExample = parseMesExamples(formattedExample); + const cleanedExample = parseMesExamples(formattedExample, isInstruct); // Insert depending on before or after position if (example.position === wi_anchor_position.before) { From e476063f32ada9daf6352d537f937336e608785c Mon Sep 17 00:00:00 2001 From: kingbri <8082010+kingbri1@users.noreply.github.com> Date: Fri, 7 Mar 2025 13:08:30 -0500 Subject: [PATCH 2/3] Macros: Update mesExamplesRaw documentation Works as a normal macro and not just with story string. Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com> --- public/scripts/templates/macros.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/templates/macros.html b/public/scripts/templates/macros.html index 50ca3e5fe..b7db29dec 100644 --- a/public/scripts/templates/macros.html +++ b/public/scripts/templates/macros.html @@ -16,7 +16,7 @@
  • {{scenario}}the Character's Scenario
  • {{persona}}your current Persona Description
  • {{mesExamples}}the Character's Dialogue Examples
  • -
  • {{mesExamplesRaw}}unformatted Dialogue Examples (only for Story String)
  • +
  • {{mesExamplesRaw}}unformatted Dialogue Examples
  • {{summary}}the latest chat summary generated by the "Summarize" extension (if available).
  • {{user}}your current Persona username
  • {{char}}the Character's name
  • From 0423cb7ad36dc30a81fb60e9ab8e47e03e9cadea Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 7 Mar 2025 22:53:30 +0200 Subject: [PATCH 3/3] Do not apply instruct formatting if on CC --- public/script.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/public/script.js b/public/script.js index 956d445a3..167c8d89d 100644 --- a/public/script.js +++ b/public/script.js @@ -2719,8 +2719,11 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re environment.mesExamples = () => { const isInstruct = power_user.instruct.enabled && main_api !== 'openai'; const mesExamplesArray = parseMesExamples(fields.mesExamples, isInstruct); - const instructExamples = formatInstructModeExamples(mesExamplesArray, name1, name2); - return instructExamples.join(''); + if (isInstruct) { + const instructExamples = formatInstructModeExamples(mesExamplesArray, name1, name2); + return instructExamples.join(''); + } + return mesExamplesArray.join(''); }; environment.mesExamplesRaw = fields.mesExamples || ''; environment.charVersion = fields.version || ''; @@ -3303,7 +3306,7 @@ class StreamingProcessor { 'send_date': chat[messageId]['send_date'], 'gen_started': chat[messageId]['gen_started'], 'gen_finished': chat[messageId]['gen_finished'], - 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])) + 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])), }; }