BotSharp/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ProviderHelper.cs

46 lines
1.3 KiB
C#
Raw Normal View History

2023-10-09 22:28:17 +00:00
using Azure.AI.OpenAI;
using Azure;
2024-06-26 03:38:01 +00:00
using OpenAI;
using System.ClientModel;
2023-10-09 22:28:17 +00:00
namespace BotSharp.Plugin.AzureOpenAI.Providers;
public class ProviderHelper
{
2024-03-25 23:14:14 +00:00
public static OpenAIClient GetClient(string provider, string model, IServiceProvider services)
2023-10-09 22:28:17 +00:00
{
var settingsService = services.GetRequiredService<ILlmProviderService>();
2024-03-25 23:14:14 +00:00
var settings = settingsService.GetSetting(provider, model);
var client = provider == "openai" ?
2024-06-26 03:38:01 +00:00
new OpenAIClient(new ApiKeyCredential(settings.ApiKey)) :
new AzureOpenAIClient(new Uri(settings.Endpoint), new AzureKeyCredential(settings.ApiKey));
2023-12-13 18:12:25 +00:00
return client;
2023-10-09 22:28:17 +00:00
}
2023-10-19 17:24:00 +00:00
public static List<RoleDialogModel> GetChatSamples(List<string> lines)
2023-10-09 22:28:17 +00:00
{
var samples = new List<RoleDialogModel>();
2023-10-19 17:24:00 +00:00
for (int i = 0; i < lines.Count; i++)
2023-10-09 22:28:17 +00:00
{
var line = lines[i];
if (string.IsNullOrEmpty(line.Trim()))
{
continue;
}
var role = line.Substring(0, line.IndexOf(' ') - 1).Trim();
var content = line.Substring(line.IndexOf(' ') + 1).Trim();
// comments
if (role == "##")
{
continue;
}
samples.Add(new RoleDialogModel(role, content));
}
return samples;
}
}