From ebf7a1da5167c3767fba2fc6cdde5e6e53fe0542 Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 15 Dec 2024 12:33:22 -0800 Subject: [PATCH] improve codestral Edit prompt --- core/llm/llms/Mistral.ts | 4 +- core/llm/templates/edit/codestral.ts | 74 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 core/llm/templates/edit/codestral.ts diff --git a/core/llm/llms/Mistral.ts b/core/llm/llms/Mistral.ts index b723016fe..124957a41 100644 --- a/core/llm/llms/Mistral.ts +++ b/core/llm/llms/Mistral.ts @@ -1,5 +1,5 @@ import { ChatMessage, LLMOptions } from "../../index.js"; -import { gptEditPrompt } from "../templates/edit.js"; +import { codestralEditPrompt } from "../templates/edit/codestral.js"; import OpenAI from "./OpenAI.js"; @@ -9,7 +9,7 @@ class Mistral extends OpenAI { apiBase: "https://api.mistral.ai/v1/", model: "codestral-latest", promptTemplates: { - edit: gptEditPrompt, + edit: codestralEditPrompt, }, maxEmbeddingBatchSize: 128, }; diff --git a/core/llm/templates/edit/codestral.ts b/core/llm/templates/edit/codestral.ts new file mode 100644 index 000000000..5234ac6c6 --- /dev/null +++ b/core/llm/templates/edit/codestral.ts @@ -0,0 +1,74 @@ +import { PromptTemplateFunction } from "../../.."; +import { dedent } from "../../../util"; + +const codestralInsertionEditPrompt: PromptTemplateFunction = (_, otherData) => { + return dedent` + \`\`\`${otherData.language} + ${otherData.prefix}[BLANK]${otherData.codeToEdit}${otherData.suffix} + \`\`\` + + Above is the file of code that the user is currently editing in. Their cursor is located at the "[BLANK]". They have requested that you fill in the "[BLANK]" with code that satisfies the following request: + + "${otherData.userInput}" + + Please generate this code. Your output will be only the code that should replace the "[BLANK]", without repeating any of the prefix or suffix, without any natural language explanation, and without messing up indentation. Here is the code that will replace the "[BLANK]":`; +}; + +const codestralFullFileEditPrompt: PromptTemplateFunction = (_, otherData) => { + return dedent` + \`\`\`${otherData.language} + ${otherData.codeToEdit} + \`\`\` + + Please rewrite the above file to address the following request: + + ${otherData.userInput} + + You should rewrite the entire file without any natural language explanation. DO NOT surround the code in a code block and DO NOT explain yourself.`; +}; + +export const codestralEditPrompt: PromptTemplateFunction = ( + history, + otherData, +) => { + if (otherData?.codeToEdit?.trim().length === 0) { + return codestralInsertionEditPrompt(history, otherData); + } else if ( + otherData?.prefix?.trim().length === 0 && + otherData?.suffix?.trim().length === 0 + ) { + return codestralFullFileEditPrompt(history, otherData); + } + + const paragraphs = [ + "The user has requested a section of code in a file to be rewritten.", + ]; + + if (otherData.prefix?.trim().length > 0) { + paragraphs.push(dedent` + This is the prefix of the file: + \`\`\`${otherData.language} + ${otherData.prefix} + \`\`\``); + } + + if (otherData.suffix?.trim().length > 0) { + paragraphs.push(dedent` + This is the suffix of the file: + \`\`\`${otherData.language} + ${otherData.suffix} + \`\`\``); + } + + paragraphs.push(dedent` + This is the "code to rewrite": + \`\`\`${otherData.language} + ${otherData.codeToEdit} + \`\`\` + + The user's request is: "${otherData.userInput}" + + Here is the new version of "code to rewrite". Make sure not to rewrite any of the prefix or suffix:`); + + return paragraphs.join("\n\n"); +};