From 2f3121c4b9bcd26dd4ff8d43bce64008594d263a Mon Sep 17 00:00:00 2001 From: Annoying Technology Date: Thu, 5 Dec 2024 16:42:14 +0100 Subject: [PATCH] Fixes #400, a bug that sents chunks too large for the embedding model --- core/indexing/docs/article.ts | 88 +++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/core/indexing/docs/article.ts b/core/indexing/docs/article.ts index 1bc74c62e..99992b415 100644 --- a/core/indexing/docs/article.ts +++ b/core/indexing/docs/article.ts @@ -25,56 +25,74 @@ function breakdownArticleComponent( max_chunk_size: number, ): Chunk[] { const chunks: Chunk[] = []; - - const lines = article.body.split("\n"); + const words = article.body.split(/\s+/); + let currentChunk = ""; let startLine = 0; - let endLine = 0; - let content = ""; let index = 0; - for (let i = 0; i < lines.length; i++) { - const line = lines[i]; - if (content.length + line.length <= max_chunk_size) { - content += `${line}\n`; - endLine = i; - } else { - chunks.push({ - content: content.trim(), - startLine: startLine, - endLine: endLine, - otherMetadata: { - title: cleanHeader(article.title), - }, - index: index, - filepath: new URL( - `${subpath}#${cleanFragment(article.title)}`, - url, - ).toString(), - digest: subpath, - }); - content = `${line}\n`; - startLine = i; - endLine = i; - index += 1; - } - } - - // Push the last chunk - if (content) { + const createChunk = ( + content: string, + currentStartLine: number, + endLine: number, + ) => { chunks.push({ content: content.trim(), - startLine: startLine, + startLine: currentStartLine, endLine: endLine, otherMetadata: { title: cleanHeader(article.title), }, - index: index, + index: index++, filepath: new URL( `${subpath}#${cleanFragment(article.title)}`, url, ).toString(), digest: subpath, }); + }; + + for (let i = 0; i < words.length; i++) { + const word = words[i]; + + // If a single word is longer than max_chunk_size, split it + if (word.length > max_chunk_size) { + // First, push the current chunk if it has content + if (currentChunk.trim().length > 0) { + createChunk(currentChunk.trim(), startLine, i - 1); + currentChunk = ""; + } + + // Split the long word into smaller pieces + let remainingWord = word; + while (remainingWord.length > 0) { + const chunk = remainingWord.slice(0, max_chunk_size); + createChunk(chunk, i, i); + remainingWord = remainingWord.slice(max_chunk_size); + } + + startLine = i + 1; + continue; + } + + // Check if adding this word would exceed max_chunk_size + if (currentChunk.length + word.length + 1 > max_chunk_size) { + // Push current chunk if it has content + if (currentChunk.trim().length > 0) { + createChunk(currentChunk.trim(), startLine, i - 1); + } + + // Start new chunk with current word + currentChunk = word; + startLine = i; + } else { + // Add word to current chunk + currentChunk = currentChunk.length > 0 ? `${currentChunk} ${word}` : word; + } + } + + // Push the last chunk if it has content + if (currentChunk.trim().length > 0) { + createChunk(currentChunk.trim(), startLine, words.length - 1); } // Don't use small chunks. Probably they're a mistake. Definitely they'll confuse the embeddings model.