Skip to content

Commit 130f82e

Browse files
committed
Simplify the prompt
1 parent 3fe1c10 commit 130f82e

2 files changed

Lines changed: 9 additions & 39 deletions

File tree

src/Services/OpenAITranslator.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class OpenAITranslator implements Translator
2424

2525
public function __construct(
2626
string $apiKey,
27-
private readonly string $model = 'gpt-4o',
27+
private readonly string $model = 'gpt-4',
2828
) {
2929
$this->client = (new Factory())->withApiKey($apiKey)->make();
3030
$this->languageCodeService = new LanguageCodeService();
@@ -82,7 +82,7 @@ public function translate(array $groups, string $targetLanguage, ?string $source
8282
throw new RuntimeException('Invalid JSON in response');
8383
}
8484

85-
// Verify translations are valid
85+
// Verify that translations are different from the original text
8686
foreach ($translations as $key => $translation) {
8787
if (! isset($strings[$key])) {
8888
throw new RuntimeException(sprintf(
@@ -95,6 +95,10 @@ public function translate(array $groups, string $targetLanguage, ?string $source
9595
if (! is_string($translation)) {
9696
throw new RuntimeException("Invalid translation for key '{$key}': expected string, got ".gettype($translation));
9797
}
98+
99+
if (mb_strtolower($translation) === mb_strtolower($strings[$key]['text'])) {
100+
throw new RuntimeException("Translation for '{$key}' is the same as the original text");
101+
}
98102
}
99103

100104
// Verify all strings are translated
@@ -164,12 +168,13 @@ private function getSystemPrompt(): string
164168
Your task is to translate text while preserving meaning and context.
165169
166170
Important rules to follow:
167-
1. Translate the text to the target language, keeping the original text if it's already appropriate in the target language
171+
1. Always translate the text to the target language, never return it unchanged
168172
2. Preserve the meaning and context of each string
169173
3. Use appropriate translations based on context
170174
4. Return ONLY a valid JSON object with translations, nothing else
171175
5. Each key in the JSON must be exactly as provided in the input
172-
6. Your response must be a valid JSON object, starting with { and ending with }
176+
6. Never return the original text unchanged
177+
7. Your response must be a valid JSON object, starting with { and ending with }
173178
174179
Example request:
175180
```

tests/Feature/TranslationTest.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -126,38 +126,3 @@ public function translate(array $groups, string $targetLanguage, ?string $source
126126

127127
expect($translation)->toBe('translated_Welcome');
128128
});
129-
130-
test('it allows translations that are similar to the original text', function () {
131-
$similarTranslator = new class implements Translator {
132-
public function translate(array $groups, string $targetLanguage, ?string $sourceLanguage = null, ?string $globalContext = null): array
133-
{
134-
$result = [];
135-
foreach ($groups as $groupName => $group) {
136-
$translatedStrings = [];
137-
foreach ($group->getStrings() as $key => $string) {
138-
// For some words like "OK" or "Menu", the translation might be the same in many languages
139-
$translatedStrings[$key] = new TranslationString(
140-
text: $string->getText(),
141-
translatedText: $string->getText(), // Same as original
142-
context: $string->getContext(),
143-
);
144-
}
145-
$result[$groupName] = new TranslationGroup($groupName, $translatedStrings);
146-
}
147-
148-
return $result;
149-
}
150-
};
151-
152-
$pollingo = Pollingo::make(translator: $similarTranslator);
153-
$translation = $pollingo
154-
->to('fr')
155-
->group('ui', [
156-
'ok' => 'OK',
157-
'menu' => 'Menu',
158-
])
159-
->translate();
160-
161-
expect($translation['ui']['ok'])->toBe('OK');
162-
expect($translation['ui']['menu'])->toBe('Menu');
163-
});

0 commit comments

Comments
 (0)