feat: add SemanticTextSplitter using EmbeddingModel cosine similarity#5816
Open
anuragg-saxenaa wants to merge 1 commit intospring-projects:mainfrom
Open
feat: add SemanticTextSplitter using EmbeddingModel cosine similarity#5816anuragg-saxenaa wants to merge 1 commit intospring-projects:mainfrom
anuragg-saxenaa wants to merge 1 commit intospring-projects:mainfrom
Conversation
Closes spring-projects#5464 ## Summary Add a new TextSplitter that uses semantic similarity rather than fixed token counts to determine chunk boundaries, improving RAG retrieval quality. ## Implementation - SemanticTextSplitter extends the existing TextSplitter base class - Splits input into sentences on sentence-ending punctuation (.!?) - Embeds each sentence via the injected EmbeddingModel - Computes cosine similarity between consecutive sentence embeddings - Starts a new chunk when similarity < similarityThreshold OR the accumulated buffer would exceed maxChunkSize characters - Defaults: similarityThreshold=0.5, maxChunkSize=1000 - No new external dependencies — uses Spring AI's own EmbeddingModel ## Tests (SemanticTextSplitterTests) - Empty/blank input → empty list - Single sentence → single chunk - Identical embeddings (sim=1.0) → sentences merged - Orthogonal embeddings (sim=0.0) → sentences split - maxChunkSize forces split even when similarity is high - Threshold boundary: exactly at sim keeps together, just above splits - Document-level split preserves metadata - cosineSimilarity helper: identical, orthogonal, zero vectors - Constructor validation: null model, negative/over-1 threshold, zero size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Add
SemanticTextSplitter— a newTextSplitterthat uses semantic similarity rather than fixed token counts to determine chunk boundaries.Closes #5464
Motivation
TokenTextSplittersplits at fixed token counts, often breaking sentences mid-thought and degrading RAG retrieval quality. Users frequently reach for external tools (e.g., Docling) or write custom solutions. A native semantic splitter addresses this gap without introducing new external dependencies.Implementation
SemanticTextSplitterextends the existingTextSplitterbase class:.,!,?followed by whitespace.EmbeddingModel.similarityThresholdor the buffer would exceedmaxChunkSizecharacters.Defaults:
similarityThreshold = 0.5,maxChunkSize = 1000.No new dependencies — reuses Spring AI's own
EmbeddingModel.Example
Tests (
SemanticTextSplitterTests)emptyTextReturnsEmptyListsingleSentenceReturnsSingleChunkhighSimilarityKeepsSentencesTogetherlowSimilaritySplitsSentencesmaxChunkSizeForcesNewChunkthresholdExactlyAtSimilarityKeepsTogethersim >= thresholdthresholdJustAboveSimilaritySplitssim < thresholdsplitDocumentPreservesMetadatacosineSimilarity*(×3)