diff --git a/config/translator.php b/config/translator.php index 140ffb1..fe8a178 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 added prompt to enhance translation quality. + * + * Example: + * 'prompt_extension' => 'This application translates medical terms consistently throughout.' + */ + '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 f140517..7d760ff 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.prompt_extension')) { + $systemPrompt .= $this->customPrompt(); +} + +$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."; @@ -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.prompt_extension')) { + $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.prompt_extension') . "\n"; + } }