Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom prompt to open AI translation #17

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => ''
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prompt_extension perhaps? custom_prompt I would assume it replaced the entire prompt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I think this is better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe additional_prompt?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely better could also consider prompt_context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added now, you can think about better name for this

],
'deepl' => [
'driver' => Bottelet\TranslationChecker\Translator\DeeplTranslator::class,
Expand Down
21 changes: 17 additions & 4 deletions src/Translator/OpenAiTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= "\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.";

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -114,4 +122,9 @@ public function isConfigured(): bool

return $openAiConfig['api_key'] && $openAiConfig['model'];
}

private function CustomPrompt(): string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method should be in camelcase customPrompt() 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed : ) @negoziator

{
return "4. " . config('translator.translators.openai.custom_prompt') . "\n";
}
}