Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds token-based chunking support to
chunk_by_title()andchunk_elements()using tiktoken, allowing users to specifymax_tokensinstead ofmax_charactersfor better alignment with LLM token limits.Closes #4127
Changes
New Parameters
max_tokensmax_characters)new_after_n_tokenstokenizer"cl100k_base") or model names ("gpt-4")Implementation Details
measure()method toChunkingOptionsthat returns chars or tokens based on modemax_tokensandmax_characterscannot be used together_split_by_tokens()method uses separator preferences with binary search fallbackFiles Changed
requirements/extra-chunking-tokens.in- New tiktoken dependencysetup.py- Addedchunking-tokensextraunstructured/chunking/base.py- Core token-based chunking logicunstructured/chunking/title.py- Updatedchunk_by_title()signatureunstructured/chunking/basic.py- Updatedchunk_elements()signaturetest_unstructured/chunking/test_base.py- Unit teststest_unstructured/chunking/test_title.py- Integration testsUsage
Installation
To use token-based chunking, install with the new extra:
pip install "unstructured[chunking-tokens]"