mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-04-02 21:21:14 +02: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) {
|
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));
|
console.log(JSON.stringify(messages, null, 2));
|
||||||
|
//Prepare messages for claude.
|
||||||
if (messages.length > 0) {
|
if (messages.length > 0) {
|
||||||
//messages[0].role = withSyspromptSupport && useSystemPrompt ? 'system' : 'user';
|
//Add the assistant's message to the end of messages.
|
||||||
if (addAssistantPostfix) {
|
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({
|
messages.push({
|
||||||
role: role,
|
role: 'assistant',
|
||||||
content: addAssistantPrefill || '',
|
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;
|
let hasUser = false;
|
||||||
const firstAssistantIndex = messages.findIndex((message, i) => {
|
const firstAssistantIndex = messages.findIndex((message, i) => {
|
||||||
if (i > 0 && (message.role === 'user' || message.content.includes('Human:'))) {
|
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;
|
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) {
|
if (withSyspromptSupport && useSystemPrompt) {
|
||||||
messages[0].role = 'system';
|
messages[0].role = 'system';
|
||||||
|
if (firstAssistantIndex > 0 && !hasUser) {
|
||||||
if (!hasUser) {
|
|
||||||
messages.splice(firstAssistantIndex, 0, {
|
messages.splice(firstAssistantIndex, 0, {
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: addSysHumanMsg || 'Let\'s get started.',
|
content: addSysHumanMsg || 'Let\'s get started.',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} 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';
|
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) {
|
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));
|
console.log(JSON.stringify(messages, null, 2));
|
||||||
|
// Convert messages to requestPrompt.
|
||||||
let setHumanMsg = addSysHumanMsg ? '\n\nHuman: ' + addSysHumanMsg : '\n\nHuman: Let\'s get started.';
|
|
||||||
let requestPrompt = messages.map((v, i) => {
|
let requestPrompt = messages.map((v, i) => {
|
||||||
// Claude doesn't support message names, so we'll just add them to the message content.
|
// Claude doesn't support message names, so we'll just add them to the message content.
|
||||||
if (v.name && v.role !== 'system') {
|
if (v.name && v.role !== 'system') {
|
||||||
@ -62,32 +60,16 @@ function convertClaudePrompt(messages, addAssistantPostfix, addAssistantPrefill,
|
|||||||
}
|
}
|
||||||
|
|
||||||
let prefix = '';
|
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 = {
|
prefix = {
|
||||||
'assistant': '\n\nAssistant: ',
|
'assistant': '\n\nAssistant: ',
|
||||||
'user': '\n\nHuman: ',
|
'user': '\n\nHuman: ',
|
||||||
'FirstMsg': '\n\nFirst message: ',
|
'system': i === 0 ? '' : v.name === 'example_assistant' ? '\n\nA: ' : v.name === 'example_user' ? '\n\nH: ' : '\n\n',
|
||||||
'system': i === 0 ? '' : v.name === 'example_assistant' ? '\n\nA: ' : v.name === 'example_user' ? '\n\nH: ' : i === messages.length - 1 ? '\n' : '\n\n',
|
'firstMsg': '\n\nFirst message: ',
|
||||||
}[v.role] ?? '\n\n';
|
}[v.role] ?? '\n\n';
|
||||||
}
|
//}
|
||||||
return prefix + v.content;
|
return prefix + v.content;
|
||||||
}).join('');
|
}).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;
|
return requestPrompt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user