From eb1b77904b9826a8fe818180dd195595b6acdff8 Mon Sep 17 00:00:00 2001 From: xbotter Date: Sun, 19 Nov 2023 21:34:13 +0800 Subject: [PATCH] save stage --- .../BotSharp.Plugin.SemanticKernel.csproj | 7 ++-- .../SemanticKernelChatCompletionProvider.cs | 10 +++--- .../SemanticKernelPlugin.cs | 33 +++++++++++++++---- .../SemanticKernelSettings.cs | 11 ------- .../SemanticKernelTextCompletionProvider.cs | 12 +++---- .../BotSharp.Plugin.SemanticKernel/readme.md | 2 ++ .../Helpers/ResultHelper.cs | 4 +-- ...manticKernelChatCompletionProviderTests.cs | 6 ++-- 8 files changed, 48 insertions(+), 37 deletions(-) delete mode 100644 src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelSettings.cs create mode 100644 src/Plugins/BotSharp.Plugin.SemanticKernel/readme.md diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/BotSharp.Plugin.SemanticKernel.csproj b/src/Plugins/BotSharp.Plugin.SemanticKernel/BotSharp.Plugin.SemanticKernel.csproj index 3aef859b..2dd2e3b3 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/BotSharp.Plugin.SemanticKernel.csproj +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/BotSharp.Plugin.SemanticKernel.csproj @@ -10,9 +10,10 @@ - - - + + + + diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs index 1b5298fc..13bca1e8 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs @@ -18,7 +18,7 @@ namespace BotSharp.Plugin.SemanticKernel /// public class SemanticKernelChatCompletionProvider : IChatCompletion { - private IKernel _kernel; + private Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion _kernelChatCompletion; private IServiceProvider _services; private ITokenStatistics _tokenStatistics; private string? _model = null; @@ -29,14 +29,14 @@ namespace BotSharp.Plugin.SemanticKernel /// /// Create a new instance of /// - /// + /// /// /// - public SemanticKernelChatCompletionProvider(IKernel kernel, + public SemanticKernelChatCompletionProvider(Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion chatCompletion, IServiceProvider services, ITokenStatistics tokenStatistics) { - this._kernel = kernel; + this._kernelChatCompletion = chatCompletion; this._services = services; this._tokenStatistics = tokenStatistics; } @@ -49,7 +49,7 @@ namespace BotSharp.Plugin.SemanticKernel Task.WaitAll(hooks.Select(hook => hook.BeforeGenerating(agent, conversations)).ToArray()); - var completion = _kernel.GetService(_model); + var completion = this._kernelChatCompletion; var agentService = _services.GetRequiredService(); var instruction = agentService.RenderedInstruction(agent); diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs index dfe1758e..b1b4e21b 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs @@ -6,20 +6,41 @@ using Microsoft.Extensions.DependencyInjection; namespace BotSharp.Plugin.SemanticKernel { + /// + /// Use Semantic Kernel as BotSharp plugin + /// public class SemanticKernelPlugin : IBotSharpPlugin { + /// public string Name => "Semantic Kernel"; + + /// public string Description => "Semantic Kernel Service"; + /// public void RegisterDI(IServiceCollection services, IConfiguration config) { - var settings = new SemanticKernelSettings(); - config.Bind("SemanticKernel", settings); + var provider = services.BuildServiceProvider().CreateScope().ServiceProvider; - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + if (provider.GetService() != null) + { + services.AddScoped(); + } + + if (provider.GetService() != null) + { + services.AddScoped(); + } + + if (provider.GetService() != null) + { + services.AddScoped(); + } + + if (provider.GetService() != null) + { + services.AddScoped(); + } } } } \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelSettings.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelSettings.cs deleted file mode 100644 index 470915a3..00000000 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelSettings.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace BotSharp.Plugin.SemanticKernel -{ - internal class SemanticKernelSettings - { - - } -} diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs index d222e850..ee38d6f2 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs @@ -19,7 +19,7 @@ namespace BotSharp.Plugin.SemanticKernel /// public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCompletion { - private readonly IKernel _kernel; + private readonly Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion _kernelTextCompletion; private readonly IServiceProvider _services; private readonly ITokenStatistics _tokenStatistics; private string? _model = null; @@ -30,16 +30,14 @@ namespace BotSharp.Plugin.SemanticKernel /// /// Create a new instance of /// - /// + /// /// /// - public SemanticKernelTextCompletionProvider(IKernel kernel, + public SemanticKernelTextCompletionProvider(Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion textCompletion, IServiceProvider services, ITokenStatistics tokenStatistics) { - Requires.NotNull(kernel, nameof(IKernel)); - - this._kernel = kernel; + this._kernelTextCompletion = textCompletion; this._services = services; this._tokenStatistics = tokenStatistics; } @@ -61,7 +59,7 @@ namespace BotSharp.Plugin.SemanticKernel Task.WaitAll(hooks.Select(hook => hook.BeforeGenerating(agent, new List { userMessage })).ToArray()); - var completion = _kernel.GetService(_model); + var completion = this._kernelTextCompletion; _tokenStatistics.StartTimer(); var result = await completion.CompleteAsync(text); _tokenStatistics.StopTimer(); diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/readme.md b/src/Plugins/BotSharp.Plugin.SemanticKernel/readme.md new file mode 100644 index 00000000..d740012a --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/readme.md @@ -0,0 +1,2 @@ +# Semantic Kernel For BotSharp + diff --git a/tests/BotSharp.Plugin.SemanticKernel.UnitTests/Helpers/ResultHelper.cs b/tests/BotSharp.Plugin.SemanticKernel.UnitTests/Helpers/ResultHelper.cs index 84d9bbcc..4a818310 100644 --- a/tests/BotSharp.Plugin.SemanticKernel.UnitTests/Helpers/ResultHelper.cs +++ b/tests/BotSharp.Plugin.SemanticKernel.UnitTests/Helpers/ResultHelper.cs @@ -15,7 +15,7 @@ namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers _response = response; } - public async Task GetChatMessageAsync(CancellationToken cancellationToken = default) + public async Task GetChatMessageAsync(CancellationToken cancellationToken = default) { return await Task.FromResult(new MockModelResult(_response)); } @@ -25,7 +25,7 @@ namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers return Task.FromResult(_response); } - public class MockModelResult : ChatMessageBase + public class MockModelResult : ChatMessage { public MockModelResult(string content) : base(AuthorRole.Assistant, content, null) { diff --git a/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelChatCompletionProviderTests.cs b/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelChatCompletionProviderTests.cs index 5be823fa..550a7b77 100644 --- a/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelChatCompletionProviderTests.cs +++ b/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelChatCompletionProviderTests.cs @@ -24,17 +24,17 @@ namespace BotSharp.Plugin.SemanticKernel.Tests { public class SemanticKernelChatCompletionProviderTests { - private readonly Mock _kernelMock; + private readonly Mock _chatCompletionMock; private readonly Mock _servicesMock; private readonly Mock _tokenStatisticsMock; private readonly SemanticKernelChatCompletionProvider _provider; public SemanticKernelChatCompletionProviderTests() { - _kernelMock = new Mock(); + _chatCompletionMock = new Mock(); _servicesMock = new Mock(); _tokenStatisticsMock = new Mock(); - _provider = new SemanticKernelChatCompletionProvider(_kernelMock.Object, _servicesMock.Object, _tokenStatisticsMock.Object); + _provider = new SemanticKernelChatCompletionProvider(_chatCompletionMock, _servicesMock.Object, _tokenStatisticsMock.Object); } [Fact]