Fixes to group join examples parsing

2d66b7204ad0352ceadde05a75d98fc37c068cd3

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

2 files changed, +23 -6Ignore whitespace
public/scripts/group-chats.js+8 -2
@@ -423,14 +423,20 @@ export function getGroupCharacterCards(groupId, characterId) {
423 * @param {string} value Value to replace423 * @param {string} value Value to replace
424 * @param {string} characterName Name of the character424 * @param {string} characterName Name of the character
425 * @param {string} fieldName Name of the field425 * @param {string} fieldName Name of the field
426 * @param {function(string): string} [preprocess] Preprocess function
426 * @returns {string} Prepared text427 * @returns {string} Prepared text
427 * */428 * */
428 function replaceAndPrepareForJoin(value, characterName, fieldName) {429 function replaceAndPrepareForJoin(value, characterName, fieldName, preprocess = null) {
429 value = value.trim();430 value = value.trim();
430 if (!value) {431 if (!value) {
431 return '';432 return '';
432 }433 }
433434
435 // Run preprocess function
436 if (typeof preprocess === 'function') {
437 value = preprocess(value);
438 }
439
434 // Prepare and replace prefixes440 // Prepare and replace prefixes
435 const prefix = customBaseChatReplace(group.generation_mode_join_prefix, fieldName, characterName);441 const prefix = customBaseChatReplace(group.generation_mode_join_prefix, fieldName, characterName);
436 const suffix = customBaseChatReplace(group.generation_mode_join_suffix, fieldName, characterName);442 const suffix = customBaseChatReplace(group.generation_mode_join_suffix, fieldName, characterName);
@@ -465,7 +471,7 @@ export function getGroupCharacterCards(groupId, characterId) {
465 descriptions.push(replaceAndPrepareForJoin(character.description, character.name, 'Description'));471 descriptions.push(replaceAndPrepareForJoin(character.description, character.name, 'Description'));
466 personalities.push(replaceAndPrepareForJoin(character.personality, character.name, 'Personality'));472 personalities.push(replaceAndPrepareForJoin(character.personality, character.name, 'Personality'));
467 scenarios.push(replaceAndPrepareForJoin(character.scenario, character.name, 'Scenario'));473 scenarios.push(replaceAndPrepareForJoin(character.scenario, character.name, 'Scenario'));
468 mesExamplesArray.push(replaceAndPrepareForJoin(character.mes_example, character.name, 'Example Messages'));474 mesExamplesArray.push(replaceAndPrepareForJoin(character.mes_example, character.name, 'Example Messages', (x) => !x.startsWith('<START>') ? `<START>\n${x}` : x));
469 }475 }
470476
471 const description = descriptions.filter(x => x.length).join('\n');477 const description = descriptions.filter(x => x.length).join('\n');
public/scripts/openai.js+15 -4
@@ -33,7 +33,7 @@ import {
33 system_message_types,33 system_message_types,
34 this_chid,34 this_chid,
35} from '../script.js';35} from '../script.js';
36import { selected_group } from './group-chats.js';36import { groups, selected_group } from './group-chats.js';
3737
38import {38import {
39 chatCompletionDefaultPrompts,39 chatCompletionDefaultPrompts,
@@ -543,11 +543,18 @@ function setupChatCompletionPromptManager(openAiSettings) {
543 * @returns {Message[]} Array of message objects543 * @returns {Message[]} Array of message objects
544 */544 */
545export function parseExampleIntoIndividual(messageExampleString, appendNamesForGroup = true) {545export 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
546 let result = []; // array of msgs551 let result = []; // array of msgs
547 let tmp = messageExampleString.split('\n');552 let tmp = messageExampleString.split('\n');
548 let cur_msg_lines = [];553 let cur_msg_lines = [];
549 let in_user = false;554 let in_user = false;
550 let in_bot = false;555 let in_bot = false;
556 let botName = name2;
557
551 // DRY my cock and balls :)558 // DRY my cock and balls :)
552 function add_msg(name, role, system_name) {559 function add_msg(name, role, system_name) {
553 // join different newlines (we split them by \n and join by \n)560 // join different newlines (we split them by \n and join by \n)
@@ -571,10 +578,14 @@ export function parseExampleIntoIndividual(messageExampleString, appendNamesForG
571 in_user = true;578 in_user = true;
572 // we were in the bot mode previously, add the message579 // we were in the bot mode previously, add the message
573 if (in_bot) {580 if (in_bot) {
574 add_msg(name2, 'system', 'example_assistant');581 add_msg(botName, 'system', 'example_assistant');
575 }582 }
576 in_bot = false;583 in_bot = false;
577 } else if (cur_str.startsWith(name2 + ':')) {584 } 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
578 in_bot = true;589 in_bot = true;
579 // we were in the user mode previously, add the message590 // we were in the user mode previously, add the message
580 if (in_user) {591 if (in_user) {
@@ -589,7 +600,7 @@ export function parseExampleIntoIndividual(messageExampleString, appendNamesForG
589 if (in_user) {600 if (in_user) {
590 add_msg(name1, 'system', 'example_user');601 add_msg(name1, 'system', 'example_user');
591 } else if (in_bot) {602 } else if (in_bot) {
592 add_msg(name2, 'system', 'example_assistant');603 add_msg(botName, 'system', 'example_assistant');
593 }604 }
594 return result;605 return result;
595}606}