Merge pull request #3629 from kingbri1/staging Make mesExamples macros consistent
Signed| @@ -2716,7 +2716,16 @@ 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 | if (isInstruct) { | ||
| 2723 | const instructExamples = formatInstructModeExamples(mesExamplesArray, name1, name2); | ||
| 2724 | return instructExamples.join(''); | ||
| 2725 | } | ||
| 2726 | return mesExamplesArray.join(''); | ||
| 2727 | }; | ||
| 2728 | environment.mesExamplesRaw = fields.mesExamples || ''; | ||
| 2720 | environment.charVersion = fields.version || ''; | 2729 | environment.charVersion = fields.version || ''; |
| 2721 | environment.char_version = fields.version || ''; | 2730 | environment.char_version = fields.version || ''; |
| 2722 | } | 2731 | } |
| @@ -3105,6 +3114,27 @@ export function getCharacterCardFields() { | |||
| 3105 | return result; | 3114 | return result; |
| 3106 | } | 3115 | } |
| 3107 | 3116 | ||
| 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 | |||
| 3108 | export function isStreamingEnabled() { | 3138 | export function isStreamingEnabled() { |
| 3109 | const noStreamSources = [chat_completion_sources.SCALE]; | 3139 | const noStreamSources = [chat_completion_sources.SCALE]; |
| 3110 | return ( | 3140 | return ( |
| @@ -3276,7 +3306,7 @@ class StreamingProcessor { | |||
| 3276 | 'send_date': chat[messageId]['send_date'], | 3306 | 'send_date': chat[messageId]['send_date'], |
| 3277 | 'gen_started': chat[messageId]['gen_started'], | 3307 | 'gen_started': chat[messageId]['gen_started'], |
| 3278 | 'gen_finished': chat[messageId]['gen_finished'], | 3308 | 'gen_finished': chat[messageId]['gen_finished'], |
| 3279 | 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])) | 3309 | 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])), |
| 3280 | }; | 3310 | }; |
| 3281 | } | 3311 | } |
| 3282 | 3312 | ||
| @@ -3993,29 +4023,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 3993 | force_name2 = false; | 4023 | force_name2 = false; |
| 3994 | } | 4024 | } |
| 3995 | 4025 | ||
| 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); | ||
| 4019 | 4027 | ||
| 4020 | ////////////////////////////////// | 4028 | ////////////////////////////////// |
| 4021 | // Extension added strings | 4029 | // Extension added strings |
| @@ -4040,7 +4048,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 4040 | } | 4048 | } |
| 4041 | 4049 | ||
| 4042 | const formattedExample = baseChatReplace(exampleMessage, name1, name2); | 4050 | const formattedExample = baseChatReplace(exampleMessage, name1, name2); |
| 4043 | const cleanedExample = parseMesExamples(formattedExample); | 4051 | const cleanedExample = parseMesExamples(formattedExample, isInstruct); |
| 4044 | 4052 | ||
| 4045 | // Insert depending on before or after position | 4053 | // Insert depending on before or after position |
| 4046 | if (example.position === wi_anchor_position.before) { | 4054 | if (example.position === wi_anchor_position.before) { |
| @@ -16,7 +16,7 @@ | |||
| 16 | <li><tt>{{scenario}}</tt> – <span data-i18n="help_macros_11">the Character's Scenario</span></li> | 16 | <li><tt>{{scenario}}</tt> – <span data-i18n="help_macros_11">the Character's Scenario</span></li> |
| 17 | <li><tt>{{persona}}</tt> – <span data-i18n="help_macros_12">your current Persona Description</span></li> | 17 | <li><tt>{{persona}}</tt> – <span data-i18n="help_macros_12">your current Persona Description</span></li> |
| 18 | <li><tt>{{mesExamples}}</tt> – <span data-i18n="help_macros_13">the Character's Dialogue Examples</span></li> | 18 | <li><tt>{{mesExamples}}</tt> – <span data-i18n="help_macros_13">the Character's Dialogue Examples</span></li> |
| 19 | <li><tt>{{mesExamplesRaw}}</tt> – <span data-i18n="help_macros_14">unformatted Dialogue Examples </span><b data-i18n="(only for Story String)">(only for Story String)</b></li> | 19 | <li><tt>{{mesExamplesRaw}}</tt> – <span data-i18n="help_macros_14">unformatted Dialogue Examples</span></li> |
| 20 | <li><tt>{{summary}}</tt> – <span data-i18n="help_macros_summary">the latest chat summary generated by the "Summarize" extension (if available).</span></li> | 20 | <li><tt>{{summary}}</tt> – <span data-i18n="help_macros_summary">the latest chat summary generated by the "Summarize" extension (if available).</span></li> |
| 21 | <li><tt>{{user}}</tt> – <span data-i18n="help_macros_15">your current Persona username</span></li> | 21 | <li><tt>{{user}}</tt> – <span data-i18n="help_macros_15">your current Persona username</span></li> |
| 22 | <li><tt>{{char}}</tt> – <span data-i18n="help_macros_16">the Character's name</span></li> | 22 | <li><tt>{{char}}</tt> – <span data-i18n="help_macros_16">the Character's name</span></li> |