Skip to content

Improve translation. #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace BotSharp.Abstraction.Translation.Models;

public class TranslationInput
{
[JsonPropertyName("id")]
public int Id { get; set; } = -1;

[JsonPropertyName("text")]
public string Text { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Templating;
using System.Reflection;

Expand Down
2 changes: 2 additions & 0 deletions src/Infrastructure/BotSharp.Core/Templating/TemplateRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Templating;
using BotSharp.Abstraction.Translation.Models;
using Fluid;

namespace BotSharp.Core.Templating;
Expand Down Expand Up @@ -30,6 +31,7 @@ public TemplateRender(IServiceProvider services, ILogger<TemplateRender> logger)
_options.MemberAccessStrategy.Register<FunctionDef>();
_options.MemberAccessStrategy.Register<FunctionParametersDef>();
_options.MemberAccessStrategy.Register<UserIdentity>();
_options.MemberAccessStrategy.Register<TranslationInput>();
}

public string Render(string template, Dictionary<string, object> dict)
Expand Down
17 changes: 13 additions & 4 deletions src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ public async Task<T> Translate<T>(Agent router, string messageId, T data, string

var keys = unique.ToArray();
var texts = unique.ToArray()
.Select((text, i) => $"{i + 1}. \"{text}\"")
.ToList();
.Select((text, i) => new TranslationInput
{
Id = i + 1,
Text = text
}).ToList();
var translatedStringList = await InnerTranslate(texts, language, template);

try
Expand Down Expand Up @@ -297,15 +300,21 @@ private T Assign<T>(T data, Dictionary<string, string> map) where T : class
/// <param name="list"></param>
/// <param name="language"></param>
/// <returns></returns>
private async Task<TranslationOutput> InnerTranslate(List<string> texts, string language, string template)
private async Task<TranslationOutput> InnerTranslate(List<TranslationInput> texts, string language, string template)
{
var jsonString = JsonSerializer.Serialize(texts, new JsonSerializerOptions
{
WriteIndented = true,
}) ;
var translator = new Agent
{
Id = Guid.Empty.ToString(),
Name = "Translator",
Instruction = "You are a translation expert.",
TemplateDict = new Dictionary<string, object>
{
{ "text_list", texts },
{ "text_list", jsonString },
{ "text_list_size", texts.Count },
{ StateConst.LANGUAGE, language }
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{% for text in text_list %}
{{ text }}
{% endfor %}
{{ text_list }}

=====
Translate the above sentences into {{ language }}.
Output the translated text in JSON {"input_lang":"original text language", "output_lang":"{{ language }}", "texts":[""]}.
Do not include the serial number before each sentence.
Do not include double quotes outside the sentence.
The number of output sentences must be {{ text_list | size }}.