Skip to content

Commit

Permalink
fix: issues with plugin initialization when the ChatGPT API key is em…
Browse files Browse the repository at this point in the history
…pty, issues with copy/paste ChatGPT API Key, removing the 256 character limit for translations
  • Loading branch information
chnrqpd committed Aug 14, 2024
1 parent d156de9 commit 742528d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
45 changes: 25 additions & 20 deletions ChatGPTTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,27 @@ public class ChatGPTTranslator : ITranslator

public ChatGPTTranslator(IPluginLog pluginLog, string apiKey, string model = "gpt-4o-mini")
{
this.chatClient = new ChatClient(model, apiKey);
this.pluginLog = pluginLog;
this.model = model;
this.temperature = 0.1f;

if (string.IsNullOrWhiteSpace(apiKey))
{
this.pluginLog.Warning("API Key is empty or invalid. ChatGPT transaltion will not be available.");
this.chatClient = null;
}
else
{
try
{
this.chatClient = new ChatClient(model, apiKey);
}
catch (Exception ex)
{
this.pluginLog.Error($"Failed to initialize GPT ChatClient: {ex.Message}");
this.chatClient = null;
}
}
}

public string Translate(string text, string sourceLanguage, string targetLanguage)
Expand All @@ -35,6 +52,12 @@ public string Translate(string text, string sourceLanguage, string targetLanguag

public async Task<string> TranslateAsync(string text, string sourceLanguage, string targetLanguage)
{

if (this.chatClient == null)
{
return "[ChatGPT translation unavailable. Please check your API key.]";
}

string cacheKey = $"{text}_{sourceLanguage}_{targetLanguage}";
if (this.translationCache.TryGetValue(cacheKey, out string cachedTranslation))
{
Expand All @@ -45,8 +68,6 @@ public async Task<string> TranslateAsync(string text, string sourceLanguage, str
Your translation should sound natural and localized, as if it were originally written in {targetLanguage}.
Consider cultural nuances, idiomatic expressions, and context to ensure the translation resonates with native {targetLanguage} speakers.
Important: The final translation MUST NOT exceed 256 characters. If the initial translation is longer, carefully adapt and shorten it while preserving the core meaning and context.
Text to translate: ""{text}""
Provide only the translated text in your response, without any explanations, additional comments, or quotation marks.";
Expand All @@ -68,27 +89,11 @@ public async Task<string> TranslateAsync(string text, string sourceLanguage, str

translatedText = translatedText.Trim('"');

if (translatedText.Length > 256)
{
prompt = @$"The following translation exceeds 256 characters. Please adapt and shorten it to fit within 256 characters while preserving the core meaning and context:
{translatedText}
Provide only the adapted translation, without any explanations, additional comments, or quotation marks.";

completion = await this.chatClient.CompleteChatAsync(prompt);
translatedText = completion.ToString().Trim().Trim('"');
}

if (!string.IsNullOrEmpty(translatedText) && translatedText.Length <= 350)
if (!string.IsNullOrEmpty(translatedText))
{
this.translationCache[cacheKey] = translatedText;
return translatedText;
}
else
{
return $"[Translation Error: Exceeded character limit ({translatedText.Length} characters)]";
}
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion PluginUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ private void EchoglossianConfigUi()
}

var chatGptApiKey = this.configuration.ChatGptApiKey;
if (ImGui.InputText(Resources.ChatGptApiKey, ref chatGptApiKey, 100))
if (ImGui.InputText(Resources.ChatGptApiKey, ref chatGptApiKey, 300))
{
saveConfig = true;
this.configuration.ChatGptApiKey = chatGptApiKey;
Expand Down

0 comments on commit 742528d

Please sign in to comment.