System prompt for Claude 2

This commit is contained in:
Cohee 2023-11-21 22:11:26 +02:00
parent 5f77b2f816
commit df4ed389bf
2 changed files with 20 additions and 2 deletions

View File

@ -3043,7 +3043,8 @@ async function sendClaudeRequest(request, response) {
controller.abort();
});
let requestPrompt = convertClaudePrompt(request.body.messages, true, !request.body.exclude_assistant);
let doSystemPrompt = request.body.model === 'claude-2' || request.body.model === 'claude-2.1';
let requestPrompt = convertClaudePrompt(request.body.messages, true, !request.body.exclude_assistant, doSystemPrompt);
if (request.body.assistant_prefill && !request.body.exclude_assistant) {
requestPrompt += request.body.assistant_prefill;

View File

@ -3,10 +3,11 @@
* @param {object[]} messages Array of messages
* @param {boolean} addHumanPrefix Add Human prefix
* @param {boolean} addAssistantPostfix Add Assistant postfix
* @param {boolean} withSystemPrompt Build system prompt before "\n\nHuman: "
* @returns {string} Prompt for Claude
* @copyright Prompt Conversion script taken from RisuAI by kwaroran (GPLv3).
*/
function convertClaudePrompt(messages, addHumanPrefix, addAssistantPostfix) {
function convertClaudePrompt(messages, addHumanPrefix, addAssistantPostfix, withSystemPrompt) {
// Claude doesn't support message names, so we'll just add them to the message content.
for (const message of messages) {
if (message.name && message.role !== "system") {
@ -15,6 +16,18 @@ function convertClaudePrompt(messages, addHumanPrefix, addAssistantPostfix) {
}
}
let systemPrompt = '';
if (withSystemPrompt) {
for (const message of messages) {
if (message.role === "system" && !message.name) {
systemPrompt += message.content + '\n\n';
messages.splice(messages.indexOf(message), 1);
} else {
break;
}
}
}
let requestPrompt = messages.map((v) => {
let prefix = '';
switch (v.role) {
@ -46,6 +59,10 @@ function convertClaudePrompt(messages, addHumanPrefix, addAssistantPostfix) {
requestPrompt = requestPrompt + '\n\nAssistant: ';
}
if (withSystemPrompt) {
requestPrompt = systemPrompt + requestPrompt;
}
return requestPrompt;
}