Elffuss BraidHub

kikocis/continue public

main · 8.5 KiBRawPermalink
1import { ChatMessage } from "../../index.js";
2import { renderChatMessage } from "../../util/messageContent.js";
3
4function templateFactory(
5 systemMessage: (msg: ChatMessage) => string,
6 userPrompt: string,
7 assistantPrompt: string,
8 separator: string,
9 prefix?: string,
10 emptySystemMessage?: string,
11): (msgs: ChatMessage[]) => string {
12 return (msgs: ChatMessage[]) => {
13 let prompt = prefix ?? "";
14
15 // Skip assistant messages at the beginning
16 while (msgs.length > 0 && msgs[0].role === "assistant") {
17 msgs.shift();
18 }
19
20 if (msgs.length > 0 && msgs[0].role === "system") {
21 prompt += systemMessage(msgs.shift()!);
22 } else if (emptySystemMessage) {
23 prompt += emptySystemMessage;
24 }
25
26 for (let i = 0; i < msgs.length; i++) {
27 const msg = msgs[i];
28 prompt += msg.role === "user" ? userPrompt : assistantPrompt;
29 prompt += msg.content;
30 if (i < msgs.length - 1) {
31 prompt += separator;
32 }
33 }
34
35 if (msgs.length > 0 && msgs[msgs.length - 1].role === "user") {
36 prompt += separator;
37 prompt += assistantPrompt;
38 }
39
40 return prompt;
41 };
42}
43
44/**
45 * @description Template for LLAMA2 messages:
46 *
47 * <s>[INST] <<SYS>>
48 * {{ system_prompt }}
49 * <</SYS>>
50 *
51 * {{ user_msg_1 }} [/INST] {{ model_answer_1 }} </s><s>[INST] {{ user_msg_2 }} [/INST] {{ model_answer_2 }} </s><s>[INST] {{ user_msg_3 }} [/INST]
52 */
53function llama2TemplateMessages(msgs: ChatMessage[]): string {
54 if (msgs.length === 0) {
55 return "";
56 }
57
58 if (msgs[0].role === "assistant") {
59 // These models aren't trained to handle assistant message coming first,
60 // and typically these are just introduction messages from Continue
61 msgs.shift();
62 }
63
64 let prompt = "";
65 let hasSystem = msgs[0].role === "system";
66
67 if (hasSystem && renderChatMessage(msgs[0]).trim() === "") {
68 hasSystem = false;
69 msgs = msgs.slice(1);
70 }
71
72 if (hasSystem) {
73 const systemMessage = `<<SYS>>\n ${msgs[0].content}\n<</SYS>>\n\n`;
74 if (msgs.length > 1) {
75 prompt += `<s>[INST] ${systemMessage} ${msgs[1].content} [/INST]`;
76 } else {
77 prompt += `[INST] ${systemMessage} [/INST]`;
78 return prompt;
79 }
80 }
81
82 for (let i = hasSystem ? 2 : 0; i < msgs.length; i++) {
83 if (msgs[i].role === "user") {
84 prompt += `[INST] ${msgs[i].content} [/INST]`;
85 } else {
86 prompt += msgs[i].content;
87 if (i < msgs.length - 1) {
88 prompt += "</s>\n<s>";
89 }
90 }
91 }
92
93 return prompt;
94}
95
96// Llama2 template with added \n to prevent Codestral from continuing user message
97function codestralTemplateMessages(msgs: ChatMessage[]): string {
98 let template = llama2TemplateMessages(msgs);
99 if (template.length === 0) {
100 return template;
101 }
102 return template + "\n";
103}
104
105function anthropicTemplateMessages(messages: ChatMessage[]): string {
106 const HUMAN_PROMPT = "\n\nHuman:";
107 const AI_PROMPT = "\n\nAssistant:";
108 let prompt = "";
109
110 // Anthropic prompt must start with a Human turn
111 if (
112 messages.length > 0 &&
113 messages[0].role !== "user" &&
114 messages[0].role !== "system"
115 ) {
116 prompt += `${HUMAN_PROMPT} Hello.`;
117 }
118 for (const msg of messages) {
119 prompt += `${
120 msg.role === "user" || msg.role === "system" ? HUMAN_PROMPT : AI_PROMPT
121 } ${msg.content} `;
122 }
123
124 prompt += AI_PROMPT;
125 return prompt;
126}
127
128`A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.
129USER: <image>{prompt}
130ASSISTANT:`;
131const llavaTemplateMessages = templateFactory(
132 () => "",
133 "USER: <image>",
134 "ASSISTANT: ",
135 "\n",
136 "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.",
137);
138
139const zephyrTemplateMessages = templateFactory(
140 (msg) => `<|system|>${msg.content}</s>\n`,
141 "<|user|>\n",
142 "<|assistant|>\n",
143 "</s>\n",
144 undefined,
145 "<|system|> </s>\n",
146);
147
148const chatmlTemplateMessages = templateFactory(
149 (msg) => `<|im_start|>${msg.role}\n${msg.content}<|im_end|>\n`,
150 "<|im_start|>user\n",
151 "<|im_start|>assistant\n",
152 "<|im_end|>\n",
153);
154
155const templateAlpacaMessages = templateFactory(
156 (msg) => `${msg.content}\n\n`,
157 "### Instruction:\n",
158 "### Response:\n",
159 "\n\n",
160 undefined,
161 "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n",
162);
163
164function deepseekTemplateMessages(msgs: ChatMessage[]): string {
165 let prompt = "";
166 let system: string | null = null;
167 prompt +=
168 "You are an AI programming assistant, utilizing the DeepSeek Coder model, developed by DeepSeek Company, and your role is to assist with questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will not answer.\n";
169 if (msgs[0].role === "system") {
170 system = renderChatMessage(msgs.shift()!);
171 }
172
173 for (let i = 0; i < msgs.length; i++) {
174 const msg = msgs[i];
175 prompt += msg.role === "user" ? "### Instruction:\n" : "### Response:\n";
176
177 if (system && msg.role === "user" && i === msgs.length - 1) {
178 prompt += `${system}\n`;
179 }
180
181 prompt += `${msg.content}`;
182
183 if (i < msgs.length - 1) {
184 prompt += msg.role === "user" ? "\n" : "<|EOT|>\n";
185 }
186 }
187
188 if (msgs.length > 0 && msgs[msgs.length - 1].role === "user") {
189 prompt += "\n";
190 prompt += "### Response:\n";
191 }
192
193 return prompt;
194}
195
196// See https://huggingface.co/microsoft/phi-2#qa-format
197const phi2TemplateMessages = templateFactory(
198 (msg) => `\n\nInstruct: ${msg.content} `,
199 "\n\nInstruct: ",
200 "\n\nOutput: ",
201 " ",
202);
203
204const phindTemplateMessages = templateFactory(
205 (msg) => `### System Prompt\n${msg.content}\n\n`,
206 "### User Message\n",
207 "### Assistant\n",
208 "\n",
209);
210
211/**
212 * OpenChat Template, used by CodeNinja
213 * GPT4 Correct User: Hello<|end_of_turn|>GPT4 Correct Assistant: Hi<|end_of_turn|>GPT4 Correct User: How are you today?<|end_of_turn|>GPT4 Correct Assistant:
214 */
215const openchatTemplateMessages = templateFactory(
216 () => "",
217 "GPT4 Correct User: ",
218 "GPT4 Correct Assistant: ",
219 "<|end_of_turn|>",
220);
221
222/**
223 * Chat template used by https://huggingface.co/TheBloke/XwinCoder-13B-GPTQ
224 *
225
226<system>: You are an AI coding assistant that helps people with programming. Write a response that appropriately completes the user's request.
227<user>: {prompt}
228<AI>:
229 */
230const xWinCoderTemplateMessages = templateFactory(
231 (msg) => `<system>: ${msg.content}`,
232 "\n<user>: ",
233 "\n<AI>: ",
234 "",
235 undefined,
236 "<system>: You are an AI coding assistant that helps people with programming. Write a response that appropriately completes the user's request.",
237);
238
239/**
240 * NeuralChat Template
241 * ### System:\n{system_input}\n### User:\n{user_input}\n### Assistant:\n
242 */
243const neuralChatTemplateMessages = templateFactory(
244 (msg) => `### System:\n${msg.content}\n`,
245 "### User:\n",
246 "### Assistant:\n",
247 "\n",
248);
249
250/**
251'<s>Source: system\n\n System prompt <step> Source: user\n\n First user query <step> Source: assistant\n\n Model response to first query <step> Source: user\n\n Second user query <step> Source: assistant\nDestination: user\n\n '
252 */
253function codeLlama70bTemplateMessages(msgs: ChatMessage[]): string {
254 let prompt = "<s>";
255
256 for (const msg of msgs) {
257 prompt += `Source: ${msg.role}\n\n ${renderChatMessage(msg).trim()}`;
258 prompt += " <step> ";
259 }
260
261 prompt += "Source: assistant\nDestination: user\n\n";
262
263 return prompt;
264}
265
266const llama3TemplateMessages = templateFactory(
267 (msg: ChatMessage) =>
268 `<|begin_of_text|><|start_header_id|>${msg.role}<|end_header_id|>\n${msg.content}<|eot_id|>\n`,
269 "<|start_header_id|>user<|end_header_id|>\n",
270 "<|start_header_id|>assistant<|end_header_id|>\n",
271 "<|eot_id|>",
272);
273
274/**
275 <start_of_turn>user
276 What is Cramer's Rule?<end_of_turn>
277 <start_of_turn>model
278 */
279const gemmaTemplateMessage = templateFactory(
280 () => "",
281 "<start_of_turn>user\n",
282 "<start_of_turn>model\n",
283 "<end_of_turn>\n",
284);
285
286const graniteTemplateMessages = templateFactory(
287 (msg) => (!!msg ? `\n\nSystem:\n ${msg.content}\n\n` : ""),
288 "Question:\n",
289 "Answer:\n",
290 "\n\n",
291 "",
292 "",
293);
294
295export {
296 anthropicTemplateMessages,
297 chatmlTemplateMessages,
298 codeLlama70bTemplateMessages,
299 deepseekTemplateMessages,
300 gemmaTemplateMessage,
301 graniteTemplateMessages,
302 llama2TemplateMessages,
303 llama3TemplateMessages,
304 llavaTemplateMessages,
305 neuralChatTemplateMessages,
306 openchatTemplateMessages,
307 phi2TemplateMessages,
308 phindTemplateMessages,
309 templateAlpacaMessages,
310 xWinCoderTemplateMessages,
311 zephyrTemplateMessages,
312 codestralTemplateMessages,
313};