Elffuss BraidHub

kikocis/continue public

bdbad06b302d · 12.3 KiBRawPermalink
1// Fill in the middle prompts
2
3import { CompletionOptions } from "../../index.js";
4import {
5 getLastNUriRelativePathParts,
6 getShortestUniqueRelativeUriPaths,
7} from "../../util/uri.js";
8import {
9 AutocompleteCodeSnippet,
10 AutocompleteSnippet,
11 AutocompleteSnippetType,
12} from "../snippets/types.js";
13
14export interface AutocompleteTemplate {
15 compilePrefixSuffix?: (
16 prefix: string,
17 suffix: string,
18 filepath: string,
19 reponame: string,
20 snippets: AutocompleteSnippet[],
21 workspaceUris: string[],
22 ) => [string, string];
23 template:
24 | string
25 | ((
26 prefix: string,
27 suffix: string,
28 filepath: string,
29 reponame: string,
30 language: string,
31 snippets: AutocompleteSnippet[],
32 workspaceUris: string[],
33 ) => string);
34 completionOptions?: Partial<CompletionOptions>;
35}
36
37// https://huggingface.co/stabilityai/stable-code-3b
38const stableCodeFimTemplate: AutocompleteTemplate = {
39 template: "<fim_prefix>{{{prefix}}}<fim_suffix>{{{suffix}}}<fim_middle>",
40 completionOptions: {
41 stop: [
42 "<fim_prefix>",
43 "<fim_suffix>",
44 "<fim_middle>",
45 "<file_sep>",
46 "<|endoftext|>",
47 "</fim_middle>",
48 "</code>",
49 ],
50 },
51};
52
53// https://github.com/QwenLM/Qwen2.5-Coder?tab=readme-ov-file#3-file-level-code-completion-fill-in-the-middle
54// This issue asks about the use of <|repo_name|> and <|file_sep|> together with <|fim_prefix|>, <|fim_suffix|> and <|fim_middle|>
55// https://github.com/QwenLM/Qwen2.5-Coder/issues/343
56const qwenCoderFimTemplate: AutocompleteTemplate = {
57 template:
58 "<|fim_prefix|>{{{prefix}}}<|fim_suffix|>{{{suffix}}}<|fim_middle|>",
59 completionOptions: {
60 stop: [
61 "<|endoftext|>",
62 "<|fim_prefix|>",
63 "<|fim_middle|>",
64 "<|fim_suffix|>",
65 "<|fim_pad|>",
66 "<|repo_name|>",
67 "<|file_sep|>",
68 "<|im_start|>",
69 "<|im_end|>",
70 ],
71 },
72};
73
74const seedCoderFimTemplate: AutocompleteTemplate = {
75 template: "<[fim-prefix]>{{{prefix}}}<[fim-suffix]>{{{suffix}}}<[fim-middle]>",
76 completionOptions: {
77 stop: [
78 "<[end▁of▁sentence]>",
79 "<[fim-prefix]>",
80 "<[fim-middle]>",
81 "<[fim-suffix]>",
82 "<[PAD▁TOKEN]>",
83 "<[SEP▁TOKEN]>",
84 "<[begin▁of▁sentence]>",
85 ],
86 },
87};
88
89const codestralFimTemplate: AutocompleteTemplate = {
90 template: "[SUFFIX]{{{suffix}}}[PREFIX]{{{prefix}}}",
91 completionOptions: {
92 stop: ["[PREFIX]", "[SUFFIX]"],
93 },
94};
95
96const codestralMultifileFimTemplate: AutocompleteTemplate = {
97 compilePrefixSuffix: (
98 prefix,
99 suffix,
100 filepath,
101 reponame,
102 snippets,
103 workspaceUris,
104 ): [string, string] => {
105 function getFileName(snippet: { uri: string; uniquePath: string }) {
106 return snippet.uri.startsWith("file://")
107 ? snippet.uniquePath
108 : snippet.uri;
109 }
110
111 if (snippets.length === 0) {
112 if (suffix.trim().length === 0 && prefix.trim().length === 0) {
113 return [
114 `+++++ ${getLastNUriRelativePathParts(workspaceUris, filepath, 2)}\n${prefix}`,
115 suffix,
116 ];
117 }
118 return [prefix, suffix];
119 }
120
121 const relativePaths = getShortestUniqueRelativeUriPaths(
122 [
123 ...snippets.map((snippet) =>
124 "filepath" in snippet ? snippet.filepath : "file:///Untitled.txt",
125 ),
126 filepath,
127 ],
128 workspaceUris,
129 );
130
131 const otherFiles = snippets
132 .map((snippet, i) => {
133 if (snippet.type === AutocompleteSnippetType.Diff) {
134 return snippet.content;
135 }
136
137 return `+++++ ${getFileName(relativePaths[i])} \n${snippet.content}`;
138 })
139 .join("\n\n");
140
141 return [
142 `${otherFiles}\n\n+++++ ${getFileName(relativePaths[relativePaths.length - 1])}\n${prefix}`,
143 suffix,
144 ];
145 },
146 template: (prefix: string, suffix: string): string => {
147 return `[SUFFIX]${suffix}[PREFIX]${prefix}`;
148 },
149 completionOptions: {
150 stop: ["[PREFIX]", "[SUFFIX]", "\n+++++ "],
151 },
152};
153
154const mercuryMultifileFimTemplate: AutocompleteTemplate = {
155 compilePrefixSuffix: (
156 prefix,
157 suffix,
158 filepath,
159 reponame,
160 snippets,
161 workspaceUris,
162 ): [string, string] => {
163 function getFileName(snippet: { uri: string; uniquePath: string }) {
164 return snippet.uri.startsWith("file://")
165 ? snippet.uniquePath
166 : snippet.uri;
167 }
168
169 // Our current snippet format doesn't work well with mercury. We need to clean this up
170 snippets = [];
171
172 if (snippets.length === 0) {
173 if (suffix.trim().length === 0 && prefix.trim().length === 0) {
174 return [
175 `<|file_sep|>${getLastNUriRelativePathParts(workspaceUris, filepath, 2)}\n<|fim_prefix|>${prefix}`,
176 suffix,
177 ];
178 }
179 return [`<|fim_prefix|>${prefix}`, suffix];
180 }
181
182 const relativePaths = getShortestUniqueRelativeUriPaths(
183 [
184 ...snippets.map((snippet) =>
185 "filepath" in snippet ? snippet.filepath : "file:///Untitled.txt",
186 ),
187 filepath,
188 ],
189 workspaceUris,
190 );
191
192 const otherFiles = snippets
193 .map((snippet, i) => {
194 if (snippet.type === AutocompleteSnippetType.Diff) {
195 return snippet.content;
196 }
197
198 return `<|file_sep|>${getFileName(relativePaths[i])} \n${snippet.content}`;
199 })
200 .join("\n\n");
201
202 return [
203 `${otherFiles}${otherFiles ? "\n\n" : ""}<|file_sep|>${getFileName(relativePaths[relativePaths.length - 1])}\n<|fim_prefix|>${prefix}`,
204 suffix,
205 ];
206 },
207 template: (prefix: string, suffix: string): string => {
208 return `${prefix}<|fim_suffix|>${suffix}<|fim_middle|>`;
209 },
210};
211
212const codegemmaFimTemplate: AutocompleteTemplate = {
213 template:
214 "<|fim_prefix|>{{{prefix}}}<|fim_suffix|>{{{suffix}}}<|fim_middle|>",
215 completionOptions: {
216 stop: [
217 "<|fim_prefix|>",
218 "<|fim_suffix|>",
219 "<|fim_middle|>",
220 "<|file_separator|>",
221 "<end_of_turn>",
222 "<eos>",
223 ],
224 },
225};
226
227// https://arxiv.org/pdf/2402.19173.pdf section 5.1
228const starcoder2FimTemplate: AutocompleteTemplate = {
229 template: (
230 prefix,
231 suffix,
232 filename,
233 reponame,
234 language,
235 snippets,
236 workspaceUris,
237 ): string => {
238 const otherFiles =
239 snippets.length === 0
240 ? ""
241 : `<file_sep>${snippets
242 .map((snippet) => {
243 return snippet.content;
244 })
245 .join("<file_sep>")}<file_sep>`;
246
247 const prompt = `${otherFiles}<fim_prefix>${prefix}<fim_suffix>${suffix}<fim_middle>`;
248 return prompt;
249 },
250 completionOptions: {
251 stop: [
252 "<fim_prefix>",
253 "<fim_suffix>",
254 "<fim_middle>",
255 "<file_sep>",
256 "<|endoftext|>",
257 ],
258 },
259};
260
261const codeLlamaFimTemplate: AutocompleteTemplate = {
262 template: "<PRE> {{{prefix}}} <SUF>{{{suffix}}} <MID>",
263 completionOptions: { stop: ["<PRE>", "<SUF>", "<MID>", "<EOT>"] },
264};
265
266// https://huggingface.co/deepseek-ai/deepseek-coder-1.3b-base
267const deepseekFimTemplate: AutocompleteTemplate = {
268 template:
269 "<|fim▁begin|>{{{prefix}}}<|fim▁hole|>{{{suffix}}}<|fim▁end|>",
270 completionOptions: {
271 stop: [
272 "<|fim▁begin|>",
273 "<|fim▁hole|>",
274 "<|fim▁end|>",
275 "//",
276 "<|end▁of▁sentence|>",
277 ],
278 },
279};
280
281// https://github.com/THUDM/CodeGeeX4/blob/main/guides/Infilling_guideline.md
282const codegeexFimTemplate: AutocompleteTemplate = {
283 template: (
284 prefix,
285 suffix,
286 filepath,
287 reponame,
288 language,
289 allSnippets,
290 workspaceUris,
291 ): string => {
292 const snippets = allSnippets.filter(
293 (snippet) => snippet.type === AutocompleteSnippetType.Code,
294 ) as AutocompleteCodeSnippet[];
295
296 const relativePaths = getShortestUniqueRelativeUriPaths(
297 [...snippets.map((snippet) => snippet.filepath), filepath],
298 workspaceUris,
299 );
300 const baseTemplate = `###PATH:${
301 relativePaths[relativePaths.length - 1]
302 }\n###LANGUAGE:${language}\n###MODE:BLOCK\n<|code_suffix|>${suffix}<|code_prefix|>${prefix}<|code_middle|>`;
303 if (snippets.length === 0) {
304 return `<|user|>\n${baseTemplate}<|assistant|>\n`;
305 }
306 const references = `###REFERENCE:\n${snippets
307 .map((snippet, i) => `###PATH:${relativePaths[i]}\n${snippet.content}\n`)
308 .join("###REFERENCE:\n")}`;
309 const prompt = `<|user|>\n${references}\n${baseTemplate}<|assistant|>\n`;
310 return prompt;
311 },
312 completionOptions: {
313 stop: [
314 "<|user|>",
315 "<|code_suffix|>",
316 "<|code_prefix|>",
317 "<|code_middle|>",
318 "<|assistant|>",
319 "<|endoftext|>",
320 ],
321 },
322};
323
324const gptAutocompleteTemplate: AutocompleteTemplate = {
325 template: `\`\`\`
326{{{prefix}}}[BLANK]{{{suffix}}}
327\`\`\`
328
329Fill in the blank to complete the code block. Your response should include only the code to replace [BLANK], without surrounding backticks.`,
330 completionOptions: { stop: ["\n"] },
331};
332
333const holeFillerTemplate: AutocompleteTemplate = {
334 template: (prefix: string, suffix: string) => {
335 // From https://github.com/VictorTaelin/AI-scripts
336 const SYSTEM_MSG = `You are a HOLE FILLER. You are provided with a file containing holes, formatted as '{{HOLE_NAME}}'. Your TASK is to complete with a string to replace this hole with, inside a <COMPLETION/> XML tag, including context-aware indentation, if needed. All completions MUST be truthful, accurate, well-written and correct.
337
338## EXAMPLE QUERY:
339
340<QUERY>
341function sum_evens(lim) {
342 var sum = 0;
343 for (var i = 0; i < lim; ++i) {
344 {{FILL_HERE}}
345 }
346 return sum;
347}
348</QUERY>
349
350TASK: Fill the {{FILL_HERE}} hole.
351
352## CORRECT COMPLETION
353
354<COMPLETION>if (i % 2 === 0) {
355 sum += i;
356 }</COMPLETION>
357
358## EXAMPLE QUERY:
359
360<QUERY>
361def sum_list(lst):
362 total = 0
363 for x in lst:
364 {{FILL_HERE}}
365 return total
366
367print sum_list([1, 2, 3])
368</QUERY>
369
370## CORRECT COMPLETION:
371
372<COMPLETION> total += x</COMPLETION>
373
374## EXAMPLE QUERY:
375
376<QUERY>
377// data Tree a = Node (Tree a) (Tree a) | Leaf a
378
379// sum :: Tree Int -> Int
380// sum (Node lft rgt) = sum lft + sum rgt
381// sum (Leaf val) = val
382
383// convert to TypeScript:
384{{FILL_HERE}}
385</QUERY>
386
387## CORRECT COMPLETION:
388
389<COMPLETION>type Tree<T>
390 = {$:"Node", lft: Tree<T>, rgt: Tree<T>}
391 | {$:"Leaf", val: T};
392
393function sum(tree: Tree<number>): number {
394 switch (tree.$) {
395 case "Node":
396 return sum(tree.lft) + sum(tree.rgt);
397 case "Leaf":
398 return tree.val;
399 }
400}</COMPLETION>
401
402## EXAMPLE QUERY:
403
404The 5th {{FILL_HERE}} is Jupiter.
405
406## CORRECT COMPLETION:
407
408<COMPLETION>planet from the Sun</COMPLETION>
409
410## EXAMPLE QUERY:
411
412function hypothenuse(a, b) {
413 return Math.sqrt({{FILL_HERE}}b ** 2);
414}
415
416## CORRECT COMPLETION:
417
418<COMPLETION>a ** 2 + </COMPLETION>`;
419
420 const fullPrompt =
421 SYSTEM_MSG +
422 `\n\n<QUERY>\n${prefix}{{FILL_HERE}}${suffix}\n</QUERY>\nTASK: Fill the {{FILL_HERE}} hole. Answer only with the CORRECT completion, and NOTHING ELSE. Do it now.\n<COMPLETION>`;
423 return fullPrompt;
424 },
425 completionOptions: {
426 stop: ["</COMPLETION>"],
427 },
428};
429
430export function getTemplateForModel(model: string): AutocompleteTemplate {
431 const lowerCaseModel = model.toLowerCase();
432
433 // if (lowerCaseModel.includes("starcoder2")) {
434 // return starcoder2FimTemplate;
435 // }
436 if (lowerCaseModel.includes("mercury")) {
437 return mercuryMultifileFimTemplate;
438 }
439
440 if (lowerCaseModel.includes("qwen") && lowerCaseModel.includes("coder")) {
441 return qwenCoderFimTemplate;
442 }
443
444 if (lowerCaseModel.includes("seed") && lowerCaseModel.includes("coder")) {
445 return seedCoderFimTemplate;
446 }
447
448 if (
449 lowerCaseModel.includes("starcoder") ||
450 lowerCaseModel.includes("star-coder") ||
451 lowerCaseModel.includes("starchat") ||
452 lowerCaseModel.includes("octocoder") ||
453 lowerCaseModel.includes("stable") ||
454 lowerCaseModel.includes("codeqwen") ||
455 lowerCaseModel.includes("qwen")
456 ) {
457 return stableCodeFimTemplate;
458 }
459
460 if (lowerCaseModel.includes("codestral")) {
461 return codestralMultifileFimTemplate;
462 }
463
464 if (lowerCaseModel.includes("codegemma")) {
465 return codegemmaFimTemplate;
466 }
467
468 if (lowerCaseModel.includes("codellama")) {
469 return codeLlamaFimTemplate;
470 }
471
472 if (lowerCaseModel.includes("deepseek")) {
473 return deepseekFimTemplate;
474 }
475
476 if (lowerCaseModel.includes("codegeex")) {
477 return codegeexFimTemplate;
478 }
479
480 if (
481 lowerCaseModel.includes("gpt") ||
482 lowerCaseModel.includes("davinci-002") ||
483 lowerCaseModel.includes("claude") ||
484 lowerCaseModel.includes("granite3") ||
485 lowerCaseModel.includes("granite-3")
486 ) {
487 return holeFillerTemplate;
488 }
489
490 return stableCodeFimTemplate;
491}