From 7b732f7d32b75cb5f4e193437cc3b4ce85168fd5 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 12 Nov 2024 23:12:52 -0800 Subject: [PATCH] better gpt-4o Edit prompt --- core/index.d.ts | 12 ++--- core/llm/templates/edit.ts | 46 +------------------ core/llm/templates/edit/claude.ts | 44 ++++++++++++++++++ core/llm/templates/edit/gpt.ts | 71 ++++++++++++++++++++++++++++++ extensions/vscode/src/VsCodeIde.ts | 7 +-- 5 files changed, 127 insertions(+), 53 deletions(-) create mode 100644 core/llm/templates/edit/claude.ts create mode 100644 core/llm/templates/edit/gpt.ts diff --git a/core/index.d.ts b/core/index.d.ts index b8e941f28..1bad9fd74 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -45,12 +45,12 @@ export interface IndexingProgressUpdate { debugInfo?: string; } -export type PromptTemplate = - | string - | (( - history: ChatMessage[], - otherData: Record, - ) => string | ChatMessage[]); +export type PromptTemplateFunction = ( + history: ChatMessage[], + otherData: Record, +) => string | ChatMessage[]; + +export type PromptTemplate = string | PromptTemplateFunction; export interface ILLM extends LLMOptions { get providerName(): ModelProvider; diff --git a/core/llm/templates/edit.ts b/core/llm/templates/edit.ts index ce0c0dc75..e6ad4d6b6 100644 --- a/core/llm/templates/edit.ts +++ b/core/llm/templates/edit.ts @@ -1,4 +1,5 @@ import { ChatMessage, PromptTemplate } from "../../index.js"; +import { gptEditPrompt } from "./edit/gpt.js"; const simplifiedEditPrompt = `Consider the following code: \`\`\`{{{language}}} @@ -18,49 +19,6 @@ Here is the edit requested: Here is the code after editing:`; -const gptEditPrompt: PromptTemplate = (_, otherData) => { - if (otherData?.codeToEdit?.trim().length === 0) { - return `\ -\`\`\`${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 paragraphs = [ - "The user has requested a section of code in a file to be rewritten.", - ]; - if (otherData.prefix?.trim().length > 0) { - paragraphs.push(`This is the prefix of the file: -\`\`\`${otherData.language} -${otherData.prefix} -\`\`\``); - } - - if (otherData.suffix?.trim().length > 0) { - paragraphs.push(`This is the suffix of the file: -\`\`\`${otherData.language} -${otherData.suffix} -\`\`\``); - } - - paragraphs.push(`This is the code to rewrite: -\`\`\`${otherData.language} -${otherData.codeToEdit} -\`\`\` - -The user's request is: "${otherData.userInput}" - -Here is the rewritten code:`); - - return paragraphs.join("\n\n"); -}; - const codellamaInfillEditPrompt = "{{filePrefix}}{{fileSuffix}}"; const START_TAG = ""; @@ -76,7 +34,7 @@ const osModelsEditPrompt: PromptTemplate = (history, otherData) => { !firstCharOfFirstLine; const suffixTag = isSuffix ? "" : ""; const suffixExplanation = isSuffix - ? " When you get to \"\", end your response." + ? ' When you get to "", end your response.' : ""; // If neither prefilling nor /v1/completions are supported, we have to use a chat prompt without putting words in the model's mouth diff --git a/core/llm/templates/edit/claude.ts b/core/llm/templates/edit/claude.ts new file mode 100644 index 000000000..5ec103fa9 --- /dev/null +++ b/core/llm/templates/edit/claude.ts @@ -0,0 +1,44 @@ +import { PromptTemplateFunction } from "../../.."; + +export const claudeEditPrompt: PromptTemplateFunction = (_, otherData) => { + if (otherData?.codeToEdit?.trim().length === 0) { + return `\ +\`\`\`${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 paragraphs = [ + "The user has requested a section of code in a file to be rewritten.", + ]; + if (otherData.prefix?.trim().length > 0) { + paragraphs.push(`This is the prefix of the file: +\`\`\`${otherData.language} +${otherData.prefix} +\`\`\``); + } + + if (otherData.suffix?.trim().length > 0) { + paragraphs.push(`This is the suffix of the file: +\`\`\`${otherData.language} +${otherData.suffix} +\`\`\``); + } + + paragraphs.push(`This is the code to rewrite: +\`\`\`${otherData.language} +${otherData.codeToEdit} +\`\`\` + +The user's request is: "${otherData.userInput}" + +Here is the rewritten code:`); + + return paragraphs.join("\n\n"); +}; diff --git a/core/llm/templates/edit/gpt.ts b/core/llm/templates/edit/gpt.ts new file mode 100644 index 000000000..4dcc39523 --- /dev/null +++ b/core/llm/templates/edit/gpt.ts @@ -0,0 +1,71 @@ +import { PromptTemplateFunction } from "../../.."; +import { dedent } from "../../../util"; + +const gptInsertionEditPrompt: 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 gptFullFileEditPrompt: 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 gptEditPrompt: PromptTemplateFunction = (history, otherData) => { + if (otherData?.codeToEdit?.trim().length === 0) { + return gptInsertionEditPrompt(history, otherData); + } else if ( + otherData?.prefix?.trim().length === 0 && + otherData?.suffix?.trim().length === 0 + ) { + return gptFullFileEditPrompt(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 rewritten code:`); + + return paragraphs.join("\n\n"); +}; diff --git a/extensions/vscode/src/VsCodeIde.ts b/extensions/vscode/src/VsCodeIde.ts index 980eb7ab4..217f33e6d 100644 --- a/extensions/vscode/src/VsCodeIde.ts +++ b/extensions/vscode/src/VsCodeIde.ts @@ -489,11 +489,13 @@ class VsCodeIde implements IDE { } async getCurrentFile() { - if (!vscode.window.activeTextEditor) {return undefined;} + if (!vscode.window.activeTextEditor) { + return undefined; + } return { isUntitled: vscode.window.activeTextEditor.document.isUntitled, path: vscode.window.activeTextEditor.document.uri.fsPath, - contents: vscode.window.activeTextEditor.document.getText() + contents: vscode.window.activeTextEditor.document.getText(), }; } @@ -629,4 +631,3 @@ class VsCodeIde implements IDE { } export { VsCodeIde }; -