Improve IRoutingHook.

This commit is contained in:
Haiping Chen 2024-02-28 14:40:59 -06:00
parent 6aa8e3dc83
commit 72c5ec7bd9
15 changed files with 132 additions and 96 deletions

View file

@ -23,13 +23,13 @@ public interface IConversationService
/// </summary>
/// <param name="agentId"></param>
/// <param name="lastDalog"></param>
/// <param name="onMessageReceived"></param>
/// <param name="onResponseReceived">Received the response from AI Agent</param>
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
/// <param name="onFunctionExecuted">This delegate is useful when you want to report progress on UI</param>
/// <returns></returns>
Task<bool> SendMessage(string agentId,
RoleDialogModel lastDalog,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onResponseReceived,
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted);

View file

@ -3,11 +3,16 @@ namespace BotSharp.Abstraction.Routing;
public interface IRoutingContext
{
string GetCurrentAgentId();
string PreviousAgentId();
string OriginAgentId { get; }
string ConversationId { get; }
string MessageId { get; }
void SetMessageId(string conversationId, string messageId);
bool IsEmpty { get; }
string IntentName { get; set; }
void Push(string agentId);
void Pop();
void Replace(string agentId);
void Empty();
int AgentCount { get; }
void Push(string agentId, string? reason = null);
void Pop(string? reason = null);
void Replace(string agentId, string? reason = null);
void Empty(string? reason = null);
}

View file

@ -4,27 +4,19 @@ namespace BotSharp.Abstraction.Routing;
public interface IRoutingHook
{
/// <summary>
/// Conversation is redirected to another agent
/// </summary>
/// <param name="toAgentId"></param>
/// <param name="message"></param>
/// <returns></returns>
Task OnConversationRedirected(string toAgentId, RoleDialogModel message);
/// <summary>
/// Routing instruction is received from Router
/// </summary>
/// <param name="instruct">routing instruction</param>
/// <param name="message">message</param>
/// <returns></returns>
Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message);
Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, RoleDialogModel message);
Task OnAgentEnqueued(string agentId, string preAgentId);
Task OnAgentEnqueued(string agentId, string preAgentId, string? reason = null);
Task OnAgentDequeued(string agentId, string currentAgentId);
Task OnAgentDequeued(string agentId, string currentAgentId, string? reason = null);
Task OnAgentReplaced(string fromAgentId, string toAgentId);
Task OnAgentReplaced(string fromAgentId, string toAgentId, string? reason = null);
Task OnAgentQueueEmptied(string agentId);
Task OnAgentQueueEmptied(string agentId, string? reason = null);
}

View file

@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Routing;
public interface IRoutingService
{
Agent Router { get; }
IRoutingContext Context { get; }
/// <summary>
/// Get routable agents

View file

@ -1,7 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings;
using System.Drawing;
@ -38,6 +37,11 @@ public partial class ConversationService
RoleDialogModel response = message;
bool stopCompletion = false;
// Enqueue receiving agent first in case it stop completion by OnMessageReceived
var routing = _services.GetRequiredService<IRoutingService>();
routing.Context.SetMessageId(_conversationId, message.MessageId);
routing.Context.Push(agent.Id);
// Before chat completion hook
foreach (var hook in hooks)
{
@ -50,13 +54,14 @@ public partial class ConversationService
if (message.StopCompletion)
{
stopCompletion = true;
routing.Context.Pop();
break;
}
}
if (!stopCompletion)
{
// Routing with reasoning
var routing = _services.GetRequiredService<IRoutingService>();
var settings = _services.GetRequiredService<RoutingSettings>();
response = agent.Type == AgentType.Routing ?
@ -93,7 +98,7 @@ public partial class ConversationService
return converation;
}
private async Task HandleAssistantMessage(RoleDialogModel response, Func<RoleDialogModel, Task> onMessageReceived)
private async Task HandleAssistantMessage(RoleDialogModel response, Func<RoleDialogModel, Task> onResponseReceived)
{
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.GetAgent(response.CurrentAgentId);
@ -130,7 +135,7 @@ public partial class ConversationService
await hook.OnResponseGenerated(response);
}
await onMessageReceived(response);
await onResponseReceived(response);
// Add to dialog history
_storage.Append(_conversationId, response);

View file

@ -1,6 +1,3 @@
using BotSharp.Abstraction.Repositories;
using System.Linq;
namespace BotSharp.Core.Conversations.Services;
/// <summary>
@ -26,7 +23,6 @@ public class ConversationStateService : IConversationStateService, IDisposable
public string GetConversationId() => _conversationId;
/// <summary>
/// Set conversation state
/// </summary>

View file

@ -25,22 +25,49 @@ public class RouteToAgentFn : IFunctionCallback
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
var states = _services.GetRequiredService<IConversationStateService>();
// Push original task agent
if (!string.IsNullOrEmpty(args.OriginalAgent) && args.OriginalAgent.Length < 32)
{
// Correct user goal agent to keep orignal task
var goalAgentInState = states.GetState("user_goal_agent", string.Empty);
if (goalAgentInState == string.Empty)
{
states.SetState("user_goal_agent", args.OriginalAgent, isNeedVersion: true);
}
else if (args.OriginalAgent == args.AgentName && args.OriginalAgent != goalAgentInState)
{
// Correct to original agent
args.OriginalAgent = goalAgentInState;
}
else if (args.OriginalAgent != args.AgentName && args.OriginalAgent != goalAgentInState)
{
// Correct to original agent
states.SetState("user_goal_agent", args.OriginalAgent, isNeedVersion: true);
}
var db = _services.GetRequiredService<IBotSharpRepository>();
var filter = new AgentFilter { AgentName = args.OriginalAgent };
var originalAgent = db.GetAgents(filter).FirstOrDefault();
if (originalAgent != null)
{
_context.Push(originalAgent.Id);
_context.Push(originalAgent.Id, $"user goal agent");
}
}
else
// Push next action agent
if (!string.IsNullOrEmpty(args.AgentName) && args.AgentName.Length < 32)
{
// Push current agent to routing stack
_context.Push(message.CurrentAgentId);
var db = _services.GetRequiredService<IBotSharpRepository>();
var filter = new AgentFilter { AgentName = args.AgentName };
var actionAgent = db.GetAgents(filter).FirstOrDefault();
if (actionAgent != null)
{
_context.Push(actionAgent.Id, args.Reason);
}
states.SetState("last_action_agent", args.AgentName, isNeedVersion: true);
}
if (string.IsNullOrEmpty(args.AgentName))
@ -67,17 +94,11 @@ public class RouteToAgentFn : IFunctionCallback
if (missingfield && message.CurrentAgentId != agentId)
{
// Stack original Agent
_context.Push(targetAgent.Id);
message.CurrentAgentId = agentId;
}
else
{
message.CurrentAgentId = targetAgent.Id;
_context.Push(agentId, reason: "redirection rule");
}
}
_context.Push(message.CurrentAgentId);
message.CurrentAgentId = _context.GetCurrentAgentId();
return true;
}
@ -153,9 +174,6 @@ public class RouteToAgentFn : IFunctionCallback
#else
logger.LogInformation($"*** Routing redirect to {record.Name.ToUpper()} ***");
#endif
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnConversationRedirected(routingRule.RedirectTo, message)
).Wait();
}
else
{

View file

@ -85,7 +85,7 @@ public class HFPlanner : IPlaner
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var context = _services.GetRequiredService<IRoutingContext>();
context.Empty();
context.Empty(reason: $"Agent queue is cleared by {nameof(HFPlanner)}");
return true;
}

View file

@ -101,7 +101,7 @@ public class NaivePlanner : IPlaner
}
else
{
context.Empty();
context.Empty(reason: $"Agent queue is cleared by {nameof(NaivePlanner)}");
}
return true;
}

View file

@ -144,7 +144,7 @@ public class SequentialPlanner : IPlaner
if (message.StopCompletion)
{
context.Empty();
context.Empty(reason: $"Agent queue is cleared by {nameof(SequentialPlanner)}");
return false;
}

View file

