Merge pull request #3629 from kingbri1/staging Make mesExamples macros consistent

b813bcad8aae925e408487e4d0cfca206ee6d98c

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +35 -27Showing whitespace changes
public/script.js+34 -26
@@ -2716,7 +2716,16 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re
27162716 environment.personality = fields.personality || '';
27172717 environment.scenario = fields.scenario || '';
27182718 environment.persona = fields.persona || '';
27192719 environment.mesExamples = fields.mesExamples() ||=> '';{
2720+ const isInstruct = power_user.instruct.enabled && main_api !== 'openai';
2721+ const mesExamplesArray = parseMesExamples(fields.mesExamples, isInstruct);
2722+ if (isInstruct) {
2723+ const instructExamples = formatInstructModeExamples(mesExamplesArray, name1, name2);
2724+ return instructExamples.join('');
2725+ }
2726+ return mesExamplesArray.join('');
2727+ };
2728+ environment.mesExamplesRaw = fields.mesExamples || '';
27202729 environment.charVersion = fields.version || '';
27212730 environment.char_version = fields.version || '';
27222731 }
@@ -3105,6 +3114,27 @@ export function getCharacterCardFields() {
31053114 return result;
31063115}
31073116
3117+/**
3118+ * Parses an examples string.
3119+ * @param {string} examplesStr
3120+ * @returns {string[]} Examples array with block heading
3121+ */
3122+export function parseMesExamples(examplesStr, isInstruct) {
3123+ if (!examplesStr || examplesStr.length === 0 || examplesStr === '<START>') {
3124+ return [];
3125+ }
3126+
3127+ if (!examplesStr.startsWith('<START>')) {
3128+ examplesStr = '<START>\n' + examplesStr.trim();
3129+ }
3130+
3131+ const exampleSeparator = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : '';
3132+ const blockHeading = main_api === 'openai' ? '<START>\n' : (exampleSeparator || (isInstruct ? '<START>\n' : ''));
3133+ const splitExamples = examplesStr.split(/<START>/gi).slice(1).map(block => `${blockHeading}${block.trim()}\n`);
3134+
3135+ return splitExamples;
3136+}
3137+
31083138export function isStreamingEnabled() {
31093139 const noStreamSources = [chat_completion_sources.SCALE];
31103140 return (
@@ -3276,7 +3306,7 @@ class StreamingProcessor {
32763306 'send_date': chat[messageId]['send_date'],
32773307 'gen_started': chat[messageId]['gen_started'],
32783308 'gen_finished': chat[messageId]['gen_finished'],
32793309 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])),
32803310 };
32813311 }
32823312
@@ -3993,29 +4023,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
39934023 force_name2 = false;
39944024 }
39954025
3996- // TODO (kingbri): Migrate to a utility function
4026+ let mesExamplesArray = parseMesExamples(mesExamples, isInstruct);
3997- /**
3998- * Parses an examples string.
3999- * @param {string} examplesStr
4000- * @returns {string[]} Examples array with block heading
4001- */
4002- function parseMesExamples(examplesStr) {
4003- if (!examplesStr || examplesStr.length === 0 || examplesStr === '<START>') {
4004- return [];
4005- }
4006-
4007- if (!examplesStr.startsWith('<START>')) {
4008- examplesStr = '<START>\n' + examplesStr.trim();
4009- }
4010-
4011- const exampleSeparator = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : '';
4012- const blockHeading = main_api === 'openai' ? '<START>\n' : (exampleSeparator || (isInstruct ? '<START>\n' : ''));
4013- const splitExamples = examplesStr.split(/<START>/gi).slice(1).map(block => `${blockHeading}${block.trim()}\n`);
4014-
4015- return splitExamples;
4016- }
4017-
4018- let mesExamplesArray = parseMesExamples(mesExamples);
40194027
40204028 //////////////////////////////////
40214029 // Extension added strings
@@ -4040,7 +4048,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
40404048 }
40414049
40424050 const formattedExample = baseChatReplace(exampleMessage, name1, name2);
40434051 const cleanedExample = parseMesExamples(formattedExample, isInstruct);
40444052
40454053 // Insert depending on before or after position
40464054 if (example.position === wi_anchor_position.before) {
public/scripts/templates/macros.html+1 -1
@@ -16,7 +16,7 @@
1616 <li><tt>&lcub;&lcub;scenario&rcub;&rcub;</tt> – <span data-i18n="help_macros_11">the Character's Scenario</span></li>
1717 <li><tt>&lcub;&lcub;persona&rcub;&rcub;</tt> – <span data-i18n="help_macros_12">your current Persona Description</span></li>
1818 <li><tt>&lcub;&lcub;mesExamples&rcub;&rcub;</tt> – <span data-i18n="help_macros_13">the Character's Dialogue Examples</span></li>
1919 <li><tt>&lcub;&lcub;mesExamplesRaw&rcub;&rcub;</tt> – <span data-i18n="help_macros_14">unformatted Dialogue Examples </span><b data-i18n="(only for Story String)">(only for Story String)</b></li>
2020 <li><tt>&lcub;&lcub;summary&rcub;&rcub;</tt> – <span data-i18n="help_macros_summary">the latest chat summary generated by the "Summarize" extension (if available).</span></li>
2121 <li><tt>&lcub;&lcub;user&rcub;&rcub;</tt> – <span data-i18n="help_macros_15">your current Persona username</span></li>
2222 <li><tt>&lcub;&lcub;char&rcub;&rcub;</tt> – <span data-i18n="help_macros_16">the Character's name</span></li>