diff --git a/BotSharp.sln b/BotSharp.sln index d0a61d4d..768ae6ee 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -63,6 +63,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MongoStorag EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.GoogleAI", "src\Plugins\BotSharp.Plugin.GoogleAI\BotSharp.Plugin.GoogleAI.csproj", "{8BC29F8A-78D6-422C-B522-10687ADC38ED}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.SemanticKernel", "src\Plugins\BotSharp.Plugin.SemanticKernel\BotSharp.Plugin.SemanticKernel.csproj", "{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -223,6 +225,14 @@ Global {8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|Any CPU.Build.0 = Release|Any CPU {8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|x64.ActiveCfg = Release|Any CPU {8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|x64.Build.0 = Release|Any CPU + {73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Debug|x64.ActiveCfg = Debug|Any CPU + {73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Debug|x64.Build.0 = Debug|Any CPU + {73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|Any CPU.Build.0 = Release|Any CPU + {73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|x64.ActiveCfg = Release|Any CPU + {73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -254,6 +264,7 @@ Global {5CD330E1-9E5A-4112-8346-6E31CA98EF78} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C} {DB3DE37B-1208-4ED3-9615-A52AD0AAD69C} = {5CD330E1-9E5A-4112-8346-6E31CA98EF78} {8BC29F8A-78D6-422C-B522-10687ADC38ED} = {D5293208-2BEF-42FC-A64C-5954F61720BA} + {73EE2CD0-3B27-4F02-A67B-762CBDD740D0} = {D5293208-2BEF-42FC-A64C-5954F61720BA} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19} diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/BotSharp.Plugin.SemanticKernel.csproj b/src/Plugins/BotSharp.Plugin.SemanticKernel/BotSharp.Plugin.SemanticKernel.csproj new file mode 100644 index 00000000..58a950cb --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/BotSharp.Plugin.SemanticKernel.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.1 + enable + + + + + + + + + + + diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs new file mode 100644 index 00000000..19cce5ed --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs @@ -0,0 +1,105 @@ +using BotSharp.Abstraction.Agents.Enums; +using BotSharp.Abstraction.Agents; +using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Conversations.Models; +using BotSharp.Abstraction.MLTasks; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime; +using System.Threading.Tasks; +using BotSharp.Abstraction.Conversations; +using Microsoft.SemanticKernel; +using Microsoft.Extensions.DependencyInjection; +using BotSharp.Abstraction.Models; + +namespace BotSharp.Plugin.SemanticKernel +{ + public class SemanticKernelChatCompletionProvider : IChatCompletion + { + private IKernel _kernel; + private IServiceProvider _services; + private ITokenStatistics _tokenStatistics; + private string? _model = null; + + public string Provider => throw new NotImplementedException(); + + public SemanticKernelChatCompletionProvider(IKernel kernel, + IServiceProvider services, + ITokenStatistics tokenStatistics) + { + this._kernel = kernel; + this._services = services; + this._tokenStatistics = tokenStatistics; + } + + + public RoleDialogModel GetChatCompletions(Agent agent, List conversations) + { + var hooks = _services.GetServices().ToList(); + + // Before chat completion hook + Task.WaitAll(hooks.Select(hook => + hook.BeforeGenerating(agent, conversations)).ToArray()); + + var completion = _kernel.GetService(_model); + + var agentService = _services.GetRequiredService(); + var instruction = agentService.RenderedInstruction(agent); + + var chatHistory = completion.CreateNewChat(instruction); + + foreach (var message in conversations) + { + if (message.Role == AgentRole.User) + { + chatHistory.AddUserMessage(message.Content); + } + else + { + chatHistory.AddAssistantMessage(message.Content); + } + } + + + var response = completion.GetChatCompletionsAsync(chatHistory) + .ContinueWith(async t => + { + var result = await t; + var message = await result.First().GetChatMessageAsync(); + return message.Content; + }).ConfigureAwait(false).GetAwaiter().GetResult() + .ConfigureAwait(false).GetAwaiter().GetResult(); + + + var msg = new RoleDialogModel(AgentRole.Assistant, response) + { + CurrentAgentId = agent.Id + }; + + // After chat completion hook + Task.WaitAll(hooks.Select(hook => + hook.AfterGenerated(msg, new TokenStatsModel + { + Model = _model ?? "default" + })).ToArray()); + + return msg; + } + + public Task GetChatCompletionsAsync(Agent agent, List conversations, Func onMessageReceived, Func onFunctionExecuting) + { + throw new NotImplementedException(); + } + + public Task GetChatCompletionsStreamingAsync(Agent agent, List conversations, Func onMessageReceived) + { + throw new NotImplementedException(); + } + + public void SetModelName(string model) + { + this._model = model; + } + } +} diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs new file mode 100644 index 00000000..93d59982 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs @@ -0,0 +1,19 @@ +using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.Plugins; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System.Text; + +namespace BotSharp.Plugin.SemanticKernel +{ + public class SemanticKernelPlugin : IBotSharpPlugin + { + public string Name => "Semantic Kernel"; + public string Description => "Semantic Kernel Service"; + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddScoped(); + services.AddScoped(); + } + } +} diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs new file mode 100644 index 00000000..e4d5cfb3 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs @@ -0,0 +1,71 @@ +using BotSharp.Abstraction.Agents.Enums; +using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Conversations.Models; +using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.MLTasks; +using Microsoft.SemanticKernel; +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using System.Linq; +using System.Collections.Generic; +using Microsoft.SemanticKernel.AI.TextCompletion; + +namespace BotSharp.Plugin.SemanticKernel +{ + public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCompletion + { + private readonly IKernel _kernel; + private readonly IServiceProvider _services; + private readonly ITokenStatistics _tokenStatistics; + private string? _model = null; + + public string Provider => "semantic-kernel"; + + public SemanticKernelTextCompletionProvider(IKernel kernel, + IServiceProvider services, + ITokenStatistics tokenStatistics) + { + this._kernel = kernel; + this._services = services; + this._tokenStatistics = tokenStatistics; + } + + + public async Task GetCompletion(string text, string agentId, string messageId) + { + var hooks = _services.GetServices().ToList(); + + // Before chat completion hook + var agent = new Agent() + { + Id = agentId + }; + var userMessage = new RoleDialogModel(AgentRole.User, text) + { + MessageId = messageId + }; + Task.WaitAll(hooks.Select(hook => + hook.BeforeGenerating(agent, new List { userMessage })).ToArray()); + + var completion = _kernel.GetService(_model); + _tokenStatistics.StartTimer(); + var result = await completion.CompleteAsync(text); + _tokenStatistics.StopTimer(); + + // After chat completion hook + Task.WaitAll(hooks.Select(hook => + hook.AfterGenerated(new RoleDialogModel(AgentRole.Assistant, result), new TokenStatsModel + { + Model = _model ?? "default" + })).ToArray()); + + return result; + } + + public void SetModelName(string model) + { + this._model = model; + } + } +}