From 0d23f06a52a3c70f7c4a3e2736e7a57c1ca717d0 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 22 Feb 2024 11:25:13 -0600 Subject: [PATCH] save rich content --- .../Loggers/Enums/ContentLogSource.cs | 9 +++++ .../Models/ConversationContentLogModel.cs | 6 +++ .../Hooks/StreamingLogHook.cs | 40 +++++++++---------- .../ConversationContentLogDocument.cs | 1 + .../Repository/MongoRepository.Log.cs | 2 + 5 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Loggers/Enums/ContentLogSource.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Enums/ContentLogSource.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Enums/ContentLogSource.cs new file mode 100644 index 00000000..f6973770 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Enums/ContentLogSource.cs @@ -0,0 +1,9 @@ +namespace BotSharp.Abstraction.Loggers.Enums; + +public static class ContentLogSource +{ + public const string UserInput = "user input"; + public const string Prompt = "prompt"; + public const string FunctionCall = "function call"; + public const string AgentResponse = "agent response"; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/ConversationContentLogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/ConversationContentLogModel.cs index ee01feb8..82a1ca11 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/ConversationContentLogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/ConversationContentLogModel.cs @@ -4,13 +4,19 @@ public class ConversationContentLogModel { [JsonPropertyName("conversation_id")] public string ConversationId { get; set; } + [JsonPropertyName("message_id")] public string MessageId { get; set; } + [JsonPropertyName("name")] public string? Name { get; set; } + [JsonPropertyName("role")] public string Role { get; set; } + [JsonPropertyName("source")] + public string Source { get; set; } + [JsonPropertyName("content")] public string Content { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 684b02d3..13997825 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -1,11 +1,11 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Loggers; +using BotSharp.Abstraction.Loggers.Enums; using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Repositories.Filters; -using BotSharp.Abstraction.Routing.Settings; using Microsoft.AspNetCore.SignalR; +using Serilog; namespace BotSharp.Plugin.ChatHub.Hooks; @@ -37,11 +37,13 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook AllowTrailingCommas = true }; } + public override async Task OnMessageReceived(RoleDialogModel message) { var conversationId = _state.GetConversationId(); var log = $"MessageId: {message.MessageId} ==>\r\n{message.Role}: {message.Content}"; - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, _user.UserName, log, message)); + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", + BuildContentLog(conversationId, _user.UserName, log, ContentLogSource.UserInput, message)); } public async Task BeforeGenerating(Agent agent, List conversations) @@ -62,7 +64,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook var agent = await agentService.LoadAgent(message.CurrentAgentId); var log = $"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs}) => {message.Content}"; log += $"\r\n<== MessageId: {message.MessageId}"; - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, agent?.Name, log, message)); + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", + BuildContentLog(conversationId, agent?.Name, log, ContentLogSource.FunctionCall, message)); } /// @@ -78,30 +81,25 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook var agentService = _services.GetRequiredService(); var conversationId = _state.GetConversationId(); var agent = await agentService.LoadAgent(message.CurrentAgentId); + var logSource = string.Empty; // Log routing output try { var inst = message.Content.JsonContent(); - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, agent?.Name, message.Content, message)); + logSource = ContentLogSource.AgentResponse; + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", + BuildContentLog(conversationId, agent?.Name, message.Content, logSource, message)); } catch { // ignore } - string log; - if (message.Role == AgentRole.Function) - { - log = $"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs}) => {message.Content}"; - log += $"\r\n<== MessageId: {message.MessageId}"; - } - else - { - log = tokenStats.Prompt; - } - - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, agent?.Name, log, message)); + var log = tokenStats.Prompt; + logSource = ContentLogSource.Prompt; + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", + BuildContentLog(conversationId, agent?.Name, log, logSource, message)); } /// @@ -127,11 +125,12 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook log += $"\r\n{richContent}"; } log += $"\r\n<== MessageId: {message.MessageId}"; - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conv.ConversationId, agent?.Name, log, message)); + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", + BuildContentLog(conv.ConversationId, agent?.Name, log, ContentLogSource.AgentResponse, message)); } } - private string BuildContentLog(string conversationId, string? name, string content, RoleDialogModel message) + private string BuildContentLog(string conversationId, string? name, string logContent, string logSource, RoleDialogModel message) { var log = new ConversationContentLogModel { @@ -139,7 +138,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook MessageId = message.MessageId, Name = name, Role = message.Role, - Content = content, + Content = logContent, + Source = logSource, CreateTime = DateTime.UtcNow }; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationContentLogDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationContentLogDocument.cs index eec715dc..32d751fe 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationContentLogDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationContentLogDocument.cs @@ -6,6 +6,7 @@ public class ConversationContentLogDocument : MongoBase public string MessageId { get; set; } public string? Name { get; set; } public string Role { get; set; } + public string Source { get; set; } public string Content { get; set; } public DateTime CreateTime { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs index efe7537c..bc4d8e0e 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs @@ -73,6 +73,7 @@ public partial class MongoRepository MessageId = messageId, Name = log.Name, Role = log.Role, + Source = log.Source, Content = log.Content, CreateTime = log.CreateTime }; @@ -91,6 +92,7 @@ public partial class MongoRepository MessageId = x.MessageId, Name = x.Name, Role = x.Role, + Source = x.Source, Content = x.Content, CreateTime = x.CreateTime })