Skip to content

Commit 71a207b

Browse files
n-o-u-r-h-a-nbot-sdk-jsrpanackalnewtorkJonas-Isr
authored
chore: Translation Module Convenience (#646)
* Implemented Convenience Function * Updating release notes + some formatting * Updates * formatting * Formatting * check * Formatting * more formatting * Formatting * run tests * Formatting * run tests * fix error1 * Formatting * Revert generated models to match origin/main # Conflicts: # orchestration/src/main/java/com/sap/ai/sdk/orchestration/model/SAPDocumentTranslationOutput.java * making 2 model classes just like main * chore: retrigger CI * checking * chore: retrigger CI * matching the CI * Formatting * chore: retrigger CI * matching the CI#2 * matching the CI#3 * chore: retrigger CI * reverting * Updating release notes * fixing unnecessary diffs * Fixing approach * Reduce method name * Further Modifications * Splitting TranslationConfig class (Alex's Idea) * Update orchestration/src/main/java/com/sap/ai/sdk/orchestration/TranslationConfig.java Co-authored-by: Jonas-Isr <[email protected]> * Formatting * Moving @with annotation * Formatting * Formatting * chore: retrigger CI * chore: retrigger CI * Adding link to supported languages table * Updating release notes * Changing method name * Update name in release notes --------- Co-authored-by: SAP Cloud SDK Bot <[email protected]> Co-authored-by: Roshin Rajan Panackal <[email protected]> Co-authored-by: Alexander Dümont <[email protected]> Co-authored-by: Jonas-Isr <[email protected]>
1 parent 2535170 commit 71a207b

File tree

6 files changed

+187
-30
lines changed

6 files changed

+187
-30
lines changed

docs/release_notes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
### 📈 Improvements
1919

20-
-
20+
- [Orchestration] Added new API `TranslationConfig#translateInputTo` to extract input config.
21+
- [Orchestration] Added new API `TranslationConfig#translateOutputTo` to extract output config.
2122

2223
### 🐛 Fixed Issues
2324

orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationModuleConfig.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,24 @@ public class OrchestrationModuleConfig {
100100
*/
101101
@Nullable GroundingModuleConfig groundingConfig;
102102

103+
/**
104+
* Configuration for translating input content before processing.
105+
*
106+
* @link <a
107+
* href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/enhance-model-consumption-with-translation">
108+
* SAP AI Core: Orchestration - Input Translation</a>
109+
* @since 1.8.0
110+
*/
103111
@Nullable SAPDocumentTranslationInput inputTranslationConfig;
104112

113+
/**
114+
* Configuration for translating output content after processing.
115+
*
116+
* @link <a
117+
* href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/enhance-model-consumption-with-output-translation">
118+
* SAP AI Core: Orchestration - Output Translation</a>
119+
* @since 1.8.0
120+
*/
105121
@Nullable SAPDocumentTranslationOutput outputTranslationConfig;
106122

107123
/** Configuration of optional streaming options for output filtering. */
@@ -304,4 +320,30 @@ public OrchestrationModuleConfig withTemplateConfig(
304320
@Nonnull final TemplateConfig templateConfig) {
305321
return this.withTemplateConfig(templateConfig.toLowLevel());
306322
}
323+
324+
/**
325+
* Configure input translation using a high-level TranslationConfig.
326+
*
327+
* @param translationConfig The translation configuration
328+
* @return A new OrchestrationModuleConfig with input translation configured
329+
*/
330+
@Tolerate
331+
@Nonnull
332+
public OrchestrationModuleConfig withInputTranslationConfig(
333+
@Nonnull final TranslationConfig.Input translationConfig) {
334+
return this.withInputTranslationConfig(translationConfig.createSAPDocumentTranslationInput());
335+
}
336+
337+
/**
338+
* Configure output translation using a high-level TranslationConfig.
339+
*
340+
* @param translationConfig The translation configuration
341+
* @return A new OrchestrationModuleConfig with output translation configured
342+
*/
343+
@Tolerate
344+
@Nonnull
345+
public OrchestrationModuleConfig withOutputTranslationConfig(
346+
@Nonnull final TranslationConfig.Output translationConfig) {
347+
return this.withOutputTranslationConfig(translationConfig.createSAPDocumentTranslationOutput());
348+
}
307349
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.sap.ai.sdk.orchestration;
2+
3+
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationInput;
4+
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationInputConfig;
5+
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationOutput;
6+
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationOutputConfig;
7+
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationOutputTargetLanguage;
8+
import javax.annotation.Nonnull;
9+
import lombok.AccessLevel;
10+
import lombok.RequiredArgsConstructor;
11+
import lombok.Value;
12+
import lombok.With;
13+
import lombok.val;
14+
15+
/**
16+
* Configuration helper for SAP Document Translation.
17+
*
18+
* @link <a
19+
* href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/sap-document-translation">SAP
20+
* AI Core: Orchestration - SAP Document Translation</a>
21+
* @since 1.14.0
22+
*/
23+
public interface TranslationConfig {
24+
/** Input configuration for translation. */
25+
@Value
26+
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
27+
class Input implements TranslationConfig {
28+
String targetLanguage;
29+
30+
@With String sourceLanguage;
31+
32+
Object ApplyTo; // Can be null
33+
34+
@Nonnull
35+
SAPDocumentTranslationInput createSAPDocumentTranslationInput() {
36+
val translationType = SAPDocumentTranslationInput.TypeEnum.SAP_DOCUMENT_TRANSLATION;
37+
val conf =
38+
SAPDocumentTranslationInputConfig.create().targetLanguage(targetLanguage).applyTo(null);
39+
return SAPDocumentTranslationInput.create().type(translationType).config(conf);
40+
}
41+
}
42+
43+
/** Output configuration for translation. */
44+
@Value
45+
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
46+
class Output implements TranslationConfig {
47+
String targetLanguage;
48+
49+
@With String sourceLanguage;
50+
51+
@Nonnull
52+
SAPDocumentTranslationOutput createSAPDocumentTranslationOutput() {
53+
val translationType = SAPDocumentTranslationOutput.TypeEnum.SAP_DOCUMENT_TRANSLATION;
54+
val tLang = SAPDocumentTranslationOutputTargetLanguage.create(targetLanguage);
55+
val conf =
56+
SAPDocumentTranslationOutputConfig.create()
57+
.targetLanguage(tLang)
58+
.sourceLanguage(sourceLanguage);
59+
return SAPDocumentTranslationOutput.create().type(translationType).config(conf);
60+
}
61+
}
62+
63+
/**
64+
* Create a new input translation configuration.
65+
*
66+
* @param targetLanguage The target language code
67+
* @link <a
68+
* href="https://help.sap.com/docs/translation-hub/sap-translation-hub/supported-languages">SAP
69+
* AI Core: Orchestration - SAP Translation Hub Table with official languages</a>
70+
* @return A TranslationConfig configured for input translation
71+
*/
72+
@Nonnull
73+
static TranslationConfig.Input translateInputTo(@Nonnull final String targetLanguage) {
74+
75+
return new TranslationConfig.Input(targetLanguage, null, null);
76+
}
77+
78+
/**
79+
* Create a new output translation configuration.
80+
*
81+
* @param targetLanguage The target language code
82+
* @link <a
83+
* href="https://help.sap.com/docs/translation-hub/sap-translation-hub/supported-languages">
84+
* SAP AI Core: Orchestration - SAP Translation Hub Table with official languages</a>
85+
* @return A TranslationConfig configured for output translation
86+
*/
87+
@Nonnull
88+
static TranslationConfig.Output translateOutputTo(@Nonnull final String targetLanguage) {
89+
90+
return new TranslationConfig.Output(targetLanguage, null);
91+
}
92+
}

orchestration/src/main/resources/spec/orchestration.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ components:
112112
$ref: "#/components/schemas/EmbeddingsInputText"
113113
type:
114114
type: string
115-
enum: ["text", "document", "query"]
115+
enum: [ "text", "document", "query" ]
116116
EmbeddingsInputText:
117117
oneOf:
118118
- type: string
@@ -124,9 +124,9 @@ components:
124124
items:
125125
type: string
126126
minLength: 1
127-
example: ["Hello", "World", "!"]
127+
example: [ "Hello", "World", "!" ]
128128
description: Text input for which embeddings need to be generated
129-
example: ["This is an input string.", "This is another input string."]
129+
example: [ "This is an input string.", "This is another input string." ]
130130
EmbeddingsModuleConfigs:
131131
type: object
132132
required:
@@ -181,7 +181,7 @@ components:
181181
The number of dimensions the resulting output embeddings should have.
182182
encoding_format:
183183
type: string
184-
enum: [float, base64, binary]
184+
enum: [ float, base64, binary ]
185185
description: >
186186
OpenAI's spec allows for 'float' and 'base64' encoding formats.
187187
normalize:
@@ -466,7 +466,7 @@ components:
466466
properties:
467467
type:
468468
type: string
469-
enum: ["text"]
469+
enum: [ "text" ]
470470
text:
471471
type: string
472472
ChatDelta:
@@ -659,7 +659,7 @@ components:
659659
description: List of delimiters to split the input text into chunks.Please note, this is a required parameter when `input_translation_module_config` or `output_translation_module_config` are configured.
660660
items:
661661
type: string
662-
example: ["\n", ".", "?", "!"]
662+
example: [ "\n", ".", "?", "!" ]
663663

664664
GenericModuleResult:
665665
type: object
@@ -702,8 +702,8 @@ components:
702702
type: string
703703
example:
704704
user:
705-
["translated user content", "another translated user content"]
706-
system: ["translated system message"]
705+
[ "translated user content", "another translated user content" ]
706+
system: [ "translated system message" ]
707707
translated_placeholders:
708708
type: object
709709
additionalProperties:
@@ -1380,7 +1380,7 @@ components:
13801380
allowlist:
13811381
description: List of strings that should not be masked
13821382
type: array
1383-
example: ["SAP", "Joule"]
1383+
example: [ "SAP", "Joule" ]
13841384
items:
13851385
type: string
13861386
mask_grounding_input:
@@ -1499,7 +1499,7 @@ components:
14991499
anyOf:
15001500
- enum:
15011501
- document_grounding_service
1502-
- {}
1502+
- { }
15031503
example: document_grounding_service
15041504
config:
15051505
type: object

orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationModuleConfigTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,43 @@ void testDpiMaskingRegex() {
138138
.value("**-**-*****"));
139139
}
140140

141+
@Test
142+
void testTranslationConfig() {
143+
var inputTranslationConfig =
144+
TranslationConfig.translateInputTo("en-US").withSourceLanguage("de-DE");
145+
var outputTranslationConfig =
146+
TranslationConfig.translateOutputTo("de-DE").withSourceLanguage("en-US");
147+
var config =
148+
new OrchestrationModuleConfig()
149+
.withLlmConfig(GPT_4O)
150+
.withInputTranslationConfig(inputTranslationConfig)
151+
.withOutputTranslationConfig(outputTranslationConfig);
152+
153+
assertThat(config.getInputTranslationConfig()).isNotNull();
154+
assertThat(config.getInputTranslationConfig().getConfig().getTargetLanguage())
155+
.isEqualTo("en-US");
156+
assertThat(config.getInputTranslationConfig().getConfig().getSourceLanguage())
157+
.isEqualTo(
158+
inputTranslationConfig
159+
.createSAPDocumentTranslationInput()
160+
.getConfig()
161+
.getSourceLanguage());
162+
163+
assertThat(config.getOutputTranslationConfig()).isNotNull();
164+
assertThat(config.getOutputTranslationConfig().getConfig().getTargetLanguage())
165+
.isEqualTo(
166+
outputTranslationConfig
167+
.createSAPDocumentTranslationOutput()
168+
.getConfig()
169+
.getTargetLanguage());
170+
assertThat(config.getOutputTranslationConfig().getConfig().getSourceLanguage())
171+
.isEqualTo(
172+
outputTranslationConfig
173+
.createSAPDocumentTranslationOutput()
174+
.getConfig()
175+
.getSourceLanguage());
176+
}
177+
141178
@Test
142179
void testParams() {
143180
// test withParams(Map<String, Object>)

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OrchestrationService.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@
2323
import com.sap.ai.sdk.orchestration.OrchestrationPrompt;
2424
import com.sap.ai.sdk.orchestration.ResponseJsonSchema;
2525
import com.sap.ai.sdk.orchestration.TemplateConfig;
26+
import com.sap.ai.sdk.orchestration.TranslationConfig;
2627
import com.sap.ai.sdk.orchestration.model.DPIEntities;
2728
import com.sap.ai.sdk.orchestration.model.DataRepositoryType;
2829
import com.sap.ai.sdk.orchestration.model.DocumentGroundingFilter;
2930
import com.sap.ai.sdk.orchestration.model.GroundingFilterSearchConfiguration;
3031
import com.sap.ai.sdk.orchestration.model.LlamaGuard38b;
3132
import com.sap.ai.sdk.orchestration.model.ResponseFormatText;
32-
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationInput;
33-
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationInputConfig;
34-
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationOutput;
35-
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationOutputConfig;
36-
import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationOutputTargetLanguage;
3733
import com.sap.ai.sdk.orchestration.model.SearchDocumentKeyValueListPair;
3834
import com.sap.ai.sdk.orchestration.model.SearchSelectOptionEnum;
3935
import com.sap.ai.sdk.orchestration.model.Template;
@@ -620,21 +616,10 @@ public OrchestrationChatResponse translation() {
620616
// https://help.sap.com/docs/translation-hub/sap-translation-hub/supported-languages?version=Cloud#translation-provider-sap-machine-translation
621617
val configWithTranslation =
622618
config
623-
.withInputTranslationConfig(
624-
SAPDocumentTranslationInput.create()
625-
.type(SAPDocumentTranslationInput.TypeEnum.SAP_DOCUMENT_TRANSLATION)
626-
.config(
627-
SAPDocumentTranslationInputConfig.create()
628-
.targetLanguage("en-US")
629-
.applyTo(null)))
619+
.withInputTranslationConfig(TranslationConfig.translateInputTo("en-US"))
630620
.withOutputTranslationConfig(
631-
SAPDocumentTranslationOutput.create()
632-
.type(SAPDocumentTranslationOutput.TypeEnum.SAP_DOCUMENT_TRANSLATION)
633-
.config(
634-
SAPDocumentTranslationOutputConfig.create()
635-
.targetLanguage(
636-
SAPDocumentTranslationOutputTargetLanguage.create("de-DE"))
637-
.sourceLanguage("en-US"))); // optional source language
621+
TranslationConfig.translateOutputTo("de-DE")
622+
.withSourceLanguage("en-US")); // optional source language
638623

639624
return client.chatCompletion(prompt, configWithTranslation);
640625
}

0 commit comments

Comments
 (0)