Skip to content

Commit f00229c

Browse files
feat: Add support for custom_instructions parameter in text translation
1 parent ef09094 commit f00229c

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,16 @@ a `TextTranslationOptions`, with the following setters:
168168
containing the ID of the style rule, or a `StyleRuleInfo` object.
169169
- `setStyleId()` is also available, accepting a string containing the style rule ID.
170170
- `setContext()`: specifies additional context to influence translations, that is not
171-
translated itself. Characters in the `context` parameter are not counted toward billing.
171+
translated itself. Characters in the `context` parameter are not counted toward billing.
172172
See the [API documentation][api-docs-context-param] for more information and
173173
example usage.
174+
- `setCustomInstructions()`: an array of instructions to customize the translation behavior.
175+
Up to 10 custom instructions can be specified, each with a maximum of 300 characters.
176+
Important: The target language must be `de`, `en`, `es`, `fr`, `it`, `ja`, `ko`, `zh`
177+
or any variants of these languages.
178+
Note: Any request with the custom instructions parameter enabled will use the
179+
`quality_optimized` model type as the default. Requests combining custom instructions
180+
and `model_type: latency_optimized` will be rejected.
174181
- `model_type`: specifies the type of translation model to use, options are:
175182
- `'quality_optimized'`: use a translation model that maximizes translation quality,
176183
at the cost of response time. This option may be unavailable for some language pairs.

deepl-java/src/main/java/com/deepl/api/TextTranslationOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class TextTranslationOptions extends BaseRequestOptions {
2626
private Iterable<String> ignoreTags;
2727
private Iterable<String> nonSplittingTags;
2828
private Iterable<String> splittingTags;
29+
private Iterable<String> customInstructions;
2930

3031
/**
3132
* Sets whether translations should lean toward formal or informal language. This option is only
@@ -175,6 +176,15 @@ public TextTranslationOptions setSplittingTags(Iterable<String> splittingTags) {
175176
return this;
176177
}
177178

179+
/**
180+
* Sets custom instructions to influence translations. By default, this value is <code>null</code>
181+
* and no custom instructions are used.
182+
*/
183+
public TextTranslationOptions setCustomInstructions(Iterable<String> customInstructions) {
184+
this.customInstructions = customInstructions;
185+
return this;
186+
}
187+
178188
/** Gets the current formality setting. */
179189
public Formality getFormality() {
180190
return formality;
@@ -234,4 +244,9 @@ public Iterable<String> getNonSplittingTags() {
234244
public Iterable<String> getSplittingTags() {
235245
return splittingTags;
236246
}
247+
248+
/** Gets the current custom instructions list. */
249+
public Iterable<String> getCustomInstructions() {
250+
return customInstructions;
251+
}
237252
}

deepl-java/src/main/java/com/deepl/api/Translator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,11 @@ private static ArrayList<KeyValuePair<String, String>> createHttpParams(
849849
if (options.getStyleId() != null) {
850850
params.add(new KeyValuePair<>("style_id", options.getStyleId()));
851851
}
852+
if (options.getCustomInstructions() != null) {
853+
for (String instruction : options.getCustomInstructions()) {
854+
params.add(new KeyValuePair<>("custom_instructions", instruction));
855+
}
856+
}
852857
addExtraBodyParameters(params, options.getExtraBodyParameters());
853858
}
854859
return params;

deepl-java/src/test/java/com/deepl/api/TranslateTextTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,27 @@ void testExtraBodyParams() throws DeepLException, InterruptedException {
349349
Assertions.assertEquals("en", result.getDetectedSourceLanguage());
350350
Assertions.assertEquals(exampleText.get("en").length(), result.getBilledCharacters());
351351
}
352+
353+
@Test
354+
void testCustomInstructions() throws DeepLException, InterruptedException {
355+
Translator translator = createTranslator();
356+
String text = "Hello world. I am testing if custom instructions are working correctly.";
357+
358+
TextResult resultWithCustomInstructions =
359+
translator.translateText(
360+
text,
361+
null,
362+
"de",
363+
new TextTranslationOptions()
364+
.setCustomInstructions(Arrays.asList("Use informal language", "Be concise")));
365+
366+
TextResult resultWithoutCustomInstructions = translator.translateText(text, null, "de");
367+
368+
Assertions.assertNotNull(resultWithCustomInstructions.getText());
369+
Assertions.assertEquals("en", resultWithCustomInstructions.getDetectedSourceLanguage());
370+
if (!isMockServer) {
371+
Assertions.assertFalse(
372+
resultWithCustomInstructions.getText().equals(resultWithoutCustomInstructions.getText()));
373+
}
374+
}
352375
}

0 commit comments

Comments
 (0)