Skip to content

Commit

Permalink
Merge pull request #22 from Tabing010102/master
Browse files Browse the repository at this point in the history
更新GalTransl prompt、修复Sakura 0.8/0.9
  • Loading branch information
fkiliver authored Oct 26, 2024
2 parents e458bb8 + 857ebad commit 1fe2c77
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 98 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- [ ] 添加退化检测(搁置,较新的模型基本不需要)
Expand Down Expand Up @@ -60,7 +60,7 @@ ModelName=Sakura
ModelVersion=1.0
MaxConcurrency=2
UseDict=True
DictMode=Full
DictMode=Partial
Dict={"アイリス":["艾莉斯","女"]}
```

Expand All @@ -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 |
Expand All @@ -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`

### 字典
#### 字典配置项
Expand Down
1 change: 1 addition & 0 deletions SakuraTranslator/SakuraTranslate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="SakuraUtil.cs" />
<Compile Include="TranslationModel.cs" />
<Compile Include="SakuraTranslateEndpoint.cs" />
</ItemGroup>
Expand Down
148 changes: 55 additions & 93 deletions SakuraTranslator/SakuraTranslateEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -42,12 +42,12 @@ public partial class SakuraTranslateEndpoint : ITranslateEndpoint

public void Initialize(IInitializationContext context)
{
_endpoint = context.GetOrCreateSetting<string>("Sakura", "Endpoint", "http://127.0.0.1:5000/v1/chat/completions");
_endpoint = context.GetOrCreateSetting<string>("Sakura", "Endpoint", "http://127.0.0.1:8080/v1/chat/completions");
_modelName = context.GetOrCreateSetting<string>("Sakura", "ModelName", "Sakura");
_modelVersion = context.GetOrCreateSetting<string>("Sakura", "ModelVersion", "1.0");
_modelType = GetTranslationModel(_modelName, _modelVersion);
if (!int.TryParse(context.GetOrCreateSetting<string>("Sakura", "MaxConcurrency", "1"), out _maxConcurrency))
{
{
_maxConcurrency = 1;
}
if (_maxConcurrency > ServicePointManager.DefaultConnectionLimit)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<PromptMessage>
var messages = new List<PromptMessage>
{
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<PromptMessage>
{
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\": " +
Expand All @@ -461,7 +436,6 @@ private string MakeSakuraPromptV1_0(string line)

private string MakeGalTranslPromptV2_6(string line)
{
string messagesStr = string.Empty;
var messages = new List<PromptMessage>
{
new PromptMessage
Expand Down Expand Up @@ -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 $"{{" +
Expand Down
19 changes: 19 additions & 0 deletions SakuraTranslator/SakuraUtil.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
5 changes: 5 additions & 0 deletions SakuraTranslator/TranslationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 1fe2c77

Please sign in to comment.