Skip to content

Commit

Permalink
#73
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceania2018 committed Jun 19, 2023
1 parent 7c945ac commit 2c5fa98
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using BotSharp.Abstraction.Models;

namespace BotSharp.Abstraction.MLTasks;

public interface IChatCompletion
{
Task<string> GetChatCompletionsAsync(List<RoleDialogModel> conversations);
}
5 changes: 4 additions & 1 deletion src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,6 +18,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
services.AddSingleton(x => settings);

services.AddSingleton<ITextCompletion, TextCompletionProvider>();
services.AddScoped<IServiceZone, ChatCompletionProvider>();
services.AddScoped<IChatCompletion, ChatCompletionProvider>();
services.AddScoped<IServiceZone, ChatCompletionService>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,7 +27,7 @@ public async Task GetChatCompletionsAsync(List<RoleDialogModel> 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 = "";
Expand Down Expand Up @@ -76,12 +77,12 @@ public string GetInstruction()
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 = "";
Expand All @@ -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<RoleDialogModel> conversations)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -31,7 +31,7 @@ public async Task<string> GetCompletion(string text)
};

var response = await client.GetCompletionsAsync(
deploymentOrModelName: _settings.DeploymentName,
deploymentOrModelName: _settings.DeploymentModel.TextCompletionModel,
completionsOptions);

// OpenAI
Expand Down
Original file line number Diff line number Diff line change
@@ -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
};
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BotSharp.Plugin.AzureOpenAI.Settings;

public class DeploymentModelSetting
{
public string? ChatCompletionModel { get; set; }
public string? TextCompletionModel { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public class PaddleSharpPlugin : IBotSharpPlugin
{
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
throw new NotImplementedException();

}
}
11 changes: 8 additions & 3 deletions src/WebStarter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
"Endpoint": "",
"InstructionFile": "Prompts\\chat-with-bob.txt",
"ChatSampleFile": "Prompts\\chat-samples.txt",
"DeploymentModel": ""
"DeploymentModel": {
"ChatCompletionModel": "",
"TextCompletionModel": ""
}
},

"MetaAi": {
Expand Down Expand Up @@ -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"
]
}
}

0 comments on commit 2c5fa98

Please sign in to comment.