@ -159,7 +159,7 @@ public partial class TwoStagePlanner : IPlaner
if (message.StopCompletion || _isTaskCompleted)
{
context.Empty();
context.Empty(reason: $"Agent queue is cleared by {nameof(TwoStagePlanner)}");
return false;
}

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing.Settings;
namespace BotSharp.Core.Routing;
@ -8,6 +7,8 @@ public class RoutingContext : IRoutingContext
private readonly IServiceProvider _services;
private readonly RoutingSettings _setting;
private string[] _routerAgentIds;
private string _conversationId;
private string _messageId;
public RoutingContext(IServiceProvider services, RoutingSettings setting)
{
@ -15,6 +16,10 @@ public class RoutingContext : IRoutingContext
_setting = setting;
}
public int AgentCount => _stack.Count;
public string ConversationId => _conversationId;
public string MessageId => _messageId;
private Stack<string> _stack { get; set; }
= new Stack<string>();
@ -45,12 +50,13 @@ public class RoutingContext : IRoutingContext
}
public bool IsEmpty => !_stack.Any();
public string GetCurrentAgentId()
{
return _stack.Peek();
}
public void Push(string agentId)
public void Push(string agentId, string? reason = null)
{
if (_stack.Count == 0 || _stack.Peek() != agentId)
{
@ -58,7 +64,7 @@ public class RoutingContext : IRoutingContext
_stack.Push(agentId);
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnAgentEnqueued(agentId, preAgentId)
await hook.OnAgentEnqueued(agentId, preAgentId, reason: reason)
).Wait();
}
}
@ -66,7 +72,7 @@ public class RoutingContext : IRoutingContext
/// <summary>
/// Pop current agent
/// </summary>
public void Pop()
public void Pop(string? reason = null)
{
if (_stack.Count == 0)
{
@ -76,11 +82,25 @@ public class RoutingContext : IRoutingContext
var agentId = _stack.Pop();
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnAgentDequeued(agentId, _stack.Peek())
await hook.OnAgentDequeued(agentId, _stack.Peek(), reason: reason)
).Wait();
}
public void Replace(string agentId)
public string PreviousAgentId()
{
if (_stack.Count == 1)
{
return GetCurrentAgentId();
}
else if (_stack.Count > 1)
{
return _stack.ToArray()[1];
}
return string.Empty;
}
public void Replace(string agentId, string? reason = null)
{
var fromAgent = agentId;
var toAgent = agentId;
@ -96,12 +116,12 @@ public class RoutingContext : IRoutingContext
_stack.Push(agentId);
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnAgentReplaced(fromAgent, toAgent)
await hook.OnAgentReplaced(fromAgent, toAgent, reason: reason)
).Wait();
}
}
public void Empty()
public void Empty(string? reason = null)
{
if (_stack.Count == 0)
{
@ -111,7 +131,13 @@ public class RoutingContext : IRoutingContext
var agentId = GetCurrentAgentId();
_stack.Clear();
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnAgentQueueEmptied(agentId)
await hook.OnAgentQueueEmptied(agentId, reason: reason)
).Wait();
}
public void SetMessageId(string conversationId, string messageId)
{
_conversationId = conversationId;
_messageId = messageId;
}
}

View file

