custouch / semantic-kernel-ERNIE-Bot

Semantic Kernel 集成文心千帆
MIT License
34 stars 8 forks source link

_kernel.InvokeAsync报错:no messages #86

Closed chlCoder closed 10 months ago

chlCoder commented 11 months ago

Microsoft.SemanticKernel.KernelException: no messages ---> ERNIE_Bot.SDK.Models.ERNIEBotException: no messages at ERNIE_Bot.SDK.ERNIEBotClient.OrganizeChatMessages(List1 messages) in D:\download\LLM\semantic-kernel-ERNIE-Bot\src\ERNIE-Bot.SDK\ERNIEBotClient.cs:line 273 at ERNIE_Bot.SDK.ERNIEBotClient.ChatAsync(ChatRequest request, ModelEndpoint modelEndpoint, CancellationToken cancellationToken) in D:\download\LLM\semantic-kernel-ERNIE-Bot\src\ERNIE-Bot.SDK\ERNIEBotClient.cs:line 67 at ERNIEBotChatCompletion.InternalCompletionsAsync(List1 messages, Nullable1 temperature, Nullable1 topP, Nullable`1 penaltyScore, String system, CancellationToken cancellationToken) in D:\download\LLM\semantic-kernel-ERNIE-Bot\src\ERNIE-Bot.SemanticKernel\ERNIEBotChatCompletion.cs:line 159

shangdf88 commented 11 months ago

我也遇到同样问题了,通过跟踪发现: 调用myKernel.InvokePromptAsync(input)时,SK库用提示词去生成一个了ChatHistory,但这个ChatHistory中的Message集合中只有一个消息,类型被标记为AuthorRole.System。我不清楚SK官方这么做是什么意思,明明是用户输入的消息,为什么标记为System。难道是因为给大模型一个初始角色设定? 但不管怎么样,到了ERNIE-Bot中,执行到了 private List ChatHistoryToMessages(ChatHistory chatHistory, out string? system) { if (chatHistory.First().Role == AuthorRole.System) { system = chatHistory.First().Content; } else { system = null; } return chatHistory .Where( => .Role != AuthorRole.System) .Select(m => new Message() { Role = AuthorRoleToMessageRole(m.Role), Content = m.Content! }).ToList(); } 最后导致消息集合中数量为0,触发了异常。 因此,我将ERNIEBotChatCompletion.cs文件中的GetChatMessageContentsAsync进行了修改,It Works! public async Task<IReadOnlyList> GetChatMessageContentsAsync(ChatHistory chat, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default) { var messages = ChatHistoryToMessages(chat, out var system); if(messages.Count == 0 && chat.Count > 0) //新添加逻辑。 { var m = chat[0]; messages.Add(new Message { Role = AuthorRoleToMessageRole(m.Role), Content = m.Content! }); } executionSettings ??= new PromptExecutionSettings();

    var settings = ERNIEBotAIRequestSettings.FromRequestSettings(executionSettings);

    ChatResponse result = await InternalCompletionsAsync(messages,
                                                         settings.Temperature,
                                                         settings.TopP,
                                                         settings.PenaltyScore,
                                                         system,
                                                         cancellationToken
                                                         );

    return new List<ChatMessageContent>() { new ERNIEBotChatMessage(result) };
}

但是不知道这个是不是仓主的意思。不改乱提pull request。坐等仓主解答。

alex521 commented 10 months ago

有解决方案了没

xbotter commented 10 months ago

抱歉这么久才回复。 文心的接口提供所的提供system参数并不在history的队列中,而是单独参数。 诚如 @shangdf88 所提供的信息,SK在某次改版之后某次改版之后,单条的message role从User 改为了System,因而造成了一些异常情况。 这个问题近期会尽快修复一下。

xbotter commented 10 months ago

0.13.1 版本已经修复了该问题,请安装最新版本的nuget包重试一下。