Improve translation.
This commit is contained in:
parent
e98f6c13de
commit
efdf5a30da
|
|
@ -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!;
|
||||
}
|
||||
|
|
@ -1,5 +1,3 @@
|
|||
using BotSharp.Abstraction.Repositories;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using BotSharp.Abstraction.Templating;
|
||||
using System.Reflection;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using BotSharp.Abstraction.Functions.Models;
|
|||
using BotSharp.Abstraction.Models;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using BotSharp.Abstraction.Templating;
|
||||
using BotSharp.Abstraction.Translation.Models;
|
||||
using Fluid;
|
||||
|
||||
namespace BotSharp.Core.Templating;
|
||||
|
|
@ -30,6 +31,7 @@ public class TemplateRender : ITemplateRender
|
|||
_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)
|
||||
|
|
|
|||
|
|
@ -57,8 +57,11 @@ public class TranslationService : ITranslationService
|
|||
|
||||
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
|
||||
|
|
@ -297,15 +300,21 @@ public class TranslationService : ITranslationService
|
|||
/// <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 }
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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 }}.
|
||||
|
|
|
|||
Loading…
Reference in a new issue