From 64a346dcac2c5a1fd17bb6cf2d07d633f69b7f44 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Fri, 23 Feb 2024 10:38:34 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20basic=20ranking=20by=20symbol=20?= =?UTF-8?q?intersection=20similarity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/autocomplete/constructPrompt.ts | 14 ++++++- core/autocomplete/ranking.ts | 59 ++++++++++++++++++++++++++++ core/autocomplete/slidingWindow.ts | 38 +----------------- 3 files changed, 74 insertions(+), 37 deletions(-) create mode 100644 core/autocomplete/ranking.ts diff --git a/core/autocomplete/constructPrompt.ts b/core/autocomplete/constructPrompt.ts index 39dcd7952..794cb7585 100644 --- a/core/autocomplete/constructPrompt.ts +++ b/core/autocomplete/constructPrompt.ts @@ -10,6 +10,7 @@ import { getBasename } from "../util"; import { getAst, getScopeAroundRange, getTreePathAtCursor } from "./ast"; import { AutocompleteLanguageInfo, LANGUAGES, Typescript } from "./languages"; +import { rankSnippets } from "./ranking"; import { slidingWindowMatcher } from "./slidingWindow"; export function languageForFilepath( @@ -82,7 +83,7 @@ export async function constructAutocompletePrompt( completeMultiline: boolean; }> { // Find external snippets - const snippets: FileWithContents[] = (await Promise.all( + let snippets: FileWithContents[] = (await Promise.all( recentlyEditedRanges .map(async (r) => { const scope = await getScopeAroundRange(r); @@ -145,6 +146,17 @@ export async function constructAutocompletePrompt( completeMultiline = shouldCompleteMultiline(treePath, cursorLine); } + // Rank / order the snippets + snippets = rankSnippets( + snippets, + fullPrefix, + fullSuffix, + options.slidingWindowPrefixPercentage, + options.slidingWindowSize + ); + + // How to add snippets to the prefix? Count separately? Always keep some of the prefix?? + // Construct basic prefix / suffix const formattedSnippets = snippets .map((snippet) => diff --git a/core/autocomplete/ranking.ts b/core/autocomplete/ranking.ts new file mode 100644 index 000000000..399df39ab --- /dev/null +++ b/core/autocomplete/ranking.ts @@ -0,0 +1,59 @@ +import { FileWithContents } from ".."; + +function getSymbolsForSnippet(snippet: string): Set { + const symbols = snippet + .split( + new RegExp( + `[s\.\:\'\,\;\"\*\&\^\|\\\#\@\$\!\%\(\)\[\]\{\}\<\>\/\?\+\-\/]`, + "g" + ) + ) + .map((s) => s.trim()); + return new Set(symbols); +} + +/** + * Calculate similarity as number of shared symbols divided by total number of unique symbols between both. + */ +export function symbolSimilarity(a: string, b: string): number { + const aSet = getSymbolsForSnippet(a); + const bSet = getSymbolsForSnippet(b); + const totalSet = new Set([...aSet, ...bSet]); + + // Avoid division by zero + if (totalSet.size === 0) { + return 0; + } + + let intersection = 0; + for (const symbol of aSet) { + if (bSet.has(symbol)) { + intersection++; + } + } + + return intersection / totalSet.size; +} + +/** + * Rank code snippets to be used in tab-autocomplete prompt. Returns a sorted version of the snippet array. + */ +export function rankSnippets( + snippets: FileWithContents[], + prefix: string, + suffix: string, + slidingWindowPrefixPercentage: number, + windowSize: number +): FileWithContents[] { + const scores = snippets.map((snippet) => { + const b = + prefix.slice(-windowSize * slidingWindowPrefixPercentage) + + suffix.slice(0, windowSize * (1 - slidingWindowPrefixPercentage)); + + return { + score: symbolSimilarity(snippet.contents, b), + snippet, + }; + }); + return scores.sort((a, b) => a.score - b.score).map((s) => s.snippet); +} diff --git a/core/autocomplete/slidingWindow.ts b/core/autocomplete/slidingWindow.ts index 5d4b99812..af483ec0a 100644 --- a/core/autocomplete/slidingWindow.ts +++ b/core/autocomplete/slidingWindow.ts @@ -1,4 +1,5 @@ import { FileWithContents } from ".."; +import { symbolSimilarity } from "./ranking"; function* slidingWindow( content: string, @@ -24,41 +25,6 @@ function* slidingWindow( } } -function getSymbolsForSnippet(snippet: string): Set { - const symbols = snippet - .split( - new RegExp( - `[s\.\:\'\,\;\"\*\&\^\|\\\#\@\$\!\%\(\)\[\]\{\}\<\>\/\?\+\-\/]`, - "g" - ) - ) - .map((s) => s.trim()); - return new Set(symbols); -} - -/** - * Calculate similarity as number of shared symbols divided by total number of unique symbols between both. - */ -function slidingWindowSimilarity(a: string, b: string): number { - const aSet = getSymbolsForSnippet(a); - const bSet = getSymbolsForSnippet(b); - const totalSet = new Set([...aSet, ...bSet]); - - // Avoid division by zero - if (totalSet.size === 0) { - return 0; - } - - let intersection = 0; - for (const symbol of aSet) { - if (bSet.has(symbol)) { - intersection++; - } - } - - return intersection / totalSet.size; -} - /** * Match by similarity over sliding windows of recent documents. * @param recentDocuments @@ -78,7 +44,7 @@ export async function slidingWindowMatcher( for (const { filepath, contents } of recentDocuments) { for (const window of slidingWindow(contents, windowSize)) { - const similarity = slidingWindowSimilarity( + const similarity = symbolSimilarity( window, prefix.slice(-windowSize * slidingWindowPrefixPercentage) + suffix.slice(windowSize * (1 - slidingWindowPrefixPercentage))