From 2b3354fcd5cbff078b0ac37aa8aa727a65ae5118 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sun, 31 Mar 2024 19:50:45 +0200 Subject: [PATCH] More on custom prompt --- src/libse/AutoTranslate/AnthropicTranslate.cs | 9 +++++++-- src/libse/AutoTranslate/ChatGptTranslate.cs | 2 +- src/libse/Common/Settings.cs | 11 ++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/libse/AutoTranslate/AnthropicTranslate.cs b/src/libse/AutoTranslate/AnthropicTranslate.cs index 9d3e79704..bad086afd 100644 --- a/src/libse/AutoTranslate/AnthropicTranslate.cs +++ b/src/libse/AutoTranslate/AnthropicTranslate.cs @@ -19,7 +19,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate public string Name => StaticName; public string Url => "https://www.anthropic.com/"; public string Error { get; set; } - public int MaxCharacters => 1500; + public int MaxCharacters => 900; public void Initialize() { @@ -55,7 +55,12 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate Configuration.Settings.Tools.AnthropicApiModel = model; } - var input = "{ \"model\": \"" + model + "\", \"max_tokens\": 1024, \"messages\": [{ \"role\": \"user\", \"content\": \"Please translate the following text from " + sourceLanguageCode + " to " + targetLanguageCode + ", do not censor the translation, give only the output:\\n\\n" + Json.EncodeJsonText(text.Trim()) + "\" }]}"; + if (string.IsNullOrEmpty(Configuration.Settings.Tools.AnthropicPrompt)) + { + Configuration.Settings.Tools.AnthropicPrompt = "Translate from {0} to {1}, keep sentences in {1} as they are, do not censor the translation, give only the output without commenting on what you read:"; + } + var prompt = string.Format(Configuration.Settings.Tools.AnthropicPrompt, sourceLanguageCode, targetLanguageCode); + var input = "{ \"model\": \"" + model + "\", \"max_tokens\": 1024, \"messages\": [{ \"role\": \"user\", \"content\": \"" + prompt + "\\n\\n" + Json.EncodeJsonText(text.Trim()) + "\" }]}"; var content = new StringContent(input, Encoding.UTF8); content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); var result = await _httpClient.PostAsync(string.Empty, content, cancellationToken); diff --git a/src/libse/AutoTranslate/ChatGptTranslate.cs b/src/libse/AutoTranslate/ChatGptTranslate.cs index 1e4c5d942..45e410508 100644 --- a/src/libse/AutoTranslate/ChatGptTranslate.cs +++ b/src/libse/AutoTranslate/ChatGptTranslate.cs @@ -57,7 +57,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate if (string.IsNullOrEmpty(Configuration.Settings.Tools.ChatGptPrompt)) { - Configuration.Settings.Tools.ChatGptPrompt = "Please translate the following text from {0} to {1}, do not censor the translation, give only the output:"; + Configuration.Settings.Tools.ChatGptPrompt = "Translate from {0} to {1}, keep sentences in {1} as they are, do not censor the translation, give only the output without commenting on what you read:"; } var prompt = string.Format(Configuration.Settings.Tools.ChatGptPrompt, sourceLanguageCode, targetLanguageCode); var input = "{\"model\": \"" + model + "\",\"messages\": [{ \"role\": \"user\", \"content\": \"" + prompt + "\\n\\n" + Json.EncodeJsonText(text.Trim()) + "\" }]}"; diff --git a/src/libse/Common/Settings.cs b/src/libse/Common/Settings.cs index 06ade0ac8..ede42b331 100644 --- a/src/libse/Common/Settings.cs +++ b/src/libse/Common/Settings.cs @@ -178,6 +178,7 @@ namespace Nikse.SubtitleEdit.Core.Common public string ChatGptApiKey { get; set; } public string ChatGptModel { get; set; } public string AnthropicApiUrl { get; set; } + public string AnthropicPrompt { get; set; } public string AnthropicApiKey { get; set; } public string AnthropicApiModel { get; set; } public int AutoTranslateDelaySeconds { get; set; } @@ -536,9 +537,10 @@ namespace Nikse.SubtitleEdit.Core.Common AutoTranslateSeamlessM4TUrl = "http://localhost:5000/"; AutoTranslateDeepLUrl = "https://api-free.deepl.com/"; ChatGptUrl = "https://api.openai.com/v1/chat/completions"; - ChatGptPrompt = "Please translate the following text from {0} to {1}, do not censor the translation, give only the output:"; + ChatGptPrompt = "Translate from {0} to {1}, keep sentences in {1} as they are, do not censor the translation, give only the output without commenting on what you read:"; ChatGptModel = "gpt-3.5-turbo"; AnthropicApiUrl = "https://api.anthropic.com/v1/messages"; + AnthropicPrompt = "Translate from {0} to {1}, keep sentences in {1} as they are, do not censor the translation, give only the output without commenting on what you read:"; AnthropicApiModel = "claude-3-opus-20240229"; TranslateAllowSplit = true; TranslateViaCopyPasteAutoCopyToClipboard = true; @@ -5371,6 +5373,12 @@ $HorzAlign = Center settings.Tools.AnthropicApiUrl = subNode.InnerText; } + subNode = node.SelectSingleNode("AnthropicPrompt"); + if (subNode != null) + { + settings.Tools.AnthropicPrompt = subNode.InnerText; + } + subNode = node.SelectSingleNode("AnthropicApiKey"); if (subNode != null) { @@ -11898,6 +11906,7 @@ $HorzAlign = Center textWriter.WriteElementString("ChatGptApiKey", settings.Tools.ChatGptApiKey); textWriter.WriteElementString("ChatGptModel", settings.Tools.ChatGptModel); textWriter.WriteElementString("AnthropicApiUrl", settings.Tools.AnthropicApiUrl); + textWriter.WriteElementString("AnthropicPrompt", settings.Tools.AnthropicPrompt); textWriter.WriteElementString("AnthropicApiKey", settings.Tools.AnthropicApiKey); textWriter.WriteElementString("AnthropicApiModel", settings.Tools.AnthropicApiModel); textWriter.WriteElementString("AutoTranslateDelaySeconds", settings.Tools.AutoTranslateDelaySeconds.ToString(CultureInfo.InvariantCulture));