merge messages after system prompt handling (oopsie)

This commit is contained in:
based 2024-03-05 05:01:36 +10:00
parent 04bb882e90
commit b8e8b0ac9e
1 changed files with 28 additions and 29 deletions

View File

@ -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({