Add message limit to chat history management

Updated `ChatService` to enforce a message limit based on the new `MessageLimit` property in `AppSettings`. Excess messages are removed before updating the cache to optimize performance. Adjusted `appsettings.json` to reflect the new configuration, changing `MaxInputTokens` from 16385 to 16384 and adding `MessageLimit` with a default value of 20.
This commit is contained in:
Marco Minerva
2025-02-20 10:30:55 +01:00
parent 596aa7cf6f
commit 8472775333
3 changed files with 13 additions and 3 deletions
@@ -156,7 +156,14 @@ public class ChatService(IChatCompletionService chatCompletionService, Tokenizer
} }
private async Task UpdateCacheAsync(Guid conversationId, ChatHistory chat, CancellationToken cancellationToken) private async Task UpdateCacheAsync(Guid conversationId, ChatHistory chat, CancellationToken cancellationToken)
=> await cache.SetAsync(conversationId.ToString(), chat, cancellationToken: cancellationToken); {
if (chat.Count > appSettings.MessageLimit)
{
chat.RemoveRange(0, chat.Count - appSettings.MessageLimit);
}
await cache.SetAsync(conversationId.ToString(), chat, cancellationToken: cancellationToken);
}
private async Task<ChatHistory> GetChatHistoryAsync(Guid conversationId, CancellationToken cancellationToken) private async Task<ChatHistory> GetChatHistoryAsync(Guid conversationId, CancellationToken cancellationToken)
{ {
@@ -15,4 +15,6 @@ public class AppSettings
public int MaxOutputTokens { get; init; } = 800; public int MaxOutputTokens { get; init; } = 800;
public TimeSpan MessageExpiration { get; init; } public TimeSpan MessageExpiration { get; init; }
public int MessageLimit { get; set; } = 20;
} }
+3 -2
View File
@@ -24,9 +24,10 @@
"MaxTokensPerParagraph": 1000, "MaxTokensPerParagraph": 1000,
"OverlapTokens": 100, "OverlapTokens": 100,
"MaxRelevantChunks": 10, "MaxRelevantChunks": 10,
"MaxInputTokens": 16385, "MaxInputTokens": 16384,
"MaxOutputTokens": 800, "MaxOutputTokens": 800,
"MessageExpiration": "00:05:00" "MessageExpiration": "00:05:00",
"MessageLimit": 20
}, },
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {