Translate
This commit is contained in:
parent
527e689338
commit
730cc8b143
|
|
@ -101,12 +101,12 @@ public class TranslationService : ITranslationService
|
|||
{
|
||||
var translatedStringList = await InnerTranslate(texts, language, template);
|
||||
|
||||
int retry = 0;
|
||||
/*int retry = 0;
|
||||
while (translatedStringList.Texts.Length != texts.Count && retry < 3)
|
||||
{
|
||||
translatedStringList = await InnerTranslate(texts, language, template);
|
||||
retry++;
|
||||
}
|
||||
}*/
|
||||
|
||||
// Override language if it's Unknown, it's used to output the corresponding language.
|
||||
var states = _services.GetRequiredService<IConversationStateService>();
|
||||
|
|
@ -119,7 +119,7 @@ public class TranslationService : ITranslationService
|
|||
var translatedTexts = translatedStringList.Texts;
|
||||
var memoryInputs = new List<TranslationMemoryInput>();
|
||||
|
||||
for (var i = 0; i < texts.Count; i++)
|
||||
for (var i = 0; i < Math.Min(texts.Count, translatedTexts.Length); i++)
|
||||
{
|
||||
map[outOfMemoryList[i].OriginalText] = translatedTexts[i].Text;
|
||||
memoryInputs.Add(new TranslationMemoryInput
|
||||
|
|
@ -375,6 +375,8 @@ public class TranslationService : ITranslationService
|
|||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
var prompt = render.Render(template, translator.TemplateDict);
|
||||
|
||||
_logger.LogInformation($"Translation prompt: {prompt}");
|
||||
|
||||
var translationDialogs = new List<RoleDialogModel>
|
||||
{
|
||||
new RoleDialogModel(AgentRole.User, prompt)
|
||||
|
|
@ -384,6 +386,8 @@ public class TranslationService : ITranslationService
|
|||
}
|
||||
};
|
||||
var response = await _completion.GetChatCompletions(translator, translationDialogs);
|
||||
|
||||
_logger.LogInformation(response.Content);
|
||||
return response.Content.JsonContent<TranslationOutput>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,19 @@
|
|||
{{ text_list }}
|
||||
|
||||
=====
|
||||
{% if language == "Chinese" %}
|
||||
将以上所有句子翻译成中文。
|
||||
|
||||
要求:
|
||||
* 以 JSON 格式输出翻译后的文本 {"input_lang":"原始文本语言", "output_count": {{ text_list_size }}, "output_lang":"{{ language }}", "texts":[{"id": 1, "text":""},{"id": 2, "text":""}]}。
|
||||
* output_count 必须等于输出中texts数组的长度。
|
||||
{% else %}
|
||||
Translate all the above sentences into {{ language }}.
|
||||
Output the translated text in JSON {"input_lang":"original text language", "output_count": {{ text_list_size }}, "output_lang":"{{ language }}", "texts":[{"id": 1, "text":""},{"id": 2, "text":""}]}.
|
||||
The "output_count" must equal the length of the "texts" array in the output.
|
||||
|
||||
Requirements:
|
||||
* Output the translated text in JSON {"input_lang":"original text language", "output_count": {{ text_list_size }}, "output_lang":"{{ language }}", "texts":[{"id": 1, "text":""},{"id": 2, "text":""}]}.
|
||||
* The "output_count" must equal the length of the "texts" array in the output.
|
||||
{% endif %}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -24,10 +24,12 @@ public class TranslationController : ControllerBase
|
|||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.LoadAgent(BuiltInAgentId.AIAssistant);
|
||||
var translator = _services.GetRequiredService<ITranslationService>();
|
||||
var text = await translator.Translate(agent, Guid.NewGuid().ToString(), model.Text, language: model.ToLang);
|
||||
var states = _services.GetRequiredService<IConversationStateService>();
|
||||
states.SetState("max_tokens", "8192");
|
||||
var text = await translator.Translate(agent, Guid.NewGuid().ToString(), model.Text.Split("\r\n"), language: model.ToLang);
|
||||
return new TranslationResponseModel
|
||||
{
|
||||
Text = text
|
||||
Text = string.Join("\r\n", text)
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue