tweak diff algo parameters for fuzzy matching

This commit is contained in:
Nate 2024-09-05 19:32:23 -07:00
parent 6eb3f4eb64
commit dabc64e125
3 changed files with 26 additions and 23 deletions

View file

@ -8,6 +8,12 @@ import path from "node:path";
// "modification" is an extra type used to represent an "old" + "new" diff line
type MyersDiffTypes = Extract<DiffLineType, "new" | "old"> | "modification";
const UNIFIED_DIFF_SYMBOLS = {
same: "",
new: "+",
old: "-",
};
async function* generateLines(lines: string[]): AsyncGenerator<string> {
for (const line of lines) {
yield line;
@ -50,13 +56,9 @@ function getMyersDiffType(diff: any): MyersDiffTypes | undefined {
}
function displayDiff(diff: DiffLine[]) {
const symbol = {
same: "",
new: "+",
old: "-",
};
return diff.map(({ type, line }) => `${symbol[type]} ${line}`).join("\n");
return diff
.map(({ type, line }) => `${UNIFIED_DIFF_SYMBOLS[type]} ${line}`)
.join("\n");
}
async function expectDiff(file: string) {
@ -237,7 +239,7 @@ describe("streamDiff(", () => {
await expectDiff("add-comments");
});
test.skip("Mock LLM example", async () => {
test("Mock LLM example", async () => {
await expectDiff("mock-llm");
});
});

View file

@ -68,24 +68,24 @@ export default Mock;
import { BaseLLM } from "../index.js";
class Mock extends BaseLLM {
- static Completion = "Test Completion";
+ private static _completion: string = "Test Completion";
- static Completion = "Test Completion";
+ private static _completion: string = "Test Completion";
static providerName: ModelProvider = "mock";
+ static get completion(): string {
+ return Mock._completion;
+ }
+
+ static set completion(value: string) {
+ Mock._completion = value;
+ }
+
+ static get completion(): string {
+ return Mock._completion;
+ }
+
+ static set completion(value: string) {
+ Mock._completion = value;
+ }
+
protected async *_streamComplete(
prompt: string,
options: CompletionOptions,
): AsyncGenerator<string> {
- yield Mock.Completion;
+ yield Mock.completion;
- yield Mock.Completion;
+ yield Mock.completion;
}
protected async *_streamChat(
@ -94,8 +94,8 @@ export default Mock;
): AsyncGenerator<ChatMessage> {
yield {
role: "assistant",
- content: Mock.Completion,
+ content: Mock.completion,
- content: Mock.Completion,
+ content: Mock.completion,
};
}
}

View file

@ -31,7 +31,8 @@ function linesMatch(lineA: string, lineB: string, linesBetween = 0): boolean {
return (
// Should be more unlikely for lines to fuzzy match if they are further away
(d / Math.max(lineA.length, lineB.length) < 0.5 - linesBetween * 0.05 ||
(d / Math.max(lineA.length, lineB.length) <=
Math.max(0, 0.48 - linesBetween * 0.06) ||
lineA.trim() === lineB.trim()) &&
lineA.trim() !== ""
);