Skip to content

Commit f8567c6

Browse files
authored
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<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?) `
1 parent 134906a commit f8567c6

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

samples/apps/copilot-chat-app/webapi/CopilotChat/Controllers/BotController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.Extensions.Options;
1414
using Microsoft.SemanticKernel;
1515
using Microsoft.SemanticKernel.Memory;
16+
using SemanticKernel.Service.CopilotChat.Extensions;
1617
using SemanticKernel.Service.CopilotChat.Models;
1718
using SemanticKernel.Service.CopilotChat.Options;
1819
using SemanticKernel.Service.CopilotChat.Storage;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
6+
namespace SemanticKernel.Service.CopilotChat.Extensions;
7+
8+
/// <summary>
9+
/// Extension methods for enabling async LINQ operations on IAsyncEnumerable sequence.
10+
/// </summary>
11+
public static class IAsyncEnumerableExtensions
12+
{
13+
/// <summary>
14+
/// Creates a List<T> from an IAsyncEnumerable<T> by enumerating it asynchronously.
15+
/// </summary>
16+
internal static async Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> source)
17+
{
18+
var result = new List<T>();
19+
await foreach (var item in source.ConfigureAwait(false))
20+
{
21+
result.Add(item);
22+
}
23+
24+
return result;
25+
}
26+
}

samples/apps/copilot-chat-app/webapi/CopilotChat/Skills/ChatSkills/SemanticChatMemoryExtractor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.SemanticKernel;
99
using Microsoft.SemanticKernel.AI.TextCompletion;
1010
using Microsoft.SemanticKernel.Orchestration;
11+
using SemanticKernel.Service.CopilotChat.Extensions;
1112
using SemanticKernel.Service.CopilotChat.Options;
1213

1314
namespace SemanticKernel.Service.CopilotChat.Skills.ChatSkills;
@@ -129,7 +130,7 @@ internal static async Task CreateMemoryAsync(
129130
minRelevanceScore: options.SemanticMemoryMinRelevance,
130131
cancellationToken: context.CancellationToken
131132
)
132-
.ToListAsync(context.CancellationToken)
133+
.ToListAsync()
133134
.ConfigureAwait(false);
134135

135136
if (memories.Count == 0)

0 commit comments

Comments
 (0)