Stream Routing context log.

This commit is contained in:
Haiping Chen 2024-02-28 10:21:14 -06:00
parent 5ab2e4d978
commit ac40aa645f
25 changed files with 224 additions and 83 deletions

View file

@ -10,12 +10,6 @@
<OutputPath>$(SolutionDir)packages</OutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Infrastructures\**" />
<EmbeddedResource Remove="Infrastructures\**" />
<None Remove="Infrastructures\**" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\arts\Icon.png">
<Pack>True</Pack>

View file

@ -69,10 +69,4 @@ public abstract class ConversationHookBase : IConversationHook
public virtual Task OnUserAgentConnectedInitially(Conversation conversation)
=> Task.CompletedTask;
public virtual Task OnConversationRedirected(string toAgentId, RoleDialogModel message)
=> Task.CompletedTask;
public virtual Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message)
=> Task.CompletedTask;
}

View file

@ -84,20 +84,4 @@ public interface IConversationHook
/// <param name="conversation"></param>
/// <returns></returns>
Task OnHumanInterventionNeeded(RoleDialogModel message);
/// <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);
}

View file

@ -0,0 +1,5 @@
namespace BotSharp.Abstraction.Infrastructures;
public class HookEmittedResult
{
}

View file

@ -0,0 +1,13 @@
namespace BotSharp.Abstraction.Routing;
public interface IRoutingContext
{
string GetCurrentAgentId();
string OriginAgentId { get; }
bool IsEmpty { get; }
string IntentName { get; set; }
void Push(string agentId);
void Pop();
void Replace(string agentId);
void Empty();
}

View file

@ -0,0 +1,30 @@
using BotSharp.Abstraction.Functions.Models;
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 OnAgentEnqueued(string agentId, string preAgentId);
Task OnAgentDequeued(string agentId, string currentAgentId);
Task OnAgentReplaced(string fromAgentId, string toAgentId);
Task OnAgentQueueEmptied(string agentId);
}

View file

@ -0,0 +1,27 @@
using BotSharp.Abstraction.Infrastructures;
namespace BotSharp.Core.Infrastructures;
public static class HookEmitter
{
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Action<T> action)
{
var logger = services.GetRequiredService<ILogger<T>>();
var result = new HookEmittedResult();
var hooks = services.GetServices<T>();
foreach (var hook in hooks)
{
try
{
action(hook);
}
catch (Exception ex)
{
logger.LogError(ex.ToString());
}
}
return result;
}
}

View file

@ -29,7 +29,7 @@ public class FallbackToRouterFn : IFunctionCallback
return false;
}
var routing = _services.GetRequiredService<RoutingContext>();
var routing = _services.GetRequiredService<IRoutingContext>();
routing.Replace(targetAgent.Id);
var router = _services.GetRequiredService<IRoutingService>();

View file

