Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BotSharp.Abstraction.Routing.Models;

namespace BotSharp.Abstraction.Conversations.Models;

public class RoleDialogModel
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using BotSharp.Abstraction.Instructs;
using BotSharp.Abstraction.Routing;
using BotSharp.Core.Routing.Hooks;
using BotSharp.Abstraction.Routing.Models;

namespace BotSharp.Core;

Expand All @@ -32,6 +33,7 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
services.AddScoped<IConversationStorage, ConversationStorage>();
services.AddScoped<IConversationService, ConversationService>();
services.AddScoped<IConversationStateService, ConversationStateService>();
services.AddScoped<RoutingContext>();

var databaseSettings = new DatabaseBasicSettings();
config.Bind("Database", databaseSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallF
{
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;
}
Expand Down
9 changes: 8 additions & 1 deletion src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -46,6 +51,8 @@ public async Task<bool> Execute(RoleDialogModel message)
}
}

_context.Push(message.CurrentAgentId);

// Set default execution data
message.ExecutionData = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Templating;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -64,7 +65,8 @@ public async Task<string> RenderIntentResponse(string agentId, RoleDialogModel m
// .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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using BotSharp.Abstraction.Agents;
using System.IO;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Abstraction.Routing.Models;

namespace BotSharp.Plugin.RoutingSpeeder;

Expand All @@ -30,15 +31,14 @@ public override async Task BeforeCompletion(RoleDialogModel message)

// 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);
Expand Down