Fixes to group join examples parsing

2d66b7204ad0352ceadde05a75d98fc37c068cd3

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

2 files changed, +23 -6Showing whitespace changes
public/scripts/group-chats.js+8 -2
@@ -423,14 +423,20 @@ export function getGroupCharacterCards(groupId, characterId) {
423423 * @param {string} value Value to replace
424424 * @param {string} characterName Name of the character
425425 * @param {string} fieldName Name of the field
426+ * @param {function(string): string} [preprocess] Preprocess function
426427 * @returns {string} Prepared text
427428 * */
428429 function replaceAndPrepareForJoin(value, characterName, fieldName, preprocess = null) {
429430 value = value.trim();
430431 if (!value) {
431432 return '';
432433 }
433434
435+ // Run preprocess function
436+ if (typeof preprocess === 'function') {
437+ value = preprocess(value);
438+ }
439+
434440 // Prepare and replace prefixes
435441 const prefix = customBaseChatReplace(group.generation_mode_join_prefix, fieldName, characterName);
436442 const suffix = customBaseChatReplace(group.generation_mode_join_suffix, fieldName, characterName);
@@ -465,7 +471,7 @@ export function getGroupCharacterCards(groupId, characterId) {
465471 descriptions.push(replaceAndPrepareForJoin(character.description, character.name, 'Description'));
466472 personalities.push(replaceAndPrepareForJoin(character.personality, character.name, 'Personality'));
467473 scenarios.push(replaceAndPrepareForJoin(character.scenario, character.name, 'Scenario'));
468474 mesExamplesArray.push(replaceAndPrepareForJoin(character.mes_example, character.name, 'Example Messages', (x) => !x.startsWith('<START>') ? `<START>\n${x}` : x));
469475 }
470476
471477 const description = descriptions.filter(x => x.length).join('\n');
public/scripts/openai.js+15 -4
@@ -33,7 +33,7 @@ import {
3333 system_message_types,
3434 this_chid,
3535} from '../script.js';
3636import { groups, selected_group } from './group-chats.js';
3737
3838import {
3939 chatCompletionDefaultPrompts,
@@ -543,11 +543,18 @@ function setupChatCompletionPromptManager(openAiSettings) {
543543 * @returns {Message[]} Array of message objects
544544 */
545545export function parseExampleIntoIndividual(messageExampleString, appendNamesForGroup = true) {
546+ const groupMembers = selected_group ? groups.find(x => x.id == selected_group)?.members : null;
547+ const groupBotNames = Array.isArray(groupMembers)
548+ ? groupMembers.map(x => characters.find(y => y.avatar === x)?.name).filter(x => x).map(x => `${x}:`)
549+ : [];
550+
546551 let result = []; // array of msgs
547552 let tmp = messageExampleString.split('\n');
548553 let cur_msg_lines = [];
549554 let in_user = false;
550555 let in_bot = false;
556+ let botName = name2;
557+
551558 // DRY my cock and balls :)
552559 function add_msg(name, role, system_name) {
553560 // join different newlines (we split them by \n and join by \n)
@@ -571,10 +578,14 @@ export function parseExampleIntoIndividual(messageExampleString, appendNamesForG
571578 in_user = true;
572579 // we were in the bot mode previously, add the message
573580 if (in_bot) {
574581 add_msg(name2botName, 'system', 'example_assistant');
575582 }
576583 in_bot = false;
577584 } else if (cur_str.startsWith(name2 + ':') || groupBotNames.some(n => cur_str.startsWith(n))) {
585+ if (!cur_str.startsWith(name2 + ':') && groupBotNames.length) {
586+ botName = cur_str.split(':')[0];
587+ }
588+
578589 in_bot = true;
579590 // we were in the user mode previously, add the message
580591 if (in_user) {
@@ -589,7 +600,7 @@ export function parseExampleIntoIndividual(messageExampleString, appendNamesForG
589600 if (in_user) {
590601 add_msg(name1, 'system', 'example_user');
591602 } else if (in_bot) {
592603 add_msg(name2botName, 'system', 'example_assistant');
593604 }
594605 return result;
595606}