Skip to content

Commit dbe6dda

Browse files
author
I750911
committed
update to support different thresholds (for testing)
1 parent 35ac052 commit dbe6dda

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/SpringAiOrchestrationController.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sap.ai.sdk.app.controllers;
22

33
import com.sap.ai.sdk.app.services.SpringAiOrchestrationService;
4+
import com.sap.ai.sdk.orchestration.AzureFilterThreshold;
45
import com.sap.ai.sdk.orchestration.OrchestrationClientException;
56
import com.sap.ai.sdk.orchestration.spring.OrchestrationSpringChatResponse;
67
import javax.annotation.Nonnull;
@@ -12,6 +13,7 @@
1213
import org.springframework.beans.factory.annotation.Autowired;
1314
import org.springframework.http.ResponseEntity;
1415
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.PathVariable;
1517
import org.springframework.web.bind.annotation.RequestMapping;
1618
import org.springframework.web.bind.annotation.RequestParam;
1719
import org.springframework.web.bind.annotation.RestController;
@@ -57,42 +59,48 @@ Object template(@Nullable @RequestParam(value = "format", required = false) fina
5759
return response.getResult().getOutput().getText();
5860
}
5961

60-
@GetMapping("/inputFiltering")
62+
@GetMapping("/inputFiltering/{policy}")
6163
@Nonnull
6264
Object inputFiltering(
63-
@Nullable @RequestParam(value = "format", required = false) final String format) {
65+
@Nullable @RequestParam(value = "format", required = false) final String format,
66+
@Nonnull @PathVariable("policy") final AzureFilterThreshold policy) {
6467

6568
final ChatResponse response;
6669
try {
67-
response = service.inputFiltering();
70+
response = service.inputFiltering(policy);
6871
} catch (OrchestrationClientException e) {
6972
final var msg = "Failed to obtain a response as the content was flagged by input filter.";
7073
log.debug(msg, e);
7174
return ResponseEntity.internalServerError().body(msg);
7275
}
7376

7477
if ("json".equals(format)) {
75-
return response;
78+
return ((OrchestrationSpringChatResponse) response)
79+
.getOrchestrationResponse()
80+
.getOriginalResponse();
7681
}
7782
return response.getResult().getOutput().getText();
7883
}
7984

80-
@GetMapping("/outputFiltering")
85+
@GetMapping("/outputFiltering/{policy}")
8186
@Nonnull
8287
Object outputFiltering(
83-
@Nullable @RequestParam(value = "format", required = false) final String format) {
88+
@Nullable @RequestParam(value = "format", required = false) final String format,
89+
@Nonnull @PathVariable("policy") final AzureFilterThreshold policy) {
8490

8591
final ChatResponse response;
8692
try {
87-
response = service.outputFiltering();
93+
response = service.outputFiltering(policy);
8894
} catch (OrchestrationClientException e) {
8995
final var msg = "Failed to obtain a response as the content was flagged by output filter.";
9096
log.debug(msg, e);
9197
return ResponseEntity.internalServerError().body(msg);
9298
}
9399

94100
if ("json".equals(format)) {
95-
return response;
101+
return ((OrchestrationSpringChatResponse) response)
102+
.getOrchestrationResponse()
103+
.getOriginalResponse();
96104
}
97105
return response.getResult().getOutput().getText();
98106
}

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.sap.ai.sdk.app.services;
22

3+
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GEMINI_1_5_FLASH;
34
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GPT_4O_MINI;
5+
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.TEMPERATURE;
46

