首页 新闻 会员 周边

如何消除 C# 编译警告 CS8425

0
悬赏园豆:30 [已解决问题] 解决于 2024-02-12 17:45

今天在实现 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?

dudu的主页 dudu | 高人七级 | 园豆:30880
提问于:2024-02-12 17:20
< >
分享
最佳答案
0

cancellationToken 参数加上 [EnumeratorCancellation] attribute 即可解决

public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(
    ChatHistory chatHistory, 
    PromptExecutionSettings? executionSettings = null, 
    Kernel? kernel = null,
    [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
}
dudu | 高人七级 |园豆:30880 | 2024-02-12 17:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册