From 5d561b64e2e1556b56ffe41d8e020d7325c949e0 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 12 Mar 2024 01:10:53 +0200 Subject: [PATCH] Fix names in completions not getting added when squashing assistant messages --- src/endpoints/prompt-converters.js | 35 ++++++++++++++---------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/endpoints/prompt-converters.js b/src/endpoints/prompt-converters.js index d5b65ce01..3e89c5a75 100644 --- a/src/endpoints/prompt-converters.js +++ b/src/endpoints/prompt-converters.js @@ -125,20 +125,30 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi // Also handle multi-modality, holy slop. let mergedMessages = []; messages.forEach((message) => { - const imageEntry = message.content[1]?.image_url; + const imageEntry = message.content?.[1]?.image_url; const imageData = imageEntry?.url; - const mimeType = imageData?.split(';')[0].split(':')[1]; - const base64Data = imageData?.split(',')[1]; + const mimeType = imageData?.split(';')?.[0].split(':')?.[1]; + const base64Data = imageData?.split(',')?.[1]; + + // Take care of name properties since claude messages don't support them + if (message.name) { + if (Array.isArray(message.content)) { + message.content[0].text = `${message.name}: ${message.content[0].text}`; + } else { + message.content = `${message.name}: ${message.content}`; + } + delete message.name; + } if (mergedMessages.length > 0 && mergedMessages[mergedMessages.length - 1].role === message.role) { - if(Array.isArray(message.content)) { - if(Array.isArray(mergedMessages[mergedMessages.length - 1].content)) { + if (Array.isArray(message.content)) { + if (Array.isArray(mergedMessages[mergedMessages.length - 1].content)) { mergedMessages[mergedMessages.length - 1].content[0].text += '\n\n' + message.content[0].text; } else { mergedMessages[mergedMessages.length - 1].content += '\n\n' + message.content[0].text; } } else { - if(Array.isArray(mergedMessages[mergedMessages.length - 1].content)) { + if (Array.isArray(mergedMessages[mergedMessages.length - 1].content)) { mergedMessages[mergedMessages.length - 1].content[0].text += '\n\n' + message.content; } else { mergedMessages[mergedMessages.length - 1].content += '\n\n' + message.content; @@ -161,19 +171,6 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi } }); - - // Take care of name properties since claude messages don't support them - mergedMessages.forEach((message) => { - if (message.name) { - if (Array.isArray(message.content)) { - message.content[0].text = `${message.name}: ${message.content[0].text}`; - } else { - message.content = `${message.name}: ${message.content}`; - } - delete message.name; - } - }); - // Shouldn't be conditional anymore, messages api expects the last role to be user unless we're explicitly prefilling if (prefillString) { mergedMessages.push({