refactor constructPrompt

This commit is contained in:
Nate 2024-10-31 15:54:29 -07:00
parent d6e54c3932
commit 40dfede087
8 changed files with 85 additions and 60 deletions

View file

@ -1,6 +1,8 @@
import { IDE, TabAutocompleteOptions } from "..";
import { AutocompleteLanguageInfo } from "./constants/AutocompleteLanguageInfo";
import { languageForFilepath } from "./constructPrompt";
import {
AutocompleteLanguageInfo,
languageForFilepath,
} from "./constants/AutocompleteLanguageInfo";
import { constructInitialPrefixSuffix } from "./templating/constructPrefixSuffix";
import { AutocompleteInput } from "./types";
import { AstPath, getAst, getTreePathAtCursor } from "./util/ast";
@ -71,6 +73,9 @@ export class HelperVars {
get pos() {
return this.input.pos;
}
get maxSnippetTokens() {
return this.options.maxPromptTokens * this.options.maxSnippetPercentage;
}
// Getters for lazy access
get fileContents(): string {

View file

@ -346,3 +346,9 @@ export const LANGUAGES: { [extension: string]: AutocompleteLanguageInfo } = {
yml: YAML,
md: Markdown,
};
export function languageForFilepath(
filepath: string,
): AutocompleteLanguageInfo {
return LANGUAGES[filepath.split(".").slice(-1)[0]] || Typescript;
}

View file

@ -1,40 +1,20 @@
import { Position, Range } from "../index.js";
import {
countTokens,
pruneLinesFromBottom,
pruneLinesFromTop,
} from "../llm/countTokens.js";
import { shouldCompleteMultiline } from "./classification/shouldCompleteMultiline.js";
import {
AutocompleteLanguageInfo,
LANGUAGES,
Typescript,
} from "./constants/AutocompleteLanguageInfo.js";
import { ContextRetrievalService } from "./context/ContextRetrievalService.js";
import {
fillPromptWithSnippets,
rankSnippets,
rankAndOrderSnippets,
removeRangeFromSnippets,
type AutocompleteSnippet,
} from "./context/ranking/index.js";
import { HelperVars } from "./HelperVars.js";
export function languageForFilepath(
filepath: string,
): AutocompleteLanguageInfo {
return LANGUAGES[filepath.split(".").slice(-1)[0]] || Typescript;
}
export async function constructAutocompletePrompt(
helper: HelperVars,
extraSnippets: AutocompleteSnippet[],
contextRetrievalService: ContextRetrievalService,
): Promise<{
prefix: string;
suffix: string;
useFim: boolean;
completeMultiline: boolean;
snippets: AutocompleteSnippet[];
}> {
function prunePrefixSuffix(helper: HelperVars) {
// Construct basic prefix
const maxPrefixTokens =
helper.options.maxPromptTokens * helper.options.prefixPercentage;
@ -56,46 +36,78 @@ export async function constructAutocompletePrompt(
helper.modelName,
);
return {
prunedPrefix,
prunedSuffix,
};
}
function filterSnippetsAlreadyInCaretWindow(
snippets: AutocompleteSnippet[],
caretWindow: string,
): AutocompleteSnippet[] {
return snippets
.map((snippet) => ({ ...snippet }))
.filter(
(s) =>
s.contents.trim() !== "" && !caretWindow.includes(s.contents.trim()),
);
}
function getRangeOfPrefixAndSuffixWithBuffer(
prefix: string,
suffix: string,
caretPos: Position,
): Range {
// Remove prefix range from snippets
const prefixLines = prefix.split("\n").length;
const suffixLines = suffix.split("\n").length;
const buffer = 8;
const prefixSuffixRangeWithBuffer = {
start: {
line: caretPos.line - prefixLines - buffer,
character: 0,
},
end: {
line: caretPos.line + suffixLines + buffer,
character: 0,
},
};
return prefixSuffixRangeWithBuffer;
}
export async function constructAutocompletePrompt(
helper: HelperVars,
extraSnippets: AutocompleteSnippet[],
contextRetrievalService: ContextRetrievalService,
): Promise<{
prefix: string;
suffix: string;
useFim: boolean;
completeMultiline: boolean;
snippets: AutocompleteSnippet[];
}> {
// Prune prefix/suffix based on token budgets
const { prunedPrefix, prunedSuffix } = prunePrefixSuffix(helper);
let snippets = await contextRetrievalService.retrieve(
prunedPrefix,
helper,
extraSnippets,
);
// Filter out empty snippets and ones that are already in the prefix/suffix
snippets = snippets
.map((snippet) => ({ ...snippet }))
.filter(
(s) =>
s.contents.trim() !== "" &&
!(prunedPrefix + prunedSuffix).includes(s.contents.trim()),
);
snippets = filterSnippetsAlreadyInCaretWindow(
snippets,
prunedPrefix + prunedSuffix,
);
// Rank / order the snippets
const scoredSnippets = rankSnippets(snippets, helper);
const scoredSnippets = rankAndOrderSnippets(snippets, helper);
// Fill maxSnippetTokens with snippets
const maxSnippetTokens =
helper.options.maxPromptTokens * helper.options.maxSnippetPercentage;
// Remove prefix range from snippets
const prefixLines = prunedPrefix.split("\n").length;
const suffixLines = prunedSuffix.split("\n").length;
const buffer = 8;
const prefixSuffixRangeWithBuffer = {
start: {
line: helper.pos.line - prefixLines - buffer,
character: 0,
},
end: {
line: helper.pos.line + suffixLines + buffer,
character: 0,
},
};
let finalSnippets = removeRangeFromSnippets(
scoredSnippets,
helper.filepath.split("://").slice(-1)[0],
prefixSuffixRangeWithBuffer,
getRangeOfPrefixAndSuffixWithBuffer(prunedPrefix, prunedSuffix, helper.pos),
);
// Filter snippets for those with best scores (must be above threshold)
@ -103,9 +115,10 @@ export async function constructAutocompletePrompt(
(snippet) =>
snippet.score >= helper.options.recentlyEditedSimilarityThreshold,
);
finalSnippets = fillPromptWithSnippets(
scoredSnippets,
maxSnippetTokens,
helper.maxSnippetTokens,
helper.modelName,
);

View file

@ -42,7 +42,7 @@ export function jaccardSimilarity(a: string, b: string): number {
/**
* Rank code snippets to be used in tab-autocomplete prompt. Returns a sorted version of the snippet array.
*/
export function rankSnippets(
export function rankAndOrderSnippets(
ranges: AutocompleteSnippet[],
helper: HelperVars,
): Required<AutocompleteSnippet>[] {

View file

@ -1,6 +1,7 @@
import { IDE } from "../..";
import { getRangeInString } from "../../util/ranges";
import { languageForFilepath } from "../constructPrompt";
import { languageForFilepath } from "../constants/AutocompleteLanguageInfo";
import { AutocompleteInput } from "../types";
/**

View file

@ -1,7 +1,7 @@
import * as fs from "node:fs";
import { homedir } from "node:os";
import path from "path";
import { languageForFilepath } from "../../autocomplete/constructPrompt.js";
import { languageForFilepath } from "../../autocomplete/constants/AutocompleteLanguageInfo.js";
import { SlashCommand } from "../../index.js";
import { stripImages } from "../../llm/images.js";

View file

@ -12,7 +12,7 @@ export const DEFAULT_AUTOCOMPLETE_OPTS: TabAutocompleteOptions = {
slidingWindowPrefixPercentage: 0.75,
slidingWindowSize: 500,
maxSnippetPercentage: 0.6,
recentlyEditedSimilarityThreshold: 0.3,
recentlyEditedSimilarityThreshold: 0.0, // Effectively no threshold.
useCache: true,
onlyMyCode: true,
useOtherFiles: true,

View file

@ -1,5 +1,5 @@
import { Chunk, IDE } from "core";
import { languageForFilepath } from "core/autocomplete/constructPrompt";
import { languageForFilepath } from "core/autocomplete/constants/AutocompleteLanguageInfo";
import { DEFAULT_IGNORE_DIRS } from "core/indexing/ignore";
import { deduplicateArray } from "core/util";
import { getParserForFile } from "core/util/treeSitter";