Allow switch ChatCompletionProvider dynamically.

This commit is contained in:
hchen2020 2023-09-08 18:32:57 -05:00
parent ec9faa6c4e
commit 040c40283e
8 changed files with 30 additions and 8 deletions

View file

@ -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; }
/// <summary>
/// Function name if LLM response function call

View file

@ -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;

View file

@ -83,4 +83,9 @@
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Functions\" />
<Folder Include="Hooks\" />
</ItemGroup>
</Project>

View file

@ -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;

View file

@ -85,7 +85,7 @@ public partial class ConversationService
wholeDialogs.Add(fn);
await GetChatCompletionsAsyncRecursively(chatCompletion,
await GetChatCompletionsAsyncRecursively(fn.ChatCompletion ?? chatCompletion,
agent,
wholeDialogs,
onMessageReceived,

View file

@ -1,4 +1,4 @@
namespace BotSharp.Core.Hooks;
namespace BotSharp.Core.Routing;
public class ReasoningHook : AgentHookBase
{

View file

@ -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;
/// <summary>
/// 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
/// </summary>
/// <returns></returns>
private bool HasMissingRequiredField(RoleDialogModel message, out string agentId)
private bool HasMissingRequiredField(RoleDialogModel message, out string agentId, out IChatCompletion? chatCompletion)
{
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
var router = _services.GetRequiredService<IAgentRouting>();
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<IChatCompletion>();
return completions.FirstOrDefault(x => x.GetType().FullName.EndsWith(chatCompletionProvider));
}
}

View file

@ -1,4 +1,4 @@
namespace BotSharp.Core.Hooks;
namespace BotSharp.Core.Routing;
public class RoutingHook : AgentHookBase
{