mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
merge messages after system prompt handling (oopsie)
This commit is contained in:
@@ -79,6 +79,34 @@ function convertClaudePrompt(messages, addAssistantPostfix, addAssistantPrefill,
|
|||||||
* @param {string} humanMsgFix Add Human message between system prompt and assistant.
|
* @param {string} humanMsgFix Add Human message between system prompt and assistant.
|
||||||
*/
|
*/
|
||||||
function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFix) {
|
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
|
// 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 = [];
|
let mergedMessages = [];
|
||||||
messages.forEach((message) => {
|
messages.forEach((message) => {
|
||||||
@@ -88,35 +116,6 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi
|
|||||||
mergedMessages.push(message);
|
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
|
// Shouldn't be conditional anymore, messages api expects the last role to be user unless we're explicitly prefilling
|
||||||
if (prefillString) {
|
if (prefillString) {
|
||||||
mergedMessages.push({
|
mergedMessages.push({
|
||||||
|
Reference in New Issue
Block a user