Configurable maxChunkSize for embedding providers (#1746)

* Add `maxChunkSize` to `EmbeddingsProvider`

* Add `maxChunkSize` to schema
This commit is contained in:
Lukas Kreussel 2024-07-16 18:05:19 +02:00 • committed by GitHub
parent 0e6b1bb064
commit 521669074e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 34 additions and 13 deletions

2
core/index.d.ts vendored
View file

@ -743,6 +743,7 @@ export interface EmbedOptions {
apiType?: string;
apiVersion?: string;
requestOptions?: RequestOptions;
maxChunkSize?: number;
}
export interface EmbeddingsProviderDescription extends EmbedOptions {
@ -751,6 +752,7 @@ export interface EmbeddingsProviderDescription extends EmbedOptions {
export interface EmbeddingsProvider {
id: string;
maxChunkSize: number;
embed(chunks: string[]): Promise<number[][]>;
}

View file

@ -36,6 +36,7 @@ export class CodebaseIndexer {
new ChunkCodebaseIndex(
this.ide.readFile.bind(this.ide),
this.continueServerClient,
config.embeddingsProvider.maxChunkSize,
), // Chunking must come first
new LanceDbIndex(
config.embeddingsProvider,

View file

@ -9,7 +9,6 @@ import {
IndexTag,
IndexingProgressUpdate,
} from "../index.js";
import { MAX_CHUNK_SIZE } from "../llm/constants.js";
import { getBasename } from "../util/index.js";
import { getLanceDbPath, migrate } from "../util/paths.js";
import { chunkDocument } from "./chunk/chunk.js";
@ -36,8 +35,6 @@ export class LanceDbIndex implements CodebaseIndex {
return `vectordb::${this.embeddingsProvider.id}`;
}
static MAX_CHUNK_SIZE = MAX_CHUNK_SIZE;
constructor(
private readonly embeddingsProvider: EmbeddingsProvider,
private readonly readFile: (filepath: string) => Promise<string>,
@ -102,7 +99,7 @@ export class LanceDbIndex implements CodebaseIndex {
for await (const chunk of chunkDocument(
items[i].path,
content,
LanceDbIndex.MAX_CHUNK_SIZE,
this.embeddingsProvider.maxChunkSize,
items[i].cacheKey,
)) {
if (chunk.content.length == 0) {

View file

@ -1,6 +1,5 @@
import { IContinueServerClient } from "../../continueServer/interface.js";
import { Chunk, IndexTag, IndexingProgressUpdate } from "../../index.js";
import { MAX_CHUNK_SIZE } from "../../llm/constants.js";
import { getBasename } from "../../util/index.js";
import { DatabaseConnection, SqliteDb, tagToString } from "../refreshIndex.js";
import {
@ -19,6 +18,7 @@ export class ChunkCodebaseIndex implements CodebaseIndex {
constructor(
private readonly readFile: (filepath: string) => Promise<string>,
private readonly continueServerClient: IContinueServerClient,
private readonly maxChunkSize: number
) {
this.readFile = readFile;
}
@ -108,7 +108,7 @@ export class ChunkCodebaseIndex implements CodebaseIndex {
for await (const chunk of chunkDocument(
item.path,
contents[i],
MAX_CHUNK_SIZE,
this.maxChunkSize,
item.cacheKey,
)) {
handleChunk(chunk);

View file

@ -1,5 +1,4 @@
import { Chunk, ChunkWithoutID } from "../../index.js";
import { MAX_CHUNK_SIZE } from "../../llm/constants.js";
import { countTokens } from "../../llm/countTokens.js";
import { supportedLanguages } from "../../util/treeSitter.js";
import { basicChunker } from "./basic.js";
@ -43,7 +42,7 @@ export async function* chunkDocument(
contents,
maxChunkSize,
)) {
if (countTokens(chunkWithoutId.content) > MAX_CHUNK_SIZE) {
if (countTokens(chunkWithoutId.content) > maxChunkSize) {
console.warn(
`Chunk with more than ${maxChunkSize} tokens constructed: `,
filepath,

View file

@ -253,7 +253,7 @@ export class DocsService {
try {
const subpathEmbeddings = await embeddingsProvider.embed(
chunkArticle(article).map((chunk) => {
chunkArticle(article, embeddingsProvider.maxChunkSize).map((chunk) => {
chunks.push(chunk);
return chunk.content;

View file

@ -1,7 +1,6 @@
import { Readability } from "@mozilla/readability";
import { JSDOM } from "jsdom";
import { Chunk } from "../../index.js";
import { MAX_CHUNK_SIZE } from "../../llm/constants.js";
import { cleanFragment, cleanHeader } from "../chunk/markdown.js";
import { PageData } from "./crawl.js";
@ -21,6 +20,7 @@ function breakdownArticleComponent(
url: string,
article: ArticleComponent,
subpath: string,
max_chunk_size: number,
): Chunk[] {
const chunks: Chunk[] = [];
@ -32,7 +32,7 @@ function breakdownArticleComponent(
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (content.length + line.length <= MAX_CHUNK_SIZE) {
if (content.length + line.length <= max_chunk_size) {
content += `${line}\n`;
endLine = i;
} else {
@ -79,7 +79,10 @@ function breakdownArticleComponent(
return chunks.filter((c) => c.content.trim().length > 20);
}
export function chunkArticle(articleResult: Article): Chunk[] {
export function chunkArticle(
articleResult: Article,
maxChunkSize: number,
): Chunk[] {
let chunks: Chunk[] = [];
for (const article of articleResult.article_components) {
@ -87,6 +90,7 @@ export function chunkArticle(articleResult: Article): Chunk[] {
articleResult.url,
article,
articleResult.subpath,
maxChunkSize,
);
chunks = [...chunks, ...articleChunks];
}

View file

@ -4,6 +4,8 @@ import {
FetchFunction,
} from "../../index.js";
import { MAX_CHUNK_SIZE } from "../../llm/constants.js";
export interface IBaseEmbeddingsProvider extends EmbeddingsProvider {
options: EmbedOptions;
fetch: FetchFunction;
@ -29,11 +31,20 @@ abstract class BaseEmbeddingsProvider implements IBaseEmbeddingsProvider {
...options,
};
this.fetch = fetch;
this.id = `${this.constructor.name}::${this.options.model}`;
// Include the `max_chunk_size` if it is not the default, since we need to create other indices for different chunk_sizes
if (this.maxChunkSize !== MAX_CHUNK_SIZE) {
this.id = `${this.constructor.name}::${this.options.model}::${this.maxChunkSize}`;
} else {
this.id = `${this.constructor.name}::${this.options.model}`;
}
}
abstract embed(chunks: string[]): Promise<number[][]>;
get maxChunkSize(): number {
return this.options.maxChunkSize ?? MAX_CHUNK_SIZE;
}
static getBatchedChunks(chunks: string[]): string[][] {
if (!this.maxBatchSize) {
console.warn(

View file

@ -1906,6 +1906,13 @@
"title": "Request Options",
"description": "Request options to be used in any fetch requests made by the embeddings provider",
"$ref": "#/definitions/RequestOptions"
},
"maxChunkSize": {
"title": "Maximum Chunk Size",
"description": "The maximum number of tokens that each chunk of a document is allowed to have",
"type": "integer",
"minimum": 128,
"exclusiveMaximum": 2147483647
}
},
"required": ["provider"],