Strategies
Sentence
One chunk per sentence, or consecutive sentences in the same section packed up to a size limit.
sentence gives one chunk per sentence. With size, it packs consecutive sentences of the same section into chunks of up to size characters.
const { chunks } = await chunkDocument( { path: "transcript.txt" }, { strategy: "sentence" },);When to use it
Section titled “When to use it”Use sentence without size for very precise search, where each result should be one statement. Use it with size for text without much structure, such as transcripts, emails, or articles written as long runs of paragraphs, when you want chunks that always end at the end of a sentence.
One-sentence chunks often lack context for the model. If you search on sentences but want to send more text to the model, use sentence-window.
How it works
Section titled “How it works”- The document is split into sentences with the JavaScript
Intl.SegmenterAPI. Each list item gives at least one sentence, and a whole table or code block counts as one sentence. - Headings are not sentences, so they aren’t in any chunk’s text. Each chunk’s
headingPathstill lists them. - Without
size, each sentence is a chunk. Withsize, consecutive sentences in the same section are packed together up tosize. - A sentence larger than
sizeis split at spaces. A table or code block larger thansizeis split by rows or lines.
Example
Section titled “Example”# Returns policy
You can return most items within 30 days of delivery. Items must be unused and in their original packaging.
## Start a return
1. Sign in and open **Orders**.2. Choose the item you want to return.3. Print the prepaid shipping label.
## Refunds
We refund the original payment method within 5 business days of receiving the item. Shipping costs are not refunded.
| Item | Refund || --- | --- || Unused item | Full price || Opened item | 80% |Each sentence, list item, and the table become chunks. The headings are left out:
import { chunkDocument } from "docchunk";
const { chunks } = await chunkDocument( { path: "returns-policy.md" }, { strategy: "sentence" },);
for (const chunk of chunks) { console.log(chunk.text); console.log("---");}You can return most items within 30 days of delivery.---Items must be unused and in their original packaging.---1. Sign in and open **Orders**.---2. Choose the item you want to return.---3. Print the prepaid shipping label.---We refund the original payment method within 5 business days of receiving the item.---Shipping costs are not refunded.---| Item | Refund || --- | --- || Unused item | Full price || Opened item | 80% |---With size: 150, the sentences of each section are packed together:
import { chunkDocument } from "docchunk";
const { chunks } = await chunkDocument( { path: "returns-policy.md" }, { strategy: "sentence", size: 150 },);
for (const chunk of chunks) { console.log(chunk.text); console.log("---");}You can return most items within 30 days of delivery. Items must be unused and in their original packaging.---1. Sign in and open **Orders**.2. Choose the item you want to return.3. Print the prepaid shipping label.---We refund the original payment method within 5 business days of receiving the item. Shipping costs are not refunded.---| Item | Refund || --- | --- || Unused item | Full price || Opened item | 80% |---Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
size |
number |
No limit | Maximum characters per chunk. With a size, consecutive sentences are packed together. A positive integer. |
minSize |
number |
1 |
Chunks smaller than this are merged into a neighbor in the same section, when the result fits in size. The default merges nothing. A positive integer, at most size. |
overlapChars |
number |
0 |
Characters of whole sentences each chunk repeats from the end of the previous chunk in its section. An integer from 0, less than size. |
Good to know
Section titled “Good to know”- Abbreviations can end a sentence.
Intl.Segmentersometimes ends a sentence after an abbreviation such as “Dr.” or “e.g.”. SettingsizeorminSizehides most of these splits, because short pieces are packed or merged with their neighbors. - Headings aren’t in the text. Use
headingPath, or theheadingPrefixoption, to keep the heading context. - Sentences never cross sections. Packing and overlap stay within one section.
Related strategies
Section titled “Related strategies”sentence-window: one sentence per chunk, with the sentences around it as context.paragraph: one chunk per block.