diff --git a/public/scripts/group-chats.js b/public/scripts/group-chats.js index 9784218ee..1152b2471 100644 --- a/public/scripts/group-chats.js +++ b/public/scripts/group-chats.js @@ -423,14 +423,20 @@ export function getGroupCharacterCards(groupId, characterId) { * @param {string} value Value to replace * @param {string} characterName Name of the character * @param {string} fieldName Name of the field + * @param {function(string): string} [preprocess] Preprocess function * @returns {string} Prepared text * */ - function replaceAndPrepareForJoin(value, characterName, fieldName) { + function replaceAndPrepareForJoin(value, characterName, fieldName, preprocess = null) { value = value.trim(); if (!value) { return ''; } + // Run preprocess function + if (typeof preprocess === 'function') { + value = preprocess(value); + } + // Prepare and replace prefixes const prefix = customBaseChatReplace(group.generation_mode_join_prefix, fieldName, characterName); const suffix = customBaseChatReplace(group.generation_mode_join_suffix, fieldName, characterName); @@ -465,7 +471,7 @@ export function getGroupCharacterCards(groupId, characterId) { descriptions.push(replaceAndPrepareForJoin(character.description, character.name, 'Description')); personalities.push(replaceAndPrepareForJoin(character.personality, character.name, 'Personality')); scenarios.push(replaceAndPrepareForJoin(character.scenario, character.name, 'Scenario')); - mesExamplesArray.push(replaceAndPrepareForJoin(character.mes_example, character.name, 'Example Messages')); + mesExamplesArray.push(replaceAndPrepareForJoin(character.mes_example, character.name, 'Example Messages', (x) => !x.startsWith('') ? `\n${x}` : x)); } const description = descriptions.filter(x => x.length).join('\n'); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 6f695ae3b..f50126abc 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -33,7 +33,7 @@ import { system_message_types, this_chid, } from '../script.js'; -import { selected_group } from './group-chats.js'; +import { groups, selected_group } from './group-chats.js'; import { chatCompletionDefaultPrompts, @@ -543,11 +543,18 @@ function setupChatCompletionPromptManager(openAiSettings) { * @returns {Message[]} Array of message objects */ export function parseExampleIntoIndividual(messageExampleString, appendNamesForGroup = true) { + const groupMembers = selected_group ? groups.find(x => x.id == selected_group)?.members : null; + const groupBotNames = Array.isArray(groupMembers) + ? groupMembers.map(x => characters.find(y => y.avatar === x)?.name).filter(x => x).map(x => `${x}:`) + : []; + let result = []; // array of msgs let tmp = messageExampleString.split('\n'); let cur_msg_lines = []; let in_user = false; let in_bot = false; + let botName = name2; + // DRY my cock and balls :) function add_msg(name, role, system_name) { // join different newlines (we split them by \n and join by \n) @@ -571,10 +578,14 @@ export function parseExampleIntoIndividual(messageExampleString, appendNamesForG in_user = true; // we were in the bot mode previously, add the message if (in_bot) { - add_msg(name2, 'system', 'example_assistant'); + add_msg(botName, 'system', 'example_assistant'); } in_bot = false; - } else if (cur_str.startsWith(name2 + ':')) { + } else if (cur_str.startsWith(name2 + ':') || groupBotNames.some(n => cur_str.startsWith(n))) { + if (!cur_str.startsWith(name2 + ':') && groupBotNames.length) { + botName = cur_str.split(':')[0]; + } + in_bot = true; // we were in the user mode previously, add the message if (in_user) { @@ -589,7 +600,7 @@ export function parseExampleIntoIndividual(messageExampleString, appendNamesForG if (in_user) { add_msg(name1, 'system', 'example_user'); } else if (in_bot) { - add_msg(name2, 'system', 'example_assistant'); + add_msg(botName, 'system', 'example_assistant'); } return result; }