Skip to content

Commit 1a4bd86

Browse files
committed
feat: Add modelType parameter to translateText()
1 parent 65bf56b commit 1a4bd86

File tree

7 files changed

+80
-6
lines changed

7 files changed

+80
-6
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## Unreleased
9+
### Added
10+
* Added `modelType` option to `translateText()` to use models with higher
11+
translation quality (available for some language pairs), or better latency.
12+
Options are `'quality_optimized'`, `'latency_optimized'`, and `'prefer_quality_optimized'`
13+
* Added the `modelTypeUsed` field to `translateText()` response, that
14+
indicates the translation model used when the `modelType` option is
15+
specified.
16+
17+
818
## [1.6.0] - 2024-09-17
919
### Added
1020
* Added `getBilledCharacters()` to text translation response.

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ There are additional optional arguments to control translation, see
9898
`translateText()` returns a `TextResult`, or a List of `TextResult`s
9999
corresponding to your input text(s). `TextResult` has the following accessors:
100100
- `getText()` returns the translated text,
101-
- `getDetectedSourceLanguage()` returns the detected source language code, and
102-
- `getBilledCharacters()` returns the number of characters billed for the text.
101+
- `getDetectedSourceLanguage()` returns the detected source language code,
102+
- `getBilledCharacters()` returns the number of characters billed for the text, and
103+
- `getModelTypeUsed()` returns the model type used for the translation.
103104

