Loading...
const chunks = [];
for (let i = 0; i < text.length; i += 500) {
chunks.push(text.slice(i, i + 500));
}const paragraphs = text.split(/\n\s*\n/);
const chunks = paragraphs.map((p, i) => ({
text: [paragraphs[i - 1] ?? '', p].join('\n\n').trim(),
source: doc.url,
position: i,
}));Splitting on paragraphs avoids cutting sentences, the previous paragraph adds overlap, and metadata enables citations.
A common start is about 10 to 20 percent of the chunk size, enough to keep a fact at a boundary whole in at least one chunk.
Yes. Split code by functions or classes, not by character count, so each chunk is a complete unit.
Small chunks match queries more precisely but lose context. Large chunks keep context but dilute the match. Test both on real questions.