Skip to content

Commit a7f663e

Browse files
feat: Add support for custom_instructions parameter in text translation
1 parent 7819c05 commit a7f663e

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ console.log(await deeplClient.translateText('How are you?', null, 'de', { formal
181181
translated itself. Characters in the `context` parameter are not counted toward billing.
182182
See the [API documentation][api-docs-context-param] for more information and
183183
example usage.
184+
- `customInstructions`: an array of instructions to customize the translation behavior.
185+
Up to 10 custom instructions can be specified, each with a maximum of 300 characters.
186+
Important: The target language must be `de`, `en`, `es`, `fr`, `it`, `ja`, `ko`, `zh`
187+
or any variants of these languages.
188+
Note: Any request with the `customInstructions` parameter enabled will use
189+
the `quality_optimized` model type as the default. Requests combining
190+
`customInstructions` and `modelType: latency_optimized` will be rejected.
184191
- `modelType`: specifies the type of translation model to use, options are:
185192
- `'quality_optimized'`: use a translation model that maximizes translation
186193
quality, at the cost of response time. This option may be unavailable for

‎src/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ export interface TranslateTextOptions extends BaseRequestOptions {
184184
/** List of XML tags containing content that should not be translated. */
185185
ignoreTags?: TagList;
186186

187+
/**
188+
* List of custom instructions to guide the translation. Maximum 10 instructions,
189+
* each with a maximum length of 300 characters.
190+
*/
191+
customInstructions?: string[];
192+
187193
/** (internal only) Override path to send translate request to. */
188194
__path?: string;
189195
}

‎src/utils.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ export function validateAndAppendTextOptions(
285285
data.append('style_id', options.styleRule.styleId);
286286
}
287287
}
288+
if (options.customInstructions !== undefined) {
289+
for (const instruction of options.customInstructions) {
290+
data.append('custom_instructions', instruction);
291+
}
292+
}
288293
}
289294

290295
/**

‎tests/translateText.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,25 @@ describe('translate text', () => {
281281
expect(result.billedCharacters).toBe(exampleText.en.length);
282282
});
283283

284+
withRealServer('should translate with custom instructions', async () => {
285+
const translator = makeTranslator();
286+
const text = 'Hello world. I am testing if custom instructions are working correctly.';
287+
const resultWithCustomInstructions = await translator.translateText(text, null, 'de', {
288+
customInstructions: ['Use informal language', 'Be concise'],
289+
});
290+
const resultWithoutCustomInstructions = await translator.translateText(text, null, 'de');
291+
expect(resultWithCustomInstructions.text).toBeTruthy();
292+
expect(resultWithoutCustomInstructions.text).not.toBe(resultWithCustomInstructions.text);
293+
});
294+
295+
it('should translate with empty custom instructions array', async () => {
296+
const translator = makeTranslator();
297+
const result = await translator.translateText(exampleText.en, null, 'de', {
298+
customInstructions: [],
299+
});
300+
expect(result.text).toBe(exampleText.de);
301+
});
302+
284303
describe('request parameter tests', () => {
285304
beforeAll(() => {
286305
nock.disableNetConnect();

0 commit comments

Comments
 (0)