57
import com.sap.ai.sdk.orchestration.AzureContentFilter;
68
import com.sap.ai.sdk.orchestration.AzureFilterThreshold;
@@ -103,21 +105,24 @@ public ChatResponse masking() {
103105
* @link <a
104106
* href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/input-filtering">SAP
105107
* AI Core: Orchestration - Input Filtering</a>
108+
* @param policy the explicitness of content that should be allowed through the filter
106109
* @return the assistant response object
107110
*/
108111
@Nonnull
109-
public ChatResponse inputFiltering() throws OrchestrationClientException {
112+
public ChatResponse inputFiltering(AzureFilterThreshold policy)
113+
throws OrchestrationClientException {
110114
val filterConfig =
111-
new AzureContentFilter()
112-
.hate(AzureFilterThreshold.ALLOW_SAFE)
113-
.selfHarm(AzureFilterThreshold.ALLOW_SAFE)
114-
.sexual(AzureFilterThreshold.ALLOW_SAFE)
115-
.violence(AzureFilterThreshold.ALLOW_SAFE);
116-
117-
val opts = new OrchestrationChatOptions(config.withInputFiltering(filterConfig));
115+
new AzureContentFilter().hate(policy).selfHarm(policy).sexual(policy).violence(policy);
116+
val opts =
117+
new OrchestrationChatOptions(
118+
config
119+
.withLlmConfig(GEMINI_1_5_FLASH.withParam(TEMPERATURE, 0.0))
120+
.withInputFiltering(filterConfig));
118121

119122
val prompt =
120-
new Prompt("Please rephrase the following sentence for me: 'I want to kill you!'", opts);
123+
new Prompt(
124+
"Please rephrase the following sentence for me: 'We shall spill blood tonight', said the operator in-charge.",
125+
opts);
121126

122127
return client.call(prompt);
123128
}
@@ -128,21 +133,24 @@ public ChatResponse inputFiltering() throws OrchestrationClientException {
128133
* @link <a
129134
* href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/output-filtering">SAP
130135
* AI Core: Orchestration - Output Filtering</a>
136+
* @param policy the explicitness of content that should be allowed through the filter
131137
* @return the assistant response object
132138
*/
133139
@Nonnull
134-
public ChatResponse outputFiltering() throws OrchestrationClientException {
140+
public ChatResponse outputFiltering(AzureFilterThreshold policy)
141+
throws OrchestrationClientException {
135142
val filterConfig =
136-
new AzureContentFilter()
137-
.hate(AzureFilterThreshold.ALLOW_SAFE)
138-
.selfHarm(AzureFilterThreshold.ALLOW_SAFE)
139-
.sexual(AzureFilterThreshold.ALLOW_SAFE)
140-
.violence(AzureFilterThreshold.ALLOW_SAFE);
141-
142-
val opts = new OrchestrationChatOptions(config.withOutputFiltering(filterConfig));
143+
new AzureContentFilter().hate(policy).selfHarm(policy).sexual(policy).violence(policy);
144+
val opts =
145+
new OrchestrationChatOptions(
146+
config
147+
.withLlmConfig(GEMINI_1_5_FLASH.withParam(TEMPERATURE, 0.0))
148+
.withOutputFiltering(filterConfig));
143149

144150
val prompt =
145-
new Prompt("Please rephrase the following sentence for me: 'I want to kill you!'", opts);
151+
new Prompt(
152+
"Please rephrase the following sentence for me: 'We shall spill blood tonight', said the operator in-charge.",
153+
opts);
146154

147155
return client.call(prompt);
148156
}

sample-code/spring-app/src/main/resources/static/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -694,24 +694,24 @@ <h5 class="mb-1">Orchestration Integration</h5>
694694
<li class="list-group-item">
695695
<div class="info-tooltip">
696696
<button type="submit"
697-
formaction="/spring-ai-orchestration/inputFiltering"
697+
formaction="/spring-ai-orchestration/inputFiltering/ALLOW_SAFE"
698698
class="link-offset-2-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover endpoint">
699-
<code>/spring-ai-orchestration/inputFiltering</code>
699+
<code>/spring-ai-orchestration/inputFiltering/ALLOW_SAFE</code>
700700
</button>
701701
<div class="tooltip-content">
702-
Apply input filtering for a request to orchestration using the SpringAI integration.
702+
Apply strict input filtering for a request to orchestration using the SpringAI integration.
703703
</div>
704704
</div>
705705
</li>
706706
<li class="list-group-item">
707707
<div class="info-tooltip">
708708
<button type="submit"
709-
formaction="/spring-ai-orchestration/outputFiltering"
709+
formaction="/spring-ai-orchestration/outputFiltering/ALLOW_SAFE"
710710
class="link-offset-2-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover endpoint">
711-
<code>/spring-ai-orchestration/outputFiltering</code>
711+
<code>/spring-ai-orchestration/outputFiltering/ALLOW_SAFE</code>
712712
</button>
713713
<div class="tooltip-content">
714-
Apply output filtering for a request to orchestration using the SpringAI integration.
714+
Apply strict output filtering for a request to orchestration using the SpringAI integration.
715715
</div>
716716
</div>
717717
</li>

0 commit comments

Comments
 (0)