Skip to content

Commit 793bba8

Browse files
feat: Add support for custom_instructions parameter in text translation
1 parent 68d192d commit 793bba8

4 files changed

Lines changed: 35 additions & 0 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ using the following keys:
136136
translated itself. Characters in the `context` parameter are not counted toward billing.
137137
See the [API documentation][api-docs-context-param] for more information and
138138
example usage.
139+
- `custom_instructions`: an array of instructions to customize the translation behavior.
140+
Up to 10 custom instructions can be specified, each with a maximum of 300 characters.
141+
Important: The target language must be `de`, `en`, `es`, `fr`, `it`, `ja`, `ko`, `zh`
142+
or any variants of these languages.
143+
Note: Any request with the `custom_instructions` parameter enabled will use the
144+
`quality_optimized` model type as the default. Requests combining
145+
`custom_instructions` and `model_type: latency_optimized` will be rejected.
139146
- `glossary`: glossary ID of glossary to use for translation.
140147
- `style_id`: specifies a style rule to use with translation, either as a string
141148
containing the ID of the style rule, or a `StyleRuleInfo` object.

src/TranslateTextOptions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,12 @@ class TranslateTextOptions
9292
* @see \DeepL\DeepLClient::getAllStyleRules()
9393
*/
9494
public const STYLE_ID = 'style_id';
95+
96+
/** Array of custom instructions to customize text translation behavior. Maximum 10 elements, each max
97+
* 300 characters. Only supported for target languages: `de`, `en`, `es`, `fr`, `it`, `ja`, `ko`, `zh`
98+
* and their variants. Note: using the `custom_instructions` parameter will use the `quality_optimized`
99+
* model type as the default. Requests combining `custom_instructions` and the `latency_optimized`
100+
* model type will be rejected.
101+
*/
102+
public const CUSTOM_INSTRUCTIONS = 'custom_instructions';
95103
}

src/Translator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,9 @@ private function validateAndAppendTextOptions(array &$params, ?array $options):
703703
throw new DeepLException('style_id must be a string or StyleRuleInfo object');
704704
}
705705
}
706+
if (isset($options[TranslateTextOptions::CUSTOM_INSTRUCTIONS])) {
707+
$params[TranslateTextOptions::CUSTOM_INSTRUCTIONS] = $options[TranslateTextOptions::CUSTOM_INSTRUCTIONS];
708+
}
706709
$this->applyExtraBodyParameters(
707710
$params,
708711
$options[TranslateTextOptions::EXTRA_BODY_PARAMETERS] ?? null

tests/TranslateTextTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,21 @@ public function testExtraBodyParams(?ClientInterface $httpClient)
415415
$this->assertEquals('en', $result->detectedSourceLang);
416416
$this->assertEquals(mb_strlen(DeepLTestBase::EXAMPLE_TEXT['en']), $result->billedCharacters);
417417
}
418+
419+
/**
420+
* @dataProvider provideHttpClient
421+
*/
422+
public function testCustomInstructions(?ClientInterface $httpClient)
423+
{
424+
$this->needsRealServer();
425+
$translator = $this->makeTranslator([TranslatorOptions::HTTP_CLIENT => $httpClient]);
426+
$input = "I am testing if custom instructions are working correctly.";
427+
$customInstructions = ['Use informal language', 'Be concise'];
428+
$resultWithCustomInstructions = $translator->translateText($input, null, 'de', [
429+
TranslateTextOptions::CUSTOM_INSTRUCTIONS => $customInstructions
430+
]);
431+
432+
$resultWithoutCustomInstructions = $translator->translateText($input, null, 'de');
433+
$this->assertNotEquals($resultWithoutCustomInstructions->text, $resultWithCustomInstructions->text);
434+
}
418435
}

0 commit comments

Comments
 (0)