diff --git a/README.md b/README.md index 111134c..ee2b56d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ # 介绍 这是一个基于XUnity.AutoTranslator和Sakura模型的Unity游戏本地翻译器,能够提供高质量离线日文翻译 -建议使用[Galtransl-v2.6翻译模型](https://github.com/SakuraLLM/Sakura-13B-Galgame),当前支持版本为Sakura v0.8/v0.9/v0.10/v1.0,GalTrans2.6 +建议使用[Galtransl-v2.6翻译模型](https://huggingface.co/SakuraLLM/GalTransl-7B-v2.6),当前支持版本为Sakura v0.8/v0.9/v0.10/v1.0,GalTrans2.6 ## TODO - [ ] 添加退化检测(搁置,较新的模型基本不需要) @@ -60,7 +60,7 @@ ModelName=Sakura ModelVersion=1.0 MaxConcurrency=2 UseDict=True -DictMode=Full +DictMode=Partial Dict={"アイリス":["艾莉斯","女"]} ``` @@ -69,8 +69,8 @@ Dict={"アイリス":["艾莉斯","女"]} | ModelName | ModelVersion | TranslationModel | |------------|--------------|------------------------| | Sakura | 0.8 | Sakura 0.8 | -| Sakura | 0.9 | Sakura/Sakura32B 0.9* | -| Sakura | 0.10 | Sakura 0.10pre1 | +| Sakura | 0.9 | Sakura 7B/14B/32B 0.9 | +| Sakura | 0.10 | Sakura 0.10 | | Sakura | 1.0 | Sakura 1.0 | | Sakura | * | Sakura 1.0 (默认) | | Sakura32B | 0.10 | Sakura32B 0.10 | @@ -79,7 +79,9 @@ Dict={"アイリス":["艾莉斯","女"]} | GalTransl | * | GalTransl 2.6 (默认) | | * | * | Sakura 1.0 (默认) | -模型相关默认值设置为Sakura 1.0,注意Sakura32B 0.9*需将`ModelName`设置为`Sakura` +模型相关默认值设置为Sakura 1.0,注意Sakura 7B/14B/32B 0.9系列模型需将`ModelName`设置为`Sakura` +其中Sakura 0.8/0.9模型需要将`Endpoint`设置为completion api(例:`http://127.0.0.1:8080/completion`) +Sakura 0.10/1.0和GalTransl模型需要将`Endpoint`设置为chat completions api(例:`http://127.0.0.1:8080/v1/chat/completions`) ### 字典 #### 字典配置项 diff --git a/SakuraTranslator/SakuraTranslate.csproj b/SakuraTranslator/SakuraTranslate.csproj index da680c4..45cb472 100644 --- a/SakuraTranslator/SakuraTranslate.csproj +++ b/SakuraTranslator/SakuraTranslate.csproj @@ -55,6 +55,7 @@ + diff --git a/SakuraTranslator/SakuraTranslateEndpoint.cs b/SakuraTranslator/SakuraTranslateEndpoint.cs index c8a19eb..7ad8f34 100644 --- a/SakuraTranslator/SakuraTranslateEndpoint.cs +++ b/SakuraTranslator/SakuraTranslateEndpoint.cs @@ -12,8 +12,8 @@ using XUnity.AutoTranslator.Plugin.Core.Endpoints; using XUnity.AutoTranslator.Plugin.Core.Utilities; -[assembly: AssemblyVersion("0.3.4")] -[assembly: AssemblyFileVersion("0.3.4")] +[assembly: AssemblyVersion("0.3.5")] +[assembly: AssemblyFileVersion("0.3.5")] namespace SakuraTranslate { @@ -42,12 +42,12 @@ public partial class SakuraTranslateEndpoint : ITranslateEndpoint public void Initialize(IInitializationContext context) { - _endpoint = context.GetOrCreateSetting("Sakura", "Endpoint", "http://127.0.0.1:5000/v1/chat/completions"); + _endpoint = context.GetOrCreateSetting("Sakura", "Endpoint", "http://127.0.0.1:8080/v1/chat/completions"); _modelName = context.GetOrCreateSetting("Sakura", "ModelName", "Sakura"); _modelVersion = context.GetOrCreateSetting("Sakura", "ModelVersion", "1.0"); _modelType = GetTranslationModel(_modelName, _modelVersion); if (!int.TryParse(context.GetOrCreateSetting("Sakura", "MaxConcurrency", "1"), out _maxConcurrency)) - { + { _maxConcurrency = 1; } if (_maxConcurrency > ServicePointManager.DefaultConnectionLimit) @@ -178,22 +178,17 @@ public IEnumerator Translate(ITranslationContext context) //var translatedText = responseText.Substring(startIndex, endIndex - startIndex); JObject jsonResponse = JObject.Parse(responseText); - string translatedText = jsonResponse["choices"]?[0]?["message"]?["content"]?.ToString(); - - - //历史遗留 - //if (translatedLine.EndsWith("<|im_end|>")) - //{ - // translatedLine = translatedLine.Substring(0, translatedLine.Length - "<|im_end|>".Length); - //} - //if (translatedLine.EndsWith("。") && !line.Trim().EndsWith("。")) - //{ - // translatedLine = translatedLine.Substring(0, translatedLine.Length - "。".Length); - //} - //if (translatedLine.EndsWith("。」") && !line.Trim().EndsWith("。」")) - //{ - // translatedLine = translatedLine.Substring(0, translatedLine.Length - "。」".Length) + "」"; - //} + string translatedText; + if (IsOpenAIEndpoint(_modelType)) + { + translatedText = jsonResponse["choices"]?[0]?["message"]?["content"]?.ToString(); + } + else + { + translatedText = jsonResponse["content"]?.ToString(); + } + + translatedText = SakuraUtil.FixTranslationEnd(untranslatedText, translatedText); // 提交翻译文本 context.Complete(translatedText); @@ -372,78 +367,58 @@ private string MakeSakura32bPromptV0_10(string line) private string MakeSakuraPromptV1_0(string line) { - string messagesStr = string.Empty; - if (_useDict == false) - { - messagesStr = SerializePromptMessages(new List + var messages = new List { new PromptMessage { Role = "system", Content = "你是一个轻小说翻译模型,可以流畅通顺地以日本轻小说的风格将日文翻译成简体中文,并联系上下文正确使用人称代词,不擅自添加原文中没有的代词。" - }, - new PromptMessage - { - Role = "user", - Content = $"将下面的日文文本翻译成中文:{line}" } - }); + }; + string dictStr; + if (_useDict == false) + { + dictStr = string.Empty; + } + if (_dictMode == "Full") + { + dictStr = _fullDictStr; } else { - var messages = new List - { - new PromptMessage - { - Role = "system", - Content = "你是一个轻小说翻译模型,可以流畅通顺地以日本轻小说的风格将日文翻译成简体中文,并联系上下文正确使用人称代词,不擅自添加原文中没有的代词。" - } - }; - string dictStr; - if (_useDict == false) - { - dictStr = string.Empty; - } - if (_dictMode == "Full") + var usedDict = _dict.Where(x => line.Contains(x.Key)); + if (usedDict.Count() > 0) { - dictStr = _fullDictStr; + var dictStrings = GetDictStringList(usedDict); + dictStr = string.Join("\n", dictStrings.ToArray()); } else { - var usedDict = _dict.Where(x => line.Contains(x.Key)); - if (usedDict.Count() > 0) - { - var dictStrings = GetDictStringList(usedDict); - dictStr = string.Join("\n", dictStrings.ToArray()); - } - else - { - dictStr = string.Empty; - } + dictStr = string.Empty; } + } - if (_useDict == false) + if (_useDict == false) + { + // 如果术语表为空,直接构建翻译指令 + messages.Add(new PromptMessage { - // 如果术语表为空,直接构建翻译指令 - messages.Add(new PromptMessage - { - Role = "user", - Content = $"将下面的日文文本翻译成中文:{line}" - }); - } - else - + Role = "user", + Content = $"将下面的日文文本翻译成中文:{line}" + }); + } + else + { + messages.Add(new PromptMessage { - messages.Add(new PromptMessage - { - Role = "user", - Content = $"根据以下术语表(可以为空):\n{dictStr}\n" + - $"将下面的日文文本根据对应关系和备注翻译成中文:{line}" - }); - } - - messagesStr = SerializePromptMessages(messages); + Role = "user", + Content = $"根据以下术语表(可以为空):\n{dictStr}\n" + + $"将下面的日文文本根据对应关系和备注翻译成中文:{line}" + }); } + + var messagesStr = SerializePromptMessages(messages); + return $"{{" + $"\"model\": \"sukinishiro\"," + $"\"messages\": " + @@ -461,7 +436,6 @@ private string MakeSakuraPromptV1_0(string line) private string MakeGalTranslPromptV2_6(string line) { - string messagesStr = string.Empty; var messages = new List { new PromptMessage @@ -493,26 +467,14 @@ private string MakeGalTranslPromptV2_6(string line) } } - if (_useDict == false) - { - // 如果术语表为空,直接构建翻译指令 - messages.Add(new PromptMessage - { - Role = "user", - Content = $"将下面的日文文本翻译成中文:{line}" - }); - } - else + messages.Add(new PromptMessage { - messages.Add(new PromptMessage - { - Role = "user", - Content = $"参考以下术语表(可为空,格式为src->dst #备注):\n{dictStr}\n" + - $"根据上述术语表的对应关系和备注,结合历史剧情和上下文,以流畅的风格将下面的文本从日文翻译成简体中文:{line}" - }); - } + Role = "user", + Content = $"参考以下术语表(可为空,格式为src->dst #备注):{(string.IsNullOrEmpty(dictStr) ? string.Empty : "\n" + dictStr)}\n\n" + + $"根据上述术语表的对应关系和备注,结合历史剧情和上下文,以流畅的风格将下面的文本从日文翻译成简体中文:{line}" + }); - messagesStr = SerializePromptMessages(messages); + var messagesStr = SerializePromptMessages(messages); //Console.WriteLine($"提交的prompt: {messagesStr}"); return $"{{" + diff --git a/SakuraTranslator/SakuraUtil.cs b/SakuraTranslator/SakuraUtil.cs new file mode 100644 index 0000000..beebd2a --- /dev/null +++ b/SakuraTranslator/SakuraUtil.cs @@ -0,0 +1,19 @@ +namespace SakuraTranslate +{ + public static class SakuraUtil + { + public static string FixTranslationEnd(string original, string translation) + { + if (translation.EndsWith("。") && !original.EndsWith("。")) + { + translation = translation.Substring(0, translation.Length - "。".Length); + } + if (translation.EndsWith("。」") && !original.EndsWith("。」")) + { + translation = translation.Substring(0, translation.Length - "。」".Length) + "」"; + } + + return translation; + } + } +} diff --git a/SakuraTranslator/TranslationModel.cs b/SakuraTranslator/TranslationModel.cs index 2e22cc5..bcb1db1 100644 --- a/SakuraTranslator/TranslationModel.cs +++ b/SakuraTranslator/TranslationModel.cs @@ -47,5 +47,10 @@ private static TranslationModel GetTranslationModel(string modelName, string mod return TranslationModel.SakuraV1_0; } } + + private static bool IsOpenAIEndpoint(TranslationModel model) + { + return !(model == TranslationModel.SakuraV0_8 || model == TranslationModel.SakuraV0_9); + } } }