Fixes #400, a bug that sents chunks too large for the embedding model
This commit is contained in:
parent
abbf2fb74c
commit
2f3121c4b9
1 changed files with 53 additions and 35 deletions
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue