From 2dd93aef42303280691dae5ef06fc24d23f31102 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 12 Jun 2023 22:27:31 -0500 Subject: [PATCH] Added ContentTransfer. --- BotSharp.sln | 2 +- .../ContentTransfers/ContentContainer.cs | 12 +++++ .../ContentTransfers/IContentTransfer.cs | 8 ++++ .../ContentTransfers/IServiceZone.cs | 8 ++++ .../ContentTransfers/TransportResult.cs | 7 +++ .../Knowledges/IKnowledgeBase.cs | 5 ++ .../IChatCompletionProvider.cs | 12 ----- .../BotSharpServiceCollectionExtensions.cs | 6 ++- .../Conversations/ConversationController.cs | 27 +++++++++++ .../ViewModels/MessageResponseModel.cs | 6 +++ .../ViewModels/NewMessageModel.cs | 6 +++ .../Infrastructures/ContentTransfer.cs | 47 +++++++++++++++++++ .../BotSharp.Core/Knowledges/KnowledgeBase.cs | 7 +++ .../LLamaSharp/ChatCompletionProvider.cs | 10 +++- .../AzureOpenAiServiceCollectionExtensions.cs | 4 +- .../TextGeneratives/ChatCompletionProvider.cs | 33 ++++++++++++- .../ChatbotUiController.cs | 24 +++++++--- .../HuggingChat/HuggingChatController.cs | 6 +-- .../HuggingChat}/TextToken.cs | 0 19 files changed, 198 insertions(+), 32 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/ContentContainer.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IContentTransfer.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IServiceZone.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/TransportResult.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeBase.cs delete mode 100644 src/Infrastructure/BotSharp.Abstraction/TextGeneratives/IChatCompletionProvider.cs create mode 100644 src/Infrastructure/BotSharp.Core/Conversations/ViewModels/MessageResponseModel.cs create mode 100644 src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs create mode 100644 src/Infrastructure/BotSharp.Core/Infrastructures/ContentTransfer.cs create mode 100644 src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeBase.cs rename src/{Infrastructure/BotSharp.Abstraction/TextGeneratives => Plugins/BotSharp.Plugin.HuggingFace/HuggingChat}/TextToken.cs (100%) diff --git a/BotSharp.sln b/BotSharp.sln index fd8207a7..da611aca 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -31,7 +31,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.WeChat", "s 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTest", "tests\UnitTest\UnitTest.csproj", "{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/ContentContainer.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/ContentContainer.cs new file mode 100644 index 00000000..6c7b3f13 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/ContentContainer.cs @@ -0,0 +1,12 @@ +using BotSharp.Abstraction.Models; + +namespace BotSharp.Abstraction.Infrastructures.ContentTransmitters; + +public class ContentContainer +{ + public string UserId { get; set; } + public string SessionId { get; set; } + public string AgentId { get; set; } + public List Conversations { get; set; } + public RoleDialogModel Output { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IContentTransfer.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IContentTransfer.cs new file mode 100644 index 00000000..984ae7c2 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IContentTransfer.cs @@ -0,0 +1,8 @@ +using BotSharp.Abstraction.Infrastructures.ContentTransfers; + +namespace BotSharp.Abstraction.Infrastructures.ContentTransmitters; + +public interface IContentTransfer +{ + Task Transport(ContentContainer input); +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IServiceZone.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IServiceZone.cs new file mode 100644 index 00000000..34b27ede --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IServiceZone.cs @@ -0,0 +1,8 @@ +using BotSharp.Abstraction.Infrastructures.ContentTransmitters; + +namespace BotSharp.Abstraction.Infrastructures.ContentTransfers; + +public interface IServiceZone +{ + Task Serving(ContentContainer content); +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/TransportResult.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/TransportResult.cs new file mode 100644 index 00000000..e846ecc2 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/TransportResult.cs @@ -0,0 +1,7 @@ +namespace BotSharp.Abstraction.Infrastructures.ContentTransfers; + +public class TransportResult +{ + public bool IsSuccess { get; set; } + public List Messages { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeBase.cs b/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeBase.cs new file mode 100644 index 00000000..840e625e --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeBase.cs @@ -0,0 +1,5 @@ +namespace BotSharp.Abstraction.Knowledges; + +public interface IKnowledgeBase +{ +} diff --git a/src/Infrastructure/BotSharp.Abstraction/TextGeneratives/IChatCompletionProvider.cs b/src/Infrastructure/BotSharp.Abstraction/TextGeneratives/IChatCompletionProvider.cs deleted file mode 100644 index 344c0dce..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/TextGeneratives/IChatCompletionProvider.cs +++ /dev/null @@ -1,12 +0,0 @@ -using BotSharp.Abstraction.Models; - -namespace BotSharp.Abstraction.TextGeneratives; - -public interface IChatCompletionProvider -{ - string GetInstruction(); - List GetChatSamples(); - - Task GetChatCompletionsAsync(List conversations, - Func onChunkReceived); -} diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index f46a1f31..b93ad355 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -1,8 +1,10 @@ using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Infrastructures.ContentTransmitters; using BotSharp.Abstraction.Users; using BotSharp.Core.Agents.Services; using BotSharp.Core.Conversations.Services; +using BotSharp.Core.Infrastructures; using BotSharp.Core.Users.Services; using BotSharp.Plugins.LLamaSharp; using Microsoft.AspNetCore.Builder; @@ -20,6 +22,8 @@ public static class BotSharpServiceCollectionExtensions services.AddScoped(); services.AddScoped(); + services.AddScoped(); + RegisterRepository(services, config); RegisterPlugins(services, config); @@ -78,6 +82,6 @@ public static class BotSharpServiceCollectionExtensions return settings; }); - // services.AddSingleton(); + // services.AddScoped(); } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs b/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs index 52d4a822..01643963 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs @@ -1,5 +1,7 @@ using BotSharp.Abstraction.ApiAdapters; using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Infrastructures.ContentTransmitters; +using BotSharp.Abstraction.Models; using BotSharp.Core.Conversations.ViewModels; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -31,4 +33,29 @@ public class ConversationController : ControllerBase, IApiAdapter { var service = _services.GetRequiredService(); } + + [HttpPost("/conversation/{sessionId}")] + public async Task SendMessage([FromBody] NewMessageModel input) + { + var transmitter = _services.GetRequiredService(); + + var container = new ContentContainer + { + Conversations = new List + { + new RoleDialogModel + { + Role = "user", + Content = input.Content + } + } + }; + + var result = await transmitter.Transport(container); + + return new MessageResponseModel + { + Content = container.Output.Content + }; + } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/MessageResponseModel.cs b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/MessageResponseModel.cs new file mode 100644 index 00000000..2a2f050d --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/MessageResponseModel.cs @@ -0,0 +1,6 @@ +namespace BotSharp.Core.Conversations.ViewModels; + +public class MessageResponseModel +{ + public string Content { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs new file mode 100644 index 00000000..24548bdc --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs @@ -0,0 +1,6 @@ +namespace BotSharp.Core.Conversations.ViewModels; + +public class NewMessageModel +{ + public string Content { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/ContentTransfer.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/ContentTransfer.cs new file mode 100644 index 00000000..bb881088 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/ContentTransfer.cs @@ -0,0 +1,47 @@ +using BotSharp.Abstraction.Infrastructures.ContentTransfers; +using BotSharp.Abstraction.Infrastructures.ContentTransmitters; +using BotSharp.Abstraction.Users; + +namespace BotSharp.Core.Infrastructures; + +public class ContentTransfer : IContentTransfer +{ + private readonly IServiceProvider _services; + private readonly ICurrentUser _user; + + public ContentTransfer(IServiceProvider services, ICurrentUser user) + { + _services = services; + _user = user; + } + + public async Task Transport(ContentContainer input) + { + input.UserId = _user.Id; + + var result = new TransportResult + { + IsSuccess = true, + Messages = new List() + }; + + var zones = _services.GetServices(); + + foreach (var zone in zones) + { + input.Output = null; + + try + { + await zone.Serving(input); + } + catch (Exception ex) + { + result.IsSuccess = false; + result.Messages.Add(ex.Message); + } + } + + return result; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeBase.cs b/src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeBase.cs new file mode 100644 index 00000000..1b074d59 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeBase.cs @@ -0,0 +1,7 @@ +using BotSharp.Abstraction.Knowledges; + +namespace BotSharp.Core.Knowledges; + +public class KnowledgeBase : IKnowledgeBase +{ +} diff --git a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs index 633165a1..e95d7f13 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs @@ -1,11 +1,12 @@ +using BotSharp.Abstraction.Infrastructures.ContentTransfers; +using BotSharp.Abstraction.Infrastructures.ContentTransmitters; using BotSharp.Abstraction.Models; -using BotSharp.Abstraction.TextGeneratives; using LLama; using System.IO; namespace BotSharp.Plugins.LLamaSharp; -public class ChatCompletionProvider : IChatCompletionProvider, IBotSharpPlugin +public class ChatCompletionProvider : IBotSharpPlugin, IServiceZone { private readonly IChatModel _model; private readonly LlamaSharpSettings _settings; @@ -80,4 +81,9 @@ public class ChatCompletionProvider : IChatCompletionProvider, IBotSharpPlugin return instruction; } + + public async Task Serving(ContentContainer content) + { + + } } diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiServiceCollectionExtensions.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiServiceCollectionExtensions.cs index 4e4d2c9e..7cf77679 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiServiceCollectionExtensions.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -using BotSharp.Abstraction.TextGeneratives; +using BotSharp.Abstraction.Infrastructures.ContentTransfers; using BotSharp.Platform.AzureAi; using BotSharp.Plugin.AzureOpenAI.TextGeneratives; using Microsoft.Extensions.Configuration; @@ -18,7 +18,7 @@ public static class AzureOpenAiServiceCollectionExtensions return settings; }); - services.AddScoped(); + services.AddScoped(); return services; } diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextGeneratives/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextGeneratives/ChatCompletionProvider.cs index 3d3f7a9f..890ba0a0 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextGeneratives/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextGeneratives/ChatCompletionProvider.cs @@ -1,7 +1,8 @@ using Azure; using Azure.AI.OpenAI; +using BotSharp.Abstraction.Infrastructures.ContentTransfers; +using BotSharp.Abstraction.Infrastructures.ContentTransmitters; using BotSharp.Abstraction.Models; -using BotSharp.Abstraction.TextGeneratives; using BotSharp.Platform.AzureAi; using System; using System.Collections.Generic; @@ -10,7 +11,7 @@ using System.Threading.Tasks; namespace BotSharp.Plugin.AzureOpenAI.TextGeneratives; -public class ChatCompletionProvider : IChatCompletionProvider +public class ChatCompletionProvider : IServiceZone { private readonly AzureOpenAiSettings _settings; @@ -75,6 +76,34 @@ public class ChatCompletionProvider : IChatCompletionProvider return string.Empty; } + public async Task Serving(ContentContainer content) + { + var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey)); + var chatCompletionsOptions = PrepareOptions(content.Conversations); + + var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel, chatCompletionsOptions); + using StreamingChatCompletions streaming = response.Value; + + string output = ""; + await foreach (var choice in streaming.GetChoicesStreaming()) + { + await foreach (var message in choice.GetMessageStreaming()) + { + if (message.Content == null) + continue; + Console.Write(message.Content); + output += message.Content; + } + } + + Console.WriteLine(); + content.Output = new RoleDialogModel + { + Role = ChatRole.Assistant.ToString(), + Content = output + }; + } + private ChatCompletionsOptions PrepareOptions(List conversations) { var prompt = GetInstruction(); diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs index 4adf2484..4a6d6d7e 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs @@ -13,21 +13,21 @@ using System; using Azure.AI.OpenAI; using BotSharp.Abstraction.ApiAdapters; using BotSharp.Plugin.ChatbotUI.ViewModels; -using BotSharp.Abstraction.TextGeneratives; +using BotSharp.Abstraction.Infrastructures.ContentTransmitters; +using Microsoft.Extensions.DependencyInjection; namespace BotSharp.Plugin.ChatbotUI.Controllers; [ApiController] public class ChatbotUiController : ControllerBase, IApiAdapter { + private readonly IServiceProvider _services; private readonly ILogger _logger; - private readonly IChatCompletionProvider _chatCompletionProvider; - public ChatbotUiController(ILogger logger, - IChatCompletionProvider chatCompletionProvider) + public ChatbotUiController(ILogger logger, IServiceProvider services) { _logger = logger; - _chatCompletionProvider = chatCompletionProvider; + _services = services; } [HttpGet("/v1/models")] @@ -64,12 +64,22 @@ public class ChatbotUiController : ControllerBase, IApiAdapter Content = x.Content }).ToList(); - await _chatCompletionProvider.GetChatCompletionsAsync(conversations, + /*await _chatCompletionProvider.GetChatCompletionsAsync(conversations, async content => { await OnChunkReceived(outputStream, content); - }); + });*/ + var transmitter = _services.GetRequiredService(); + + var container = new ContentContainer + { + Conversations = conversations + }; + + var result = await transmitter.Transport(container); + + await OnChunkReceived(outputStream, container.Output.Content); await OnEventCompleted(outputStream); } diff --git a/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/HuggingChatController.cs b/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/HuggingChatController.cs index 40411ef8..3944ef6f 100644 --- a/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/HuggingChatController.cs +++ b/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/HuggingChatController.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction; using BotSharp.Abstraction.ApiAdapters; using Microsoft.AspNetCore.Mvc; using Microsoft.Net.Http.Headers; @@ -10,16 +9,13 @@ using System.Threading.Tasks; using BotSharp.Plugin.HuggingFace.HuggingChat.ViewModels; using BotSharp.Abstraction.TextGeneratives; using System.Collections.Generic; -using System.Threading; namespace BotSharp.Plugin.HuggingFace.HuggingChat; public class HuggingChatController : ControllerBase, IApiAdapter { - private readonly IChatCompletionProvider _chatCompletionProvider; - public HuggingChatController(IChatCompletionProvider chatCompletionProvider) + public HuggingChatController() { - _chatCompletionProvider = chatCompletionProvider; } /*[HttpPost("/conversation")] diff --git a/src/Infrastructure/BotSharp.Abstraction/TextGeneratives/TextToken.cs b/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/TextToken.cs similarity index 100% rename from src/Infrastructure/BotSharp.Abstraction/TextGeneratives/TextToken.cs rename to src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat/TextToken.cs