⚡️ fill prompt with highest scoring snippets
This commit is contained in:
parent
62e1c15c85
commit
7af502c2f3
9 changed files with 59 additions and 50 deletions
|
|
@ -10,7 +10,7 @@ import { getBasename } from "../util";
|
|||
|
||||
import { getAst, getScopeAroundRange, getTreePathAtCursor } from "./ast";
|
||||
import { AutocompleteLanguageInfo, LANGUAGES, Typescript } from "./languages";
|
||||
import { rankSnippets } from "./ranking";
|
||||
import { AutocompleteSnippet, fillPromptWithSnippets, rankSnippets } from "./ranking";
|
||||
import { slidingWindowMatcher } from "./slidingWindow";
|
||||
|
||||
export function languageForFilepath(
|
||||
|
|
@ -94,14 +94,11 @@ export async function constructAutocompletePrompt(
|
|||
fullSuffix: string,
|
||||
clipboardText: string,
|
||||
language: AutocompleteLanguageInfo,
|
||||
getDefinitions: (
|
||||
document: RangeInFileWithContents,
|
||||
cursorIndex: number
|
||||
) => Promise<RangeInFileWithContents[]>,
|
||||
options: TabAutocompleteOptions,
|
||||
recentlyEditedRanges: RangeInFileWithContents[],
|
||||
recentlyEditedDocuments: RangeInFileWithContents[],
|
||||
modelName: string
|
||||
modelName: string,
|
||||
extraSnippets: AutocompleteSnippet[]
|
||||
): Promise<{
|
||||
prefix: string;
|
||||
suffix: string;
|
||||
|
|
@ -109,7 +106,7 @@ export async function constructAutocompletePrompt(
|
|||
completeMultiline: boolean;
|
||||
}> {
|
||||
// Find external snippets
|
||||
let snippets: RangeInFileWithContents[] = [];
|
||||
let snippets: RangeInFileWithContents[] = extraSnippets;
|
||||
|
||||
const windowAroundCursor =
|
||||
fullPrefix.slice(
|
||||
|
|
@ -140,15 +137,17 @@ export async function constructAutocompletePrompt(
|
|||
snippets.push(...(recentlyEdited as any));
|
||||
|
||||
// Rank / order the snippets
|
||||
snippets = rankSnippets(snippets, windowAroundCursor).filter(
|
||||
const scoredSnippets = rankSnippets(snippets, windowAroundCursor).filter(
|
||||
// Filter out snippets that are already in prefix
|
||||
(snippet) => snippet.score < 0.98
|
||||
);
|
||||
|
||||
// How to add snippets to the prefix? Count separately? Always keep some of the prefix??
|
||||
// Fill maxSnippetTokens with snippets
|
||||
const maxSnippetTokens = options.maxPromptTokens * options.maxSnippetPercentage;
|
||||
const finalSnippets = fillPromptWithSnippets(scoredSnippets, maxSnippetTokens, modelName);
|
||||
|
||||
// Construct basic prefix / suffix
|
||||
const formattedSnippets = snippets
|
||||
const formattedSnippets = finalSnippets
|
||||
.map((snippet) =>
|
||||
formatExternalSnippet(snippet.filepath, snippet.contents, language)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -10,4 +10,5 @@ export const DEFAULT_AUTOCOMPLETE_OPTS: TabAutocompleteOptions = {
|
|||
multilineCompletions: "auto",
|
||||
slidingWindowPrefixPercentage: 0.75,
|
||||
slidingWindowSize: 500,
|
||||
maxSnippetPercentage: 0.6
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { RangeInFileWithContents } from "../commands/util";
|
||||
import { countTokens } from "../llm/countTokens";
|
||||
|
||||
export type AutocompleteSnippet = RangeInFileWithContents & {
|
||||
score: number;
|
||||
|
|
@ -110,3 +111,24 @@ function mergeOverlappingRangeContents(
|
|||
const numOverlapping = first.range.end.line - second.range.start.line;
|
||||
return firstLines.slice(-numOverlapping).join("\n") + "\n" + second.contents;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fill the allowed space with snippets
|
||||
*/
|
||||
export function fillPromptWithSnippets(snippets: AutocompleteSnippet[], maxSnippetTokens: number, modelName: string): AutocompleteSnippet[] {
|
||||
let tokensRemaining = maxSnippetTokens;
|
||||
const keptSnippets: AutocompleteSnippet[] = []
|
||||
for (let i = 0; i < snippets.length; i++) {
|
||||
const snippet = snippets[i];
|
||||
const tokenCount = countTokens(snippet.contents, modelName)
|
||||
if (tokensRemaining - tokenCount >= 0) {
|
||||
tokensRemaining -= tokenCount;
|
||||
keptSnippets.push(snippet);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return keptSnippets;
|
||||
};
|
||||
|
|
@ -59,6 +59,11 @@ export const defaultConfig: SerializedContinueConfig = {
|
|||
{ name: "problems", params: {} },
|
||||
{ name: "codebase", params: {} },
|
||||
],
|
||||
tabAutocompleteModel: {
|
||||
title: "Starcoder 3b",
|
||||
provider: "ollama",
|
||||
model: "starcoder-3b",
|
||||
}
|
||||
};
|
||||
|
||||
export const defaultConfigJetBrains: SerializedContinueConfig = {
|
||||
|
|
|
|||
1
core/index.d.ts
vendored
1
core/index.d.ts
vendored
|
|
@ -588,6 +588,7 @@ export interface TabAutocompleteOptions {
|
|||
multilineCompletions: "always" | "never" | "auto";
|
||||
slidingWindowPrefixPercentage: number;
|
||||
slidingWindowSize: number;
|
||||
maxSnippetPercentage: number;
|
||||
}
|
||||
|
||||
export interface SerializedContinueConfig {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import {
|
|||
stopAtSimilarLine,
|
||||
streamWithNewLines,
|
||||
} from "core/autocomplete/lineStream";
|
||||
import { AutocompleteSnippet } from "core/autocomplete/ranking";
|
||||
import { getTemplateForModel } from "core/autocomplete/templates";
|
||||
import { GeneratorReuseManager } from "core/autocomplete/util";
|
||||
import { streamLines } from "core/diff/util";
|
||||
|
|
@ -76,6 +77,14 @@ export async function getTabCompletion(
|
|||
Math.min(pos.line + 1, document.lineCount - 1)
|
||||
).text;
|
||||
const clipboardText = await vscode.env.clipboard.readText();
|
||||
|
||||
let extrasSnippets = (await Promise.race([
|
||||
getDefinitionsFromLsp(document.uri.fsPath, fullPrefix + fullSuffix, fullPrefix.length, ide),
|
||||
new Promise((resolve) => {
|
||||
setTimeout(() => resolve([]), 100);
|
||||
})
|
||||
])) as AutocompleteSnippet[]
|
||||
|
||||
const { prefix, suffix, completeMultiline } =
|
||||
await constructAutocompletePrompt(
|
||||
document.uri.toString(),
|
||||
|
|
@ -83,11 +92,11 @@ export async function getTabCompletion(
|
|||
fullSuffix,
|
||||
clipboardText,
|
||||
lang,
|
||||
(document, cursorIndex) => getDefinitionsFromLsp(document, cursorIndex, ide),
|
||||
options,
|
||||
await recentlyEditedTracker.getRecentlyEditedRanges(),
|
||||
await recentlyEditedTracker.getRecentlyEditedDocuments(),
|
||||
llm.model
|
||||
llm.model,
|
||||
extrasSnippets
|
||||
);
|
||||
|
||||
const { template, completionOptions } = options.template
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { IDE, RangeInFile } from "core";
|
||||
import { getAst, getTreePathAtCursor } from "core/autocomplete/ast";
|
||||
import { AutocompleteSnippet } from "core/autocomplete/ranking";
|
||||
import { RangeInFileWithContents } from "core/commands/util";
|
||||
import * as vscode from "vscode";
|
||||
import Parser from "web-tree-sitter";
|
||||
|
|
@ -65,11 +66,12 @@ async function getDefinitionsForNode(
|
|||
*/
|
||||
|
||||
export async function getDefinitionsFromLsp(
|
||||
document: RangeInFileWithContents,
|
||||
filepath: string,
|
||||
contents: string,
|
||||
cursorIndex: number,
|
||||
ide: IDE
|
||||
): Promise<RangeInFileWithContents[]> {
|
||||
const ast = await getAst(document.filepath, document.contents);
|
||||
): Promise<AutocompleteSnippet[]> {
|
||||
const ast = await getAst(filepath, contents);
|
||||
if (!ast) return [];
|
||||
|
||||
const treePath = await getTreePathAtCursor(ast, cursorIndex);
|
||||
|
|
@ -77,7 +79,7 @@ export async function getDefinitionsFromLsp(
|
|||
|
||||
const results: RangeInFileWithContents[] = [];
|
||||
for (const node of treePath.reverse()) {
|
||||
const definitions = await getDefinitionsForNode(document.filepath, node);
|
||||
const definitions = await getDefinitionsForNode(filepath, node);
|
||||
results.push(
|
||||
...(await Promise.all(
|
||||
definitions.map(async (def) => ({
|
||||
|
|
@ -97,5 +99,8 @@ export async function getDefinitionsFromLsp(
|
|||
);
|
||||
}
|
||||
|
||||
return results;
|
||||
return results.map((result) => ({
|
||||
...result,
|
||||
score: 0.8
|
||||
}));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,33 +0,0 @@
|
|||
import { IDE } from "core";
|
||||
import { AutocompleteSnippet } from "core/autocomplete/ranking";
|
||||
import * as vscode from "vscode";
|
||||
|
||||
export class AutocompletePromptBuilder {
|
||||
constructor(private readonly ide: IDE) {}
|
||||
|
||||
async getDefinition(
|
||||
uri: string,
|
||||
line: number,
|
||||
character: number
|
||||
): Promise<AutocompleteSnippet | undefined> {
|
||||
const definitions = (await vscode.commands.executeCommand(
|
||||
"vscode.executeDefinitionProvider",
|
||||
vscode.Uri.parse(uri),
|
||||
new vscode.Position(line, character)
|
||||
)) as any;
|
||||
|
||||
if (definitions[0]?.targetRange) {
|
||||
return {
|
||||
filepath: uri,
|
||||
contents: await this.ide.readRangeInFile(
|
||||
definitions[0].targetUri.fsPath,
|
||||
definitions[0].targetRange
|
||||
),
|
||||
range: definitions[0].targetRange,
|
||||
score: 0.8
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue