Skip to content

Commit 537a232

Browse files
authored
.Net: Updated chat history reducers to include system message (#10344)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Related: #10102 This PR contains changes to the chat history reducer implementations to avoid redaction of system messages if they exist in chat history. It's important to keep system messages in the chat history so the model can follow initial instructions independently from history redaction. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent 10c1dfe commit 537a232

8 files changed

Lines changed: 90 additions & 16 deletions

File tree

‎dotnet/src/IntegrationTests/Connectors/Google/Gemini/GeminiChatCompletionTests.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.IO;
55
using System.Linq;
66
using System.Net.Http;
7-
using System.Net.Http.Json;
87
using System.Text;
98
using System.Text.Json;
109
using System.Threading.Tasks;

‎dotnet/src/IntegrationTests/Connectors/Google/TestsBase.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System;
4-
using System.Net.Http;
5-
using System.Threading.Tasks;
64
using Microsoft.Extensions.Configuration;
75
using Microsoft.SemanticKernel.ChatCompletion;
86
using Microsoft.SemanticKernel.Connectors.Google;

‎dotnet/src/SemanticKernel.Core/AI/ChatCompletion/ChatHistoryReducerExtensions.cs‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,26 @@ internal static class ChatHistoryReducerExtensions
2020
/// <param name="chatHistory">The source history</param>
2121
/// <param name="startIndex">The index of the first message to extract</param>
2222
/// <param name="finalIndex">The index of the last message to extract</param>
23+
/// <param name="systemMessage">An optional system message content to include</param>
2324
/// <param name="filter">The optional filter to apply to each message</param>
24-
public static IEnumerable<ChatMessageContent> Extract(this IReadOnlyList<ChatMessageContent> chatHistory, int startIndex, int? finalIndex = null, Func<ChatMessageContent, bool>? filter = null)
25+
public static IEnumerable<ChatMessageContent> Extract(
26+
this IReadOnlyList<ChatMessageContent> chatHistory,
27+
int startIndex,
28+
int? finalIndex = null,
29+
ChatMessageContent? systemMessage = null,
30+
Func<ChatMessageContent, bool>? filter = null)
2531
{
2632
int maxIndex = chatHistory.Count - 1;
2733
if (startIndex > maxIndex)
2834
{
2935
yield break;
3036
}
3137

38+
if (systemMessage is not null)
39+
{
40+
yield return systemMessage;
41+
}
42+
3243
finalIndex ??= maxIndex;
3344

3445
finalIndex = Math.Min(finalIndex.Value, maxIndex);
@@ -84,9 +95,17 @@ public static int LocateSummarizationBoundary(this IReadOnlyList<ChatMessageCont
8495
/// This is useful when messages have been injected that are not part of the raw dialog
8596
/// (such as summarization).
8697
/// </param>
98+
/// <param name="hasSystemMessage">Indicates whether chat history contains system message.</param>
8799
/// <returns>An index that identifies the starting point for a reduced history that does not orphan sensitive content.</returns>
88-
public static int LocateSafeReductionIndex(this IReadOnlyList<ChatMessageContent> chatHistory, int targetCount, int? thresholdCount = null, int offsetCount = 0)
100+
public static int LocateSafeReductionIndex(
101+
this IReadOnlyList<ChatMessageContent> chatHistory,
102+
int targetCount,
103+
int? thresholdCount = null,
104+
int offsetCount = 0,
105+
bool hasSystemMessage = false)
89106
{
107+
targetCount -= hasSystemMessage ? 1 : 0;
108+
90109
// Compute the index of the truncation threshold
91110
int thresholdIndex = chatHistory.Count - (thresholdCount ?? 0) - targetCount;
92111

‎dotnet/src/SemanticKernel.Core/AI/ChatCompletion/ChatHistorySummarizationReducer.cs‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ public ChatHistorySummarizationReducer(IChatCompletionService service, int targe
8989
/// <inheritdoc/>
9090
public async Task<IEnumerable<ChatMessageContent>?> ReduceAsync(IReadOnlyList<ChatMessageContent> chatHistory, CancellationToken cancellationToken = default)
9191
{
92+
var systemMessage = chatHistory.FirstOrDefault(l => l.Role == AuthorRole.System);
93+
9294
// Identify where summary messages end and regular history begins
9395
int insertionPoint = chatHistory.LocateSummarizationBoundary(SummaryMetadataKey);
9496

9597
// First pass to determine the truncation index
96-
int truncationIndex = chatHistory.LocateSafeReductionIndex(this._targetCount, this._thresholdCount, insertionPoint);
98+
int truncationIndex = chatHistory.LocateSafeReductionIndex(
99+
this._targetCount,
100+
this._thresholdCount,
101+
insertionPoint,
102+
hasSystemMessage: systemMessage is not null);
97103

98104
IEnumerable<ChatMessageContent>? truncatedHistory = null;
99105

@@ -104,17 +110,17 @@ public ChatHistorySummarizationReducer(IChatCompletionService service, int targe
104110
chatHistory.Extract(
105111
this.UseSingleSummary ? 0 : insertionPoint,
106112
truncationIndex,
107-
(m) => m.Items.Any(i => i is FunctionCallContent || i is FunctionResultContent));
113+
filter: (m) => m.Items.Any(i => i is FunctionCallContent || i is FunctionResultContent));
108114

109115
try
110116
{
111117
// Summarize
112118
ChatHistory summarizationRequest = [.. summarizedHistory, new ChatMessageContent(AuthorRole.System, this.SummarizationInstructions)];
113-
ChatMessageContent summary = await this._service.GetChatMessageContentAsync(summarizationRequest, cancellationToken: cancellationToken).ConfigureAwait(false);
114-
summary.Metadata = new Dictionary<string, object?> { { SummaryMetadataKey, true } };
119+
ChatMessageContent summaryMessage = await this._service.GetChatMessageContentAsync(summarizationRequest, cancellationToken: cancellationToken).ConfigureAwait(false);
120+
summaryMessage.Metadata = new Dictionary<string, object?> { { SummaryMetadataKey, true } };
115121

116122
// Assembly the summarized history
117-
truncatedHistory = AssemblySummarizedHistory(summary);
123+
truncatedHistory = AssemblySummarizedHistory(summaryMessage, systemMessage);
118124
}
119125
catch
120126
{
@@ -128,8 +134,13 @@ public ChatHistorySummarizationReducer(IChatCompletionService service, int targe
128134
return truncatedHistory;
129135

130136
// Inner function to assemble the summarized history
131-
IEnumerable<ChatMessageContent> AssemblySummarizedHistory(ChatMessageContent? summary)
137+
IEnumerable<ChatMessageContent> AssemblySummarizedHistory(ChatMessageContent? summaryMessage, ChatMessageContent? systemMessage)
132138
{
139+
if (systemMessage is not null)
140+
{
141+
yield return systemMessage;
142+
}
143+
133144
if (insertionPoint > 0 && !this.UseSingleSummary)
134145
{
135146
for (int index = 0; index <= insertionPoint - 1; ++index)
@@ -138,9 +149,9 @@ IEnumerable<ChatMessageContent> AssemblySummarizedHistory(ChatMessageContent? su
138149
}
139150
}
140151

141-
if (summary != null)
152+
if (summaryMessage is not null)
142153
{
143-
yield return summary;
154+
yield return summaryMessage;
144155
}
145156

146157
for (int index = truncationIndex; index < chatHistory.Count; ++index)

‎dotnet/src/SemanticKernel.Core/AI/ChatCompletion/ChatHistoryTruncationReducer.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
67
using System.Threading;
78
using System.Threading.Tasks;
89

@@ -42,15 +43,17 @@ public ChatHistoryTruncationReducer(int targetCount, int? thresholdCount = null)
4243
/// <inheritdoc/>
4344
public Task<IEnumerable<ChatMessageContent>?> ReduceAsync(IReadOnlyList<ChatMessageContent> chatHistory, CancellationToken cancellationToken = default)
4445
{
46+
var systemMessage = chatHistory.FirstOrDefault(l => l.Role == AuthorRole.System);
47+
4548
// First pass to determine the truncation index
46-
int truncationIndex = chatHistory.LocateSafeReductionIndex(this._targetCount, this._thresholdCount);
49+
int truncationIndex = chatHistory.LocateSafeReductionIndex(this._targetCount, this._thresholdCount, hasSystemMessage: systemMessage is not null);
4750

4851
IEnumerable<ChatMessageContent>? truncatedHistory = null;
4952

5053
if (truncationIndex > 0)
5154
{
5255
// Second pass to truncate the history
53-
truncatedHistory = chatHistory.Extract(truncationIndex);
56+
truncatedHistory = chatHistory.Extract(truncationIndex, systemMessage: systemMessage);
5457
}
5558

5659
return Task.FromResult(truncatedHistory);

‎dotnet/src/SemanticKernel.UnitTests/AI/ChatCompletion/ChatHistorySummarizationReducerTests.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,27 @@ public async Task VerifyChatHistoryRereducedAsync()
224224
VerifySummarization(messages[1]);
225225
}
226226

227+
/// <summary>
228+
/// Validate history reduced and system message preserved when source history exceeds target threshold.
229+
/// </summary>
230+
[Fact]
231+
public async Task VerifySystemMessageIsNotReducedAsync()
232+
{
233+
// Arrange
234+
Mock<IChatCompletionService> mockCompletionService = this.CreateMockCompletionService();
235+
IReadOnlyList<ChatMessageContent> sourceHistory = MockChatHistoryGenerator.CreateSimpleHistory(20, includeSystemMessage: true).ToArray();
236+
ChatHistorySummarizationReducer reducer = new(mockCompletionService.Object, 10);
237+
238+
// Act
239+
IEnumerable<ChatMessageContent>? reducedHistory = await reducer.ReduceAsync(sourceHistory);
240+
241+
// Assert
242+
ChatMessageContent[] messages = VerifyReducedHistory(reducedHistory, 11);
243+
VerifySummarization(messages[1]);
244+
245+
Assert.Contains(messages, m => m.Role == AuthorRole.System);
246+
}
247+
227248
private static ChatMessageContent[] VerifyReducedHistory(IEnumerable<ChatMessageContent>? reducedHistory, int expectedCount)
228249
{
229250
Assert.NotNull(reducedHistory);

‎dotnet/src/SemanticKernel.UnitTests/AI/ChatCompletion/ChatHistoryTruncationReducerTests.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ public async Task VerifyChatHistoryRereducedAsync()
137137
VerifyReducedHistory(reducedHistory, 10);
138138
}
139139

140+
/// <summary>
141+
/// Validate history reduced and system message preserved when source history exceeds target threshold.
142+
/// </summary>
143+
[Fact]
144+
public async Task VerifySystemMessageIsNotReducedAsync()
145+
{
146+
// Arrange
147+
IReadOnlyList<ChatMessageContent> sourceHistory = MockChatHistoryGenerator.CreateSimpleHistory(20, includeSystemMessage: true).ToArray();
148+
ChatHistoryTruncationReducer reducer = new(10);
149+
150+
// Act
151+
IEnumerable<ChatMessageContent>? reducedHistory = await reducer.ReduceAsync(sourceHistory);
152+
153+
// Assert
154+
VerifyReducedHistory(reducedHistory, 10);
155+
Assert.Contains(reducedHistory!, m => m.Role == AuthorRole.System);
156+
}
157+
140158
private static void VerifyReducedHistory(IEnumerable<ChatMessageContent>? reducedHistory, int expectedCount)
141159
{
142160
Assert.NotNull(reducedHistory);

‎dotnet/src/SemanticKernel.UnitTests/AI/ChatCompletion/MockChatHistoryGenerator.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ internal static class MockChatHistoryGenerator
1414
/// <summary>
1515
/// Create a homogeneous list of assistant messages.
1616
/// </summary>
17-
public static IEnumerable<ChatMessageContent> CreateSimpleHistory(int messageCount)
17+
public static IEnumerable<ChatMessageContent> CreateSimpleHistory(int messageCount, bool includeSystemMessage = false)
1818
{
19+
if (includeSystemMessage)
20+
{
21+
yield return new ChatMessageContent(AuthorRole.System, "system message");
22+
}
23+
1924
for (int index = 0; index < messageCount; ++index)
2025
{
2126
yield return new ChatMessageContent(AuthorRole.Assistant, $"message #{index}");

0 commit comments

Comments
 (0)