vicuna.mjs example formatted for Vicuna-1.1
Tries to be as card-agnostic as possible but check the reply instruction anyway. Try with it on or off. I found that final instruction harmed output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | import {
addStoppingStrings,
getLastChatMessage,
popLastAssistantMessage,
popLastChatMessages,
replaceTemplates,
} from "../src/utils.mjs";
export default ({ user, assistant, messages, config, generationConfig }) => {
addStoppingStrings(config, ["\nUSER:", "\nASSISTANT:"]);
const ooc = "";
const systemPrompt =
"";
const newConversation = `USER: ${ooc}Let's start a new roleplay.`;
const newExample = `USER: ${ooc}Here are some old roleplays that we did before.`;
const context = `USER: `;//`USER: ${ooc}I'm roleplaying as ${user} and you're roleplaying as ${assistant}. Here's the context for this roleplay:\n`;
const contextResponse = `${ooc}Okay. I will take that info into account to roleplay as ${assistant}.`;
const characterBias = replaceTemplates(config.characterBias, config);
const impersonationPrompt = replaceTemplates(
config.impersonationPrompt,
config
);
const silentMessage = replaceTemplates(config.silentMessage, config);
let impersonationPromptFound = false;
const addNames = false;
const userName = () => (addNames ? `${user}: ` : "");
const assistantName = () => (addNames ? `${assistant}: ` : "");
const beforeSystem = "\n\n";
const afterSystem = "\n";
const beforeUser = "\n\nUSER: ";
const afterUser = "\n";
const beforeAssistant = "\n\nASSISTANT: ";
const afterAssistant = config.backendType === "koboldcpp" ? "\n" : "</s>\n";
const addReplyInstruction = true;
const addFinalInstruction = false;
const replyInstruction = ({
you,
}) => `${beforeUser}${ooc}Write a continuation for this roleplay, follow these rules:
- The plot is developed slowly.
- Flesh out every scene with copious detail, narration, and dialogue.
- Never speak or act for ${user}.${afterUser}${beforeAssistant}${ooc}Okay. I will follow these rules and the description above. The most engaging, descriptive and creative continuation for this roleplay is this:${afterAssistant}`;
const finalInstruction = ({ you }) =>
`${beforeUser}${ooc}Continue the roleplay. Stay in character and write at least two paragraphs.${afterUser}`;
let prompt = [];
if (systemPrompt) {
prompt.push({
role: "system",
metadata: { type: "system-prompt" },
prunable: false,
content: `${beforeSystem}${systemPrompt}${afterSystem}`,
});
}
for (const msg of messages) {
const { metadata } = msg;
let content = msg.content.trim();
if (metadata.type === "new-conversation") {
if (newConversation) {
prompt.push({
...msg,
prunable: false,
content: `${beforeSystem}${newConversation}${afterSystem}`,
});
}
} else if (metadata.type === "new-example-dialogue") {
if (newExample && metadata.chatIndex === 0) {
prompt.push({
...msg,
prunable: false,
content: `${beforeSystem}${newExample}${afterSystem}`,
});
}
} else if (metadata.type === "context") {
prompt.push({
...msg,
prunable: false,
content: `${beforeSystem}${context}${content}${afterSystem}`,
});
if (contextResponse) {
prompt.push({
role: "assistant",
metadata: { type: "context-response" },
prunable: false,
content: `${beforeAssistant}${contextResponse}${afterAssistant}`,
});
}
} else if (metadata.type === "example-assistant") {
prompt.push({
...msg,
prunable: !(
config.keepExampleMessagesInPrompt ||
metadata.exampleAssistantMsgIndex === 0
),
content: `${beforeAssistant}${assistantName()}${content}${afterAssistant}`,
});
} else if (metadata.type === "example-user") {
prompt.push({
...msg,
prunable: !config.keepExampleMessagesInPrompt,
content: `${beforeUser}${userName()}${content}${afterUser}`,
});
} else if (metadata.type === "other" || metadata.type === "jailbreak") {
prompt.push({
...msg,
prunable: false,
content: `${beforeSystem}${content}${afterSystem}`,
});
} else if (metadata.type === "impersonation-prompt") {
impersonationPromptFound = true;
} else if (metadata.type === "assistant-msg") {
prompt.push({
...msg,
prunable: true,
content: `${beforeAssistant}${assistantName()}${content}${afterAssistant}`,
});
} else if (metadata.type === "user-msg") {
prompt.push({
...msg,
prunable: true,
content: `${beforeUser}${userName()}${content}${afterUser}`,
});
}
}
const last = getLastChatMessage(prompt);
const lastMessages = popLastChatMessages(prompt, 2);
const you = impersonationPromptFound ? user : assistant;
prompt.push({
role: "system",
metadata: { type: "superbig-injection-point" },
prunable: true,
content: "",
});
if (addReplyInstruction) {
prompt.push({
role: "system",
metadata: { type: "reply-instruction" },
prunable: false,
content: replyInstruction({ you }),
});
}
for (const msg of lastMessages) {
prompt.push(msg);
}
if (impersonationPromptFound || last?.role === "user" || silentMessage) {
if (last?.role === "assistant" && silentMessage) {
prompt.push({
role: "user",
metadata: { type: "silent-message" },
prunable: false,
content: `${beforeUser}${userName()}${silentMessage}${afterUser}`,
});
}
if (impersonationPromptFound) {
prompt.push({
role: "system",
metadata: { type: "impersonation-prompt" },
prunable: false,
content: `${beforeSystem}${impersonationPrompt}${afterSystem}`,
});
}
if (addFinalInstruction) {
prompt.push({
role: "system",
metadata: { type: "reply-instruction" },
prunable: false,
content: finalInstruction({ you }),
});
}
prompt.push({
role: impersonationPromptFound ? "user" : "assistant",
metadata: { type: "reply-to-complete" },
prunable: false,
content:
`${impersonationPromptFound ? beforeUser : beforeAssistant}${
impersonationPromptFound ? userName() : assistantName()
}`.trimRight() + characterBias,
});
} else {
const msg = popLastAssistantMessage(prompt);
const end = msg.content.length - afterAssistant.length;
msg.content = msg.content.substring(0, end);
prompt.push(msg);
}
if (impersonationPromptFound) {
generationConfig.max_new_tokens = config.impersonationMaxNewTokens;
}
return prompt;
};
|