| 1 | import { Chunk } from "../../../"; |
| 2 | import { Telemetry } from "../../../util/posthog"; |
| 3 | import { findUriInDirs } from "../../../util/uri"; |
| 4 | import { requestFilesFromRepoMap } from "../repoMapRequest"; |
| 5 | import { deduplicateChunks } from "../util"; |
| 6 | |
| 7 | import BaseRetrievalPipeline, { |
| 8 | RetrievalPipelineRunArguments, |
| 9 | } from "./BaseRetrievalPipeline"; |
| 10 | |
| 11 | export default class NoRerankerRetrievalPipeline extends BaseRetrievalPipeline { |
| 12 | async run(args: RetrievalPipelineRunArguments): Promise<Chunk[]> { |
| 13 | const { input, nFinal, filterDirectory, config } = this.options; |
| 14 | |
| 15 | // We give 1/4 weight to recently edited files, 1/4 to full text search, |
| 16 | // and the remaining 1/2 to embeddings |
| 17 | const recentlyEditedNFinal = nFinal * 0.25; |
| 18 | const ftsNFinal = nFinal * 0.25; |
| 19 | const embeddingsNFinal = nFinal - recentlyEditedNFinal - ftsNFinal; |
| 20 | |
| 21 | let retrievalResults: Chunk[] = []; |
| 22 | |
| 23 | let ftsChunks: Chunk[] = []; |
| 24 | try { |
| 25 | ftsChunks = await this.retrieveFts(args, ftsNFinal); |
| 26 | } catch (error) { |
| 27 | await Telemetry.captureError("no_reranker_fts_retrieval", error); |
| 28 | // console.error("Error retrieving FTS chunks:", error); |
| 29 | } |
| 30 | |
| 31 | let embeddingsChunks: Chunk[] = []; |
| 32 | try { |
| 33 | embeddingsChunks = !!config.selectedModelByRole.embed |
| 34 | ? await this.retrieveEmbeddings(input, embeddingsNFinal) |
| 35 | : []; |
| 36 | } catch (error) { |
| 37 | await Telemetry.captureError("no_reranker_embeddings_retrieval", error); |
| 38 | console.error("Error retrieving embeddings:", error); |
| 39 | } |
| 40 | |
| 41 | let recentlyEditedFilesChunks: Chunk[] = []; |
| 42 | try { |
| 43 | recentlyEditedFilesChunks = |
| 44 | await this.retrieveAndChunkRecentlyEditedFiles(recentlyEditedNFinal); |
| 45 | } catch (error) { |
| 46 | await Telemetry.captureError( |
| 47 | "no_reranker_recently_edited_retrieval", |
| 48 | error, |
| 49 | ); |
| 50 | console.error("Error retrieving recently edited files:", error); |
| 51 | } |
| 52 | |
| 53 | let repoMapChunks: Chunk[] = []; |
| 54 | try { |
| 55 | repoMapChunks = await requestFilesFromRepoMap( |
| 56 | this.options.llm, |
| 57 | this.options.config, |
| 58 | this.options.ide, |
| 59 | input, |
| 60 | filterDirectory, |
| 61 | ); |
| 62 | } catch (error) { |
| 63 | await Telemetry.captureError("no_reranker_repo_map_retrieval", error); |
| 64 | console.error("Error retrieving repo map chunks:", error); |
| 65 | } |
| 66 | |
| 67 | if (this.options.config.experimental?.codebaseToolCallingOnly) { |
| 68 | let toolBasedChunks: Chunk[] = []; |
| 69 | try { |
| 70 | toolBasedChunks = await this.retrieveWithTools(input); |
| 71 | } catch (error) { |
| 72 | console.error("Error retrieving tool based chunks:", error); |
| 73 | } |
| 74 | retrievalResults.push(...toolBasedChunks); |
| 75 | } else { |
| 76 | retrievalResults.push( |
| 77 | ...recentlyEditedFilesChunks, |
| 78 | ...ftsChunks, |
| 79 | ...embeddingsChunks, |
| 80 | ...repoMapChunks, |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | if (filterDirectory) { |
| 85 | // Backup if the individual retrieval methods don't listen |
| 86 | retrievalResults = retrievalResults.filter( |
| 87 | (chunk) => |
| 88 | !!findUriInDirs(chunk.filepath, [filterDirectory]).foundInDir, |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | const deduplicatedRetrievalResults: Chunk[] = |
| 93 | deduplicateChunks(retrievalResults); |
| 94 | |
| 95 | return deduplicatedRetrievalResults; |
| 96 | } |
| 97 | } |