今天在实现 semantic kernel 的 IChatCompletionService 接口的 GetStreamingChatMessageContentsAsync 方法时,出现下面的 build warning
Async-iterator 'xxx' has one or more parameters of type 'CancellationToken' but none of them is decorated with the 'EnumeratorCancellation' attribute, so the cancellation token parameter from the generated 'IAsyncEnumerable<>.GetAsyncEnumerator' will be unconsumed
对应的实现代码如下
public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(
ChatHistory chatHistory,
PromptExecutionSettings? executionSettings = null,
Kernel? kernel = null,
CancellationToken cancellationToken = default)
{
var chatMessages = chatHistory
.Where(x => !string.IsNullOrEmpty(x.Content))
.Select(x => new ChatMessage(x.Role.ToString(), x.Content!)).
ToList();
var responses = _dashScopeClient.TextGeneration.ChatStreamed(
_modelId,
chatMessages,
cancellationToken: cancellationToken);
await foreach (var response in responses)
{
yield return new StreamingChatMessageContent(new AuthorRole(chatMessages.First().Role), response.Output.Text);
}
}
请问如何消除这个 warning?
给 cancellationToken
参数加上 [EnumeratorCancellation]
attribute 即可解决
public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(
ChatHistory chatHistory,
PromptExecutionSettings? executionSettings = null,
Kernel? kernel = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
}