Skip to content

Commit d8c7ed8

Browse files
feat: Add support for custom_instructions parameter in text translation
1 parent 57aa2ce commit d8c7ed8

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

DeepL/TextTranslateOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,11 @@ public TextTranslateOptions(StyleRuleInfo styleRule) : this() {
8181
/// </summary>
8282
/// <seealso cref="ModelType" />
8383
public ModelType? ModelType { get; set; }
84+
85+
/// <summary>
86+
/// A list of custom instructions to guide the translation.
87+
/// Maximum of 10 instructions, each with a maximum length of 300 characters.
88+
/// </summary>
89+
public List<string> CustomInstructions { get; } = new List<string>();
8490
}
8591
}

DeepL/Translator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,12 @@ private String ConstructUserAgentString(bool sendPlatformInfo = true, AppInfo? a
956956
bodyParams.Add(("model_type", options.ModelType.Value.ToApiValue()));
957957
}
958958

959+
if (options.CustomInstructions.Count > 0) {
960+
foreach (var instruction in options.CustomInstructions) {
961+
bodyParams.Add(("custom_instructions", instruction));
962+
}
963+
}
964+
959965
AddExtraBodyParameters(bodyParams, options.ExtraBodyParameters);
960966

961967
return bodyParams;

DeepLTests/TranslateTextTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,5 +354,29 @@ public async Task TestExtraBodyParametersAllowsOverride() {
354354
Assert.Equal("en", result.DetectedSourceLanguageCode);
355355
Assert.Equal(ExampleText("en").Length, result.BilledCharacters);
356356
}
357+
358+
[RealServerOnlyFact]
359+
public async Task TestCustomInstructions() {
360+
var translator = CreateTestTranslator();
361+
var options = new TextTranslateOptions();
362+
var text = "I am testing if custom instructions are working correctly.";
363+
options.CustomInstructions.Add("Use informal language");
364+
options.CustomInstructions.Add("Be concise");
365+
366+
var result_with_custom_instructions = await translator.TranslateTextAsync(
367+
text,
368+
null,
369+
"DE",
370+
options);
371+
372+
var result_without_custom_instructions = await translator.TranslateTextAsync(
373+
text,
374+
null,
375+
"DE");
376+
377+
Assert.NotNull(result_with_custom_instructions);
378+
Assert.NotNull(result_with_custom_instructions.Text);
379+
Assert.NotEqual(result_with_custom_instructions.Text, result_without_custom_instructions.Text);
380+
}
357381
}
358382
}

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ foreach (var formality in new[] { Formality.Less, Formality.More }) {
139139
translated itself. Characters in the `context` parameter are not counted toward billing.
140140
See the [API documentation][api-docs-context-param] for more information and
141141
example usage.
142+
- `CustomInstructions`: an array of instructions to customize the translation behavior.
143+
Up to 10 custom instructions can be specified, each with a maximum of 300 characters.
144+
Important: The target language must be `de`, `en`, `es`, `fr`, `it`, `ja`, `ko`, `zh`
145+
or any variants of these languages.
146+
Note: Any request with the `CustomInstructions` parameter enabled will use
147+
`quality_optimized` models as the default. Requests combining
148+
`CustomInstructions` and `ModelType: latency_optimized` will be rejected.
142149
- `ModelType`: specifies the type of translation model to use, options are:
143150
- `'quality_optimized'` (`ModelType.QualityOptimized`): use a translation
144151
model that maximizes translation quality, at the cost of response time.

0 commit comments

Comments
 (0)