mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-03-23 23:40:11 +01:00
Update prompt-converters.js
This commit is contained in:
parent
d6e5ceaf93
commit
04f3aa8b66
@ -12,21 +12,17 @@
|
||||
|
||||
function convertClaudePrompt(messages, addAssistantPostfix, addAssistantPrefill, withSyspromptSupport, useSystemPrompt, addSysHumanMsg) {
|
||||
|
||||
// Find the index of the first message with an assistant role and check for a "'user' role/Human:" before it.
|
||||
console.log(JSON.stringify(messages, null, 2));
|
||||
//Prepare messages for claude.
|
||||
if (messages.length > 0) {
|
||||
//messages[0].role = withSyspromptSupport && useSystemPrompt ? 'system' : 'user';
|
||||
//Add the assistant's message to the end of messages.
|
||||
if (addAssistantPostfix) {
|
||||
let role = 'assistant';
|
||||
if (messages.length > 0 && messages[messages.length - 1].role === 'assistant' || messages[messages.length - 1].content.includes('Assistant:')) {
|
||||
role = 'system';
|
||||
}
|
||||
messages.push({
|
||||
role: role,
|
||||
role: 'assistant',
|
||||
content: addAssistantPrefill || '',
|
||||
});
|
||||
}
|
||||
|
||||
// Find the index of the first message with an assistant role and check for a "'user' role/Human:" before it.
|
||||
let hasUser = false;
|
||||
const firstAssistantIndex = messages.findIndex((message, i) => {
|
||||
if (i > 0 && (message.role === 'user' || message.content.includes('Human:'))) {
|
||||
@ -34,26 +30,28 @@ function convertClaudePrompt(messages, addAssistantPostfix, addAssistantPrefill,
|
||||
}
|
||||
return message.role === 'assistant' && i > 0;
|
||||
});
|
||||
// When 2.1 and 'Use system prompt" checked, switches to system prompt format by setting the first message's role to 'system'.
|
||||
// Also, insert the human's message before the first the assistant one, in case there are no such message or prefix found.
|
||||
if (withSyspromptSupport && useSystemPrompt) {
|
||||
messages[0].role = 'system';
|
||||
|
||||
if (!hasUser) {
|
||||
if (firstAssistantIndex > 0 && !hasUser) {
|
||||
messages.splice(firstAssistantIndex, 0, {
|
||||
role: 'user',
|
||||
content: addSysHumanMsg || 'Let\'s get started.',
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Otherwise, use the default message format by setting the first message's role to 'user'(compatible with all claude models including 2.1.)
|
||||
messages[0].role = 'user';
|
||||
// Also, fix messages order for default claude format when(messages > Context Size) by merging two messages with "\n\nHuman: " prefixes into one before the first Assistant's message.
|
||||
if (firstAssistantIndex > 0) {
|
||||
messages[firstAssistantIndex - 1].role = firstAssistantIndex - 1 !== 0 && messages[firstAssistantIndex - 1].role === 'user' ? 'FirstMsg' : messages[firstAssistantIndex - 1].role;
|
||||
messages[firstAssistantIndex - 1].role = firstAssistantIndex - 1 !== 0 && messages[firstAssistantIndex - 1].role === 'user' ? 'firstMsg' : messages[firstAssistantIndex - 1].role;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(JSON.stringify(messages, null, 2));
|
||||
|
||||
let setHumanMsg = addSysHumanMsg ? '\n\nHuman: ' + addSysHumanMsg : '\n\nHuman: Let\'s get started.';
|
||||
// Convert messages to requestPrompt.
|
||||
let requestPrompt = messages.map((v, i) => {
|
||||
// Claude doesn't support message names, so we'll just add them to the message content.
|
||||
if (v.name && v.role !== 'system') {
|
||||
@ -62,32 +60,16 @@ function convertClaudePrompt(messages, addAssistantPostfix, addAssistantPrefill,
|
||||
}
|
||||
|
||||
let prefix = '';
|
||||
// Switches to system prompt format by adding empty prefix to the first message of the assistant, when the "use system prompt" checked and the model is 2.1.
|
||||
// Otherwise, use the default message format by adding "Human: " prefix to the first message(compatible with all claude models including 2.1.)
|
||||
/* if (i === 0) {
|
||||
prefix = withSyspromptSupport && useSystemPrompt ? '' : '\n\nHuman: ';
|
||||
// For system prompt format. If there is no message with role "user" or prefix "Human:" change the first assistant's prefix(insert the human's message).
|
||||
} else if (i === firstAssistantIndex && !hasUser && withSyspromptSupport && useSystemPrompt) {
|
||||
prefix = `${setHumanMsg}\n\nAssistant: `;
|
||||
//prefix = addSysHumanMsg ? '\n\nHuman: ' + addSysHumanMsg + '\n\nAssistant: ' : '\n\nHuman: Let\'s get started.\n\nAssistant: ';
|
||||
// Merge two messages with "\n\nHuman: " prefixes into one before the first Assistant's message. Fix messages order for default claude format when(messages > Context Size).
|
||||
} else*/ if (i > 0 && i === firstAssistantIndex - 1 && v.role === 'user' && !(withSyspromptSupport && useSystemPrompt)) {
|
||||
prefix = '\n\nFirst message: ';
|
||||
//Define role prefixes(role : prefix). Set the correct prefix according to the role/name.
|
||||
} else {
|
||||
prefix = {
|
||||
'assistant': '\n\nAssistant: ',
|
||||
'user': '\n\nHuman: ',
|
||||
'FirstMsg': '\n\nFirst message: ',
|
||||
'system': i === 0 ? '' : v.name === 'example_assistant' ? '\n\nA: ' : v.name === 'example_user' ? '\n\nH: ' : i === messages.length - 1 ? '\n' : '\n\n',
|
||||
}[v.role] ?? '\n\n';
|
||||
}
|
||||
prefix = {
|
||||
'assistant': '\n\nAssistant: ',
|
||||
'user': '\n\nHuman: ',
|
||||
'system': i === 0 ? '' : v.name === 'example_assistant' ? '\n\nA: ' : v.name === 'example_user' ? '\n\nH: ' : '\n\n',
|
||||
'firstMsg': '\n\nFirst message: ',
|
||||
}[v.role] ?? '\n\n';
|
||||
//}
|
||||
return prefix + v.content;
|
||||
}).join('');
|
||||
|
||||
//Add the assistant suffix(if the option unchecked), add a prefill after it(if filled). Also Add the first human message before the assistant suffix(when using sysprompt and there are no other messages with the role 'Assistant').
|
||||
//requestPrompt += addAssistantPostfix ? `${withSyspromptSupport && useSystemPrompt && firstAssistantIndex === -1 ? setHumanMsg : ''}\n\nAssistant: ${addAssistantPrefill ? addAssistantPrefill : ''}` : '';
|
||||
requestPrompt += '';
|
||||
return requestPrompt;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user