From b8e8b0ac9e5444957e02e6da0c6c714ade681e77 Mon Sep 17 00:00:00 2001 From: based Date: Tue, 5 Mar 2024 05:01:36 +1000 Subject: [PATCH] merge messages after system prompt handling (oopsie) --- src/endpoints/prompt-converters.js | 57 +++++++++++++++--------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/src/endpoints/prompt-converters.js b/src/endpoints/prompt-converters.js index a8c0ed54b..b8c5c21e9 100644 --- a/src/endpoints/prompt-converters.js +++ b/src/endpoints/prompt-converters.js @@ -79,6 +79,34 @@ function convertClaudePrompt(messages, addAssistantPostfix, addAssistantPrefill, * @param {string} humanMsgFix Add Human message between system prompt and assistant. */ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFix) { + let systemPrompt = ''; + if (useSysPrompt) { + // Collect all the system messages up until the first instance of a non-system message, and then remove them from the messages array. + let i; + for (i = 0; i < messages.length; i++) { + if (messages[i].role !== 'system') { + break; + } + systemPrompt += `${messages[i].content}\n\n`; + } + + messages.splice(0, i); + + // Check if the first message in the array is of type user, if not, interject with humanMsgFix or a blank message. + if (messages.length > 0 && messages[0].role !== 'user') { + messages.unshift({ + role: 'user', + content: humanMsgFix || '', + }); + } + } + // Now replace all further messages that have the role 'system' with the role 'user'. (or all if we're not using one) + messages.forEach((message) => { + if (message.role === 'system') { + message.role = 'user'; + } + }); + // Since the messaging endpoint only supports user assistant roles in turns, we have to merge messages with the same role if they follow eachother let mergedMessages = []; messages.forEach((message) => { @@ -88,35 +116,6 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi mergedMessages.push(message); } }); - - let systemPrompt = ''; - if (useSysPrompt) { - // Collect all the system messages up until the first instance of a non-system message, and then remove them from the messages array. - let i; - for (i = 0; i < mergedMessages.length; i++) { - if (mergedMessages[i].role !== 'system') { - break; - } - systemPrompt += `${mergedMessages[i].content}\n\n`; - } - - mergedMessages.splice(0, i); - - // Check if the first message in the array is of type user, if not, interject with humanMsgFix or a blank message. - if (mergedMessages.length > 0 && mergedMessages[0].role !== 'user') { - mergedMessages.unshift({ - role: 'user', - content: humanMsgFix || '', - }); - } - } - // Now replace all further messages that have the role 'system' with the role 'user'. (or all if we're not using one) - mergedMessages.forEach((message) => { - if (message.role === 'system') { - message.role = 'user'; - } - }); - // Shouldn't be conditional anymore, messages api expects the last role to be user unless we're explicitly prefilling if (prefillString) { mergedMessages.push({