Merge pull request #1981 from teslamotors/user/rleidle/add-filter-for-large-files-and-no-extension
Turn of chunking for files with more than 1m characters and files lacking extensions
This commit is contained in:
commit
d8de6da30a
7 changed files with 63 additions and 12 deletions
|
|
@ -20,6 +20,7 @@ export interface RetrievalPipelineOptions {
|
|||
nRetrieve: number;
|
||||
nFinal: number;
|
||||
tags: BranchAndDir[];
|
||||
pathSep: string;
|
||||
filterDirectory?: string;
|
||||
}
|
||||
|
||||
|
|
@ -30,8 +31,10 @@ export interface IRetrievalPipeline {
|
|||
export default class BaseRetrievalPipeline implements IRetrievalPipeline {
|
||||
private lanceDbIndex: LanceDbIndex;
|
||||
constructor(protected readonly options: RetrievalPipelineOptions) {
|
||||
this.lanceDbIndex = new LanceDbIndex(options.embeddingsProvider, (path) =>
|
||||
options.ide.readFile(path),
|
||||
this.lanceDbIndex = new LanceDbIndex(
|
||||
options.embeddingsProvider,
|
||||
(path) => options.ide.readFile(path),
|
||||
options.pathSep
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -70,6 +70,7 @@ export async function retrieveContextItemsFromEmbeddings(
|
|||
nFinal,
|
||||
nRetrieve,
|
||||
tags,
|
||||
pathSep: await extras.ide.pathSep(),
|
||||
embeddingsProvider: extras.embeddingsProvider,
|
||||
reranker: extras.reranker,
|
||||
filterDirectory,
|
||||
|
|
|
|||
|
|
@ -35,16 +35,19 @@ export class CodebaseIndexer {
|
|||
|
||||
protected async getIndexesToBuild(): Promise<CodebaseIndex[]> {
|
||||
const config = await this.configHandler.loadConfig();
|
||||
const pathSep = await this.ide.pathSep();
|
||||
|
||||
const indexes = [
|
||||
new ChunkCodebaseIndex(
|
||||
this.ide.readFile.bind(this.ide),
|
||||
pathSep,
|
||||
this.continueServerClient,
|
||||
config.embeddingsProvider.maxChunkSize,
|
||||
), // Chunking must come first
|
||||
new LanceDbIndex(
|
||||
config.embeddingsProvider,
|
||||
this.ide.readFile.bind(this.ide),
|
||||
pathSep,
|
||||
this.continueServerClient,
|
||||
),
|
||||
new FullTextSearchCodebaseIndex(),
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import {
|
|||
} from "../index.js";
|
||||
import { getBasename } from "../util/index.js";
|
||||
import { getLanceDbPath, migrate } from "../util/paths.js";
|
||||
import { chunkDocument } from "./chunk/chunk.js";
|
||||
import { chunkDocument, shouldChunk } from "./chunk/chunk.js";
|
||||
import { DatabaseConnection, SqliteDb, tagToString } from "./refreshIndex.js";
|
||||
import {
|
||||
CodebaseIndex,
|
||||
|
|
@ -40,6 +40,7 @@ export class LanceDbIndex implements CodebaseIndex {
|
|||
constructor(
|
||||
private readonly embeddingsProvider: EmbeddingsProvider,
|
||||
private readonly readFile: (filepath: string) => Promise<string>,
|
||||
private readonly pathSep: string,
|
||||
private readonly continueServerClient?: IContinueServerClient,
|
||||
) {}
|
||||
|
||||
|
|
@ -86,6 +87,9 @@ export class LanceDbIndex implements CodebaseIndex {
|
|||
|
||||
private async packToRows(item: PathAndCacheKey): Promise<LanceDbRow[]> {
|
||||
const content = await this.readFile(item.path);
|
||||
if (!shouldChunk(this.pathSep, item.path, content)) {
|
||||
return [];
|
||||
}
|
||||
const chunks: Chunk[] = [];
|
||||
const chunkParams = {
|
||||
filepath: item.path,
|
||||
|
|
@ -99,10 +103,6 @@ export class LanceDbIndex implements CodebaseIndex {
|
|||
throw new Error("did not chunk properly");
|
||||
}
|
||||
chunks.push(chunk);
|
||||
if (chunks.length > 20) {
|
||||
// Too many chunks to index, probably a larger file than we want to include
|
||||
throw new Error("too large to index");
|
||||
}
|
||||
}
|
||||
const embeddings = await this.chunkListToEmbedding(chunks);
|
||||
if (chunks.length !== embeddings.length) {
|
||||
|
|
@ -266,8 +266,6 @@ export class LanceDbIndex implements CodebaseIndex {
|
|||
status: "indexing",
|
||||
};
|
||||
|
||||
console.log(results.compute);
|
||||
|
||||
const dbRows = await this.computeRows(results.compute);
|
||||
await this.insertRows(sqlite, dbRows);
|
||||
await Promise.all(
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import {
|
|||
RefreshIndexResults,
|
||||
type CodebaseIndex,
|
||||
} from "../types.js";
|
||||
import { chunkDocument } from "./chunk.js";
|
||||
import { chunkDocument, shouldChunk } from "./chunk.js";
|
||||
|
||||
export class ChunkCodebaseIndex implements CodebaseIndex {
|
||||
relativeExpectedTime: number = 1;
|
||||
|
|
@ -20,6 +20,7 @@ export class ChunkCodebaseIndex implements CodebaseIndex {
|
|||
|
||||
constructor(
|
||||
private readonly readFile: (filepath: string) => Promise<string>,
|
||||
private readonly pathSep: string,
|
||||
private readonly continueServerClient: IContinueServerClient,
|
||||
private readonly maxChunkSize: number,
|
||||
) {
|
||||
|
|
@ -154,7 +155,7 @@ export class ChunkCodebaseIndex implements CodebaseIndex {
|
|||
|
||||
private async packToChunks(pack: PathAndCacheKey): Promise<Chunk[]> {
|
||||
const contents = await this.readFile(pack.path);
|
||||
if (!contents.length) {
|
||||
if (!shouldChunk(this.pathSep, pack.path, contents)) {
|
||||
return [];
|
||||
}
|
||||
const chunks: Chunk[] = [];
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Chunk, ChunkWithoutID } from "../../index.js";
|
||||
import { Chunk, ChunkWithoutID, IDE } from "../../index.js";
|
||||
import { countTokens, countTokensAsync } from "../../llm/countTokens.js";
|
||||
import { supportedLanguages } from "../../util/treeSitter.js";
|
||||
import { basicChunker } from "./basic.js";
|
||||
|
|
@ -77,3 +77,16 @@ export async function* chunkDocument({
|
|||
yield chunk;
|
||||
}
|
||||
}
|
||||
|
||||
export function shouldChunk(pathSep: string, filepath: string, contents: string): boolean {
|
||||
if (contents.length > 1000000) {
|
||||
// if a file has more than 1m characters then skip it
|
||||
return false;
|
||||
}
|
||||
if (contents.length === 0) {
|
||||
return false;
|
||||
}
|
||||
const basename = filepath.split(pathSep).pop();
|
||||
// files without extensions are often binary files, skip it if so
|
||||
return basename?.includes(".") ?? false;
|
||||
}
|
||||
32
core/test/indexing/chunk/chunk.test.ts
Normal file
32
core/test/indexing/chunk/chunk.test.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { shouldChunk } from "../../../indexing/chunk/chunk";
|
||||
import path from "path";
|
||||
|
||||
describe("shouldChunk", () => {
|
||||
test("should chunk a typescript file", () => {
|
||||
const filePath = "directory/file.ts";
|
||||
const fileContent = generateString(10000);
|
||||
expect(shouldChunk(path.sep, filePath, fileContent)).toBe(true);
|
||||
});
|
||||
|
||||
test("should not chunk a large typescript file", () => {
|
||||
const filePath = "directory/file.ts";
|
||||
const fileContent = generateString(1500000);
|
||||
expect(shouldChunk(path.sep, filePath, fileContent)).toBe(false);
|
||||
});
|
||||
|
||||
test("should not chunk an empty file", () => {
|
||||
const filePath = "directory/file.ts";
|
||||
const fileContent = generateString(0);
|
||||
expect(shouldChunk(path.sep, filePath, fileContent)).toBe(false);
|
||||
});
|
||||
|
||||
test("should not chunk a file without extension", () => {
|
||||
const filePath = "directory/with.dot/filename";
|
||||
const fileContent = generateString(10000);
|
||||
expect(shouldChunk(path.sep, filePath, fileContent)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
function generateString(length: number) {
|
||||
return "a".repeat(length);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue