Pass char/user names for Claude example messages converter

This commit is contained in:
Cohee 2024-03-08 08:31:36 +02:00
parent e9d0c5bb05
commit 15eb18740e
3 changed files with 12 additions and 2 deletions

View File

@ -1603,6 +1603,8 @@ async function sendOpenAIRequest(type, messages, signal) {
'stop': getCustomStoppingStrings(openai_max_stop_strings), 'stop': getCustomStoppingStrings(openai_max_stop_strings),
'chat_completion_source': oai_settings.chat_completion_source, 'chat_completion_source': oai_settings.chat_completion_source,
'n': canMultiSwipe ? oai_settings.n : undefined, 'n': canMultiSwipe ? oai_settings.n : undefined,
'user_name': name1,
'char_name': name2,
}; };
// Empty array will produce a validation error // Empty array will produce a validation error

View File

@ -35,7 +35,7 @@ async function sendClaudeRequest(request, response) {
controller.abort(); controller.abort();
}); });
let use_system_prompt = (request.body.model.startsWith('claude-2') || request.body.model.startsWith('claude-3')) && request.body.claude_use_sysprompt; let use_system_prompt = (request.body.model.startsWith('claude-2') || request.body.model.startsWith('claude-3')) && request.body.claude_use_sysprompt;
let converted_prompt = convertClaudeMessages(request.body.messages, request.body.assistant_prefill, use_system_prompt, request.body.human_sysprompt_message); let converted_prompt = convertClaudeMessages(request.body.messages, request.body.assistant_prefill, use_system_prompt, request.body.human_sysprompt_message, request.body.char_name, request.body.user_name);
// Add custom stop sequences // Add custom stop sequences
const stopSequences = ['\n\nHuman:', '\n\nSystem:', '\n\nAssistant:']; const stopSequences = ['\n\nHuman:', '\n\nSystem:', '\n\nAssistant:'];
if (Array.isArray(request.body.stop)) { if (Array.isArray(request.body.stop)) {

View File

@ -77,8 +77,10 @@ function convertClaudePrompt(messages, addAssistantPostfix, addAssistantPrefill,
* @param {string} prefillString User determined prefill string * @param {string} prefillString User determined prefill string
* @param {boolean} useSysPrompt See if we want to use a system prompt * @param {boolean} useSysPrompt See if we want to use a system prompt
* @param {string} humanMsgFix Add Human message between system prompt and assistant. * @param {string} humanMsgFix Add Human message between system prompt and assistant.
* @param {string} charName Character name
* @param {string} userName User name
*/ */
function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFix) { function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFix, charName = '', userName = '') {
let systemPrompt = ''; let systemPrompt = '';
if (useSysPrompt) { 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. // Collect all the system messages up until the first instance of a non-system message, and then remove them from the messages array.
@ -87,6 +89,12 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi
if (messages[i].role !== 'system') { if (messages[i].role !== 'system') {
break; break;
} }
if (userName && messages[i].name === 'example_user') {
messages[i].content = `${userName}: ${messages[i].content}`;
}
if (charName && messages[i].name === 'example_assistant') {
messages[i].content = `${charName}: ${messages[i].content}`;
}
systemPrompt += `${messages[i].content}\n\n`; systemPrompt += `${messages[i].content}\n\n`;
} }