diff --git a/BotSharp.sln b/BotSharp.sln index da611aca..1f03d0eb 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -33,6 +33,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{32FAFFFE EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTest", "tests\UnitTest\UnitTest.csproj", "{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.MetaAI", "src\Plugins\BotSharp.Plugin.MetaAI\BotSharp.Plugin.MetaAI.csproj", "{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -113,6 +115,14 @@ Global {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 + {6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Debug|x64.ActiveCfg = Debug|Any CPU + {6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Debug|x64.Build.0 = Debug|Any CPU + {6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Release|Any CPU.Build.0 = Release|Any CPU + {6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Release|x64.ActiveCfg = Release|Any CPU + {6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -129,6 +139,7 @@ Global {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} + {6D8D18A9-86D7-455E-81EC-9682C30AB7E7} = {D5293208-2BEF-42FC-A64C-5954F61720BA} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19} diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 04e328d7..4944dfce 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -70,7 +70,6 @@ - diff --git a/src/Infrastructure/BotSharp.Core/Plugins/fastText/fastTextEmbeddingProvider.cs b/src/Infrastructure/BotSharp.Core/Plugins/fastText/fastTextEmbeddingProvider.cs deleted file mode 100644 index 9abd0360..00000000 --- a/src/Infrastructure/BotSharp.Core/Plugins/fastText/fastTextEmbeddingProvider.cs +++ /dev/null @@ -1,23 +0,0 @@ -using BotSharp.Abstraction.MLTasks; -using FastText.NetWrapper; - -namespace BotSharp.Core.Plugins.fastText; - -public class fastTextEmbeddingProvider : ITextEmbedding -{ - FastTextWrapper fastText; - public fastTextEmbeddingProvider() - { - fastText = new FastTextWrapper(); - - if (!fastText.IsModelReady()) - { - fastText.LoadModel(@"D:\Service Mesh\prediction\WebStarter\tmp_data\models\crawl-300d-2M-subword.bin"); - } - } - - public float[] GetVector(string text) - { - return fastText.GetSentenceVector(text); - } -} diff --git a/src/Infrastructure/BotSharp.Core/Plugins/fastText/fastTextPlugin.cs b/src/Infrastructure/BotSharp.Core/Plugins/fastText/fastTextPlugin.cs deleted file mode 100644 index 850bf0bc..00000000 --- a/src/Infrastructure/BotSharp.Core/Plugins/fastText/fastTextPlugin.cs +++ /dev/null @@ -1,12 +0,0 @@ -using BotSharp.Abstraction.MLTasks; -using Microsoft.Extensions.Configuration; - -namespace BotSharp.Core.Plugins.fastText; - -public class fastTextPlugin : IBotSharpPlugin -{ - public void RegisterDI(IServiceCollection services, IConfiguration config) - { - services.AddSingleton(); - } -} diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs index 5053b6da..593511fb 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs @@ -1,8 +1,7 @@ using BotSharp.Abstraction.Infrastructures.ContentTransfers; using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Plugins; -using BotSharp.Plugin.AzureOpenAI.TextGeneratives; -using BotSharp.Plugin.AzureOpenAI.TextTasks; +using BotSharp.Plugin.AzureOpenAI.Providers; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiServiceCollectionExtensions.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiServiceCollectionExtensions.cs deleted file mode 100644 index 7cf77679..00000000 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiServiceCollectionExtensions.cs +++ /dev/null @@ -1,25 +0,0 @@ -using BotSharp.Abstraction.Infrastructures.ContentTransfers; -using BotSharp.Platform.AzureAi; -using BotSharp.Plugin.AzureOpenAI.TextGeneratives; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; - -namespace BotSharp.Core; - -public static class AzureOpenAiServiceCollectionExtensions -{ - public static IServiceCollection AddAzureOpenAiPlatform(this IServiceCollection services, IConfiguration config) - { - var settings = new AzureOpenAiSettings(); - config.Bind("AzureOpenAi", settings); - - services.AddSingleton(x => - { - return settings; - }); - - services.AddScoped(); - - return services; - } -} \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextTasks/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs similarity index 98% rename from src/Plugins/BotSharp.Plugin.AzureOpenAI/TextTasks/ChatCompletionProvider.cs rename to src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index ffdbfe9a..184f0764 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextTasks/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -9,7 +9,7 @@ using System.Collections.Generic; using System.IO; using System.Threading.Tasks; -namespace BotSharp.Plugin.AzureOpenAI.TextGeneratives; +namespace BotSharp.Plugin.AzureOpenAI.Providers; public class ChatCompletionProvider : IServiceZone { diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextTasks/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/TextCompletionProvider.cs similarity index 96% rename from src/Plugins/BotSharp.Plugin.AzureOpenAI/TextTasks/TextCompletionProvider.cs rename to src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/TextCompletionProvider.cs index 4cca2f24..d3fe0ce3 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextTasks/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/TextCompletionProvider.cs @@ -5,7 +5,7 @@ using BotSharp.Platform.AzureAi; using System; using System.Threading.Tasks; -namespace BotSharp.Plugin.AzureOpenAI.TextTasks; +namespace BotSharp.Plugin.AzureOpenAI.Providers; public class TextCompletionProvider : ITextCompletion { diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextGeneratives/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextGeneratives/ChatCompletionProvider.cs deleted file mode 100644 index 890ba0a0..00000000 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/TextGeneratives/ChatCompletionProvider.cs +++ /dev/null @@ -1,130 +0,0 @@ -using Azure; -using Azure.AI.OpenAI; -using BotSharp.Abstraction.Infrastructures.ContentTransfers; -using BotSharp.Abstraction.Infrastructures.ContentTransmitters; -using BotSharp.Abstraction.Models; -using BotSharp.Platform.AzureAi; -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading.Tasks; - -namespace BotSharp.Plugin.AzureOpenAI.TextGeneratives; - -public class ChatCompletionProvider : IServiceZone -{ - private readonly AzureOpenAiSettings _settings; - - public ChatCompletionProvider(AzureOpenAiSettings settings) - { - _settings = settings; - } - - public async Task GetChatCompletionsAsync(List conversations, - Func onChunkReceived) - { - var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey)); - var chatCompletionsOptions = PrepareOptions(conversations); - - var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel, chatCompletionsOptions); - using StreamingChatCompletions streaming = response.Value; - - string content = ""; - await foreach (var choice in streaming.GetChoicesStreaming()) - { - await foreach (var message in choice.GetMessageStreaming()) - { - if (message.Content == null) - continue; - Console.Write(message.Content); - content += message.Content; - await onChunkReceived(message.Content); - } - } - - Console.WriteLine(); - } - - public List GetChatSamples() - { - var samples = new List(); - if (!string.IsNullOrEmpty(_settings.ChatSampleFile)) - { - var lines = File.ReadAllLines(_settings.ChatSampleFile); - for (int i = 0; i < lines.Length; i++) - { - var line = lines[i]; - var role = line.Substring(0, line.IndexOf(' ') - 1); - var content = line.Substring(line.IndexOf(' ') + 1); - - samples.Add(new RoleDialogModel - { - Role = role, - Content = content - }); - } - } - return samples; - } - - public string GetInstruction() - { - if (!string.IsNullOrEmpty(_settings.InstructionFile)) - { - return File.ReadAllText(_settings.InstructionFile); - } - 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(); - var chatCompletionsOptions = new ChatCompletionsOptions() - { - Messages = - { - new ChatMessage(ChatRole.System, prompt) - } - }; - - foreach (var message in GetChatSamples()) - { - chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content)); - } - - foreach (var message in conversations) - { - chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content)); - } - - return chatCompletionsOptions; - } -} diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/BotSharp.Plugin.MetaAI.csproj b/src/Plugins/BotSharp.Plugin.MetaAI/BotSharp.Plugin.MetaAI.csproj new file mode 100644 index 00000000..4183f4bc --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MetaAI/BotSharp.Plugin.MetaAI.csproj @@ -0,0 +1,23 @@ + + + + netstandard2.1 + enable + 10 + + + + + + + + + + + + + + + + + diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs b/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs new file mode 100644 index 00000000..4d3ec9a5 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs @@ -0,0 +1,22 @@ +using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.Plugins; +using BotSharp.Plugin.MetaAI.Providers; +using BotSharp.Plugin.MetaAI.Settings; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace BotSharp.Plugin.MetaAI; + +public class MetaAiPlugin : IBotSharpPlugin +{ + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + var settings = new MetaAiSettings(); + config.Bind("MetaAi", settings); + services.AddSingleton(x => settings); + services.AddSingleton(x => settings.fastText); + + services.AddSingleton(); + } +} diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs new file mode 100644 index 00000000..0e2a27a1 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs @@ -0,0 +1,27 @@ +using BotSharp.Abstraction.MLTasks; +using BotSharp.Plugin.MetaAI.Settings; +using FastText.NetWrapper; + +namespace BotSharp.Plugin.MetaAI.Providers; + +public class fastTextEmbeddingProvider : ITextEmbedding +{ + private FastTextWrapper _fastText; + private readonly fastTextSetting _settings; + + public fastTextEmbeddingProvider(fastTextSetting settings) + { + _settings = settings; + _fastText = new FastTextWrapper(); + + if (!_fastText.IsModelReady()) + { + _fastText.LoadModel(settings.ModelPath); + } + } + + public float[] GetVector(string text) + { + return _fastText.GetSentenceVector(text); + } +} diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/Settings/MetaAiSettings.cs b/src/Plugins/BotSharp.Plugin.MetaAI/Settings/MetaAiSettings.cs new file mode 100644 index 00000000..cde5121c --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MetaAI/Settings/MetaAiSettings.cs @@ -0,0 +1,6 @@ +namespace BotSharp.Plugin.MetaAI.Settings; + +public class MetaAiSettings +{ + public fastTextSetting fastText { get; set; } +} diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/Settings/fastTextSetting.cs b/src/Plugins/BotSharp.Plugin.MetaAI/Settings/fastTextSetting.cs new file mode 100644 index 00000000..73ec2060 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MetaAI/Settings/fastTextSetting.cs @@ -0,0 +1,6 @@ +namespace BotSharp.Plugin.MetaAI.Settings; + +public class fastTextSetting +{ + public string ModelPath { get; set; } +} diff --git a/src/WebStarter/WebStarter.csproj b/src/WebStarter/WebStarter.csproj index 406935a2..304e16d9 100644 --- a/src/WebStarter/WebStarter.csproj +++ b/src/WebStarter/WebStarter.csproj @@ -31,6 +31,7 @@ + diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 8211f423..f3c049c0 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -30,6 +30,12 @@ "DeploymentModel": "" }, + "MetaAi": { + "fastText": { + "ModelPath": "crawl-300d-2M-subword.bin" + } + }, + "Database": { "MongoDb": { "Master": "mongodb://localhost:27017/chat-ui" @@ -45,7 +51,8 @@ "PluginLoader": { "Assemblies": [ "BotSharp.Core", - "BotSharp.Plugin.AzureOpenAI" + "BotSharp.Plugin.AzureOpenAI", + "BotSharp.Plugin.MetaAI" ] } }