feat: retrieve five most relevant code chunks given a code snippet
This commit is contained in:
parent
4f7ec0dc13
commit
c8c0472d81
3 changed files with 136 additions and 1 deletions
|
|
@ -77,6 +77,21 @@ export default class BaseRetrievalPipeline implements IRetrievalPipeline {
|
|||
);
|
||||
}
|
||||
|
||||
// Add this to BaseRetrievalPipeline.ts
|
||||
protected async ensureLanceDbInitialized(): Promise<boolean> {
|
||||
if (!this.lanceDbIndex) {
|
||||
// Re-run the initialization logic
|
||||
const embedModel = this.options.config.selectedModelByRole.embed;
|
||||
if (!embedModel) {
|
||||
return false;
|
||||
}
|
||||
this.lanceDbIndex = await LanceDbIndex.create(embedModel, (uri) =>
|
||||
this.options.ide.readFile(uri),
|
||||
);
|
||||
}
|
||||
return this.lanceDbIndex !== null;
|
||||
}
|
||||
|
||||
private getCleanedTrigrams(
|
||||
query: RetrievalPipelineRunArguments["query"],
|
||||
): string[] {
|
||||
|
|
@ -163,12 +178,19 @@ export default class BaseRetrievalPipeline implements IRetrievalPipeline {
|
|||
input: string,
|
||||
n: number,
|
||||
): Promise<Chunk[]> {
|
||||
if (!this.lanceDbIndex) {
|
||||
const initialized = await this.ensureLanceDbInitialized();
|
||||
if (!initialized || !this.lanceDbIndex) {
|
||||
console.warn(
|
||||
"LanceDB index not available, skipping embeddings retrieval",
|
||||
);
|
||||
return [];
|
||||
}
|
||||
// if (!this.lanceDbIndex) {
|
||||
// console.warn(
|
||||
// "LanceDB index not available, skipping embeddings retrieval",
|
||||
// );
|
||||
// return [];
|
||||
// }
|
||||
|
||||
return this.lanceDbIndex.retrieve(
|
||||
input,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,99 @@
|
|||
import { BranchAndDir, Chunk, ContinueConfig, IDE, ILLM } from "../../..";
|
||||
import BaseRetrievalPipeline, {
|
||||
RetrievalPipelineRunArguments,
|
||||
} from "./BaseRetrievalPipeline";
|
||||
|
||||
export default class NextEditRetrievalPipeline extends BaseRetrievalPipeline {
|
||||
async run(args: RetrievalPipelineRunArguments): Promise<Chunk[]> {
|
||||
if (args.includeEmbeddings) {
|
||||
await this.ensureLanceDbInitialized();
|
||||
}
|
||||
// Collect chunks from multiple sources.
|
||||
const [embeddingChunks, ftsChunks, recentChunks, toolChunks] =
|
||||
await Promise.all([
|
||||
// Get chunks using embeddings similarity (vector search).
|
||||
args.includeEmbeddings
|
||||
? this.retrieveEmbeddings(args.query, this.options.nRetrieve)
|
||||
: Promise.resolve([]),
|
||||
|
||||
// Get chunks using full-text search.
|
||||
this.retrieveFts(args, this.options.nRetrieve),
|
||||
|
||||
// Get chunks from recently edited files.
|
||||
this.retrieveAndChunkRecentlyEditedFiles(this.options.nRetrieve),
|
||||
|
||||
// Get chunks using LLM-guided tool selection.
|
||||
this.retrieveWithTools(args.query),
|
||||
]);
|
||||
|
||||
// Combine all chunks.
|
||||
let allChunks = [
|
||||
...embeddingChunks,
|
||||
...ftsChunks,
|
||||
...recentChunks,
|
||||
...toolChunks,
|
||||
];
|
||||
|
||||
// Remove duplicates by digest.
|
||||
const uniqueDigests = new Set<string>();
|
||||
const uniqueChunks = allChunks.filter((chunk) => {
|
||||
if (uniqueDigests.has(chunk.digest)) {
|
||||
return false;
|
||||
}
|
||||
uniqueDigests.add(chunk.digest);
|
||||
return true;
|
||||
});
|
||||
|
||||
// If we have too many chunks and embedding retrieval is available,
|
||||
// prioritize chunks from embedding search as they're likely more semantically relevant.
|
||||
if (uniqueChunks.length > this.options.nFinal) {
|
||||
// First include embedding chunks (most semantically relevant).
|
||||
const result: Chunk[] = [...embeddingChunks];
|
||||
|
||||
// Then add unique chunks from other sources until we reach nFinal.
|
||||
const remainingChunks = uniqueChunks.filter(
|
||||
(chunk) => !embeddingChunks.some((ec) => ec.digest === chunk.digest),
|
||||
);
|
||||
|
||||
result.push(
|
||||
...remainingChunks.slice(0, this.options.nFinal - result.length),
|
||||
);
|
||||
|
||||
return result.slice(0, this.options.nFinal);
|
||||
}
|
||||
|
||||
// Otherwise just return the unique chunks up to nFinal.
|
||||
return uniqueChunks.slice(0, this.options.nFinal);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTopRelevantCodeChunks(
|
||||
codeSnippet: string,
|
||||
options: {
|
||||
ide: IDE;
|
||||
llm: ILLM;
|
||||
config: ContinueConfig;
|
||||
tags: BranchAndDir[];
|
||||
filterDirectory?: string;
|
||||
},
|
||||
): Promise<Chunk[]> {
|
||||
const retrievalPipeline = new NextEditRetrievalPipeline({
|
||||
llm: options.llm,
|
||||
config: options.config,
|
||||
ide: options.ide,
|
||||
input: codeSnippet,
|
||||
nRetrieve: 10, // retrieve more initially for better filtering
|
||||
nFinal: 5, // return top 5
|
||||
tags: options.tags,
|
||||
filterDirectory: options.filterDirectory,
|
||||
});
|
||||
|
||||
const chunks = await retrievalPipeline.run({
|
||||
query: codeSnippet,
|
||||
tags: options.tags,
|
||||
filterDirectory: options.filterDirectory,
|
||||
includeEmbeddings: true,
|
||||
});
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ import { AutocompleteDebouncer } from "../autocomplete/util/AutocompleteDebounce
|
|||
import AutocompleteLruCache from "../autocomplete/util/AutocompleteLruCache.js";
|
||||
import { HelperVars } from "../autocomplete/util/HelperVars.js";
|
||||
import { AutocompleteInput } from "../autocomplete/util/types.js";
|
||||
import { getTopRelevantCodeChunks } from "../context/retrieval/pipelines/NextEditRetrievalPipeline.js";
|
||||
import { myersDiff } from "../diff/myers.js";
|
||||
import { PrefetchQueue } from "../util/PrefetchQueue.js";
|
||||
import { replaceEscapedCharacters } from "../util/text.js";
|
||||
|
|
@ -572,6 +573,19 @@ export class NextEditProvider {
|
|||
position: helper.pos,
|
||||
});
|
||||
|
||||
const { config } = await this.configHandler.loadConfig();
|
||||
const chunks = await getTopRelevantCodeChunks(
|
||||
"pow(number, exponent) {\n return number ** exponent;\n}",
|
||||
{
|
||||
ide: this.ide,
|
||||
llm: llm,
|
||||
config: config!,
|
||||
tags: [],
|
||||
// filterDirectory: getUriPathBasename(helper.workspaceUris[0]),
|
||||
},
|
||||
);
|
||||
console.log("chunks:", JSON.stringify(chunks));
|
||||
|
||||
this.prefetchQueue.loadResource(refQueue);
|
||||
await this.prefetchQueue.initialize();
|
||||
return await this.prefetchQueue.pop();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue