diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs index 4dac7e68..49bd2bf1 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs @@ -1,9 +1,57 @@ using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.MLTasks.Settings; namespace BotSharp.Core.Infrastructures; public class CompletionProvider { + public static object GetCompletion(IServiceProvider services, string? provider = null, string? model = null) + { + var state = services.GetRequiredService(); + + if (string.IsNullOrEmpty(provider)) + { + provider = state.GetState("provider", "azure-openai"); + } + + if (string.IsNullOrEmpty(model)) + { + model = state.GetState("model", "gpt-35-turbo-instruct"); + } + + var settingsService = services.GetRequiredService(); + var settings = settingsService.GetSetting(provider, model); + + if(settings.Type == LlmModelType.Text) + { + var completions = services.GetServices(); + var completer = completions.FirstOrDefault(x => x.Provider == provider); + if (completer == null) + { + var logger = services.GetRequiredService>(); + logger.LogError($"Can't resolve text completion provider by {provider}"); + } + + completer.SetModelName(model); + + return completer; + } + else + { + var completions = services.GetServices(); + var completer = completions.FirstOrDefault(x => x.Provider == provider); + if (completer == null) + { + var logger = services.GetRequiredService>(); + logger.LogError($"Can't resolve chat completion provider by {provider}"); + } + + completer.SetModelName(model); + + return completer; + } + } + public static IChatCompletion GetChatCompletion(IServiceProvider services, string? provider = null, string? model = null) { var completions = services.GetServices(); diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs index 30d2caa5..8e520293 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs @@ -1,6 +1,8 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Instructs.Models; +using BotSharp.Abstraction.MLTasks; + namespace BotSharp.Core.Instructs; public partial class InstructService : IInstructService @@ -46,13 +48,33 @@ public partial class InstructService : IInstructService agentService.RenderedInstruction(agent) : agentService.RenderedTemplate(agent, templateName); - var completer = CompletionProvider.GetTextCompletion(_services); - var result = await completer.GetCompletion(prompt, agentId, message.MessageId); + var completer = CompletionProvider.GetCompletion(_services); var response = new InstructResult { - MessageId = message.MessageId, - Text = result + MessageId = message.MessageId }; + if (completer is ITextCompletion textCompleter) + { + var result = await textCompleter.GetCompletion(prompt, agentId, message.MessageId); + response.Text = result; + } + else if (completer is IChatCompletion chatCompleter) + { + var result = chatCompleter.GetChatCompletions(new Agent + { + Id = agentId, + Name = agent.Name + }, new List + { + new RoleDialogModel(AgentRole.User, prompt) + { + CurrentAgentId = agentId, + MessageId = message.MessageId + } + }); + response.Text = result.Content; + } + foreach (var hook in hooks) {