Skip to content

Commit b5625c2

Browse files
committed
fix: Incorrect LLM contexts
1 parent f398057 commit b5625c2

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

src/Translator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public static async Task DisplayLoop()
241241

242242
if (Setting.ContextAware && !TranslateAPI.IsLLMBased)
243243
{
244-
translatedText = await TranslateAPI.TranslateFunction($"{Caption.ContextPreviousCaption} 🔤{text}🔤", token);
244+
translatedText = await TranslateAPI.TranslateFunction($"{Caption.AwareContextsCaption} 🔤 {text} 🔤", token);
245245
translatedText = RegexPatterns.TargetSentence().Match(translatedText).Groups[1].Value;
246246
}
247247
else

src/apis/TranslateAPI.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public static async Task<string> OpenAI(string text, CancellationToken token = d
6161
new BaseLLMConfig.Message { role = "system", content = string.Format(Prompt, language) },
6262
new BaseLLMConfig.Message { role = "user", content = $"🔤 {text} 🔤" }
6363
};
64+
6465
if (Translator.Setting.ContextAware)
6566
{
66-
foreach (var entry in Translator.Caption.DisplayLogCards)
67+
foreach (var entry in Translator.Caption.AwareContexts)
6768
{
6869
string translatedText = entry.TranslatedText;
6970
if (translatedText.Contains("[ERROR]") || translatedText.Contains("[WARNING]"))
@@ -139,9 +140,10 @@ public static async Task<string> Ollama(string text, CancellationToken token = d
139140
new BaseLLMConfig.Message { role = "system", content = string.Format(Prompt, language) },
140141
new BaseLLMConfig.Message { role = "user", content = $"🔤 {text} 🔤" }
141142
};
143+
142144
if (Translator.Setting.ContextAware)
143145
{
144-
foreach (var entry in Translator.Caption.DisplayLogCards)
146+
foreach (var entry in Translator.Caption.AwareContexts)
145147
{
146148
string translatedText = entry.TranslatedText;
147149
if (translatedText.Contains("[ERROR]") || translatedText.Contains("[WARNING]"))
@@ -201,9 +203,10 @@ public static async Task<string> OpenRouter(string text, CancellationToken token
201203
new BaseLLMConfig.Message { role = "system", content = string.Format(Prompt, language) },
202204
new BaseLLMConfig.Message { role = "user", content = $"🔤 {text} 🔤" }
203205
};
206+
204207
if (Translator.Setting.ContextAware)
205208
{
206-
foreach (var entry in Translator.Caption.DisplayLogCards)
209+
foreach (var entry in Translator.Caption.AwareContexts)
207210
{
208211
string translatedText = entry.TranslatedText;
209212
if (translatedText.Contains("[ERROR]") || translatedText.Contains("[WARNING]"))

src/models/Caption.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public class Caption : INotifyPropertyChanged
2323
public string TranslatedCaption { get; set; } = string.Empty;
2424

2525
public Queue<TranslationHistoryEntry> Contexts { get; } = new(MAX_CONTEXTS);
26-
public string ContextPreviousCaption => GetPreviousCaption(
26+
27+
public IEnumerable<TranslationHistoryEntry> AwareContexts => Contexts.Reverse().Take(
28+
Math.Min(Translator.Setting.NumContexts, Contexts.Count)).Reverse();
29+
public string AwareContextsCaption => GetPreviousCaption(
2730
Math.Min(Translator.Setting.NumContexts, Contexts.Count));
2831

2932
public IEnumerable<TranslationHistoryEntry> DisplayLogCards => Contexts.Reverse().Take(

src/utils/RegexPatterns.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static partial class RegexPatterns
3838
[GeneratedRegex(@"[^0-9.]")]
3939
public static partial Regex VersionNumber();
4040

41-
[GeneratedRegex(@"🔤(.*)🔤")]
41+
[GeneratedRegex(@"🔤\s*(.*)\s*🔤")]
4242
public static partial Regex TargetSentence();
4343
}
4444
}

0 commit comments

Comments
 (0)