104105
```java
105106
class Example { // Continuing class Example from above
@@ -166,6 +167,13 @@ a `TextTranslationOptions`, with the following setters:
166167
translated itself. Characters in the `context` parameter are not counted toward billing.
167168
See the [API documentation][api-docs-context-param] for more information and
168169
example usage.
170+
- `model_type`: specifies the type of translation model to use, options are:
171+
- `'quality_optimized'`: use a translation model that maximizes translation quality,
172+
at the cost of response time. This option may be unavailable for some language pairs.
173+
- `'prefer_quality_optimized'`: use the highest-quality translation model for the given
174+
language pair.
175+
- `'latency_optimized'`: use a translation model that minimizes response time, at the
176+
cost of translation quality.
169177
- `setTagHandling()`: type of tags to parse before translation, options are
170178
`"html"` and `"xml"`.
171179

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
// license that can be found in the LICENSE file.
44
package com.deepl.api;
55

6+
import org.jetbrains.annotations.Nullable;
7+
68
/** The result of a text translation. */
79
public class TextResult {
810
private final String text;
911
private final String detectedSourceLanguage;
1012
private final int billedCharacters;
13+
private final @Nullable String modelTypeUsed;
1114

1215
/** Constructs a new instance. */
13-
public TextResult(String text, String detectedSourceLanguage, int billedCharacters) {
16+
public TextResult(
17+
String text,
18+
String detectedSourceLanguage,
19+
int billedCharacters,
20+
@Nullable String modelTypeUsed) {
1421
this.text = text;
1522
this.detectedSourceLanguage = LanguageCode.standardize(detectedSourceLanguage);
1623
this.billedCharacters = billedCharacters;
24+
this.modelTypeUsed = modelTypeUsed;
1725
}
1826

1927
/** The translated text. */
@@ -30,4 +38,9 @@ public String getDetectedSourceLanguage() {
3038
public int getBilledCharacters() {
3139
return billedCharacters;
3240
}
41+
42+
/** Model type used for the translation of this text. */
43+
public @Nullable String getModelTypeUsed() {
44+
return modelTypeUsed;
45+
}
3346
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class TextTranslationOptions {
2020
private boolean preserveFormatting = false;
2121
private String context;
2222
private String tagHandling;
23+
private String modelType;
2324
private boolean outlineDetection = true;
2425
private Iterable<String> ignoreTags;
2526
private Iterable<String> nonSplittingTags;
@@ -106,6 +107,20 @@ public TextTranslationOptions setTagHandling(String tagHandling) {
106107
return this;
107108
}
108109

110+
/**
111+
* Set the type of model to use for a text translation. Currently supported values: <code>
112+
* "quality_optimized"</code> use a translation model that maximizes translation quality, at the
113+
* cost of response time. This option may be unavailable for some language pairs and the API would
114+
* respond with an error in this case; <code>"prefer_quality_optimized"</code> use the
115+
* highest-quality translation model for the given language pair; <code>"latency_optimized"
116+
* </code> use a translation model that minimizes response time, at the cost of translation
117+
* quality.
118+
*/
119+
public TextTranslationOptions setModelType(String modelType) {
120+
this.modelType = modelType;
121+
return this;
122+
}
123+
109124
/**
110125
* Sets whether outline detection is used; set to <code>false</code> to disable automatic tag
111126
* detection, default is <code>true</code>.
@@ -167,6 +182,11 @@ public String getContext() {
167182
return context;
168183
}
169184

185+
/** Gets the current model type. */
186+
public String getModelType() {
187+
return modelType;
188+
}
189+
170190
/** Gets the current tag handling setting. */
171191
public String getTagHandling() {
172192
return tagHandling;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,9 @@ private static ArrayList<KeyValuePair<String, String>> createHttpParams(
808808
if (options.getContext() != null) {
809809
params.add(new KeyValuePair<>("context", options.getContext()));
810810
}
811+
if (options.getModelType() != null) {
812+
params.add(new KeyValuePair<>("model_type", options.getModelType()));
813+
}
811814
if (options.getTagHandling() != null) {
812815
params.add(new KeyValuePair<>("tag_handling", options.getTagHandling()));
813816
}

deepl-java/src/main/java/com/deepl/api/parsing/TextResultDeserializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ class TextResultDeserializer implements JsonDeserializer<TextResult> {
1616
public TextResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
1717
throws JsonParseException {
1818
JsonObject jsonObject = json.getAsJsonObject();
19+
JsonElement modelType = jsonObject.get("model_type_used");
1920
return new TextResult(
2021
jsonObject.get("text").getAsString(),
2122
jsonObject.get("detected_source_language").getAsString(),
22-
jsonObject.get("billed_characters").getAsInt());
23+
jsonObject.get("billed_characters").getAsInt(),
24+
modelType != null ? (modelType.getAsString()) : null);
2325
}
2426
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// license that can be found in the LICENSE file.
44
package com.deepl.api;
55

6-
import static org.mockito.Mockito.*;
7-
86
import java.io.*;
97
import java.net.*;
108
import java.time.*;
@@ -13,6 +11,7 @@
1311
import org.junit.jupiter.api.*;
1412
import org.junit.jupiter.params.ParameterizedTest;
1513
import org.junit.jupiter.params.provider.Arguments;
14+
import org.junit.jupiter.params.provider.CsvSource;
1615
import org.junit.jupiter.params.provider.MethodSource;
1716
import org.mockito.MockedConstruction;
1817
import org.mockito.Mockito;
@@ -49,6 +48,25 @@ void testExampleTranslation() throws DeepLException, InterruptedException {
4948
}
5049
}
5150

51+
@ParameterizedTest
52+
@CsvSource({
53+
"quality_optimized,quality_optimized",
54+
"prefer_quality_optimized,quality_optimized",
55+
"latency_optimized,latency_optimized"
56+
})
57+
void testModelType(String modelTypeArg, String expectedModelType)
58+
throws DeepLException, InterruptedException {
59+
Translator translator = createTranslator();
60+
String sourceLang = "de";
61+
TextResult result =
62+
translator.translateText(
63+
exampleText.get(sourceLang),
64+
sourceLang,
65+
"en-US",
66+
new TextTranslationOptions().setModelType(modelTypeArg));
67+
Assertions.assertEquals(expectedModelType, result.getModelTypeUsed());
68+
}
69+
5270
@Test
5371
void testInvalidServerUrl() {
5472
Assertions.assertThrows(

0 commit comments

Comments
 (0)