diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index 2740c8c8..0435c3db 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Agents.Enums; +using BotSharp.Abstraction.MLTasks; namespace BotSharp.Abstraction.Conversations.Models; @@ -11,6 +12,7 @@ public class RoleDialogModel public DateTime CreatedAt { get; set; } = DateTime.UtcNow; public string Content { get; set; } public string CurrentAgentId { get; set; } + public IChatCompletion ChatCompletion { get; set; } /// /// Function name if LLM response function call diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs index 22578515..8e0b24c5 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs @@ -22,6 +22,9 @@ public class RoutingRecord [JsonPropertyName("disabled")] public bool Disabled { get; set; } + [JsonPropertyName("completion_provider")] + public string CompletionProvider { get; set; } + public override string ToString() { return Name; diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index b9af74c5..a6165320 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -83,4 +83,9 @@ + + + + + diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index 1dda05a6..5d0b89f0 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -1,6 +1,4 @@ using BotSharp.Abstraction.Functions; -using BotSharp.Core.Functions; -using BotSharp.Core.Hooks; using BotSharp.Core.Routing; using BotSharp.Core.Templating; using Microsoft.AspNetCore.Builder; diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs index c42d63fe..e8c704c1 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs @@ -85,7 +85,7 @@ public partial class ConversationService wholeDialogs.Add(fn); - await GetChatCompletionsAsyncRecursively(chatCompletion, + await GetChatCompletionsAsyncRecursively(fn.ChatCompletion ?? chatCompletion, agent, wholeDialogs, onMessageReceived, diff --git a/src/Infrastructure/BotSharp.Core/Hooks/ReasoningHook.cs b/src/Infrastructure/BotSharp.Core/Routing/ReasoningHook.cs similarity index 89% rename from src/Infrastructure/BotSharp.Core/Hooks/ReasoningHook.cs rename to src/Infrastructure/BotSharp.Core/Routing/ReasoningHook.cs index f754733f..63112d4d 100644 --- a/src/Infrastructure/BotSharp.Core/Hooks/ReasoningHook.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/ReasoningHook.cs @@ -1,4 +1,4 @@ -namespace BotSharp.Core.Hooks; +namespace BotSharp.Core.Routing; public class ReasoningHook : AgentHookBase { diff --git a/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs similarity index 83% rename from src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs rename to src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs index bc3ca00a..3085c768 100644 --- a/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs @@ -1,7 +1,8 @@ using BotSharp.Abstraction.Functions; +using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Routing.Models; -namespace BotSharp.Core.Functions; +namespace BotSharp.Core.Routing; /// /// Router calls this function to set the Active Agent according to the context @@ -26,7 +27,7 @@ public class RouteToAgentFn : IFunctionCallback } else { - var missingfield = HasMissingRequiredField(message, out var agentId); + var missingfield = HasMissingRequiredField(message, out var agentId, out var chatCompletion); if (missingfield && message.CurrentAgentId != agentId) { message.CurrentAgentId = agentId; @@ -36,6 +37,7 @@ public class RouteToAgentFn : IFunctionCallback message.CurrentAgentId = agentId; message.ExecutionResult = $"Routed to {args.AgentName}"; } + message.ChatCompletion = chatCompletion; } return true; @@ -45,11 +47,12 @@ public class RouteToAgentFn : IFunctionCallback /// If the target agent needs some required fields but the /// /// - private bool HasMissingRequiredField(RoleDialogModel message, out string agentId) + private bool HasMissingRequiredField(RoleDialogModel message, out string agentId, out IChatCompletion? chatCompletion) { var args = JsonSerializer.Deserialize(message.FunctionArgs); var router = _services.GetRequiredService(); var routingRule = router.GetRecordByName(args.AgentName); + chatCompletion = null; if (routingRule == null) { @@ -58,6 +61,11 @@ public class RouteToAgentFn : IFunctionCallback return true; } + if (!string.IsNullOrEmpty(routingRule.CompletionProvider)) + { + chatCompletion = GetChatCompletion(routingRule.CompletionProvider); + } + agentId = routingRule.AgentId; // Check required fields @@ -100,4 +108,10 @@ public class RouteToAgentFn : IFunctionCallback return hasMissingField; } + + private IChatCompletion? GetChatCompletion(string chatCompletionProvider) + { + var completions = _services.GetServices(); + return completions.FirstOrDefault(x => x.GetType().FullName.EndsWith(chatCompletionProvider)); + } } diff --git a/src/Infrastructure/BotSharp.Core/Hooks/RoutingHook.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingHook.cs similarity index 93% rename from src/Infrastructure/BotSharp.Core/Hooks/RoutingHook.cs rename to src/Infrastructure/BotSharp.Core/Routing/RoutingHook.cs index aba0ffc2..851a9a60 100644 --- a/src/Infrastructure/BotSharp.Core/Hooks/RoutingHook.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingHook.cs @@ -1,4 +1,4 @@ -namespace BotSharp.Core.Hooks; +namespace BotSharp.Core.Routing; public class RoutingHook : AgentHookBase {