Skip to content

Commit aefb65d

Browse files
feat: add modelType option for text translation
1 parent f1ae3af commit aefb65d

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ following properties:
127127
- `text` is the translated text,
128128
- `detectedSourceLang` is the detected source language code,
129129
- `billedCharacters` is the number of characters billed for the text.
130+
- `modelTypeUsed` indicates the translation model used, but is `undefined`
131+
unless the `modelType` option is specified.
130132

131133
```javascript
132134
// Translate text into a target language, in this case, French:
@@ -180,6 +182,14 @@ console.log(await translator.translateText('How are you?', null, 'de', { formali
180182
translated itself. Characters in the `context` parameter are not counted toward billing.
181183
See the [API documentation][api-docs-context-param] for more information and
182184
example usage.
185+
- `modelType`: specifies the type of translation model to use, options are:
186+
- `'quality_optimized'`: use a translation model that maximizes translation
187+
quality, at the cost of response time. This option may be unavailable for
188+
some language pairs.
189+
- `'prefer_quality_optimized'`: use the highest-quality translation model
190+
for the given language pair.
191+
- `'latency_optimized'`: use a translation model that minimizes response
192+
time, at the cost of translation quality.
183193
- `tagHandling`: type of tags to parse before translation, options are `'html'`
184194
and `'xml'`.
185195

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ export interface TextResult {
228228
* Number of characters billed for this text.
229229
*/
230230
readonly billedCharacters: number;
231+
232+
/**
233+
* The translation model type used, if available.
234+
*/
235+
readonly modelTypeUsed?: string;
231236
}
232237

233238
/**
@@ -384,6 +389,9 @@ function validateAndAppendTextOptions(data: URLSearchParams, options?: Translate
384389
if (options.context !== undefined) {
385390
data.append('context', options.context);
386391
}
392+
if (options.modelType !== undefined) {
393+
data.append('model_type', options.modelType);
394+
}
387395
if (options.nonSplittingTags !== undefined) {
388396
data.append('non_splitting_tags', joinTagList(options.nonSplittingTags));
389397
}

src/parsing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ interface TextResultApiResponse {
6161
text: string;
6262
detected_source_language: string;
6363
billed_characters: number;
64+
model_type_used?: string;
6465
}
6566

6667
/**
@@ -312,6 +313,7 @@ export function parseTextResultArray(json: string): TextResult[] {
312313
translation.detected_source_language,
313314
) as SourceLanguageCode,
314315
billedCharacters: translation.billed_characters,
316+
modelTypeUsed: translation.model_type_used,
315317
};
316318
});
317319
} catch (error) {

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export interface TranslatorOptions {
7676
export type Formality = 'less' | 'more' | 'default' | 'prefer_less' | 'prefer_more';
7777
export type SentenceSplittingMode = 'off' | 'on' | 'nonewlines' | 'default';
7878
export type TagHandlingMode = 'html' | 'xml';
79+
export type ModelType = 'quality_optimized' | 'latency_optimized' | 'prefer_quality_optimized';
7980
export type GlossaryId = string;
8081
export type TagList = string | string[];
8182

@@ -136,6 +137,9 @@ export interface TranslateTextOptions {
136137
* See the API documentation for more information and example usage. */
137138
context?: string;
138139

140+
/** Type of translation model to use. */
141+
modelType?: ModelType;
142+
139143
/** List of XML tags that should be used to split text into sentences. */
140144
splittingTags?: TagList;
141145

tests/translateText.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
withMockServer,
1515
withRealServer,
1616
} from './core';
17-
import { QuotaExceededError, TranslateTextOptions } from 'deepl-node';
17+
import { ModelType, QuotaExceededError, TranslateTextOptions } from 'deepl-node';
1818

1919
describe('translate text', () => {
2020
it('should translate a single text', async () => {
@@ -25,6 +25,19 @@ describe('translate text', () => {
2525
expect(result.billedCharacters).toBe(exampleText.en.length);
2626
});
2727

28+
it.each([['quality_optimized'], ['latency_optimized'], ['prefer_quality_optimized']])(
29+
'should translate using model_type = %s',
30+
async (modelTypeStr) => {
31+
const translator = makeTranslator();
32+
const modelType = modelTypeStr as ModelType;
33+
const result = await translator.translateText(exampleText.en, 'en', 'de', {
34+
modelType: modelType,
35+
});
36+
const expectedModelTypeUsed = modelType.replace('prefer_', '');
37+
expect(result.modelTypeUsed).toBe(expectedModelTypeUsed);
38+
},
39+
);
40+
2841
it('should translate an array of texts', async () => {
2942
const translator = makeTranslator();
3043
const result = await translator.translateText([exampleText.fr, exampleText.en], null, 'de');

0 commit comments

Comments
 (0)