Skip to content

Commit 3b89064

Browse files
authored
Merge pull request #462 from hchen2020/master
Improve translation.
2 parents 186cdad + a538f9d commit 3b89064

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace BotSharp.Abstraction.Translation.Models;
2+
3+
public class TranslationInput
4+
{
5+
[JsonPropertyName("id")]
6+
public int Id { get; set; } = -1;
7+
8+
[JsonPropertyName("text")]
9+
public string Text { get; set; } = null!;
10+
}

src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using BotSharp.Abstraction.Repositories;
2-
using BotSharp.Abstraction.Routing.Models;
31
using BotSharp.Abstraction.Templating;
42
using System.Reflection;
53

src/Infrastructure/BotSharp.Core/Templating/TemplateRender.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using BotSharp.Abstraction.Models;
44
using BotSharp.Abstraction.Routing.Models;
55
using BotSharp.Abstraction.Templating;
6+
using BotSharp.Abstraction.Translation.Models;
67
using Fluid;
78

89
namespace BotSharp.Core.Templating;
@@ -30,6 +31,7 @@ public TemplateRender(IServiceProvider services, ILogger<TemplateRender> logger)
3031
_options.MemberAccessStrategy.Register<FunctionDef>();
3132
_options.MemberAccessStrategy.Register<FunctionParametersDef>();
3233
_options.MemberAccessStrategy.Register<UserIdentity>();
34+
_options.MemberAccessStrategy.Register<TranslationInput>();
3335
}
3436

3537
public string Render(string template, Dictionary<string, object> dict)

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ public async Task<T> Translate<T>(Agent router, string messageId, T data, string
5757

5858
var keys = unique.ToArray();
5959
var texts = unique.ToArray()
60-
.Select((text, i) => $"{i + 1}. \"{text}\"")
61-
.ToList();
60+
.Select((text, i) => new TranslationInput
61+
{
62+
Id = i + 1,
63+
Text = text
64+
}).ToList();
6265
var translatedStringList = await InnerTranslate(texts, language, template);
6366

6467
try
@@ -297,15 +300,21 @@ private T Assign<T>(T data, Dictionary<string, string> map) where T : class
297300
/// <param name="list"></param>
298301
/// <param name="language"></param>
299302
/// <returns></returns>
300-
private async Task<TranslationOutput> InnerTranslate(List<string> texts, string language, string template)
303+
private async Task<TranslationOutput> InnerTranslate(List<TranslationInput> texts, string language, string template)
301304
{
305+
var jsonString = JsonSerializer.Serialize(texts, new JsonSerializerOptions
306+
{
307+
WriteIndented = true,
308+
}) ;
302309
var translator = new Agent
303310
{
304311
Id = Guid.Empty.ToString(),
305312
Name = "Translator",
313+
Instruction = "You are a translation expert.",
306314
TemplateDict = new Dictionary<string, object>
307315
{
308-
{ "text_list", texts },
316+
{ "text_list", jsonString },
317+
{ "text_list_size", texts.Count },
309318
{ StateConst.LANGUAGE, language }
310319
}
311320
};
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
{% for text in text_list %}
2-
{{ text }}
3-
{% endfor %}
1+
{{ text_list }}
2+
43
=====
54
Translate the above sentences into {{ language }}.
65
Output the translated text in JSON {"input_lang":"original text language", "output_lang":"{{ language }}", "texts":[""]}.
7-
Do not include the serial number before each sentence.
8-
Do not include double quotes outside the sentence.
9-
The number of output sentences must be {{ text_list | size }}.

0 commit comments

Comments
 (0)