Elffuss BraidHub

kikocis/continue public

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