Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions dotnet/src/SemanticKernel.Core/Text/TextChunker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,13 @@ private static List<string> ProcessParagraphs(List<string> paragraphs, int adjus
{
var newSecondLastParagraph = string.Join(" ", secondLastParagraphTokens);
var newLastParagraph = string.Join(" ", lastParagraphTokens);
var mergedParagraph = $"{newSecondLastParagraph} {newLastParagraph}";

paragraphs[paragraphs.Count - 2] = $"{newSecondLastParagraph} {newLastParagraph}";
paragraphs.RemoveAt(paragraphs.Count - 1);
if (GetTokenCount(mergedParagraph, tokenCounter) <= adjustedMaxTokensPerParagraph)
{
paragraphs[paragraphs.Count - 2] = mergedParagraph;
paragraphs.RemoveAt(paragraphs.Count - 1);
}
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions dotnet/src/SemanticKernel.UnitTests/Text/TextChunkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,45 @@ public void CanSplitTextParagraphsWithCustomTokenCounter()
Assert.Equal(expected, result);
}

[Fact]
public void SplitPlainTextParagraphsWithCustomTokenCounterDoesNotMergePastTokenLimit()
{
List<string> input =
[
"abcdefghijklmnopqr",
"abc"
];

var expected = new[]
{
"abcdefghijklmnopqr",
"abc"
};

var result = TextChunker.SplitPlainTextParagraphs(input, 20, tokenCounter: static (input) => input.Length);

Assert.Equal(expected, result);
}

[Fact]
public void SplitPlainTextParagraphsWithCustomTokenCounterStillMergesWhenCandidateFits()
{
List<string> input =
[
"abcdefghijklmnop",
"abc"
];

var expected = new[]
{
"abcdefghijklmnop abc"
};

var result = TextChunker.SplitPlainTextParagraphs(input, 20, tokenCounter: static (input) => input.Length);

Assert.Equal(expected, result);
}

[Fact]
public void CanSplitTextParagraphsWithOverlapAndCustomTokenCounter()
{
Expand Down
Loading