From 6554c43dbc173ca79dee7b595d9823164b5d3183 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Tue, 7 May 2024 17:16:07 -0500 Subject: [PATCH] Only show playload to AI. --- .../Conversations/Models/RoleDialogModel.cs | 3 +++ .../Conversations/Services/ConversationService.SendMessage.cs | 3 +++ .../Conversations/Services/ConversationStorage.cs | 2 +- .../Routing/RoutingService.GetConversationContent.cs | 2 +- .../Providers/ChatCompletionProvider.cs | 2 +- .../Providers/ChatCompletionProvider.cs | 2 +- 6 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index e019eae4..bc9ea0ef 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -28,6 +28,9 @@ public class RoleDialogModel : ITrackableMessage public string? SecondaryContent { get; set; } + /// + /// Postback content + /// [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] [JsonPropertyName("payload")] public string? Payload { get; set; } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index f8460e3a..cf7f74f0 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -51,6 +51,9 @@ public partial class ConversationService fileService.SaveMessageFiles(_conversationId, message.MessageId, message.Files); message.Files?.Clear(); + // Save payload + message.Payload = string.IsNullOrEmpty(replyMessage.Payload) ? message.Content : replyMessage.Payload; + // Before chat completion hook foreach (var hook in hooks) { diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs index 9bb8f103..5abc4fc5 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs @@ -104,7 +104,7 @@ public class ConversationStorage : IConversationStorage var meta = dialog.MetaData; var content = dialog.Content; var secondaryContent = dialog.SecondaryContent; - var payload = dialog.Payload; + var payload = string.IsNullOrEmpty(dialog.Payload) ? null : dialog.Payload; var role = meta.Role; var currentAgentId = meta.AgentId; var messageId = meta.MessageId; diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetConversationContent.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetConversationContent.cs index 5825a2c0..eb690b9c 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetConversationContent.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetConversationContent.cs @@ -16,7 +16,7 @@ public partial class RoutingService role = agent.Name; } - conversation += $"{role}: {dialog.SecondaryContent ?? dialog.Content}\r\n"; + conversation += $"{role}: {dialog.Payload ?? dialog.SecondaryContent ?? dialog.Content}\r\n"; } return conversation; diff --git a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs index aaebfc33..87202109 100644 --- a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs @@ -123,7 +123,7 @@ public class ChatCompletionProvider : IChatCompletion { if (conv.Role == AgentRole.User) { - messages.Add(new Message(RoleType.User, conv.Content)); + messages.Add(new Message(RoleType.User, conv.Payload ?? conv.Content)); } else if (conv.Role == AgentRole.Assistant) { diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index d2077ae6..4bca97ff 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -278,7 +278,7 @@ public class ChatCompletionProvider : IChatCompletion } else if (message.Role == ChatRole.User) { - var userMessage = new ChatRequestUserMessage(message.Content) + var userMessage = new ChatRequestUserMessage(message.Payload ?? message.Content) { // To display Planner name in log Name = message.FunctionName,