Skip to content

Commit 50b3c47

Browse files
MatKuhrbot-sdk-js
andauthored
fix: Unmasking Output Parsing (#165)
* fix: Unmasking Output Parsing * Formatting * Adjust test * Adjust test --------- Co-authored-by: SAP Cloud SDK Bot <[email protected]>
1 parent 9cd08d1 commit 50b3c47

File tree

7 files changed

+71
-53
lines changed

7 files changed

+71
-53
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.sap.ai.sdk.orchestration;
2+
3+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
4+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
5+
import com.sap.ai.sdk.orchestration.client.model.LLMChoice;
6+
import com.sap.ai.sdk.orchestration.client.model.LLMModuleResultSynchronous;
7+
import lombok.AccessLevel;
8+
import lombok.NoArgsConstructor;
9+
10+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
11+
final class JacksonMixins {
12+
/** Mixin to enforce a specific subtype to be deserialized always. */
13+
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
14+
@JsonDeserialize(as = LLMModuleResultSynchronous.class)
15+
interface LLMModuleResultMixIn {}
16+
17+
/** Mixin to enforce a specific subtype to be deserialized always. */
18+
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
19+
@JsonDeserialize(as = LLMChoice.class)
20+
interface ModuleResultsOutputUnmaskingInnerMixIn {}
21+
22+
/** Mixin to suppress @JsonTypeInfo for oneOf interfaces. */
23+
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
24+
interface NoTypeInfoMixin {}
25+
}

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ public class OrchestrationClient {
4545
.visibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
4646
.visibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
4747
.serializationInclusion(JsonInclude.Include.NON_NULL)
48-
.mixIn(LLMModuleResult.class, LLMModuleResultMixIn.class)
49-
.mixIn(ModuleResultsOutputUnmaskingInner.class, NoTypeInfoMixin.class)
50-
.mixIn(FilterConfig.class, NoTypeInfoMixin.class)
51-
.mixIn(MaskingProviderConfig.class, NoTypeInfoMixin.class)
52-
.mixIn(TemplatingModuleConfig.class, NoTypeInfoMixin.class)
48+
.mixIn(LLMModuleResult.class, JacksonMixins.LLMModuleResultMixIn.class)
49+
.mixIn(
50+
ModuleResultsOutputUnmaskingInner.class,
51+
JacksonMixins.ModuleResultsOutputUnmaskingInnerMixIn.class)
52+
.mixIn(FilterConfig.class, JacksonMixins.NoTypeInfoMixin.class)
53+
.mixIn(MaskingProviderConfig.class, JacksonMixins.NoTypeInfoMixin.class)
54+
.mixIn(TemplatingModuleConfig.class, JacksonMixins.NoTypeInfoMixin.class)
5355
.build();
5456
}
5557

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void messagesHistory() throws IOException {
298298
}
299299

300300
@Test
301-
void maskingAnonymization() throws IOException {
301+
void maskingPseudonymization() throws IOException {
302302
stubFor(
303303
post(urlPathEqualTo("/v2/inference/deployments/abcdef0123456789/completion"))
304304
.willReturn(
@@ -307,7 +307,7 @@ void maskingAnonymization() throws IOException {
307307
.withHeader("Content-Type", "application/json")));
308308

309309
final var maskingConfig =
310-
createMaskingConfig(DPIConfig.MethodEnum.ANONYMIZATION, DPIEntities.PHONE);
310+
createMaskingConfig(DPIConfig.MethodEnum.PSEUDONYMIZATION, DPIEntities.PHONE);
311311

312312
final var result = client.chatCompletion(prompt, config.withMaskingConfig(maskingConfig));
313313

@@ -316,9 +316,7 @@ void maskingAnonymization() throws IOException {
316316
assertThat(inputMasking.getMessage()).isEqualTo("Input to LLM is masked successfully.");
317317
assertThat(inputMasking.getData()).isNotNull();
318318
final var choices = ((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices();
319-
assertThat(choices.get(0).getMessage().getContent())
320-
.isEqualTo(
321-
"I'm sorry, I cannot provide information about specific individuals, including their nationality.");
319+
assertThat(choices.get(0).getMessage().getContent()).contains("Hi Mallory");
322320

323321
// verify that the request is sent correctly
324322
try (var requestInputStream = fileLoader.apply("maskingRequest.json")) {
Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,84 @@
11
{
2-
"request_id": "4c1b423d-f3a9-443e-9876-091888b0d585",
2+
"request_id": "c252c73e-849f-4fa7-8b35-b16f3434a0da",
33
"module_results": {
4-
"grounding": null,
54
"templating": [
5+
{
6+
"role": "system",
7+
"content": "Please write an initial response to the below user feedback, stating that we are working on the feedback and will get back to them soon.\nPlease make sure to address the user in person and end with \"Best regards, the AI SDK team\".\n"
8+
},
69
{
710
"role": "user",
8-
"content": "What is the nationality of Patrick Morgan +49 (970) 333-3833"
11+
"content": "Username: Mallory\nuserEmail: [email protected]\nDate: 2022-01-01\n\nI think the SDK is good, but could use some further enhancements.\nMy architect Alice and manager Bob pointed out that we need the grounding capabilities, which aren't supported yet.\n"
912
}
1013
],
1114
"input_masking": {
1215
"message": "Input to LLM is masked successfully.",
1316
"data": {
1417
"masked_template": [
18+
{
19+
"role": "system",
20+
"content": "Please write an initial response to the below user feedback, stating that we are working on the feedback and will get back to them soon.\nPlease make sure to address the user in person and end with \"Best regards, the AI SDK team\".\n"
21+
},
1522
{
1623
"role": "user",
17-
"content": "What is the nationality of Patrick Morgan MASKED_PHONE_NUMBER"
24+
"content": "Username: MASKED_PERSON_2\nuserEmail: MASKED_PERSON_3MASKED_EMAIL_1\nDate: 2022-01-01\n\nI think the SDK is good, but could use some further enhancements.\nMy architect MASKED_PERSON_1 and manager MASKED_PERSON_4 pointed out that we need the grounding capabilities, which aren't supported yet.\n"
1825
}
1926
]
2027
}
2128
},
22-
"input_filtering": null,
2329
"llm": {
30+
"id": "chatcmpl-AUr7GVVoQbg52GRbADpkx4hQvDBiz",
2431
"object": "chat.completion",
25-
"id": "chatcmpl-ADVDtCV54jp7BCKxeyWs3MWPFNx3N",
26-
"created": 1727781649,
32+
"created": 1731917382,
2733
"model": "gpt-35-turbo",
28-
"system_fingerprint": "fp_e49e4201a9",
34+
"system_fingerprint": "fp_808245b034",
2935
"choices": [
3036
{
3137
"index": 0,
3238
"message": {
3339
"role": "assistant",
34-
"content": "I'm sorry, I cannot provide information about specific individuals, including their nationality."
35-
},
36-
"logprobs": {
40+
"content": "Hi MASKED_PERSON_2,\n\nThank you for your feedback on the SDK. We appreciate your insights and are currently working on further enhancements, including the grounding capabilities that your architect and manager have highlighted. We will take your suggestions into consideration and aim to address them in our future updates.\n\nBest regards, the AI SDK team"
3741
},
3842
"finish_reason": "stop"
3943
}
4044
],
4145
"usage": {
42-
"completion_tokens": 16,
43-
"prompt_tokens": 18,
44-
"total_tokens": 34
46+
"completion_tokens": 64,
47+
"prompt_tokens": 134,
48+
"total_tokens": 198
4549
}
4650
},
47-
"output_filtering": null,
48-
"output_unmasking": []
51+
"output_unmasking": [
52+
{
53+
"index": 0,
54+
"message": {
55+
"role": "assistant",
56+
"content": "Hi Mallory,\n\nThank you for your feedback on the SDK. We appreciate your insights and are currently working on further enhancements, including the grounding capabilities that your architect and manager have highlighted. We will take your suggestions into consideration and aim to address them in our future updates.\n\nBest regards, the AI SDK team"
57+
},
58+
"finish_reason": "stop"
59+
}
60+
]
4961
},
5062
"orchestration_result": {
63+
"id": "chatcmpl-AUr7GVVoQbg52GRbADpkx4hQvDBiz",
5164
"object": "chat.completion",
52-
"id": "chatcmpl-ADVDtCV54jp7BCKxeyWs3MWPFNx3N",
53-
"created": 1727781649,
65+
"created": 1731917382,
5466
"model": "gpt-35-turbo",
55-
"system_fingerprint": "fp_e49e4201a9",
67+
"system_fingerprint": "fp_808245b034",
5668
"choices": [
5769
{
5870
"index": 0,
5971
"message": {
6072
"role": "assistant",
61-
"content": "I'm sorry, I cannot provide information about specific individuals, including their nationality."
62-
},
63-
"logprobs": {
73+
"content": "Hi Mallory,\n\nThank you for your feedback on the SDK. We appreciate your insights and are currently working on further enhancements, including the grounding capabilities that your architect and manager have highlighted. We will take your suggestions into consideration and aim to address them in our future updates.\n\nBest regards, the AI SDK team"
6474
},
6575
"finish_reason": "stop"
6676
}
6777
],
6878
"usage": {
69-
"completion_tokens": 16,
70-
"prompt_tokens": 18,
71-
"total_tokens": 34
79+
"completion_tokens": 64,
80+
"prompt_tokens": 134,
81+
"total_tokens": 198
7282
}
7383
}
7484
}

orchestration/src/test/resources/maskingRequest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"masking_providers": [
2424
{
2525
"type": "sap_data_privacy_integration",
26-
"method": "anonymization",
26+
"method": "pseudonymization",
2727
"entities": [
2828
{
2929
"type": "profile-phone"

0 commit comments

Comments
 (0)