Merge pull request #3128 from continuedev/tomasz/fix-truncated-completions
Fix overly eager suffix matching
This commit is contained in:
commit
315f75e75e
3 changed files with 120 additions and 94 deletions
|
|
@ -171,46 +171,45 @@ describe("stopAtStartOf", () => {
|
|||
controlPlaneEnv.CONTROL_PLANE_URL,
|
||||
),
|
||||
`;
|
||||
|
||||
/* Some LLMs, such as Codestral, repeat the suffix of the query. To test our filtering, we cut the sample code at random positions, remove a part of the input
|
||||
and construct a response, containing the removed part and the suffix. The goal of the stopAtStartOf() method is to detect the start of the suffix in the response */
|
||||
it("should stop if the start of the suffix is reached", async () => {
|
||||
const removeLength = 10;
|
||||
for (let i = 0; i < sampleCode.length - removeLength - 20; i++) {
|
||||
const removed = sampleCode.slice(i, i + removeLength);
|
||||
const suffix = sampleCode.slice(i + removeLength);
|
||||
const response = removed + suffix;
|
||||
const suffix = `
|
||||
const data = await response.json();
|
||||
return data.items;
|
||||
}`;
|
||||
const mockStream = createMockStream(sampleCode.split(/(?! )/g));
|
||||
const result = stopAtStartOf(mockStream, suffix);
|
||||
|
||||
// split the response but keep spaces
|
||||
const mockStream = createMockStream(response.split(/(?! )/g));
|
||||
const result = stopAtStartOf(mockStream, suffix);
|
||||
|
||||
const resultStr = await streamToString(result);
|
||||
if (resultStr !== removed) {
|
||||
throw new Error(
|
||||
`i=${i} result:\n${resultStr}\n\nremoved:\n${removed}\n\nsuffix:\n${suffix}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
const resultStr = await streamToString(result);
|
||||
expect(resultStr).toBe(` {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: \`Bearer \${this.workOsAccessToken}\`,
|
||||
},
|
||||
},
|
||||
);
|
||||
`);
|
||||
});
|
||||
it("should stop if the start of the suffix is reached, even if the suffix has a prefix", async () => {
|
||||
const removeLength = 10;
|
||||
for (let i = 0; i < sampleCode.length - removeLength - 20; i++) {
|
||||
const removed = sampleCode.slice(i, i + removeLength);
|
||||
let suffix = sampleCode.slice(i + removeLength);
|
||||
const response = removed + suffix;
|
||||
// add a prefix to the suffix
|
||||
suffix = "strange words;\n which start the suffix#" + suffix;
|
||||
const suffix = `
|
||||
xxxconst data = await response.json();
|
||||
return data.items;
|
||||
}`;
|
||||
const mockStream = createMockStream(sampleCode.split(/(?! )/g));
|
||||
const result = stopAtStartOf(mockStream, suffix);
|
||||
|
||||
// split the response but keep spaces
|
||||
const mockStream = createMockStream(response.split(/(?! )/g));
|
||||
const result = stopAtStartOf(mockStream, suffix);
|
||||
|
||||
const resultStr = await streamToString(result);
|
||||
if (resultStr !== removed) {
|
||||
throw new Error(
|
||||
`i=${i} result:\n${resultStr}\n\nremoved:\n${removed}\n\nsuffix:\n${suffix}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
const resultStr = await streamToString(result);
|
||||
expect(resultStr).toBe(` {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: \`Bearer \${this.workOsAccessToken}\`,
|
||||
},
|
||||
},
|
||||
);
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -119,8 +119,9 @@ export async function* stopAtStopTokens(
|
|||
}
|
||||
|
||||
/**
|
||||
* Asynchronously yields characters from the input stream, stopping if a sequence contained in the beginning of the suffix is encountered.
|
||||
* */
|
||||
* Asynchronously yields characters from the input stream.
|
||||
* Stops if the beginning of the suffix is detected in the stream.
|
||||
*/
|
||||
export async function* stopAtStartOf(
|
||||
stream: AsyncGenerator<string>,
|
||||
suffix: string,
|
||||
|
|
@ -132,42 +133,31 @@ export async function* stopAtStartOf(
|
|||
}
|
||||
return;
|
||||
}
|
||||
// We use sequenceLength * 1.5 as a heuristic to make sure we don't miss the sequence if the
|
||||
// stream is not perfectly aligned with the sequence (small whitespace differences etc).
|
||||
const targetPart = suffix
|
||||
.trimStart()
|
||||
.slice(0, Math.floor(sequenceLength * 1.5));
|
||||
|
||||
const n = Math.min(suffix.length, 3 * sequenceLength);
|
||||
let prev = new Array(n + 1).fill(0);
|
||||
let res = 0;
|
||||
let buffer = "";
|
||||
|
||||
for await (const chunk of stream) {
|
||||
const s1 = chunk;
|
||||
const m = chunk.length;
|
||||
for (let i = 1; i <= m; i++) {
|
||||
// Create a temporary array to store the current row
|
||||
let curr = new Array(n + 1).fill(0);
|
||||
for (let j = 1; j <= n; j++) {
|
||||
if (s1[i - 1] === suffix[j - 1]) {
|
||||
curr[j] = prev[j - 1] + 1;
|
||||
res = Math.max(res, curr[j]);
|
||||
} else {
|
||||
curr[j] = 0;
|
||||
}
|
||||
}
|
||||
buffer += chunk;
|
||||
|
||||
// Move the current row's data to the previous row
|
||||
prev = curr;
|
||||
// Check if the targetPart contains contains the buffer at any point
|
||||
if (buffer.length >= sequenceLength && targetPart.includes(buffer)) {
|
||||
return; // Stop processing when the sequence is found
|
||||
}
|
||||
|
||||
if (res > sequenceLength) {
|
||||
return;
|
||||
}
|
||||
|
||||
buffer += s1[i - 1];
|
||||
|
||||
while (buffer.length > sequenceLength) {
|
||||
yield buffer[0];
|
||||
buffer = buffer.slice(1);
|
||||
}
|
||||
// Yield chunk by chunk, ensuring not to exceed sequenceLength in the buffer
|
||||
while (buffer.length > sequenceLength) {
|
||||
yield buffer[0];
|
||||
buffer = buffer.slice(1);
|
||||
}
|
||||
}
|
||||
|
||||
yield buffer;
|
||||
// Yield the remaining buffer if it is not contained in the `targetPart`
|
||||
if (buffer.length > 0) {
|
||||
yield buffer;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -153,35 +153,6 @@ public class Calculator {
|
|||
expectedCompletion: `
|
||||
POSTGRES_DB: mydb`,
|
||||
},
|
||||
{
|
||||
description:
|
||||
"Should filter out suffix generated by LLM (sample from Codestral)",
|
||||
filename: "test.js",
|
||||
input: `
|
||||
add(number) {
|
||||
this.result += number;
|
||||
return this;
|
||||
}
|
||||
// Add the subtract method
|
||||
subtract(number) {<|fim|>
|
||||
|
||||
subtract(number) {
|
||||
this.result -= number;
|
||||
return this;
|
||||
}
|
||||
|
||||
multiply(number) {
|
||||
this.result *= number;
|
||||
return this;
|
||||
}`,
|
||||
llmOutput: ` this.result -= number;
|
||||
return this;
|
||||
}
|
||||
// Add the multiply method
|
||||
multiply(number) {
|
||||
`,
|
||||
expectedCompletion: undefined,
|
||||
},
|
||||
{
|
||||
description: "Should enforce bracket matching in JSON files",
|
||||
filename: "test.json",
|
||||
|
|
@ -2164,4 +2135,70 @@ export default {
|
|||
llmOutput: "}",
|
||||
expectedCompletion: "}",
|
||||
},
|
||||
{
|
||||
description:
|
||||
"Should handle autocomplete in two similar TypeScript functions",
|
||||
filename: "List.svelte",
|
||||
input: `
|
||||
import { createClient, RedisClientType } from "redis";
|
||||
import { IKeyValueStore } from "./index.js";
|
||||
|
||||
export class RedisKeyValueStore implements IKeyValueStore {
|
||||
private client: RedisClientType;
|
||||
|
||||
constructor(redisUrl: string) {
|
||||
this.client = createClient({
|
||||
url: redisUrl
|
||||
.replace("https://", "redis://")
|
||||
.replace("http://", "redis://"),
|
||||
});
|
||||
this.client.on("connect", () => console.log("Redis Connected"));
|
||||
this.client.on("error", (err) => console.log("Redis Client Error", err));
|
||||
this.client.connect();
|
||||
}
|
||||
public async has(tableName: string, key: string): Promise<boolean> {
|
||||
return (await this.client.exists(this._getKey(tableName, key))) > 0;
|
||||
}
|
||||
|
||||
public async keys(tableName: string): Promise<string[]> {
|
||||
const keys = await this.client.keys(this._getTableKey(tableName));
|
||||
return keys.map((key) => key.split("::")[1]);
|
||||
}
|
||||
|
||||
public async put(
|
||||
tableName: string,
|
||||
key: string,
|
||||
value: string,
|
||||
): Promise<void> {
|
||||
await this.client.set(this._getKey(tableName, key), value);
|
||||
}
|
||||
|
||||
public async get(
|
||||
tableName: string,
|
||||
key: string,
|
||||
): Promise<string | undefined> {
|
||||
const value = await this.client.get(this._getKey(tableName, key));
|
||||
return value ?? undefined;
|
||||
}
|
||||
|
||||
public async deleteAll(tableName: string): Promise<void> {
|
||||
await this.client.del(this._getTableKey(tableName));
|
||||
}
|
||||
|
||||
<|fim|>
|
||||
|
||||
|
||||
public async remove(tableName: string, key: string): Promise<boolean> {
|
||||
const result = await this.client.del(this._getKey(tableName, key));
|
||||
return result > 0;
|
||||
}
|
||||
}
|
||||
`,
|
||||
llmOutput: ` public async delete(tableName: string, key: string): Promise<void> {
|
||||
await this.client.del(this._getKey(tableName, key));
|
||||
}`,
|
||||
expectedCompletion: ` public async delete(tableName: string, key: string): Promise<void> {
|
||||
await this.client.del(this._getKey(tableName, key));
|
||||
}`,
|
||||
},
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue