Skip to content

Commit 882754d

Browse files
feat: Add support for custom_instructions parameter in text translation
1 parent 0d0572f commit 882754d

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ arguments are:
161161
`get_glossary()`.
162162
- `context`: specifies additional context to influence translations, that is not
163163
translated itself. Characters in the `context` parameter are not counted toward billing.
164-
See the [API documentation][api-docs-context-param] for more information and
164+
See the [API documentation][api-docs-context-param] for more information and
165165
example usage.
166166
- `model_type`: specifies the type of translation model to use, options are:
167167
- `'quality_optimized'` (`ModelType.QUALITY_OPTIMIZED`): use a translation
@@ -175,6 +175,14 @@ arguments are:
175175
and `'xml'`.
176176
- `style_rule`: specifies a style rule to use with translation, either as a string
177177
containing the ID of the style rule, or a `StyleRuleInfo` object.
178+
- `custom_instructions`: an array of instructions to customize the text
179+
translation behavior. Up to 10 custom instructions can be specified, each with
180+
a maximum of 300 characters.
181+
Important: The target language must be `de`, `en`, `es`, `fr`, `it`, `ja`,
182+
`ko`, `zh` or any variants of these languages.
183+
Note: Any request with the `custom_instructions` parameter enabled will use
184+
the `quality_optimized` model type as the default. Requests combining
185+
`custom_instructions` and `model_type: latency_optimized` will be rejected.
178186
- `extra_body_parameters`: Dictionary of extra parameters to pass in the body of
179187
the HTTP request. Mostly used by DeepL employees to test functionality, or for
180188
beta programs.

deepl/__main__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ def add_common_arguments(subparser: argparse.ArgumentParser):
385385
type=str,
386386
help="ID of style rule to use for translation",
387387
)
388+
parser_text.add_argument(
389+
"--custom-instructions",
390+
dest="custom_instructions",
391+
action="append",
392+
type=str,
393+
help="custom instructions to guide translation (can be specified "
394+
"multiple times, max 10 instructions, each max 300 characters)",
395+
)
388396
parser_text.add_argument(
389397
"text",
390398
nargs="+",

deepl/translator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ def translate_text(
382382
ignore_tags: Union[str, List[str], None] = None,
383383
model_type: Union[str, ModelType, None] = None,
384384
style_rule: Union[str, StyleRuleInfo, None] = None,
385+
custom_instructions: Optional[List[str]] = None,
385386
extra_body_parameters: Optional[dict] = None,
386387
) -> Union[TextResult, List[TextResult]]:
387388
"""Translate text(s) into the target language.
@@ -427,6 +428,9 @@ def translate_text(
427428
:type ignore_tags: List of XML tags or comma-separated-list of tags.
428429
:param model_type: (Optional) Controls whether the translation engine
429430
should use a potentially slower model to achieve higher quality.
431+
:param custom_instructions: (Optional) List of custom instructions to
432+
guide the translation. Maximum of 10 instructions, each with a
433+
maximum length of 300 characters.
430434
:param extra_body_parameters: (Optional) Additional key/value pairs to
431435
include in the JSON request body sent to the API. If provided,
432436
keys in this dict will be added to the request body. Existing
@@ -490,6 +494,8 @@ def join_tags(tag_argument: Union[str, Iterable[str]]) -> List[str]:
490494
request_data["splitting_tags"] = join_tags(splitting_tags)
491495
if ignore_tags is not None:
492496
request_data["ignore_tags"] = join_tags(ignore_tags)
497+
if custom_instructions is not None:
498+
request_data["custom_instructions"] = custom_instructions
493499

494500
# Do not overwrite keys that were explicitly set by this method.
495501
if extra_body_parameters:

tests/test_translate_text.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,28 @@ def test_extra_body_params(translator):
418418
assert example_text["FR"] == res.text
419419
assert "EN" == res.detected_source_lang
420420
assert res.billed_characters == len(example_text["EN"])
421+
422+
423+
@needs_real_server
424+
def test_custom_instructions(translator):
425+
text = "I am testing if custom instructions are working correctly."
426+
no_custom_instructions_result = translator.translate_text(
427+
text,
428+
target_lang="DE",
429+
)
430+
431+
custom_instructions_result = translator.translate_text(
432+
text,
433+
target_lang="DE",
434+
custom_instructions=["Use informal language", "Be concise"],
435+
)
436+
437+
assert isinstance(custom_instructions_result, deepl.TextResult)
438+
assert custom_instructions_result.text is not None
439+
assert len(custom_instructions_result.text) > 0
440+
assert custom_instructions_result.detected_source_lang == "EN"
441+
442+
# Check that applying custom instructions results in different translations
443+
assert (
444+
custom_instructions_result.text != no_custom_instructions_result.text
445+
)

0 commit comments

Comments
 (0)