@ -1,8 +1,3 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Planning;
using BotSharp.Abstraction.Routing.Settings;
@ -14,8 +9,11 @@ public partial class RoutingService : IRoutingService
{
private readonly IServiceProvider _services;
private readonly RoutingSettings _settings;
private readonly IRoutingContext _context;
private readonly ILogger _logger;
private Agent _router;
public IRoutingContext Context => _context;
public Agent Router => _router;
public void ResetRecursiveCounter()
@ -25,10 +23,12 @@ public partial class RoutingService : IRoutingService
public RoutingService(IServiceProvider services,
RoutingSettings settings,
IRoutingContext context,
ILogger<RoutingService> logger)
{
_services = services;
_settings = settings;
_context = context;
_logger = logger;
}
@ -80,15 +80,14 @@ public partial class RoutingService : IRoutingService
var conv = _services.GetRequiredService<IConversationService>();
var dialogs = conv.GetDialogHistory();
var context = _services.GetRequiredService<IRoutingContext>();
var executor = _services.GetRequiredService<IExecutor>();
var planner = GetPlanner(_router);
context.Push(_router.Id);
_context.Push(_router.Id);
int loopCount = 0;
while (loopCount < planner.MaxLoopCount && !context.IsEmpty)
while (loopCount < planner.MaxLoopCount && !_context.IsEmpty)
{
loopCount++;
@ -99,7 +98,7 @@ public partial class RoutingService : IRoutingService
var inst = await planner.GetNextInstruction(_router, message.MessageId, dialogs);
await HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnConversationRouting(inst, message)
await hook.OnRoutingInstructionReceived(inst, message)
);
// Save states

View file

@ -18,6 +18,10 @@ global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Agents.Settings;
global using BotSharp.Abstraction.Conversations.Settings;
global using BotSharp.Abstraction.Agents.Enums;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.Functions.Models;
global using BotSharp.Abstraction.Repositories;
global using BotSharp.Abstraction.Repositories.Filters;
global using BotSharp.Core.Repository;
global using BotSharp.Core.Routing;
global using BotSharp.Core.Agents.Services;

View file

@ -18,7 +18,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
private readonly IConversationStateService _state;
private readonly IUserIdentity _user;
private readonly IAgentService _agentService;
private string _messageId;
private readonly IRoutingContext _routingCtx;
public StreamingLogHook(
ConversationSetting convSettings,
@ -26,7 +26,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
IHubContext<SignalRHub> chatHub,
IConversationStateService state,
IUserIdentity user,
IAgentService agentService)
IAgentService agentService,
IRoutingContext routingCtx)
{
_convSettings = convSettings;
_services = serivces;
@ -34,6 +35,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
_state = state;
_user = user;
_agentService = agentService;
_routingCtx = routingCtx;
_serializerOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
@ -45,7 +47,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
public override async Task OnMessageReceived(RoleDialogModel message)
{
_messageId = message.MessageId;
var conversationId = _state.GetConversationId();
var log = $"{message.Content}";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
@ -163,79 +164,68 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
}
#region IRoutingHook
public async Task OnAgentEnqueued(string agentId, string preAgentId)
public async Task OnAgentEnqueued(string agentId, string preAgentId, string? reason = null)
{
var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(agentId);
var preAgent = await _agentService.LoadAgent(preAgentId);
var log = $"{agent.Name} is enqueued";
var log = $"{agent.Name} is enqueued{(reason != null ? $" ({reason})" : "")}";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, preAgent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
{
MessageId = _messageId
MessageId = _routingCtx.MessageId
}));
}
public async Task OnAgentDequeued(string agentId, string currentAgentId)
public async Task OnAgentDequeued(string agentId, string currentAgentId, string? reason = null)
{
var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(agentId);
var currentAgent = await _agentService.LoadAgent(currentAgentId);
var log = $"{agent.Name} is dequeued";
var log = $"{agent.Name} is dequeued{(reason != null ? $" ({reason})" : "")}, current agent is {currentAgent.Name}";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, agent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
{
MessageId = _messageId
MessageId = _routingCtx.MessageId
}));
}
public async Task OnAgentReplaced(string fromAgentId, string toAgentId)
public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string? reason = null)
{
var conversationId = _state.GetConversationId();
var fromAgent = await _agentService.LoadAgent(fromAgentId);
var toAgent = await _agentService.LoadAgent(toAgentId);
var log = $"{fromAgent.Name} is replaced to {toAgent.Name}";
var log = $"{fromAgent.Name} is replaced to {toAgent.Name}{(reason != null ? $" ({reason})" : "")}";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, toAgent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
{
MessageId = _messageId
MessageId = _routingCtx.MessageId
}));
}
public async Task OnAgentQueueEmptied(string agentId)
public async Task OnAgentQueueEmptied(string agentId, string? reason = null)
{
var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(agentId);
var log = $"Agent queue is cleared.";
var log = reason ?? "Agent queue is cleared";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, agent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
{
MessageId = _messageId
MessageId = _routingCtx.MessageId
}));
}
public async Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message)
public async Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, RoleDialogModel message)
{
var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var log = JsonSerializer.Serialize(instruct, _serializerOptions);
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, agent?.Name, log, ContentLogSource.AgentResponse, message));
}
public async Task OnConversationRedirected(string toAgentId, RoleDialogModel message)
{
var conversationId = _state.GetConversationId();
var fromAgent = await _agentService.LoadAgent(message.CurrentAgentId);
var toAgent = await _agentService.LoadAgent(toAgentId);
var log = $"{message.Content}\r\n=====\r\nREDIRECTED TO {toAgent.Name}";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, fromAgent.Name, log, ContentLogSource.HardRule, message));
BuildContentLog(conversationId, agent.Name, log, ContentLogSource.AgentResponse, message));
}
#endregion
}