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

35 lines
993 B
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-18 08:35:02 +00:00
if (provider == null)
2023-09-14 16:42:48 +00:00
{
provider = state.GetState("provider", "azure-openai");
2023-09-14 16:42:48 +00:00
}
if (model == null)
{
model = state.GetState("model", "gpt-3.5-turbo");
}
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
}
}