diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructService.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructService.cs index 476a9f01..0669a267 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructService.cs @@ -4,5 +4,5 @@ namespace BotSharp.Abstraction.Instructs; public interface IInstructService { - Task Execute(string agentId, RoleDialogModel message, string? templateName = null); + Task Execute(string agentId, RoleDialogModel message, string? templateName = null, string? instruction = null); } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs index 49bd2bf1..17a0ebe7 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.MLTasks.Settings; @@ -5,67 +6,58 @@ namespace BotSharp.Core.Infrastructures; public class CompletionProvider { - public static object GetCompletion(IServiceProvider services, string? provider = null, string? model = null) + public static object GetCompletion(IServiceProvider services, + string? provider = null, + string? model = null, + AgentLlmConfig? agentConfig = null) { var state = services.GetRequiredService(); + var agentSetting = services.GetRequiredService(); if (string.IsNullOrEmpty(provider)) { - provider = state.GetState("provider", "azure-openai"); + provider = agentConfig?.Provider ?? agentSetting.LlmConfig?.Provider; + provider = state.GetState("provider", provider ?? "azure-openai"); } if (string.IsNullOrEmpty(model)) { - model = state.GetState("model", "gpt-35-turbo-instruct"); + model = agentConfig?.Model ?? agentSetting.LlmConfig?.Model; + model = state.GetState("model", model ?? "gpt-35-turbo-4k"); } var settingsService = services.GetRequiredService(); var settings = settingsService.GetSetting(provider, model); - if(settings.Type == LlmModelType.Text) + 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; + return GetTextCompletion(services, provider: provider, model: model); } 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; + return GetChatCompletion(services, provider: provider, model: model); } } - public static IChatCompletion GetChatCompletion(IServiceProvider services, string? provider = null, string? model = null) + public static IChatCompletion GetChatCompletion(IServiceProvider services, + string? provider = null, + string? model = null, + AgentLlmConfig? agentConfig = null) { var completions = services.GetServices(); - + var agentSetting = services.GetRequiredService(); var state = services.GetRequiredService(); if (string.IsNullOrEmpty(provider)) { - provider = state.GetState("provider", "azure-openai"); + provider = agentConfig?.Provider ?? agentSetting.LlmConfig?.Provider; + provider = state.GetState("provider", provider ?? "azure-openai"); } if (string.IsNullOrEmpty(model)) { - model = state.GetState("model", "gpt-35-turbo-4k"); + model = agentConfig?.Model ?? agentSetting.LlmConfig?.Model; + model = state.GetState("model", model ?? "gpt-35-turbo-4k"); } var completer = completions.FirstOrDefault(x => x.Provider == provider); @@ -80,20 +72,25 @@ public class CompletionProvider return completer; } - public static ITextCompletion GetTextCompletion(IServiceProvider services, string? provider = null, string? model = null) + public static ITextCompletion GetTextCompletion(IServiceProvider services, + string? provider = null, + string? model = null, + AgentLlmConfig? agentConfig = null) { var completions = services.GetServices(); - + var agentSetting = services.GetRequiredService(); var state = services.GetRequiredService(); if (string.IsNullOrEmpty(provider)) { - provider = state.GetState("provider", "azure-openai"); + provider = agentConfig?.Provider ?? agentSetting.LlmConfig?.Provider; + provider = state.GetState("provider", provider ?? "azure-openai"); } if (string.IsNullOrEmpty(model)) { - model = state.GetState("model", "gpt-35-turbo-instruct"); + model = agentConfig?.Model ?? agentSetting.LlmConfig?.Model; + model = state.GetState("model", model ?? "gpt-35-turbo-instruct"); } var completer = completions.FirstOrDefault(x => x.Provider == provider); diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs index 8e520293..e3b0df9f 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs @@ -16,7 +16,7 @@ public partial class InstructService : IInstructService _logger = logger; } - public async Task Execute(string agentId, RoleDialogModel message, string? templateName = null) + public async Task Execute(string agentId, RoleDialogModel message, string? templateName = null, string? instruction = null) { var agentService = _services.GetRequiredService(); Agent agent = await agentService.LoadAgent(agentId); @@ -48,7 +48,8 @@ public partial class InstructService : IInstructService agentService.RenderedInstruction(agent) : agentService.RenderedTemplate(agent, templateName); - var completer = CompletionProvider.GetCompletion(_services); + var completer = CompletionProvider.GetCompletion(_services, + agentConfig: agent.LlmConfig); var response = new InstructResult { MessageId = message.MessageId @@ -63,7 +64,8 @@ public partial class InstructService : IInstructService var result = chatCompleter.GetChatCompletions(new Agent { Id = agentId, - Name = agent.Name + Name = agent.Name, + Instruction = instruction }, new List { new RoleDialogModel(AgentRole.User, prompt) diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs index f38050f0..51d0a22f 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs @@ -19,12 +19,10 @@ public partial class RoutingService } var agentService = _services.GetRequiredService(); - var agentSetting = _services.GetRequiredService(); var agent = await agentService.LoadAgent(agentId); var chatCompletion = CompletionProvider.GetChatCompletion(_services, - provider: agent?.LlmConfig?.Provider ?? agentSetting.LlmConfig.Provider, - model: agent.LlmConfig?.Model ?? agentSetting.LlmConfig.Model); + agentConfig: agent.LlmConfig); var message = dialogs.Last(); var response = chatCompletion.GetChatCompletions(agent, dialogs); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs index c2199cb8..31c1ecb8 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs @@ -25,12 +25,14 @@ public class InstructModeController : ControllerBase, IApiAdapter input.States.ForEach(x => state.SetState(x.Split('=')[0], x.Split('=')[1])); state.SetState("provider", input.Provider) .SetState("model", input.Model) + .SetState("instruction", input.Instruction) .SetState("input_text", input.Text); var instructor = _services.GetRequiredService(); var result = await instructor.Execute(agentId, new RoleDialogModel(AgentRole.User, input.Text), - templateName: input.Template); + templateName: input.Template, + instruction: input.Instruction); result.States = state.GetStates(); diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs index 33e5e168..3b265e04 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs @@ -4,6 +4,10 @@ namespace BotSharp.OpenAPI.ViewModels.Instructs; public class InstructMessageModel : IncomingMessageModel { + /// + /// System prompt + /// + public string? Instruction { get; set; } public override string Channel { get; set; } = ConversationChannel.OpenAPI; public string? Template { get; set; } }