From 65d91373e90419c82a5dd61eaadf323ac28de6ff Mon Sep 17 00:00:00 2001 From: xbotter Date: Tue, 16 Jan 2024 14:54:57 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=80=20Update=20ERNIEBotChatCompletion?= =?UTF-8?q?=20message=20handling=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🔀 Update ERNIEBotChatCompletion message handling - Refactor the `ChatHistoryToMessages` method to handle cases where the `chatHistory` contains only one message. - Update the return type of the `ChatHistoryToMessages` method to include the `system` parameter. - Improve code readability and remove unnecessary code. * 🔧 Update version to 0.13.1 --- src/Directory.Build.props | 12 ++++---- .../ERNIEBotChatCompletion.cs | 28 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 3d1e9ff..7262a1b 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -8,24 +8,24 @@ 12 Custouch LICENSE - + https://github.com/custouch/semantic-kernel-ERNIE-Bot https://github.com/custouch/semantic-kernel-ERNIE-Bot git - 0.13.0 + 0.13.1 ..\..\nupkgs readme.md SKEXP0001;SKEXP0002;SKEXP0052;SKEXP0003 - + - + True \ - + True @@ -36,5 +36,5 @@ true - + diff --git a/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs b/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs index 6c707d7..9d2ad12 100644 --- a/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs +++ b/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs @@ -12,11 +12,9 @@ public class ERNIEBotChatCompletion : IChatCompletionService, ITextGenerationSer { protected readonly ERNIEBotClient _client; private readonly ModelEndpoint _modelEndpoint; - private readonly Dictionary _attributes = new(); + private readonly Dictionary _attributes = new(); - public IReadOnlyDictionary Attributes => this._attributes; - - IReadOnlyDictionary IAIService.Attributes => throw new NotImplementedException(); + public IReadOnlyDictionary Attributes => this._attributes; public ERNIEBotChatCompletion(ERNIEBotClient client, ModelEndpoint? modelEndpoint = null) { @@ -116,26 +114,30 @@ public ChatHistory CreateNewChat(string? instructions = null) } private List StringToMessages(string text) { - return new List() - { + return + [ new Message() { - Role = MessageRole.User, - Content = text + Role = MessageRole.User, + Content = text } - }; + ]; } private List ChatHistoryToMessages(ChatHistory chatHistory, out string? system) { - if (chatHistory.First().Role == AuthorRole.System) + system = null; + + if (chatHistory.Count == 1) { - system = chatHistory.First().Content; + return StringToMessages(chatHistory.First().Content!); } - else + + if (chatHistory.First().Role == AuthorRole.System) { - system = null; + system = chatHistory.First().Content; } + return chatHistory .Where(_ => _.Role != AuthorRole.System) .Select(m => new Message()