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>
| @@ -2716,7 +2716,13 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re | |||
| 2716 | environment.personality = fields.personality || ''; | 2716 | environment.personality = fields.personality || ''; |
| 2717 | environment.scenario = fields.scenario || ''; | 2717 | environment.scenario = fields.scenario || ''; |
| 2718 | environment.persona = fields.persona || ''; | 2718 | environment.persona = fields.persona || ''; |
| 2719 | environment.mesExamples = fields.mesExamples || ''; | 2719 | environment.mesExamples = () => { |
| 2720 | const isInstruct = power_user.instruct.enabled && main_api !== 'openai'; | ||
| 2721 | const mesExamplesArray = parseMesExamples(fields.mesExamples, isInstruct); | ||
| 2722 | const instructExamples = formatInstructModeExamples(mesExamplesArray, name1, name2); | ||
| 2723 | return instructExamples.join(''); | ||
| 2724 | }; | ||
| 2725 | environment.mesExamplesRaw = fields.mesExamples || ''; | ||
| 2720 | environment.charVersion = fields.version || ''; | 2726 | environment.charVersion = fields.version || ''; |
| 2721 | environment.char_version = fields.version || ''; | 2727 | environment.char_version = fields.version || ''; |
| 2722 | } | 2728 | } |
| @@ -3105,6 +3111,27 @@ export function getCharacterCardFields() { | |||
| 3105 | return result; | 3111 | return result; |
| 3106 | } | 3112 | } |
| 3107 | 3113 | ||
| 3114 | /** | ||
| 3115 | * Parses an examples string. | ||
| 3116 | * @param {string} examplesStr | ||
| 3117 | * @returns {string[]} Examples array with block heading | ||
| 3118 | */ | ||
| 3119 | export function parseMesExamples(examplesStr, isInstruct) { | ||
| 3120 | if (!examplesStr || examplesStr.length === 0 || examplesStr === '<START>') { | ||
| 3121 | return []; | ||
| 3122 | } | ||
| 3123 | |||
| 3124 | if (!examplesStr.startsWith('<START>')) { | ||
| 3125 | examplesStr = '<START>\n' + examplesStr.trim(); | ||
| 3126 | } | ||
| 3127 | |||
| 3128 | const exampleSeparator = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : ''; | ||
| 3129 | const blockHeading = main_api === 'openai' ? '<START>\n' : (exampleSeparator || (isInstruct ? '<START>\n' : '')); | ||
| 3130 | const splitExamples = examplesStr.split(/<START>/gi).slice(1).map(block => `${blockHeading}${block.trim()}\n`); | ||
| 3131 | |||
| 3132 | return splitExamples; | ||
| 3133 | } | ||
| 3134 | |||
| 3108 | export function isStreamingEnabled() { | 3135 | export function isStreamingEnabled() { |
| 3109 | const noStreamSources = [chat_completion_sources.SCALE]; | 3136 | const noStreamSources = [chat_completion_sources.SCALE]; |
| 3110 | return ( | 3137 | return ( |
| @@ -3993,29 +4020,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 3993 | force_name2 = false; | 4020 | force_name2 = false; |
| 3994 | } | 4021 | } |
| 3995 | 4022 | ||
| 3996 | // TODO (kingbri): Migrate to a utility function | 4023 | 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); | ||
| 4019 | 4024 | ||
| 4020 | ////////////////////////////////// | 4025 | ////////////////////////////////// |
| 4021 | // Extension added strings | 4026 | // Extension added strings |
| @@ -4040,7 +4045,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 4040 | } | 4045 | } |
| 4041 | 4046 | ||
| 4042 | const formattedExample = baseChatReplace(exampleMessage, name1, name2); | 4047 | const formattedExample = baseChatReplace(exampleMessage, name1, name2); |
| 4043 | const cleanedExample = parseMesExamples(formattedExample); | 4048 | const cleanedExample = parseMesExamples(formattedExample, isInstruct); |
| 4044 | 4049 | ||
| 4045 | // Insert depending on before or after position | 4050 | // Insert depending on before or after position |
| 4046 | if (example.position === wi_anchor_position.before) { | 4051 | if (example.position === wi_anchor_position.before) { |