diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs index 60307439..bf242ecc 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs @@ -2,16 +2,6 @@ namespace BotSharp.Abstraction.Agents.Settings; public class AgentSettings { - /// - /// Router Agent Id - /// - public string RouterId { get; set; } - - /// - /// Reasoner Agent Id - /// - public string ReasonerId { get; set; } - public string DataDir { get; set; } public string TemplateFormat { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs index c9836d4b..7cad03ff 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs @@ -7,5 +7,4 @@ public class ConversationSetting public bool EnableKnowledgeBase { get; set; } public bool ShowVerboseLog { get; set; } public int MaxRecursiveDepth { get; set; } = 3; - public bool EnableReasoning { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfileRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfileRecord.cs new file mode 100644 index 00000000..753881ec --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfileRecord.cs @@ -0,0 +1,12 @@ +using System.Text.Json.Serialization; + +namespace BotSharp.Abstraction.Routing.Models; + +public class RoutingProfileRecord +{ + [JsonPropertyName("name")] + public string Name { get; set; } + + [JsonPropertyName("agent_ids")] + public string[] AgentIds { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Settings/RoutingSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Settings/RoutingSettings.cs new file mode 100644 index 00000000..b9cc51a9 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Settings/RoutingSettings.cs @@ -0,0 +1,14 @@ +namespace BotSharp.Abstraction.Routing.Settings; + +public class RoutingSettings +{ + /// + /// Router Agent Id + /// + public string RouterId { get; set; } + + /// + /// Reasoner Agent Id + /// + public string ReasonerId { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index f0e69fbe..8316e274 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -7,6 +7,7 @@ using BotSharp.Core.Templating; using BotSharp.Core.Plugins.Knowledges.Services; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; +using BotSharp.Abstraction.Routing.Settings; namespace BotSharp.Core; @@ -44,13 +45,14 @@ public static class BotSharpServiceCollectionExtensions services.AddSingleton(); // Register router + var routingSettings = new RoutingSettings(); + config.Bind("Router", routingSettings); + services.AddSingleton((IServiceProvider x) => routingSettings); + services.AddScoped(); services.AddScoped(); - services.AddScoped(p => - { - var setting = p.GetRequiredService(); - return setting.EnableReasoning ? p.GetRequiredService() : p.GetRequiredService(); - }); + services.AddScoped(); + services.AddScoped(); // Register function callback services.AddScoped(); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index 24376c21..322b549f 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -2,6 +2,7 @@ using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.Routing.Settings; using BotSharp.Core.Routing; namespace BotSharp.Core.Conversations.Services; @@ -33,8 +34,8 @@ public partial class ConversationService stateService.Load(); stateService.SetState("channel", lastDialog.Channel); - var router = _services.GetRequiredService(); - Agent agent = await router.LoadRouter(); + var agentService = _services.GetRequiredService(); + Agent agent = await agentService.LoadAgent(agentId); _logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}"); @@ -69,7 +70,8 @@ public partial class ConversationService } // reasoning - if (_settings.EnableReasoning) + var settings = _services.GetRequiredService(); + if (settings.ReasonerId == agent.Id) { var simulator = _services.GetRequiredService(); var reasonedContext = await simulator.Enter(agent, wholeDialogs); @@ -96,7 +98,6 @@ public partial class ConversationService { if (reasonedContext.CurrentAgentId != agent.Id) { - var agentService = _services.GetRequiredService(); agent = await agentService.LoadAgent(reasonedContext.CurrentAgentId); } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Reasoner.cs b/src/Infrastructure/BotSharp.Core/Routing/Reasoner.cs index c08f83ac..182bedb6 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Reasoner.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Reasoner.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Routing.Settings; + namespace BotSharp.Core.Routing; public class Reasoner : Router @@ -6,7 +8,7 @@ public class Reasoner : Router public Reasoner(IServiceProvider services, ILogger logger, - AgentSettings settings) : base(services, logger, settings) + RoutingSettings settings) : base(services, logger, settings) { } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Router.cs b/src/Infrastructure/BotSharp.Core/Routing/Router.cs index 594ee734..c1f025c4 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Router.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Router.cs @@ -1,7 +1,7 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Routing.Models; +using BotSharp.Abstraction.Routing.Settings; using System.IO; -using static Tensorflow.ApiDef.Types; namespace BotSharp.Core.Routing; @@ -9,13 +9,13 @@ public class Router : IAgentRouting { protected readonly IServiceProvider _services; protected readonly ILogger _logger; - protected readonly AgentSettings _settings; + protected readonly RoutingSettings _settings; public virtual string AgentId => _settings.RouterId; public Router(IServiceProvider services, ILogger logger, - AgentSettings settings) + RoutingSettings settings) { _services = services; _logger = logger; @@ -32,8 +32,24 @@ public class Router : IAgentRouting { var agentSettings = _services.GetRequiredService(); var dbSettings = _services.GetRequiredService(); - var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, agentSettings.RouterId, "route.json"); - return JsonSerializer.Deserialize(File.ReadAllText(filePath)); + var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, _settings.RouterId, "route.json"); + var records = JsonSerializer.Deserialize(File.ReadAllText(filePath)); + + // check if routing profile is specified + filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json"); + if (File.Exists(filePath)) + { + var state = _services.GetRequiredService(); + var name = state.GetState("channel"); + var profiles = JsonSerializer.Deserialize(File.ReadAllText(filePath)); + var spcificedProfile = profiles.FirstOrDefault(x => x.Name == name); + if (spcificedProfile != null) + { + records = records.Where(x => spcificedProfile.AgentIds.Contains(x.AgentId)).ToArray(); + } + } + + return records; } public RoutingRecord GetRecordByName(string name) diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 026a7874..364416cd 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -40,7 +40,8 @@ public class ConversationController : ControllerBase, IApiAdapter [HttpPost("/conversation/{agentId}/{conversationId}")] public async Task SendMessage([FromRoute] string agentId, [FromRoute] string conversationId, - [FromBody] NewMessageModel input) + [FromBody] NewMessageModel input, + [FromQuery] string? channel = "openapi") { var conv = _services.GetRequiredService(); @@ -50,7 +51,7 @@ public class ConversationController : ControllerBase, IApiAdapter await conv.SendMessage(agentId, conversationId, new RoleDialogModel("user", input.Text) { - Channel = "webapi" + Channel = channel }, async msg => {