From f66261a52cb57c092bedeca7164ae5fb468a2375 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 6 Oct 2024 14:28:52 +0300 Subject: [PATCH] Mistral: Squash user messages that follow tools --- src/prompt-converters.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/prompt-converters.js b/src/prompt-converters.js index c27ff8f8a..d3e910859 100644 --- a/src/prompt-converters.js +++ b/src/prompt-converters.js @@ -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') {