BotSharp/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs

63 lines
1.9 KiB
C#
Raw Normal View History

2023-09-09 15:37:38 +00:00
using BotSharp.Abstraction.MLTasks;
namespace BotSharp.Core.Infrastructures;
public class CompletionProvider
{
public static IChatCompletion GetChatCompletion(IServiceProvider services, string? provider = null, string? model = null)
2023-09-09 15:37:38 +00:00
{
var completions = services.GetServices<IChatCompletion>();
2023-09-14 16:42:48 +00:00
2023-09-14 01:41:51 +00:00
var state = services.GetRequiredService<IConversationStateService>();
2023-09-20 22:08:14 +00:00
if (string.IsNullOrEmpty(provider))
2023-09-14 16:42:48 +00:00
{
2023-12-13 18:12:25 +00:00
provider = state.GetState("provider", "azure-openai");
2023-09-14 16:42:48 +00:00
}
2023-09-20 22:08:14 +00:00
if (string.IsNullOrEmpty(model))
{
2023-12-13 18:12:25 +00:00
model = state.GetState("model", "gpt-35-turbo-4k");
}
var completer = completions.FirstOrDefault(x => x.Provider == provider);
if (completer == null)
{
var logger = services.GetRequiredService<ILogger<CompletionProvider>>();
logger.LogError($"Can't resolve completion provider by {provider}");
}
completer.SetModelName(model);
return completer;
2023-09-09 15:37:38 +00:00
}
2023-10-09 22:28:17 +00:00
public static ITextCompletion GetTextCompletion(IServiceProvider services, string? provider = null, string? model = null)
{
var completions = services.GetServices<ITextCompletion>();
var state = services.GetRequiredService<IConversationStateService>();
if (string.IsNullOrEmpty(provider))
{
2023-12-13 18:12:25 +00:00
provider = state.GetState("provider", "azure-openai");
2023-10-09 22:28:17 +00:00
}
if (string.IsNullOrEmpty(model))
{
2023-12-13 18:12:25 +00:00
model = state.GetState("model", "gpt-35-turbo-instruct");
2023-10-09 22:28:17 +00:00
}
var completer = completions.FirstOrDefault(x => x.Provider == provider);
if (completer == null)
{
var logger = services.GetRequiredService<ILogger<CompletionProvider>>();
logger.LogError($"Can't resolve completion provider by {provider}");
}
completer.SetModelName(model);
return completer;
}
2023-09-09 15:37:38 +00:00
}