Merge pull request #3191 from SillyTavern/group-join-examples

Fixes to group join examples parsing
This commit is contained in:
Cohee
2024-12-21 20:15:59 +02:00
committed by GitHub
4 changed files with 169 additions and 109 deletions

View File

@ -275,6 +275,20 @@ export function getGroupMembers(groupId = selected_group) {
return group?.members.map(member => characters.find(x => x.avatar === member)) ?? [];
}
/**
* Retrieves the member names of a group. If the group is not selected, an empty array is returned.
* @returns {string[]} An array of character names representing the members of the group.
*/
export function getGroupNames() {
if (!selected_group) {
return [];
}
const groupMembers = groups.find(x => x.id == selected_group)?.members;
return Array.isArray(groupMembers)
? groupMembers.map(x => characters.find(y => y.avatar === x)?.name).filter(x => x)
: [];
}
/**
* Finds the character ID for a group member.
* @param {string} arg 0-based member index or character name
@ -423,14 +437,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 +485,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('<START>') ? `<START>\n${x}` : x));
}
const description = descriptions.filter(x => x.length).join('\n');

View File

@ -33,7 +33,7 @@ import {
system_message_types,
this_chid,
} from '../script.js';
import { selected_group } from './group-chats.js';
import { getGroupNames, selected_group } from './group-chats.js';
import {
chatCompletionDefaultPrompts,
@ -543,11 +543,15 @@ function setupChatCompletionPromptManager(openAiSettings) {
* @returns {Message[]} Array of message objects
*/
export function parseExampleIntoIndividual(messageExampleString, appendNamesForGroup = true) {
const groupBotNames = getGroupNames().map(name => `${name}:`);
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 +575,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 +597,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;
}
@ -1866,6 +1874,7 @@ async function sendOpenAIRequest(type, messages, signal) {
'n': canMultiSwipe ? oai_settings.n : undefined,
'user_name': name1,
'char_name': name2,
'group_names': getGroupNames(),
};
// Empty array will produce a validation error