From ab3c6089a69251fa2260318f9165d141ee4cd01f Mon Sep 17 00:00:00 2001 From: Joanna Ren <101223@smsassist.com> Date: Tue, 23 Apr 2024 17:13:38 -0500 Subject: [PATCH] update language detection --- .../Routing/Handlers/ResponseToUserRoutingHandler.cs | 2 +- .../Routing/Handlers/RouteToAgentRoutingHandler.cs | 2 +- .../BotSharp.Core/Translation/TranslationService.cs | 12 ++++++++++-- .../templates/planner_prompt.naive.liquid | 1 + 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Routing/Handlers/ResponseToUserRoutingHandler.cs b/src/Infrastructure/BotSharp.Core/Routing/Handlers/ResponseToUserRoutingHandler.cs index 81befde72..cca6615ee 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Handlers/ResponseToUserRoutingHandler.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Handlers/ResponseToUserRoutingHandler.cs @@ -23,7 +23,7 @@ public class ResponseToUserRoutingHandler : RoutingHandlerBase, IRoutingHandler new ParameterPropertyDef("user_message_in_english", "Translate user message from non-English to English"), new ParameterPropertyDef("language", - "Language detected based on the latest message that USER sent, could be English, Spanish or Chinese.", + "User prefered language, considering the whole conversation. Language could be English, Spanish or Chinese.", required: true), }; diff --git a/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs b/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs index a852c5f9e..6aa129989 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs @@ -32,7 +32,7 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler new ParameterPropertyDef("user_message_in_english", "Translate user message from non-English to English"), new ParameterPropertyDef("language", - "Language detected based on the latest message that USER sent, could be English, Spanish or Chinese.", + "User prefered language, considering the whole conversation. Language could be English, Spanish or Chinese.", required: true), }; diff --git a/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs b/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs index a461dc1b8..9d441e770 100644 --- a/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs +++ b/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs @@ -1,6 +1,7 @@ using Amazon.Runtime.Internal.Transform; using BotSharp.Abstraction.Options; using BotSharp.Abstraction.Templating; +using BotSharp.Abstraction.Translation; using BotSharp.Abstraction.Translation.Attributes; using Newtonsoft.Json; using System.Reflection; @@ -28,16 +29,23 @@ public async Task Translate(Agent router, string messageId, T data, string { _router = router; _messageId = messageId; + + var unique = new HashSet(); + Collect(data, ref unique); + if (unique.Count == 0) + { + return data; + } + var cloned = data; if (clone) { cloned = Clone(data); } - var unique = new HashSet(); - Collect(cloned, ref unique); var map = await InnerTranslate(unique, language); cloned = Assign(cloned, map); + return cloned; } diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.naive.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.naive.liquid index f7388be6c..5177a0021 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.naive.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.naive.liquid @@ -10,3 +10,4 @@ Expected user goal agent is {{ expected_user_goal_agent }}. {%- else -%} User goal agent is inferred based on user initial request. {%- endif %} +Detect language based on the overall content in [CONVERSATION] and only include user message. \ No newline at end of file