semantic-text-splitter
Large language models (LLMs) can be used for many tasks, but often have a limited context size that can be smaller than documents you might want to use. To use documents of larger length, you often have to split your text into chunks to fit within this context size.
This crate provides methods for splitting longer pieces of text into smaller chunks, aiming to maximize a desired chunk size, but still splitting at semantically sensible boundaries whenever possible.
Get Started
By Number of Characters
from semantic_text_splitter import CharacterTextSplitter
# Maximum number of characters in a chunk
max_characters = 1000
# Optionally can also have the splitter not trim whitespace for you
splitter = CharacterTextSplitter(trim_chunks=False)
chunks = splitter.chunks("your document text", max_characters)
With Huggingface Tokenizer
from semantic_text_splitter import HuggingFaceTextSplitter
from tokenizers import Tokenizer
# Maximum number of tokens in a chunk
max_characters = 1000
# Optionally can also have the splitter not trim whitespace for you
tokenizer = Tokenizer.from_pretrained("bert-base-uncased")
splitter = HuggingFaceTextSplitter(tokenizer, trim_chunks=False)
chunks = splitter.chunks("your document text", max_characters)
With Tiktoken Tokenizer
from semantic_text_splitter import TiktokenTextSplitter
# Maximum number of tokens in a chunk
max_tokens = 1000
# Optionally can also have the splitter not trim whitespace for you
splitter = TiktokenTextSplitter("gpt-3.5-turbo", trim_chunks=False)
chunks = splitter.chunks("your document text", max_tokens)
Using a Range for Chunk Capacity
You also have the option of specifying your chunk capacity as a range.
Once a chunk has reached a length that falls within the range it will be returned.
It is always possible that a chunk may be returned that is less than the start value, as adding the next piece of text may have made it larger than the end capacity.
from semantic_text_splitter import CharacterTextSplitter
# Optionally can also have the splitter trim whitespace for you
splitter = CharacterTextSplitter()
# Maximum number of characters in a chunk. Will fill up the
# chunk until it is somewhere in this range.
chunks = splitter.chunks("your document text", chunk_capacity=(200, 1000))
Method
To preserve as much semantic meaning within a chunk as possible, a recursive approach is used, starting at larger semantic units and, if that is too large, breaking it up into the next largest unit. Here is an example of the steps used:
- Split the text by a given level
- For each section, does it fit within the chunk size?
- Yes. Merge as many of these neighboring sections into a chunk as possible to maximize chunk length.
- No. Split by the next level and repeat.
The boundaries used to split the text if using the top-level chunks method, in descending length:
- Descending sequence length of newlines. (Newline is
\r\n,\n, or\r) Each unique length of consecutive newline sequences is treated as its own semantic level. - Unicode Sentence Boundaries
- Unicode Word Boundaries
- Unicode Grapheme Cluster Boundaries
- Characters
Splitting doesn't occur below the character level, otherwise you could get partial bytes of a char, which may not be a valid unicode str.
Note on sentences: There are lots of methods of determining sentence breaks, all to varying degrees of accuracy, and many requiring ML models to do so. Rather than trying to find the perfect sentence breaks, we rely on unicode method of sentence boundaries, which in most cases is good enough for finding a decent semantic breaking point if a paragraph is too large, and avoids the performance penalties of many other methods.
Inspiration
This crate was inspired by LangChain's TextSplitter. But, looking into the implementation, there was potential for better performance as well as better semantic chunking.
A big thank you to the unicode-rs team for their unicode-segmentation crate that manages a lot of the complexity of matching the Unicode rules for words and sentences.
Metadata
Release files for semantic-text-splitter 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| semantic_text_splitter-0.2.2.tar.gz | 226.5 kB | Without provenance | Details for semantic_text_splitter-0.2.2.tar.gz |
Built distributions (wheels)
Total release size: 40.0 MB
Release files / semantic_text_splitter-0.2.2.tar.gz
| Download URL | semantic_text_splitter-0.2.2.tar.gz |
|---|---|
| Size | 226.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
efe1e7b9638315729254e700cf5c174342a689ec3bab667ea250128631357a7c
|
|
BLAKE2b-256 checksum How to use checksums |
a75a6eeb0369f2488d69b0bbb1dcfca116a405f6dccb89b2dea98e1947c1ef83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-win_amd64.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-win_amd64.whl |
|---|---|
| Size | 3.0 MB |
| Tags | CPython 3.7 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
c79bac326cd7db47a1c69dadd3c6354ec721d9191c0d98c997a70177e39727ea
|
|
BLAKE2b-256 checksum How to use checksums |
3a82db35cc70ea3a9dd69841de1fa5e48d2108141e5abf112afacd905422ee1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-win32.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-win32.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.7 Windows x86-32 abi3 |
|
SHA-256 checksum How to use checksums |
13cbefba3b33bcdbf7bd7b89121727c70bd7800793f13e16a819ed8f52587fec
|
|
BLAKE2b-256 checksum How to use checksums |
26c4529359b3bd9bb5edcdf9f6a49fccc80be4dca2051dd0cc7a74e4145fde1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 4.5 MB |
| Tags | CPython 3.7 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
85efe783416bcdd426db049bfc99820ed0f760cc1f3a97c40692750b005b1aca
|
|
BLAKE2b-256 checksum How to use checksums |
0bae91e5e9b12be80f5c8adc8d922d3e585ffa55f36e748762f2d33ce0711efe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl |
|---|---|
| Size | 4.8 MB |
| Tags | CPython 3.7 Linux glibc 2.17+ IBM System/390x abi3 |
|
SHA-256 checksum How to use checksums |
d3ac9843dee51a56ee178f437486194a2d74c0b83b82596fe2fe0057030adc99
|
|
BLAKE2b-256 checksum How to use checksums |
83270dfe80e09cbb72a0cc5ae3f05bf0011718bc94d1f1a0cd540a475a2e2836
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl |
|---|---|
| Size | 4.8 MB |
| Tags | CPython 3.7 Linux glibc 2.17+ PowerPC 64-le abi3 |
|
SHA-256 checksum How to use checksums |
923ebc9fb3a74fd3dd8cd188018662b8cef269179f3ee4a40c17d77a40db0eaa
|
|
BLAKE2b-256 checksum How to use checksums |
19521cd9e3910f791fd6c26a6382e7c3027228a1a409cf30584ab1f6a4f7e140
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl |
|---|---|
| Size | 4.3 MB |
| Tags | CPython 3.7 Linux glibc 2.17+ ARMv7l abi3 |
|
SHA-256 checksum How to use checksums |
2b640e6b10da83cf8394d23d3ee7294e05e801147eb0f5b65b770b335d71269a
|
|
BLAKE2b-256 checksum How to use checksums |
e09154f8f70d816e95abdd01a6a658a642ed645e37297bfd701c2441abc8dec5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 4.4 MB |
| Tags | CPython 3.7 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
527f6c214d0f0f714bbe0422368ecf1b7cadbbe96856d64b79e4102057d29af8
|
|
BLAKE2b-256 checksum How to use checksums |
80d8ef690fd17f9e5440bd07383a6ea9e61d0db5dcdac393d48f8d1d89cff5ef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-manylinux_2_12_i686.manylinux2010_i686.whl |
|---|---|
| Size | 4.5 MB |
| Tags | CPython 3.7 Linux glibc 2.12+ x86-32 abi3 |
|
SHA-256 checksum How to use checksums |
adede2e002934dab5004c65dc35479eb48c082b4a7e4446eb7c4ed7533c579fc
|
|
BLAKE2b-256 checksum How to use checksums |
49360456e28718f23c7fe1549910582330498157cf2b69ca18be29428af8d26f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-macosx_11_0_arm64.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 3.3 MB |
| Tags | CPython 3.7 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
4d73cdef3a7880c2051597c062d6b90c45b1a113b59aa62b4aabfb60e717b507
|
|
BLAKE2b-256 checksum How to use checksums |
d1c6fd17feb7aca4a4b2fbe8f33eae85568ef8e85ff1ed5fdd9b500f4ee5669e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Release files / semantic_text_splitter-0.2.2-cp37-abi3-macosx_10_7_x86_64.whl
| Download URL | semantic_text_splitter-0.2.2-cp37-abi3-macosx_10_7_x86_64.whl |
|---|---|
| Size | 3.4 MB |
| Tags | CPython 3.7 abi3 macOS 10.7+ x86-64 |
|
SHA-256 checksum How to use checksums |
452b993ee4cc756ba68059dad8115063252badf88fc37bd92240cc216d7ce843
|
|
BLAKE2b-256 checksum How to use checksums |
180f148f0105ca0ff6d8c9048194454e1907a7f5887ce9b4edb7d87b656051f3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.1.0
|
Security for semantic-text-splitter 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.