Mistral: Squash user messages that follow tools

This commit is contained in:
Cohee 2024-10-06 14:28:52 +03:00
parent 303df1087a
commit f66261a52c
1 changed files with 22 additions and 0 deletions

View File

@ -544,6 +544,28 @@ function convertMistralMessages(messages, charName = '', userName = '') {
}
});
// If user role message immediately follows a tool message, append it to the last user message
const fixToolMessages = () => {
let rerun = true;
while (rerun) {
rerun = false;
messages.forEach((message, i) => {
if (i === messages.length - 1) {
return;
}
if (message.role === 'tool' && messages[i + 1].role === 'user') {
const lastUserMessage = messages.slice(0, i).findLastIndex(m => m.role === 'user' && m.content);
if (lastUserMessage !== -1) {
messages[lastUserMessage].content += '\n\n' + messages[i + 1].content;
messages.splice(i + 1, 1);
rerun = true;
}
}
});
}
}
fixToolMessages();
// If system role message immediately follows an assistant message, change its role to user
for (let i = 0; i < messages.length - 1; i++) {
if (messages[i].role === 'assistant' && messages[i + 1].role === 'system') {