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