From f8567c6190e8c1787492cea3e0295aa10d2f7e4d Mon Sep 17 00:00:00 2001 From: Teresa Hoang <125500434+teresaqhoang@users.noreply.github.com> Date: Mon, 5 Jun 2023 12:57:27 -0700 Subject: [PATCH] Adding local ToListAsync method in Copilot Chat (#1346) ### Motivation and Context The `System.Linq.Async` package reference was removed from the core libraries so that everyone using SK wasn't forced to use it as well. This adds a helper `ToListAsync` method to enable async conversion of `IAsyncEnumerable` to `List` object. ### Description Was seeing the following error when using local project references, so this is an effort to future proof any SK nuget updates `'IAsyncEnumerable' does not contain a definition for 'ToListAsync' and no accessible extension method 'ToListAsync' accepting a first argument of type 'IAsyncEnumerable' could be found (are you missing a using directive or an assembly reference?) ` --- .../CopilotChat/Controllers/BotController.cs | 1 + .../Extensions/IAsyncEnumerableExtensions.cs | 26 +++++++++++++++++++ .../ChatSkills/SemanticChatMemoryExtractor.cs | 3 ++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 samples/apps/copilot-chat-app/webapi/CopilotChat/Extensions/IAsyncEnumerableExtensions.cs diff --git a/samples/apps/copilot-chat-app/webapi/CopilotChat/Controllers/BotController.cs b/samples/apps/copilot-chat-app/webapi/CopilotChat/Controllers/BotController.cs index a81baafd24c0..6d62de1b1bf0 100644 --- a/samples/apps/copilot-chat-app/webapi/CopilotChat/Controllers/BotController.cs +++ b/samples/apps/copilot-chat-app/webapi/CopilotChat/Controllers/BotController.cs @@ -13,6 +13,7 @@ using Microsoft.Extensions.Options; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Memory; +using SemanticKernel.Service.CopilotChat.Extensions; using SemanticKernel.Service.CopilotChat.Models; using SemanticKernel.Service.CopilotChat.Options; using SemanticKernel.Service.CopilotChat.Storage; diff --git a/samples/apps/copilot-chat-app/webapi/CopilotChat/Extensions/IAsyncEnumerableExtensions.cs b/samples/apps/copilot-chat-app/webapi/CopilotChat/Extensions/IAsyncEnumerableExtensions.cs new file mode 100644 index 000000000000..033364acc2e2 --- /dev/null +++ b/samples/apps/copilot-chat-app/webapi/CopilotChat/Extensions/IAsyncEnumerableExtensions.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace SemanticKernel.Service.CopilotChat.Extensions; + +/// +/// Extension methods for enabling async LINQ operations on IAsyncEnumerable sequence. +/// +public static class IAsyncEnumerableExtensions +{ + /// + /// Creates a List from an IAsyncEnumerable by enumerating it asynchronously. + /// + internal static async Task> ToListAsync(this IAsyncEnumerable source) + { + var result = new List(); + await foreach (var item in source.ConfigureAwait(false)) + { + result.Add(item); + } + + return result; + } +} diff --git a/samples/apps/copilot-chat-app/webapi/CopilotChat/Skills/ChatSkills/SemanticChatMemoryExtractor.cs b/samples/apps/copilot-chat-app/webapi/CopilotChat/Skills/ChatSkills/SemanticChatMemoryExtractor.cs index aa612fe6dbcc..e08bd64f0aa2 100644 --- a/samples/apps/copilot-chat-app/webapi/CopilotChat/Skills/ChatSkills/SemanticChatMemoryExtractor.cs +++ b/samples/apps/copilot-chat-app/webapi/CopilotChat/Skills/ChatSkills/SemanticChatMemoryExtractor.cs @@ -8,6 +8,7 @@ using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.AI.TextCompletion; using Microsoft.SemanticKernel.Orchestration; +using SemanticKernel.Service.CopilotChat.Extensions; using SemanticKernel.Service.CopilotChat.Options; namespace SemanticKernel.Service.CopilotChat.Skills.ChatSkills; @@ -129,7 +130,7 @@ internal static async Task CreateMemoryAsync( minRelevanceScore: options.SemanticMemoryMinRelevance, cancellationToken: context.CancellationToken ) - .ToListAsync(context.CancellationToken) + .ToListAsync() .ConfigureAwait(false); if (memories.Count == 0)