better gpt-4o Edit prompt

This commit is contained in:
Nate 2024-11-12 23:12:52 -08:00
parent 9895ed68c8
commit 7b732f7d32
5 changed files with 127 additions and 53 deletions

12
core/index.d.ts vendored
View file

@ -45,12 +45,12 @@ export interface IndexingProgressUpdate {
debugInfo?: string;
}
export type PromptTemplate =
| string
| ((
history: ChatMessage[],
otherData: Record<string, string>,
) => string | ChatMessage[]);
export type PromptTemplateFunction = (
history: ChatMessage[],
otherData: Record<string, string>,
) => string | ChatMessage[];
export type PromptTemplate = string | PromptTemplateFunction;
export interface ILLM extends LLMOptions {
get providerName(): ModelProvider;

View file

@ -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}}<FILL>{{fileSuffix}}";
const START_TAG = "<START EDITING HERE>";
@ -76,7 +34,7 @@ const osModelsEditPrompt: PromptTemplate = (history, otherData) => {
!firstCharOfFirstLine;
const suffixTag = isSuffix ? "<STOP EDITING HERE>" : "";
const suffixExplanation = isSuffix
? " When you get to \"<STOP EDITING HERE>\", end your response."
? ' When you get to "<STOP EDITING HERE>", 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

View file

@ -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");
};

View file

@ -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");
};

View file

@ -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 };