Skip to content

Commit 49ba08a

Browse files
committed
Fixing approach
1 parent 5b2a54d commit 49ba08a

File tree

6 files changed

+114
-77
lines changed

6 files changed

+114
-77
lines changed

orchestration/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<coverage.instruction>94%</coverage.instruction>
4242
<coverage.branch>75%</coverage.branch>
4343
<coverage.method>93%</coverage.method>
44-
<coverage.class>98%</coverage.class>
44+
<coverage.class>100%</coverage.class>
4545
</properties>
4646

4747
<dependencies>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,30 @@ public OrchestrationModuleConfig withTemplateConfig(
320320
@Nonnull final TemplateConfig templateConfig) {
321321
return this.withTemplateConfig(templateConfig.toLowLevel());
322322
}
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 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 translationConfig) {
347+
return this.withOutputTranslationConfig(translationConfig.createSAPDocumentTranslationOutput());
348+
}
323349
}

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

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import lombok.Getter;
1111
import lombok.RequiredArgsConstructor;
1212
import lombok.Value;
13+
import lombok.With;
14+
import lombok.val;
1315

1416
/**
1517
* Configuration helper for SAP Document Translation.
@@ -19,70 +21,81 @@
1921
* AI Core: Orchestration - SAP Document Translation</a>
2022
*/
2123
@Value
24+
@With
2225
@Getter(AccessLevel.PACKAGE)
2326
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
2427
public class TranslationConfig {
25-
@Nonnull SAPDocumentTranslationInput.TypeEnum translationType;
28+
SAPDocumentTranslationInput inputConfig;
29+
SAPDocumentTranslationOutput outputConfig;
30+
String sourceLanguage;
2631

2732
/**
28-
* Create a builder for the SAP_DOCUMENT_TRANSLATION translation provider
33+
* Create a new input translation configuration.
2934
*
30-
* @return The Builder instance.
35+
* @param targetLanguage The target language code
36+
* @return A TranslationConfig configured for input translation
3137
*/
3238
@Nonnull
33-
public static Builder inputTranslation() {
34-
return new TranslationConfig.Builder(
35-
SAPDocumentTranslationInput.TypeEnum.SAP_DOCUMENT_TRANSLATION);
39+
public static TranslationConfig createInputConfig(@Nonnull final String targetLanguage) {
40+
val translationType = SAPDocumentTranslationInput.TypeEnum.SAP_DOCUMENT_TRANSLATION;
41+
val inputConfig =
42+
SAPDocumentTranslationInput.create()
43+
.type(translationType)
44+
.config(
45+
SAPDocumentTranslationInputConfig.create()
46+
.targetLanguage(targetLanguage)
47+
.applyTo(null));
48+
return new TranslationConfig(inputConfig, null, null);
3649
}
3750

3851
/**
39-
* Create a builder for the SAP_DOCUMENT_TRANSLATION translation provider
52+
* Create a new output translation configuration.
4053
*
41-
* @return The Builder2 instance.
54+
* @param targetLanguage The target language code
55+
* @return A TranslationConfig configured for output translation
4256
*/
4357
@Nonnull
44-
public static Builder2 outputTranslation() {
45-
return new TranslationConfig.Builder2(
46-
SAPDocumentTranslationOutput.TypeEnum.SAP_DOCUMENT_TRANSLATION);
58+
public static TranslationConfig createOutputConfig(@Nonnull final String targetLanguage) {
59+
val translationType = SAPDocumentTranslationOutput.TypeEnum.SAP_DOCUMENT_TRANSLATION;
60+
val outputConfig =
61+
SAPDocumentTranslationOutput.create()
62+
.type(translationType)
63+
.config(
64+
SAPDocumentTranslationOutputConfig.create()
65+
.targetLanguage(
66+
SAPDocumentTranslationOutputTargetLanguage.create(targetLanguage))
67+
.sourceLanguage("en-US"));
68+
return new TranslationConfig(null, outputConfig, null);
4769
}
4870

49-
/** Builder helper class. */
50-
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
51-
public static class Builder {
52-
53-
private final SAPDocumentTranslationInput.TypeEnum translationType;
54-
55-
/**
56-
* Get the input translation config with default values.
57-
*
58-
* @return The SAPDocumentTranslationInput instance.
59-
*/
60-
@Nonnull
61-
public SAPDocumentTranslationInput getInputTranslationConfig() {
62-
return SAPDocumentTranslationInput.create()
63-
.type(translationType)
64-
.config(SAPDocumentTranslationInputConfig.create().targetLanguage("en-US").applyTo(null));
65-
}
71+
/**
72+
* Set the source language for translation
73+
*
74+
* @param language The source language code
75+
* @return A new TranslationConfig with the specified source language
76+
*/
77+
@Nonnull
78+
public TranslationConfig sourceLanguage(@Nonnull final String language) {
79+
return this.withSourceLanguage(language);
6680
}
6781

68-
/** Sample builder class */
69-
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
70-
public static class Builder2 {
71-
private final SAPDocumentTranslationOutput.TypeEnum translationType;
82+
/**
83+
* Build and return the input translation configuration
84+
*
85+
* @return The SAPDocumentTranslationInput configuration
86+
*/
87+
@Nonnull
88+
SAPDocumentTranslationInput createSAPDocumentTranslationInput() {
89+
return inputConfig;
90+
}
7291

73-
/**
74-
* Get the output translation config with default values.
75-
*
76-
* @return The SAPDocumentTranslationOutput instance.
77-
*/
78-
@Nonnull
79-
public SAPDocumentTranslationOutput getOutputTranslationConfig() {
80-
return SAPDocumentTranslationOutput.create()
81-
.type(translationType)
82-
.config(
83-
SAPDocumentTranslationOutputConfig.create()
84-
.targetLanguage(SAPDocumentTranslationOutputTargetLanguage.create("de-DE"))
85-
.sourceLanguage("en-US"));
86-
}
92+
/**
93+
* Build and return the output translation configuration
94+
*
95+
* @return The SAPDocumentTranslationOutput configuration
96+
*/
97+
@Nonnull
98+
SAPDocumentTranslationOutput createSAPDocumentTranslationOutput() {
99+
return outputConfig;
87100
}
88101
}

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:
@@ -465,7 +465,7 @@ components:
465465
properties:
466466
type:
467467
type: string
468-
enum: ["text"]
468+
enum: [ "text" ]
469469
text:
470470
type: string
471471
ChatDelta:
@@ -639,7 +639,7 @@ components:
639639
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.
640640
items:
641641
type: string
642-
example: ["\n", ".", "?", "!"]
642+
example: [ "\n", ".", "?", "!" ]
643643

644644
GenericModuleResult:
645645
type: object
@@ -682,8 +682,8 @@ components:
682682
type: string
683683
example:
684684
user:
685-
["translated user content", "another translated user content"]
686-
system: ["translated system message"]
685+
[ "translated user content", "another translated user content" ]
686+
system: [ "translated system message" ]
687687
translated_placeholders:
688688
type: object
689689
additionalProperties:
@@ -1356,7 +1356,7 @@ components:
13561356
allowlist:
13571357
description: List of strings that should not be masked
13581358
type: array
1359-
example: ["SAP", "Joule"]
1359+
example: [ "SAP", "Joule" ]
13601360
items:
13611361
type: string
13621362
mask_grounding_input:
@@ -1475,7 +1475,7 @@ components:
14751475
anyOf:
14761476
- enum:
14771477
- document_grounding_service
1478-
- {}
1478+
- { }
14791479
example: document_grounding_service
14801480
config:
14811481
type: object

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,27 @@ void testDpiMaskingRegex() {
140140

141141
@Test
142142
void testTranslationConfig() {
143-
var translationConfig = TranslationConfig.inputTranslation().getInputTranslationConfig();
143+
var inputTranslationConfig =
144+
TranslationConfig.createInputConfig("en-US").sourceLanguage("de-DE");
145+
var outputTranslationConfig =
146+
TranslationConfig.createOutputConfig("de-DE").sourceLanguage("en-US");
144147
var config =
145148
new OrchestrationModuleConfig()
146149
.withLlmConfig(GPT_4O)
147-
.withInputTranslationConfig(translationConfig);
150+
.withInputTranslationConfig(inputTranslationConfig)
151+
.withOutputTranslationConfig(outputTranslationConfig);
148152

149153
assertThat(config.getInputTranslationConfig()).isNotNull();
150-
assertThat(config.getInputTranslationConfig().getType()).isEqualTo(translationConfig.getType());
151154
assertThat(config.getInputTranslationConfig().getConfig().getTargetLanguage())
152-
.isEqualTo(translationConfig.getConfig().getTargetLanguage());
153-
154-
var translationConfig2 = TranslationConfig.inputTranslation().getInputTranslationConfig();
155-
var config2 =
156-
new OrchestrationModuleConfig()
157-
.withLlmConfig(GPT_4O)
158-
.withInputTranslationConfig(translationConfig2);
159-
160-
assertThat(config2.getInputTranslationConfig()).isNotNull();
161-
assertThat(config2.getInputTranslationConfig().getType())
162-
.isEqualTo(translationConfig2.getType());
163-
assertThat(config2.getInputTranslationConfig().getConfig().getTargetLanguage())
164-
.isEqualTo(translationConfig2.getConfig().getTargetLanguage());
155+
.isEqualTo("en-US");
156+
assertThat(config.getInputTranslationConfig().getConfig().getSourceLanguage())
157+
.isEqualTo(inputTranslationConfig.getInputConfig().getConfig().getSourceLanguage());
158+
159+
assertThat(config.getOutputTranslationConfig()).isNotNull();
160+
assertThat(config.getOutputTranslationConfig().getConfig().getTargetLanguage())
161+
.isEqualTo(outputTranslationConfig.getOutputConfig().getConfig().getTargetLanguage());
162+
assertThat(config.getOutputTranslationConfig().getConfig().getSourceLanguage())
163+
.isEqualTo(outputTranslationConfig.getOutputConfig().getConfig().getSourceLanguage());
165164
}
166165

167166
@Test

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,11 +616,10 @@ public OrchestrationChatResponse translation() {
616616
// https://help.sap.com/docs/translation-hub/sap-translation-hub/supported-languages?version=Cloud#translation-provider-sap-machine-translation
617617
val configWithTranslation =
618618
config
619-
.withInputTranslationConfig(
620-
TranslationConfig.inputTranslation().getInputTranslationConfig())
619+
.withInputTranslationConfig(TranslationConfig.createInputConfig("en-US"))
621620
.withOutputTranslationConfig(
622-
TranslationConfig.outputTranslation()
623-
.getOutputTranslationConfig()); // optional source language
621+
TranslationConfig.createOutputConfig("de-DE")
622+
.sourceLanguage("en-US")); // optional source language
624623

625624
return client.chatCompletion(prompt, configWithTranslation);
626625
}

0 commit comments

Comments
 (0)