From 071a64ea2163e8e707451f1a4eb14c21dfc0a03c Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Fri, 9 Jun 2023 21:56:04 -0500 Subject: [PATCH] Remove IPlatformMidware. --- BotSharp.sln | 13 ++++++++ .../BotSharp.Abstraction/IPlatformMidware.cs | 11 ------- .../{GlobalUsing.cs => Using.cs} | 0 .../BotSharp.Core/BotSharp.Core.csproj | 6 +++- .../BotSharpServiceCollectionExtensions.cs | 10 ++++-- .../BotSharp.Core/Plugins/PlatformMidware.cs | 32 ------------------- .../LLamaSharp/LLamaSharpPlugin.cs | 6 ++++ .../{GlobalUsing.cs => Using.cs} | 3 +- .../ChatbotUiController.cs | 10 +++--- .../HuggingChat/HuggingChatController.cs | 10 +++--- tests/UnitTest/UnitTest.csproj | 19 +++++++++++ tests/UnitTest/UnitTest1.cs | 11 +++++++ tests/UnitTest/Usings.cs | 1 + 13 files changed, 75 insertions(+), 57 deletions(-) delete mode 100644 src/Infrastructure/BotSharp.Abstraction/IPlatformMidware.cs rename src/Infrastructure/BotSharp.Abstraction/{GlobalUsing.cs => Using.cs} (100%) delete mode 100644 src/Infrastructure/BotSharp.Core/Plugins/PlatformMidware.cs create mode 100644 src/Infrastructure/BotSharp.Core/Plugins/TextGeneratives/LLamaSharp/LLamaSharpPlugin.cs rename src/Infrastructure/BotSharp.Core/{GlobalUsing.cs => Using.cs} (70%) create mode 100644 tests/UnitTest/UnitTest.csproj create mode 100644 tests/UnitTest/UnitTest1.cs create mode 100644 tests/UnitTest/Usings.cs diff --git a/BotSharp.sln b/BotSharp.sln index 9dc4eb64..fd8207a7 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -29,6 +29,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.FacebookMes EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.WeChat", "src\Plugins\BotSharp.Plugin.WeChat\BotSharp.Plugin.WeChat.csproj", "{2323A7A3-E938-488D-A57E-638638054BC4}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest", "tests\UnitTest\UnitTest.csproj", "{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -101,6 +105,14 @@ Global {2323A7A3-E938-488D-A57E-638638054BC4}.Release|Any CPU.Build.0 = Release|Any CPU {2323A7A3-E938-488D-A57E-638638054BC4}.Release|x64.ActiveCfg = Release|Any CPU {2323A7A3-E938-488D-A57E-638638054BC4}.Release|x64.Build.0 = Release|Any CPU + {0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Debug|x64.ActiveCfg = Debug|Any CPU + {0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Debug|x64.Build.0 = Debug|Any CPU + {0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Release|Any CPU.Build.0 = Release|Any CPU + {0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Release|x64.ActiveCfg = Release|Any CPU + {0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -116,6 +128,7 @@ Global {68C7C9E9-496B-4004-A1F8-75FFB8C06C76} = {42226933-0A0D-48E8-85EC-AFE773A1F841} {0149F6C2-4CF8-4904-A545-54F3278BEA74} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00} {2323A7A3-E938-488D-A57E-638638054BC4} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00} + {0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19} diff --git a/src/Infrastructure/BotSharp.Abstraction/IPlatformMidware.cs b/src/Infrastructure/BotSharp.Abstraction/IPlatformMidware.cs deleted file mode 100644 index cb58a3c5..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/IPlatformMidware.cs +++ /dev/null @@ -1,11 +0,0 @@ -using BotSharp.Abstraction.Conversations; -using BotSharp.Abstraction.Models; - -namespace BotSharp.Abstraction; - -public interface IPlatformMidware -{ - ISessionService SessionService { get; } - Task GetChatCompletionsAsync(List conversations, - Func onChunkReceived); -} diff --git a/src/Infrastructure/BotSharp.Abstraction/GlobalUsing.cs b/src/Infrastructure/BotSharp.Abstraction/Using.cs similarity index 100% rename from src/Infrastructure/BotSharp.Abstraction/GlobalUsing.cs rename to src/Infrastructure/BotSharp.Abstraction/Using.cs diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index e623f623..617c7071 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -62,15 +62,19 @@ - + + + + + diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index 648f2156..f7e6b7e0 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -3,7 +3,6 @@ using BotSharp.Abstraction.TextGeneratives; using BotSharp.Core.Conversations; using BotSharp.Core.Plugins.TextGeneratives.LLamaSharp; using BotSharp.Core.Repository; -using BotSharp.Core.Services; using EntityFrameworkCore.BootKit; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; @@ -15,11 +14,13 @@ public static class BotSharpServiceCollectionExtensions { public static IServiceCollection AddBotSharp(this IServiceCollection services, IConfiguration config) { - services.AddScoped(); services.AddSingleton(); services.AddSingleton(); RegisterRepository(services, config); + + RegisterPlugins(services, config); + return services; } @@ -66,4 +67,9 @@ public static class BotSharpServiceCollectionExtensions }); services.AddSingleton(); } + + public static void RegisterPlugins(IServiceCollection services, IConfiguration config) + { + + } } diff --git a/src/Infrastructure/BotSharp.Core/Plugins/PlatformMidware.cs b/src/Infrastructure/BotSharp.Core/Plugins/PlatformMidware.cs deleted file mode 100644 index ee3c5459..00000000 --- a/src/Infrastructure/BotSharp.Core/Plugins/PlatformMidware.cs +++ /dev/null @@ -1,32 +0,0 @@ -using BotSharp.Abstraction.Conversations; -using BotSharp.Abstraction.Models; -using BotSharp.Abstraction.TextGeneratives; -using Microsoft.Extensions.DependencyInjection; - -namespace BotSharp.Core.Services; - -public class PlatformMidware : IPlatformMidware -{ - private readonly IServiceProvider _services; - private readonly ISessionService _sessionService; - - public PlatformMidware(IServiceProvider services) - { - _services = services; - _sessionService = services.GetRequiredService(); - } - - public ISessionService SessionService => _sessionService; - - public async Task GetChatCompletionsAsync(List conversations, - Func onChunkReceived) - { - var handlers = _services.GetServices().ToList(); - for (int i = 0; i < handlers.Count(); i++) - { - var handler = handlers[i]; - await handler.GetChatCompletionsAsync(conversations, - onChunkReceived); - } - } -} diff --git a/src/Infrastructure/BotSharp.Core/Plugins/TextGeneratives/LLamaSharp/LLamaSharpPlugin.cs b/src/Infrastructure/BotSharp.Core/Plugins/TextGeneratives/LLamaSharp/LLamaSharpPlugin.cs new file mode 100644 index 00000000..e518c52e --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Plugins/TextGeneratives/LLamaSharp/LLamaSharpPlugin.cs @@ -0,0 +1,6 @@ +namespace BotSharp.Core.Plugins.TextGeneratives.LLamaSharp; + +public class LLamaSharpPlugin : IBotSharpPlugin +{ + +} diff --git a/src/Infrastructure/BotSharp.Core/GlobalUsing.cs b/src/Infrastructure/BotSharp.Core/Using.cs similarity index 70% rename from src/Infrastructure/BotSharp.Core/GlobalUsing.cs rename to src/Infrastructure/BotSharp.Core/Using.cs index b431e4ed..61f87ce3 100644 --- a/src/Infrastructure/BotSharp.Core/GlobalUsing.cs +++ b/src/Infrastructure/BotSharp.Core/Using.cs @@ -3,4 +3,5 @@ global using System.Collections.Generic; global using System.Text; global using System.Threading.Tasks; global using BotSharp.Abstraction; -global using System.Linq; \ No newline at end of file +global using System.Linq; +global using BotSharp.Abstraction.Plugins; \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs index a3b2d225..4adf2484 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction; using BotSharp.Abstraction.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -14,6 +13,7 @@ using System; using Azure.AI.OpenAI; using BotSharp.Abstraction.ApiAdapters; using BotSharp.Plugin.ChatbotUI.ViewModels; +using BotSharp.Abstraction.TextGeneratives; namespace BotSharp.Plugin.ChatbotUI.Controllers; @@ -21,13 +21,13 @@ namespace BotSharp.Plugin.ChatbotUI.Controllers; public class ChatbotUiController : ControllerBase, IApiAdapter { private readonly ILogger _logger; - private readonly IPlatformMidware _platform; + private readonly IChatCompletionProvider _chatCompletionProvider; public ChatbotUiController(ILogger logger, - IPlatformMidware platform) + IChatCompletionProvider chatCompletionProvider) { _logger = logger; - _platform = platform; + _chatCompletionProvider = chatCompletionProvider; } [HttpGet("/v1/models")] @@ -64,7 +64,7 @@ public class ChatbotUiController : ControllerBase, IApiAdapter Content = x.Content }).ToList(); - await _platform.GetChatCompletionsAsync(conversations, + await _chatCompletionProvider.GetChatCompletionsAsync(conversations, async content => { await OnChunkReceived(outputStream, content); diff --git a/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/HuggingChatController.cs b/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/HuggingChatController.cs index 3a9b6aa2..40411ef8 100644 --- a/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/HuggingChatController.cs +++ b/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/HuggingChatController.cs @@ -16,13 +16,13 @@ namespace BotSharp.Plugin.HuggingFace.HuggingChat; public class HuggingChatController : ControllerBase, IApiAdapter { - private readonly IPlatformMidware _platform; - public HuggingChatController(IPlatformMidware platform) + private readonly IChatCompletionProvider _chatCompletionProvider; + public HuggingChatController(IChatCompletionProvider chatCompletionProvider) { - _platform = platform; + _chatCompletionProvider = chatCompletionProvider; } - [HttpPost("/conversation")] + /*[HttpPost("/conversation")] public async Task NewSession([FromBody] ConversationCreationModel conversationCreationModel) { var session = await _platform.SessionService.NewSession("anonymous"); @@ -36,7 +36,7 @@ public class HuggingChatController : ControllerBase, IApiAdapter public string SummarizeTitle([FromRoute] string id) { return "SummarizeTitle"; - } + }*/ [HttpPost("/models/OpenAssistant/{model}")] public async Task SendMessage([FromRoute] string model, [FromBody] ChatInput message) diff --git a/tests/UnitTest/UnitTest.csproj b/tests/UnitTest/UnitTest.csproj new file mode 100644 index 00000000..ed22915e --- /dev/null +++ b/tests/UnitTest/UnitTest.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + false + true + + + + + + + + + + diff --git a/tests/UnitTest/UnitTest1.cs b/tests/UnitTest/UnitTest1.cs new file mode 100644 index 00000000..ab3cd866 --- /dev/null +++ b/tests/UnitTest/UnitTest1.cs @@ -0,0 +1,11 @@ +namespace UnitTest +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public void TestMethod1() + { + } + } +} \ No newline at end of file diff --git a/tests/UnitTest/Usings.cs b/tests/UnitTest/Usings.cs new file mode 100644 index 00000000..ab67c7ea --- /dev/null +++ b/tests/UnitTest/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file