From 243006682ecd35064879f6ffd412621609519935 Mon Sep 17 00:00:00 2001 From: Watheq Alshowaiter Date: Sun, 3 Nov 2024 01:05:44 +0300 Subject: [PATCH 1/3] add custom prompt to open AI translaiton --- config/translator.php | 8 ++++++++ src/Translator/OpenAiTranslator.php | 21 +++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/config/translator.php b/config/translator.php index 140ffb1..f29e286 100644 --- a/config/translator.php +++ b/config/translator.php @@ -31,6 +31,14 @@ 'model' => env('OPENAI_MODEL', 'gpt-4o'), 'api_key' => env('OPENAI_API_KEY'), 'organization_id' => env('OPENAI_ORGANIZATION'), + + /** + * Custom prompt to enhance translation quality. + * + * Example: + * 'custom_prompt' => 'This application translates medical terms consistently throughout.' + */ + 'custom_prompt' => '' ], 'deepl' => [ 'driver' => Bottelet\TranslationChecker\Translator\DeeplTranslator::class, diff --git a/src/Translator/OpenAiTranslator.php b/src/Translator/OpenAiTranslator.php index f140517..c66ad81 100644 --- a/src/Translator/OpenAiTranslator.php +++ b/src/Translator/OpenAiTranslator.php @@ -17,9 +17,13 @@ public function translate(string $text, string $targetLanguage, string $sourceLa Instructions: 1. Translate the entire string from {$sourceLanguage} to {$targetLanguage}. 2. Words prefixed with a colon (:) are special tokens. Do not translate these tokens, keep them as is. -3. Maintain the original structure and formatting of the input string. +3. Maintain the original structure and formatting of the input string."; -Input format: A single string in {$sourceLanguage}, potentially containing words prefixed with colons. +if (config('translator.translators.openai.custom_prompt')) { + $systemPrompt .= $this->CustomPrompt(); +} + +$systemPrompt .= "Input format: A single string in {$sourceLanguage}, potentially containing words prefixed with colons. Output format: respond with a single string in {$targetLanguage}, potentially containing words prefixed with colons."; @@ -53,9 +57,13 @@ public function translateBatch(array $texts, string $targetLanguage, string $sou Instructions: 1. Translate each string from {$sourceLanguage} to {$targetLanguage}. 2. Words prefixed with a colon (:) are special tokens. Do not translate these tokens, keep them as is. -3. Maintain the original structure and formatting of each input string. +3. Maintain the original structure and formatting of each input string.\n"; + +if (config('translator.translators.openai.custom_prompt')) { + $systemPrompt .= $this->CustomPrompt(); +} -Input format: An array of strings in {$sourceLanguage}, potentially containing words prefixed with colons. +$systemPrompt .="\nInput format: An array of strings in {$sourceLanguage}, potentially containing words prefixed with colons. Output format: Respond with a single JSON object. Each key-value pair in this object should represent one translation: - Key: The original string in {$sourceLanguage} @@ -114,4 +122,9 @@ public function isConfigured(): bool return $openAiConfig['api_key'] && $openAiConfig['model']; } + + private function CustomPrompt(): string + { + return "4. " . config('translator.translators.openai.custom_prompt') . "\n"; + } } From 9ce3ae0ff0e3ff1799adf75067ff1cd92da9fbaf Mon Sep 17 00:00:00 2001 From: Watheq Alshowaiter Date: Sun, 3 Nov 2024 01:08:42 +0300 Subject: [PATCH 2/3] aync "\n" in translate and translateBatch --- src/Translator/OpenAiTranslator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Translator/OpenAiTranslator.php b/src/Translator/OpenAiTranslator.php index c66ad81..7a8ff50 100644 --- a/src/Translator/OpenAiTranslator.php +++ b/src/Translator/OpenAiTranslator.php @@ -23,7 +23,7 @@ public function translate(string $text, string $targetLanguage, string $sourceLa $systemPrompt .= $this->CustomPrompt(); } -$systemPrompt .= "Input format: A single string in {$sourceLanguage}, potentially containing words prefixed with colons. +$systemPrompt .= "\nInput format: A single string in {$sourceLanguage}, potentially containing words prefixed with colons. Output format: respond with a single string in {$targetLanguage}, potentially containing words prefixed with colons."; From 3e99fd64eb52b3c4040b08f5704186f98755f5f0 Mon Sep 17 00:00:00 2001 From: Watheq Alshowaiter Date: Mon, 4 Nov 2024 13:07:10 +0300 Subject: [PATCH 3/3] improve custom prompt and chang name to prompt_extension --- config/translator.php | 6 +++--- docs/translation-services/openai.md | 4 ++++ src/Translator/OpenAiTranslator.php | 12 ++++++------ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/config/translator.php b/config/translator.php index f29e286..fe8a178 100644 --- a/config/translator.php +++ b/config/translator.php @@ -33,12 +33,12 @@ 'organization_id' => env('OPENAI_ORGANIZATION'), /** - * Custom prompt to enhance translation quality. + * Custom added prompt to enhance translation quality. * * Example: - * 'custom_prompt' => 'This application translates medical terms consistently throughout.' + * 'prompt_extension' => 'This application translates medical terms consistently throughout.' */ - 'custom_prompt' => '' + 'prompt_extension' => '' ], 'deepl' => [ 'driver' => Bottelet\TranslationChecker\Translator\DeeplTranslator::class, diff --git a/docs/translation-services/openai.md b/docs/translation-services/openai.md index 23ac42b..bfe5536 100644 --- a/docs/translation-services/openai.md +++ b/docs/translation-services/openai.md @@ -11,4 +11,8 @@ For OpenAI, you need to set the following environment variables: OPENAI_API_KEY=your_api_key OPENAI_API_BASE=your_api_base ``` + +You can added your custom `prompt_extension` in the `translator.php` config files, for more accurate translations. + + For more information, see [OpenAI Setup](https://platform.openai.com/docs/guides/production-best-practices/setting-up-your-organization) diff --git a/src/Translator/OpenAiTranslator.php b/src/Translator/OpenAiTranslator.php index 7a8ff50..7d760ff 100644 --- a/src/Translator/OpenAiTranslator.php +++ b/src/Translator/OpenAiTranslator.php @@ -19,8 +19,8 @@ public function translate(string $text, string $targetLanguage, string $sourceLa 2. Words prefixed with a colon (:) are special tokens. Do not translate these tokens, keep them as is. 3. Maintain the original structure and formatting of the input string."; -if (config('translator.translators.openai.custom_prompt')) { - $systemPrompt .= $this->CustomPrompt(); +if (config('translator.translators.openai.prompt_extension')) { + $systemPrompt .= $this->customPrompt(); } $systemPrompt .= "\nInput format: A single string in {$sourceLanguage}, potentially containing words prefixed with colons. @@ -59,8 +59,8 @@ public function translateBatch(array $texts, string $targetLanguage, string $sou 2. Words prefixed with a colon (:) are special tokens. Do not translate these tokens, keep them as is. 3. Maintain the original structure and formatting of each input string.\n"; -if (config('translator.translators.openai.custom_prompt')) { - $systemPrompt .= $this->CustomPrompt(); +if (config('translator.translators.openai.prompt_extension')) { + $systemPrompt .= $this->customPrompt(); } $systemPrompt .="\nInput format: An array of strings in {$sourceLanguage}, potentially containing words prefixed with colons. @@ -123,8 +123,8 @@ public function isConfigured(): bool return $openAiConfig['api_key'] && $openAiConfig['model']; } - private function CustomPrompt(): string + private function customPrompt(): string { - return "4. " . config('translator.translators.openai.custom_prompt') . "\n"; + return "4. " . config('translator.translators.openai.prompt_extension') . "\n"; } }