Merge pull request #163 from hchen2020/master

Add RoutingContext to manage redirection stack automatically.
This commit is contained in:
Haiping 2023-10-02 15:22:25 -05:00 committed by GitHub
commit 0974d0eab0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 13 deletions

View file

@ -1,3 +1,5 @@
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Abstraction.Conversations.Models;
public class RoleDialogModel
@ -28,11 +30,6 @@ public class RoleDialogModel
/// </summary>
public object ExecutionData { get; set; }
/// <summary>
/// Intent name
/// </summary>
public string IntentName { get; set; }
/// <summary>
/// Stop conversation completion
/// </summary>

View file

@ -0,0 +1,40 @@
namespace BotSharp.Abstraction.Routing.Models;
public class RoutingContext
{
private Stack<string> _stack { get; set; }
= new Stack<string>();
/// <summary>
/// Intent name
/// </summary>
public string IntentName { get; set; }
public string OriginAgentId
=> _stack.Last();
public string CurrentAgentId
=> _stack.Peek();
public void Push(string agentId)
{
if (_stack.Count == 0 || _stack.Peek() != agentId)
{
_stack.Push(agentId);
}
}
/// <summary>
/// Pop current agent
/// </summary>
/// <returns>Return next agent</returns>
public string Pop()
{
if (_stack.Count > 1)
{
_stack.Pop();
}
return _stack.Peek();
}
}

View file

@ -10,6 +10,7 @@ using BotSharp.Core.Instructs;
using BotSharp.Abstraction.Instructs;
using BotSharp.Abstraction.Routing;
using BotSharp.Core.Routing.Hooks;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Core;
@ -32,6 +33,7 @@ public static class BotSharpServiceCollectionExtensions
services.AddScoped<IConversationStorage, ConversationStorage>();
services.AddScoped<IConversationService, ConversationService>();
services.AddScoped<IConversationStateService, ConversationStateService>();
services.AddScoped<RoutingContext>();
var databaseSettings = new DatabaseBasicSettings();
config.Bind("Database", databaseSettings);

View file

@ -37,11 +37,13 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
{
AgentName = inst.AgentName
}),
CurrentAgentId = routing.Dialogs.Last().CurrentAgentId
};
var ret = await function.Execute(message);
var result = await routing.InvokeAgent(message.CurrentAgentId);
var context = _services.GetRequiredService<RoutingContext>();
var result = await routing.InvokeAgent(context.CurrentAgentId);
return result;
}

View file

@ -13,16 +13,21 @@ public class RouteToAgentFn : IFunctionCallback
{
public string Name => "route_to_agent";
private readonly IServiceProvider _services;
private readonly RoutingContext _context;
public RouteToAgentFn(IServiceProvider services)
public RouteToAgentFn(IServiceProvider services, RoutingContext context)
{
_services = services;
_context = context;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
// Push to routing stack
_context.Push(message.CurrentAgentId);
if (string.IsNullOrEmpty(args.AgentName))
{
message.ExecutionResult = $"missing agent name";
@ -46,6 +51,8 @@ public class RouteToAgentFn : IFunctionCallback
}
}
_context.Push(message.CurrentAgentId);
// Set default execution data
message.ExecutionData = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
return true;

View file

@ -5,7 +5,7 @@ namespace BotSharp.Core.Routing;
public partial class RoutingService
{
const int MAXIMUM_RECURSION_DEPTH = 2;
const int MAXIMUM_RECURSION_DEPTH = 3;
int CurrentRecursionDepth = 0;
public async Task<RoleDialogModel> InvokeAgent(string agentId)
{

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Templating;
using System.IO;
using System.Reflection;
@ -64,7 +65,8 @@ public class ResponseTemplateService : IResponseTemplateService
// .ToList();
var db = _services.GetRequiredService<IBotSharpRepository>();
var responses = db.GetAgentResponses(agentId, "intent", message.IntentName);
var context = _services.GetRequiredService<RoutingContext>();
var responses = db.GetAgentResponses(agentId, "intent", context.IntentName);
if (responses.Count == 0)
{

View file

@ -11,6 +11,7 @@ using BotSharp.Plugin.RoutingSpeeder.Providers;
using BotSharp.Abstraction.Agents;
using System.IO;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Plugin.RoutingSpeeder;
@ -30,15 +31,14 @@ public class RoutingConversationHook: ConversationHookBase
// intentClassifier.Train();
// Utilize local discriminative model to predict intent
var predText = intentClassifier.Predict(vector);
var context = _services.GetRequiredService<RoutingContext>();
context.IntentName = intentClassifier.Predict(vector);
if (string.IsNullOrEmpty(predText))
if (string.IsNullOrEmpty(context.IntentName))
{
return;
}
message.IntentName = predText;
// Render by template
var templateService = _services.GetRequiredService<IResponseTemplateService>();
var response = await templateService.RenderIntentResponse(_agent.Id, message);