@ -14,9 +14,9 @@ public class RouteToAgentFn : IFunctionCallback
{
public string Name => "route_to_agent";
private readonly IServiceProvider _services;
private readonly RoutingContext _context;
private readonly IRoutingContext _context;
public RouteToAgentFn(IServiceProvider services, RoutingContext context)
public RouteToAgentFn(IServiceProvider services, IRoutingContext context)
{
_services = services;
_context = context;
@ -153,11 +153,9 @@ public class RouteToAgentFn : IFunctionCallback
#else
logger.LogInformation($"*** Routing redirect to {record.Name.ToUpper()} ***");
#endif
var hooks = _services.GetServices<IConversationHook>();
foreach (var hook in hooks)
{
hook.OnConversationRedirected(routingRule.RedirectTo, message).Wait();
}
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnConversationRedirected(routingRule.RedirectTo, message)
).Wait();
}
else
{

View file

@ -39,7 +39,7 @@ public class RetrieveDataFromAgentRoutingHandler : RoutingHandlerBase, IRoutingH
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
{
var context = _services.GetRequiredService<RoutingContext>();
var context = _services.GetRequiredService<IRoutingContext>();
var agentId = context.GetCurrentAgentId();
var dialogs = new List<RoleDialogModel>
{

View file

@ -39,7 +39,7 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
{
var context = _services.GetRequiredService<RoutingContext>();
var context = _services.GetRequiredService<IRoutingContext>();
var function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == inst.Function);
message.FunctionArgs = JsonSerializer.Serialize(inst);
var ret = await function.Execute(message);

View file

@ -2,7 +2,7 @@ using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Planning;
using BotSharp.Abstraction.Templating;
@ -75,7 +75,7 @@ public class HFPlanner : IPlaner
var filter = new AgentFilter { AgentName = inst.AgentName };
var agent = db.GetAgents(filter).FirstOrDefault();
var context = _services.GetRequiredService<RoutingContext>();
var context = _services.GetRequiredService<IRoutingContext>();
context.Push(agent.Id);
}
@ -84,7 +84,7 @@ public class HFPlanner : IPlaner
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var context = _services.GetRequiredService<RoutingContext>();
var context = _services.GetRequiredService<IRoutingContext>();
context.Empty();
return true;
}

View file

@ -87,7 +87,7 @@ public class NaivePlanner : IPlaner
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var context = _services.GetRequiredService<RoutingContext>();
var context = _services.GetRequiredService<IRoutingContext>();
if (inst.UnmatchedAgent)
{
var unmatchedAgentId = context.GetCurrentAgentId();

View file

@ -140,7 +140,7 @@ public class SequentialPlanner : IPlaner
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var context = _services.GetRequiredService<RoutingContext>();
var context = _services.GetRequiredService<IRoutingContext>();
if (message.StopCompletion)
{

View file

@ -155,7 +155,7 @@ public partial class TwoStagePlanner : IPlaner
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var context = _services.GetRequiredService<RoutingContext>();
var context = _services.GetRequiredService<IRoutingContext>();
if (message.StopCompletion || _isTaskCompleted)
{

View file

@ -1,11 +1,9 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing.Settings;
using Microsoft.Extensions.DependencyInjection;
namespace BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Core.Routing;
public class RoutingContext
public class RoutingContext : IRoutingContext
{
private readonly IServiceProvider _services;
private readonly RoutingSettings _setting;
@ -56,7 +54,12 @@ public class RoutingContext
{
if (_stack.Count == 0 || _stack.Peek() != agentId)
{
var preAgentId = _stack.Count == 0 ? agentId : _stack.Peek();
_stack.Push(agentId);
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnAgentEnqueued(agentId, preAgentId)
).Wait();
}
}
@ -65,24 +68,50 @@ public class RoutingContext
/// </summary>
public void Pop()
{
_stack.Pop();
if (_stack.Count == 0)
{
return;
}
var agentId = _stack.Pop();
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnAgentDequeued(agentId, _stack.Peek())
).Wait();
}
public void Replace(string agentId)
{
var fromAgent = agentId;
var toAgent = agentId;
if (_stack.Count == 0)
{
_stack.Push(agentId);
}
else if (_stack.Peek() != agentId)
{
fromAgent = _stack.Peek();
_stack.Pop();
_stack.Push(agentId);
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnAgentReplaced(fromAgent, toAgent)
).Wait();
}
}
public void Empty()
{
if (_stack.Count == 0)
{
return;
}
var agentId = GetCurrentAgentId();
_stack.Clear();
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnAgentQueueEmptied(agentId)
).Wait();
}
}

View file

@ -23,7 +23,7 @@ public class RoutingPlugin : IBotSharpPlugin
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<RoutingContext>();
services.AddScoped<IRoutingContext, RoutingContext>();
// Register router
services.AddScoped(provider =>

View file

@ -64,7 +64,7 @@ public partial class RoutingService
// Pass execution result to LLM to get response
if (!message.StopCompletion)
{
var routing = _services.GetRequiredService<RoutingContext>();
var routing = _services.GetRequiredService<IRoutingContext>();
// Find response template
var templateService = _services.GetRequiredService<IResponseTemplateService>();

View file

@ -80,7 +80,7 @@ public partial class RoutingService : IRoutingService
var conv = _services.GetRequiredService<IConversationService>();
var dialogs = conv.GetDialogHistory();
var context = _services.GetRequiredService<RoutingContext>();
var context = _services.GetRequiredService<IRoutingContext>();
var executor = _services.GetRequiredService<IExecutor>();
var planner = GetPlanner(_router);
@ -98,11 +98,9 @@ public partial class RoutingService : IRoutingService
// Get instruction from Planner
var inst = await planner.GetNextInstruction(_router, message.MessageId, dialogs);
var hooks = _services.GetServices<IConversationHook>();
foreach (var hook in hooks)
{
await hook.OnConversationRouting(inst, message);
}
await HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnConversationRouting(inst, message)
);
// Save states
states.SaveStateByArgs(inst.Arguments);

View file

@ -68,8 +68,8 @@ public class ResponseTemplateService : IResponseTemplateService
// .ToList();
var db = _services.GetRequiredService<IBotSharpRepository>();
var context = _services.GetRequiredService<RoutingContext>();
var responses = db.GetAgentResponses(agentId, "intent", context.IntentName);
var routing = _services.GetRequiredService<IRoutingContext>();
var responses = db.GetAgentResponses(agentId, "intent", routing.IntentName);
if (responses.Count == 0)
{

View file

@ -7,6 +7,7 @@ global using System.Text.Json;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
global using EntityFrameworkCore.BootKit;
global using BotSharp.Abstraction.Routing;
global using BotSharp.Abstraction.Plugins;
global using BotSharp.Abstraction.Agents;
global using BotSharp.Abstraction.Conversations;
@ -18,6 +19,7 @@ global using BotSharp.Abstraction.Agents.Settings;
global using BotSharp.Abstraction.Conversations.Settings;
global using BotSharp.Abstraction.Agents.Enums;
global using BotSharp.Core.Repository;
global using BotSharp.Core.Routing;
global using BotSharp.Core.Agents.Services;
global using BotSharp.Core.Conversations.Services;
global using BotSharp.Core.Infrastructures;

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Loggers;
using BotSharp.Abstraction.Routing;
using BotSharp.Plugin.ChatHub.Hooks;
using Microsoft.Extensions.Configuration;
@ -20,5 +21,6 @@ public class ChatHubPlugin : IBotSharpPlugin
services.AddScoped<IConversationHook, ChatHubConversationHook>();
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
services.AddScoped<IConversationHook, StreamingLogHook>();
services.AddScoped<IRoutingHook, StreamingLogHook>();
}
}

