// Copyright (c) Microsoft. All rights reserved. using System.Runtime.CompilerServices; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.ChatCompletion; namespace ChatCompletion; /// /// Instance of which will invoke a delegate /// to reduce the size of the before sending it to the model. /// public sealed class ChatCompletionServiceWithReducer(IChatCompletionService service, IChatHistoryReducer reducer) : IChatCompletionService { private static IReadOnlyDictionary EmptyAttributes { get; } = new Dictionary(); public IReadOnlyDictionary Attributes => EmptyAttributes; /// public async Task> GetChatMessageContentsAsync( ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default) { var reducedMessages = await reducer.ReduceAsync(chatHistory, cancellationToken).ConfigureAwait(false); var reducedHistory = reducedMessages is null ? chatHistory : new ChatHistory(reducedMessages); return await service.GetChatMessageContentsAsync(reducedHistory, executionSettings, kernel, cancellationToken).ConfigureAwait(false); } /// public async IAsyncEnumerable GetStreamingChatMessageContentsAsync( ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) { var reducedMessages = await reducer.ReduceAsync(chatHistory, cancellationToken).ConfigureAwait(false); var history = reducedMessages is null ? chatHistory : new ChatHistory(reducedMessages); var messages = service.GetStreamingChatMessageContentsAsync(history, executionSettings, kernel, cancellationToken); await foreach (var message in messages) { yield return message; } } }