Skip to content

Commit

Permalink
πŸ”€ Update ERNIEBotChatCompletion message handling (#95)
Browse files Browse the repository at this point in the history
* πŸ”€ 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
  • Loading branch information
xbotter committed Jan 16, 2024
1 parent 3ab6938 commit 65d9137
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
12 changes: 6 additions & 6 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
<LangVersion>12</LangVersion>
<Authors>Custouch</Authors>
<PackageLicenseFile>LICENSE</PackageLicenseFile>

<PackageProjectUrl>https://github.com/custouch/semantic-kernel-ERNIE-Bot</PackageProjectUrl>
<RepositoryUrl>https://github.com/custouch/semantic-kernel-ERNIE-Bot</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>0.13.0</Version>
<Version>0.13.1</Version>
<PackageOutputPath>..\..\nupkgs</PackageOutputPath>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<NoWarn>SKEXP0001;SKEXP0002;SKEXP0052;SKEXP0003</NoWarn>

</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<None Include="readme.md">
<Pack>True</Pack>
Expand All @@ -36,5 +36,5 @@
<PropertyGroup Condition="$(Configuration) == 'Release'">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

</Project>
28 changes: 15 additions & 13 deletions src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ public class ERNIEBotChatCompletion : IChatCompletionService, ITextGenerationSer
{
protected readonly ERNIEBotClient _client;
private readonly ModelEndpoint _modelEndpoint;
private readonly Dictionary<string, string> _attributes = new();
private readonly Dictionary<string, object?> _attributes = new();

public IReadOnlyDictionary<string, string> Attributes => this._attributes;

IReadOnlyDictionary<string, object?> IAIService.Attributes => throw new NotImplementedException();
public IReadOnlyDictionary<string, object?> Attributes => this._attributes;

public ERNIEBotChatCompletion(ERNIEBotClient client, ModelEndpoint? modelEndpoint = null)
{
Expand Down Expand Up @@ -116,26 +114,30 @@ public ChatHistory CreateNewChat(string? instructions = null)
}
private List<Message> StringToMessages(string text)
{
return new List<Message>()
{
return
[
new Message()
{
Role = MessageRole.User,
Content = text
Role = MessageRole.User,
Content = text
}
};
];
}

private List<Message> 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()
Expand Down

0 comments on commit 65d9137

Please sign in to comment.