Class TokenTextSplitter
java.lang.Object
org.springframework.ai.transformer.splitter.TextSplitter
org.springframework.ai.transformer.splitter.TokenTextSplitter
A
TextSplitter that splits text into chunks of a target size in tokens.- Author:
- Raphael Yu, Christian Tzolov, Ricken Bazolo, Seunghwan Jung, Jemin Huh
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated, for removal: This API element is subject to removal in a future version.TokenTextSplitter(boolean keepSeparator) Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.0-M3, usebuilder()instead.TokenTextSplitter(int chunkSize, int minChunkSizeChars, int minChunkLengthToEmbed, int maxNumChunks, boolean keepSeparator, List<Character> punctuationMarks) Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.0-M3, usebuilder()instead.TokenTextSplitter(com.knuddels.jtokkit.api.EncodingType encodingType) Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.0-M3, usebuilder()instead.TokenTextSplitter(com.knuddels.jtokkit.api.EncodingType encodingType, boolean keepSeparator) Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.0-M3, usebuilder()instead. -
Method Summary
Modifier and TypeMethodDescriptionstatic TokenTextSplitter.Builderbuilder()Splits text into chunks based on token count, using the overlap configured on this splitter.Splits text into chunks based on token count.protected intgetLastPunctuationIndex(String chunkText) Methods inherited from class org.springframework.ai.transformer.splitter.TextSplitter
apply, isCopyContentFormatter, setCopyContentFormatter, split, splitMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.ai.document.DocumentTransformer
transform
-
Constructor Details
-
TokenTextSplitter
Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.0-M3, usebuilder()instead. -
TokenTextSplitter
Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.0-M3, usebuilder()instead. -
TokenTextSplitter
@Deprecated(since="2.0.0-M3", forRemoval=true) public TokenTextSplitter(com.knuddels.jtokkit.api.EncodingType encodingType) Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.0-M3, usebuilder()instead. -
TokenTextSplitter
@Deprecated(since="2.0.0-M3", forRemoval=true) public TokenTextSplitter(com.knuddels.jtokkit.api.EncodingType encodingType, boolean keepSeparator) Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.0-M3, usebuilder()instead. -
TokenTextSplitter
@Deprecated(since="2.0.0-M3", forRemoval=true) public TokenTextSplitter(int chunkSize, int minChunkSizeChars, int minChunkLengthToEmbed, int maxNumChunks, boolean keepSeparator, List<Character> punctuationMarks) Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.0-M3, usebuilder()instead.
-
-
Method Details
-
builder
-
splitText
- Specified by:
splitTextin classTextSplitter
-
doSplit
Splits text into chunks based on token count, using the overlap configured on this splitter.Subclasses that override this method replace the whole algorithm, so overlap does not apply to them unless they call
doSplit(String, int, int).- Parameters:
text- the text to splitchunkSize- the target chunk size in tokens- Returns:
- list of text chunks
-
doSplit
Splits text into chunks based on token count.Punctuation-based splitting only applies when the token count exceeds the chunk size (
tokens.size() > chunkSize). Text that exactly matches or is smaller than the chunk size is returned as a single chunk without punctuation-based truncation.- Parameters:
text- the text to splitchunkSize- the target chunk size in tokenschunkOverlap- the number of tokens each chunk shares with the one before it- Returns:
- list of text chunks
- Since:
- 2.1.0
-
getLastPunctuationIndex
-
builder()instead.