From 2c5fa98dfe32bcf266ccae0167dae226944b4028 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 19 Jun 2023 13:32:49 -0500 Subject: [PATCH] #73 --- .../MLTasks/IChatCompletion.cs | 8 +++++ .../AzureOpenAiPlugin.cs | 5 +++- .../Providers/ChatCompletionProvider.cs | 22 ++++++-------- .../Providers/TextCompletionProvider.cs | 4 +-- .../Services/ChatCompletionService.cs | 29 +++++++++++++++++++ .../{ => Settings}/AzureOpenAiSettings.cs | 5 ++-- .../Settings/DeploymentModelSetting.cs | 7 +++++ .../PaddleSharpPlugin.cs | 2 +- src/WebStarter/appsettings.json | 11 +++++-- 9 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs create mode 100644 src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs rename src/Plugins/BotSharp.Plugin.AzureOpenAI/{ => Settings}/AzureOpenAiSettings.cs (64%) create mode 100644 src/Plugins/BotSharp.Plugin.AzureOpenAI/Settings/DeploymentModelSetting.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs new file mode 100644 index 000000000..34da12872 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs @@ -0,0 +1,8 @@ +using BotSharp.Abstraction.Models; + +namespace BotSharp.Abstraction.MLTasks; + +public interface IChatCompletion +{ + Task GetChatCompletionsAsync(List conversations); +} diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs index 593511fb9..5c85452cc 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs @@ -2,6 +2,8 @@ using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Plugins; using BotSharp.Plugin.AzureOpenAI.Providers; +using BotSharp.Plugin.AzureOpenAI.Services; +using BotSharp.Plugin.AzureOpenAI.Settings; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -16,6 +18,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config) services.AddSingleton(x => settings); services.AddSingleton(); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(); } } \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index 184f0764e..9b23344b5 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -2,16 +2,17 @@ using Azure.AI.OpenAI; using BotSharp.Abstraction.Infrastructures.ContentTransfers; using BotSharp.Abstraction.Infrastructures.ContentTransmitters; +using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Models; -using BotSharp.Platform.AzureAi; +using BotSharp.Plugin.AzureOpenAI.Settings; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace BotSharp.Plugin.AzureOpenAI.Providers; - -public class ChatCompletionProvider : IServiceZone + +public class ChatCompletionProvider : IChatCompletion { private readonly AzureOpenAiSettings _settings; @@ -26,7 +27,7 @@ public async Task GetChatCompletionsAsync(List conversations, var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey)); var chatCompletionsOptions = PrepareOptions(conversations); - var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentName, chatCompletionsOptions); + var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions); using StreamingChatCompletions streaming = response.Value; string content = ""; @@ -76,12 +77,12 @@ public string GetInstruction() return string.Empty; } - public async Task Serving(ContentContainer content) + public async Task GetChatCompletionsAsync(List conversations) { var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey)); - var chatCompletionsOptions = PrepareOptions(content.Conversations); + var chatCompletionsOptions = PrepareOptions(conversations); - var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentName, chatCompletionsOptions); + var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions); using StreamingChatCompletions streaming = response.Value; string output = ""; @@ -96,12 +97,7 @@ public async Task Serving(ContentContainer content) } } - Console.WriteLine(); - content.Output = new RoleDialogModel - { - Role = ChatRole.Assistant.ToString(), - Content = output - }; + return output; } private ChatCompletionsOptions PrepareOptions(List conversations) diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/TextCompletionProvider.cs index d3fe0ce3b..8ee9bfbc9 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/TextCompletionProvider.cs @@ -1,9 +1,9 @@ using Azure.AI.OpenAI; using Azure; using BotSharp.Abstraction.MLTasks; -using BotSharp.Platform.AzureAi; using System; using System.Threading.Tasks; +using BotSharp.Plugin.AzureOpenAI.Settings; namespace BotSharp.Plugin.AzureOpenAI.Providers; @@ -31,7 +31,7 @@ public async Task GetCompletion(string text) }; var response = await client.GetCompletionsAsync( - deploymentOrModelName: _settings.DeploymentName, + deploymentOrModelName: _settings.DeploymentModel.TextCompletionModel, completionsOptions); // OpenAI diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs new file mode 100644 index 000000000..9d9f0b944 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs @@ -0,0 +1,29 @@ +using Azure.AI.OpenAI; +using BotSharp.Abstraction.Infrastructures.ContentTransfers; +using BotSharp.Abstraction.Infrastructures.ContentTransmitters; +using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.Models; +using System.Threading.Tasks; + +namespace BotSharp.Plugin.AzureOpenAI.Services; + +public class ChatCompletionService : IServiceZone +{ + private readonly IChatCompletion _chatCompletion; + + public ChatCompletionService(IChatCompletion chatCompletion) + { + _chatCompletion = chatCompletion; + } + + public async Task Serving(ContentContainer content) + { + var output = await _chatCompletion.GetChatCompletionsAsync(content.Conversations); + + content.Output = new RoleDialogModel + { + Role = ChatRole.Assistant.ToString(), + Content = output + }; + } +} diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiSettings.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Settings/AzureOpenAiSettings.cs similarity index 64% rename from src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiSettings.cs rename to src/Plugins/BotSharp.Plugin.AzureOpenAI/Settings/AzureOpenAiSettings.cs index f94500c4b..950c549e6 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiSettings.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Settings/AzureOpenAiSettings.cs @@ -1,10 +1,11 @@ -namespace BotSharp.Platform.AzureAi; +namespace BotSharp.Plugin.AzureOpenAI.Settings; public class AzureOpenAiSettings { public string ApiKey { get; set; } = string.Empty; public string Endpoint { get; set; } = string.Empty; - public string DeploymentName { get; set; } = string.Empty; + public DeploymentModelSetting DeploymentModel { get; set; } + = new DeploymentModelSetting(); public string InstructionFile { get; set; } = string.Empty; public string ChatSampleFile { get; set; } = string.Empty; } diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Settings/DeploymentModelSetting.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Settings/DeploymentModelSetting.cs new file mode 100644 index 000000000..24618190d --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Settings/DeploymentModelSetting.cs @@ -0,0 +1,7 @@ +namespace BotSharp.Plugin.AzureOpenAI.Settings; + +public class DeploymentModelSetting +{ + public string? ChatCompletionModel { get; set; } + public string? TextCompletionModel { get; set; } +} diff --git a/src/Plugins/BotSharp.Plugin.PaddleSharp/PaddleSharpPlugin.cs b/src/Plugins/BotSharp.Plugin.PaddleSharp/PaddleSharpPlugin.cs index 2944b43e0..f679c4e7d 100644 --- a/src/Plugins/BotSharp.Plugin.PaddleSharp/PaddleSharpPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.PaddleSharp/PaddleSharpPlugin.cs @@ -9,6 +9,6 @@ public class PaddleSharpPlugin : IBotSharpPlugin { public void RegisterDI(IServiceCollection services, IConfiguration config) { - throw new NotImplementedException(); + } } diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 58994c38d..90f666a25 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -31,7 +31,10 @@ "Endpoint": "", "InstructionFile": "Prompts\\chat-with-bob.txt", "ChatSampleFile": "Prompts\\chat-samples.txt", - "DeploymentModel": "" + "DeploymentModel": { + "ChatCompletionModel": "", + "TextCompletionModel": "" + } }, "MetaAi": { @@ -62,13 +65,15 @@ "BotSharp.Core", "BotSharp.Plugin.AzureOpenAI", "BotSharp.Plugin.MetaAI", - "BotSharp.Plugin.Qdrant" + "BotSharp.Plugin.Qdrant", + "BotSharp.Plugin.PaddleSharp" ], "Plugins": [ // "LLamaSharpPlugin", "AzureOpenAiPlugin", "MetaAiPlugin", - "QdrantPlugin" + "QdrantPlugin", + "PaddleSharpPlugin" ] } }