Skip to content

Commit 6bcb0ae

Browse files
authored
Merge pull request #448 from hchen2020/master
Fix translator.
2 parents bdcf8c5 + 76dc908 commit 6bcb0ae

File tree

8 files changed

+52
-23
lines changed

8 files changed

+52
-23
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public class AgentSettings
55
public string DataDir { get; set; } = string.Empty;
66
public string TemplateFormat { get; set; } = "liquid";
77
public string HostAgentId { get; set; } = string.Empty;
8+
public bool EnableTranslator { get; set; } = false;
89

910
/// <summary>
1011
/// This is the default LLM config for agent
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BotSharp.Abstraction.Translation.Models;
2+
3+
public class TranslationOutput
4+
{
5+
[JsonPropertyName("input_lang")]
6+
public string InputLanguage { get; set; } = null!;
7+
8+
[JsonPropertyName("output_lang")]
9+
public string OutputLanguage { get; set; } = LanguageType.ENGLISH;
10+
11+
[JsonPropertyName("texts")]
12+
public string[] Texts { get; set; } = Array.Empty<string>();
13+
}

src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
2828
required: true),
2929
new ParameterPropertyDef("is_new_task",
3030
"whether the user is requesting a new task that is different from the previous topic.",
31-
type: "boolean"),
32-
new ParameterPropertyDef("language",
33-
"User preferred language, considering the whole conversation. Language could be English, Spanish or Chinese.",
34-
required: true)
31+
type: "boolean")
3532
};
3633

3734
public RouteToAgentRoutingHandler(IServiceProvider services, ILogger<RouteToAgentRoutingHandler> logger, RoutingSettings settings)

src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetConversationContent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public async Task<string> GetConversationContent(List<RoleDialogModel> dialogs,
1616
role = agent.Name;
1717
}
1818

19-
conversation += $"{role}: {dialog.Payload ?? dialog.SecondaryContent ?? dialog.Content}\r\n";
19+
conversation += $"{role}: {dialog.Payload ?? dialog.Content}\r\n";
2020
}
2121

2222
return conversation;

src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,29 @@ public async Task<RoleDialogModel> InstructLoop(RoleDialogModel message, List<Ro
8282

8383
_context.Push(_router.Id);
8484

85-
dialogs.Add(message);
86-
87-
// Get first instruction
88-
_router.TemplateDict["conversation"] = await GetConversationContent(dialogs);
89-
var inst = await planner.GetNextInstruction(_router, message.MessageId, dialogs);
90-
9185
// Handle multi-language for input
92-
var translator = _services.GetRequiredService<ITranslationService>();
93-
94-
var language = states.GetState("language", inst.Language);
95-
if (language != LanguageType.UNKNOWN && language != LanguageType.ENGLISH)
86+
var agentSettings = _services.GetRequiredService<AgentSettings>();
87+
if (agentSettings.EnableTranslator)
9688
{
97-
message.SecondaryContent = message.Content;
98-
message.Content = await translator.Translate(_router, message.MessageId, message.Content,
99-
language: LanguageType.ENGLISH,
100-
clone: false);
89+
var translator = _services.GetRequiredService<ITranslationService>();
90+
91+
var language = states.GetState("language", LanguageType.UNKNOWN);
92+
if (language != LanguageType.ENGLISH)
93+
{
94+
message.SecondaryContent = message.Content;
95+
message.Content = await translator.Translate(_router, message.MessageId, message.Content,
96+
language: LanguageType.ENGLISH,
97+
clone: false);
98+
}
10199
}
102100

101+
dialogs.Add(message);
103102
storage.Append(convService.ConversationId, message);
104103

104+
// Get first instruction
105+
_router.TemplateDict["conversation"] = await GetConversationContent(dialogs);
106+
var inst = await planner.GetNextInstruction(_router, message.MessageId, dialogs);
107+
105108
int loopCount = 1;
106109
while (true)
107110
{

src/Infrastructure/BotSharp.Core/Translation/TranslationResponseHook.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public TranslationResponseHook(IServiceProvider services,
2121
}
2222
public override async Task OnResponseGenerated(RoleDialogModel message)
2323
{
24+
var agentSettings = _services.GetRequiredService<AgentSettings>();
25+
if (!agentSettings.EnableTranslator)
26+
{
27+
return;
28+
}
29+
2430
// Handle multi-language for output
2531
var agentService = _services.GetRequiredService<IAgentService>();
2632
var router = await agentService.LoadAgent(AIAssistant);

src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using BotSharp.Abstraction.Infrastructures.Enums;
12
using BotSharp.Abstraction.MLTasks;
23
using BotSharp.Abstraction.Options;
34
using BotSharp.Abstraction.Templating;
5+
using BotSharp.Abstraction.Translation.Models;
46
using System.Collections;
57
using System.Reflection;
68

@@ -57,7 +59,13 @@ public async Task<T> Translate<T>(Agent router, string messageId, T data, string
5759

5860
try
5961
{
60-
var translatedTexts = translatedStringList.JsonArrayContent<string>();
62+
// Override language if it's Unknown, it's used to output the corresponding language.
63+
var states = _services.GetRequiredService<IConversationStateService>();
64+
var inputLanguage = string.IsNullOrEmpty(translatedStringList.InputLanguage) ? LanguageType.ENGLISH : translatedStringList.InputLanguage;
65+
var languageState = states.GetState("language", inputLanguage);
66+
states.SetState("language", languageState, activeRounds: 1);
67+
68+
var translatedTexts = translatedStringList.Texts;
6169
var map = new Dictionary<string, string>();
6270

6371
for (var i = 0; i < texts.Length; i++)
@@ -283,7 +291,7 @@ private T Assign<T>(T data, Dictionary<string, string> map) where T : class
283291
/// <param name="list"></param>
284292
/// <param name="language"></param>
285293
/// <returns></returns>
286-
private async Task<string> InnerTranslate(string texts, string language, string template)
294+
private async Task<TranslationOutput> InnerTranslate(string texts, string language, string template)
287295
{
288296
var translator = new Agent
289297
{
@@ -308,7 +316,7 @@ private async Task<string> InnerTranslate(string texts, string language, string
308316
}
309317
};
310318
var response = await _completion.GetChatCompletions(translator, translationDialogs);
311-
return response.Content;
319+
return response.Content.JsonContent<TranslationOutput>();
312320
}
313321

314322
#region Type methods
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{ text_list }}
22

33
=====
4-
Translate the above sentences in the list into {{ language }}, output the translated text in JSON array [""].
4+
Translate the above sentences in the list into {{ language }}.
5+
Output the translated text in JSON {"input_lang":"", "output_lang":"{{ language }}", "texts":[""]}, input_lang is based on the original sentences.

0 commit comments

Comments
 (0)