Skip to main content
Language models have a token limit. You should not exceed the token limit. When you split your text into chunks it is therefore a good idea to count the number of tokens. There are many tokenizers. When you count tokens in your text you should use the same tokenizer as used in the language model.

tiktoken

tiktoken is a fast BPE tokenizer created by OpenAI.
We can use tiktoken to estimate tokens used. It will probably be more accurate for the OpenAI models.
  1. How the text is split: by character passed in.
  2. How the chunk size is measured: by tiktoken tokenizer.
CharacterTextSplitter, RecursiveCharacterTextSplitter, and TokenTextSplitter can be used with tiktoken directly.
To split with a CharacterTextSplitter and then merge chunks with tiktoken, use its .from_tiktoken_encoder() method. Note that splits from this method can be larger than the chunk size measured by the tiktoken tokenizer. The .from_tiktoken_encoder() method takes either encoding_name as an argument (e.g. cl100k_base), or the model_name (e.g. gpt-4). All additional arguments like chunk_size, chunk_overlap, and separators are used to instantiate CharacterTextSplitter:
To implement a hard constraint on the chunk size, we can use RecursiveCharacterTextSplitter.from_tiktoken_encoder, where each split will be recursively split if it has a larger size:
We can also load a TokenTextSplitter splitter, which works with tiktoken directly and will ensure each split is smaller than chunk size.
Some written languages (e.g. Chinese and Japanese) have characters which encode to two or more tokens. Using the TokenTextSplitter directly can split the tokens for a character between two chunks causing malformed Unicode characters. Use RecursiveCharacterTextSplitter.from_tiktoken_encoder or CharacterTextSplitter.from_tiktoken_encoder to ensure chunks contain valid Unicode strings.

spaCy

spaCy is an open-source software library for advanced natural language processing, written in the programming languages Python and Cython.
LangChain implements splitters based on the spaCy tokenizer.
  1. How the text is split: by spaCy tokenizer.
  2. How the chunk size is measured: by number of characters.

SentenceTransformers

The SentenceTransformersTokenTextSplitter is a specialized text splitter for use with the sentence-transformer models. The default behaviour is to split the text into chunks that fit the token window of the sentence transformer model that you would like to use. To split text and constrain token counts according to the sentence-transformers tokenizer, instantiate a SentenceTransformersTokenTextSplitter. You can optionally specify:
  • chunk_overlap: integer count of token overlap;
  • model_name: sentence-transformer model name, defaulting to "sentence-transformers/all-mpnet-base-v2";
  • tokens_per_chunk: desired token count per chunk.

NLTK

The Natural Language Toolkit, or more commonly NLTK, is a suite of libraries and programs for symbolic and statistical natural language processing (NLP) for English written in the Python programming language.
Rather than just splitting on “\n\n”, we can use NLTK to split based on NLTK tokenizers.
  1. How the text is split: by NLTK tokenizer.
  2. How the chunk size is measured: by number of characters.

KoNLPY

KoNLPy: Korean NLP in Python is a Python package for natural language processing (NLP) of the Korean language.
Token splitting involves the segmentation of text into smaller, more manageable units called tokens. These tokens are often words, phrases, symbols, or other meaningful elements crucial for further processing and analysis. In languages like English, token splitting typically involves separating words by spaces and punctuation marks. The effectiveness of token splitting largely depends on the tokenizer’s understanding of the language structure, ensuring the generation of meaningful tokens. Since tokenizers designed for the English language are not equipped to understand the unique semantic structures of other languages, such as Korean, they cannot be effectively used for Korean language processing.

Token splitting for korean with KoNLPy’s kkma analyzer

In case of Korean text, KoNLPY includes at morphological analyzer called Kkma (Korean Knowledge Morpheme Analyzer). Kkma provides detailed morphological analysis of Korean text. It breaks down sentences into words and words into their respective morphemes, identifying parts of speech for each token. It can segment a block of text into individual sentences, which is particularly useful for processing long texts.

Usage considerations

While Kkma is renowned for its detailed analysis, it is important to note that this precision may impact processing speed. Thus, Kkma is best suited for applications where analytical depth is prioritized over rapid text processing.

Hugging Face tokenizer

Hugging Face has many tokenizers. We use Hugging Face tokenizer, the GPT2TokenizerFast to count the text length in tokens.
  1. How the text is split: by character passed in.
  2. How the chunk size is measured: by number of tokens calculated by the Hugging Face tokenizer.