⚡️ filter out extreme repetitions from autocomplete

This commit is contained in:
Nate Sesti 2024-06-11 15:28:59 -07:00
parent 683c1221af
commit 8a4003672b
3 changed files with 30 additions and 1 deletions

View file

@ -1,4 +1,5 @@
import type { ILLM } from "..";
import { longestCommonSubsequence } from "../util/lcs";
import { lineIsRepeated } from "./lineStream";
function rewritesLineAbove(completion: string, prefix: string): boolean {
@ -19,6 +20,29 @@ function rewritesLineAbove(completion: string, prefix: string): boolean {
return lineIsRepeated(lineAbove, firstLineOfCompletion);
}
const MAX_REPETITION_FREQ_TO_CHECK = 3;
function isExtremeRepetition(completion: string): boolean {
const lines = completion.split("\n");
if (lines.length < 6) {
return false;
}
for (let freq = 1; freq < MAX_REPETITION_FREQ_TO_CHECK; freq++) {
const lcs = longestCommonSubsequence(lines[0], lines[freq]);
if (lcs.length > 5 || lcs.length > lines[0].length * 0.5) {
let matchCount = 0;
for (let i = 0; i < lines.length; i += freq) {
if (lines[i].includes(lcs)) {
matchCount++;
}
}
if (matchCount * freq > 8 || (matchCount * freq) / lines.length > 0.8) {
return true;
}
}
}
return false;
}
export function postprocessCompletion({
completion,
llm,
@ -40,6 +64,11 @@ export function postprocessCompletion({
return undefined;
}
// Filter out repetitions of many lines in a row
if (isExtremeRepetition(completion)) {
return undefined;
}
// Remove trailing whitespace
completion = completion.trimEnd();

View file

@ -1,9 +1,9 @@
/* Terminal emulator - commented because node-pty is causing problems. */
import { longestCommonSubsequence } from "core/util/lcs";
import * as os from "node:os";
import stripAnsi from "strip-ansi";
import * as vscode from "vscode";
import { longestCommonSubsequence } from "../util/lcs";
function loadNativeModule<T>(id: string): T | null {
try {