This commit is contained in:
Haiping Chen 2023-06-19 13:32:49 -05:00
parent 7c945ac2b2
commit 2c5fa98dfe
9 changed files with 71 additions and 22 deletions

View file

@ -0,0 +1,8 @@
using BotSharp.Abstraction.Models;
namespace BotSharp.Abstraction.MLTasks;
public interface IChatCompletion
{
Task<string> GetChatCompletionsAsync(List<RoleDialogModel> conversations);
}

View file

@ -2,6 +2,8 @@ using BotSharp.Abstraction.Infrastructures.ContentTransfers;
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 class AzureOpenAiPlugin : IBotSharpPlugin
services.AddSingleton(x => settings);
services.AddSingleton<ITextCompletion, TextCompletionProvider>();
services.AddScoped<IServiceZone, ChatCompletionProvider>();
services.AddScoped<IChatCompletion, ChatCompletionProvider>();
services.AddScoped<IServiceZone, ChatCompletionService>();
}
}

View file

@ -2,16 +2,17 @@ using Azure;
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 class ChatCompletionProvider : IServiceZone
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 class ChatCompletionProvider : IServiceZone
return string.Empty;
}
public async Task Serving(ContentContainer content)
public async Task<string> GetChatCompletionsAsync(List<RoleDialogModel> 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 class ChatCompletionProvider : IServiceZone
}
}
Console.WriteLine();
content.Output = new RoleDialogModel
{
Role = ChatRole.Assistant.ToString(),
Content = output
};
return output;
}
private ChatCompletionsOptions PrepareOptions(List<RoleDialogModel> conversations)

View file

@ -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 class TextCompletionProvider : ITextCompletion
};
var response = await client.GetCompletionsAsync(
deploymentOrModelName: _settings.DeploymentName,
deploymentOrModelName: _settings.DeploymentModel.TextCompletionModel,
completionsOptions);
// OpenAI

View file

@ -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
};
}
}

View file

@ -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;
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Plugin.AzureOpenAI.Settings;
public class DeploymentModelSetting
{
public string? ChatCompletionModel { get; set; }
public string? TextCompletionModel { get; set; }
}

View file

@ -9,6 +9,6 @@ public class PaddleSharpPlugin : IBotSharpPlugin
{
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
throw new NotImplementedException();
}
}

View file

@ -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"
]
}
}