Skip to content

Commit

Permalink
Adding local ToListAsync method in Copilot Chat (microsoft#1346)
Browse files Browse the repository at this point in the history
### 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<T>` to `List<T>` 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<MemoryQueryResult>' does not contain a definition for
'ToListAsync' and no accessible extension method 'ToListAsync' accepting
a first argument of type 'IAsyncEnumerable<MemoryQueryResult>' could be
found (are you missing a using directive or an assembly reference?) `
  • Loading branch information
teresaqhoang authored Jun 5, 2023
1 parent 134906a commit f8567c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Collections.Generic;
using System.Threading.Tasks;

namespace SemanticKernel.Service.CopilotChat.Extensions;

/// <summary>
/// Extension methods for enabling async LINQ operations on IAsyncEnumerable sequence.
/// </summary>
public static class IAsyncEnumerableExtensions
{
/// <summary>
/// Creates a List<T> from an IAsyncEnumerable<T> by enumerating it asynchronously.
/// </summary>
internal static async Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> source)
{
var result = new List<T>();
await foreach (var item in source.ConfigureAwait(false))
{
result.Add(item);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f8567c6

Please sign in to comment.