View file

@ -4,11 +4,12 @@ using BotSharp.Abstraction.Loggers;
using BotSharp.Abstraction.Loggers.Enums;
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing;
using Microsoft.AspNetCore.SignalR;
namespace BotSharp.Plugin.ChatHub.Hooks;
public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IRoutingHook
{
private readonly ConversationSetting _convSettings;
private readonly JsonSerializerOptions _serializerOptions;
@ -17,6 +18,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
private readonly IConversationStateService _state;
private readonly IUserIdentity _user;
private readonly IAgentService _agentService;
private string _messageId;
public StreamingLogHook(
ConversationSetting convSettings,
@ -43,32 +45,13 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
public override async Task OnMessageReceived(RoleDialogModel message)
{
_messageId = message.MessageId;
var conversationId = _state.GetConversationId();
var log = $"{message.Role}: {message.Content}";
var log = $"{message.Content}";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, _user.UserName, log, ContentLogSource.UserInput, message));
}
public override async Task OnConversationRouting(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 override 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));
}
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
{
if (!_convSettings.ShowVerboseLog) return;
@ -147,6 +130,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
CreateTime = DateTime.UtcNow
};
var json = JsonSerializer.Serialize(log, _serializerOptions);
var convSettings = _services.GetRequiredService<ConversationSetting>();
if (convSettings.EnableContentLog)
{
@ -154,7 +139,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
db.SaveConversationContentLog(log);
}
return JsonSerializer.Serialize(log, _serializerOptions);
return json;
}
private string BuildStateLog(string conversationId, Dictionary<string, string> states, RoleDialogModel message)
@ -176,4 +161,81 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
return JsonSerializer.Serialize(log, _serializerOptions);
}
#region IRoutingHook
public async Task OnAgentEnqueued(string agentId, string preAgentId)
{
var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(agentId);
var preAgent = await _agentService.LoadAgent(preAgentId);
var log = $"{agent.Name} is enqueued";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, preAgent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
{
MessageId = _messageId
}));
}
public async Task OnAgentDequeued(string agentId, string currentAgentId)
{
var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(agentId);
var currentAgent = await _agentService.LoadAgent(currentAgentId);
var log = $"{agent.Name} is dequeued";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, currentAgent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
{
MessageId = _messageId
}));
}
public async Task OnAgentReplaced(string fromAgentId, string toAgentId)
{
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}";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, toAgent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
{
MessageId = _messageId
}));
}
public async Task OnAgentQueueEmptied(string agentId)
{
var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(agentId);
var log = $"Agent queue is cleared.";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, agent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
{
MessageId = _messageId
}));
}
public async Task OnConversationRouting(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));
}
#endregion
}

View file

@ -12,6 +12,7 @@ using BotSharp.Abstraction.Agents;
using System.IO;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing;
namespace BotSharp.Plugin.RoutingSpeeder;
@ -31,7 +32,7 @@ public class RoutingConversationHook: ConversationHookBase
// intentClassifier.Train();
// Utilize local discriminative model to predict intent
var context = _services.GetRequiredService<RoutingContext>();
var context = _services.GetRequiredService<IRoutingContext>();
context.IntentName = intentClassifier.Predict(vector);
if (string.IsNullOrEmpty(context.IntentName))

View file

@ -9,6 +9,8 @@
"isPublic": true,
"profiles": [ "tool", "sql" ],
"llmConfig": {
"model": "gpt-4-0125",
"model3": "gpt-35-turbo-1106",
"max_recursion_depth": 10
}
}