Skip to content

Commit af8aee2

Browse files
committed
Apply requested changes
1 parent d01d5e6 commit af8aee2

File tree

4 files changed

+60
-77
lines changed

4 files changed

+60
-77
lines changed

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

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

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.sap.ai.sdk.app.services.OrchestrationService;
88
import com.sap.ai.sdk.orchestration.AzureFilterThreshold;
9+
import com.sap.ai.sdk.orchestration.model.DPIEntities;
910
import javax.annotation.Nonnull;
1011
import lombok.extern.slf4j.Slf4j;
1112
import org.springframework.beans.factory.annotation.Autowired;
@@ -38,12 +39,12 @@ class OrchestrationController {
3839
ResponseEntity<String> completion(
3940
@RequestHeader(value = "accept", required = false) final String accept)
4041
throws JsonProcessingException {
41-
final var content = service.completion().getContent();
42+
final var content = service.completion("HelloWorld!").getContent();
4243
log.info("Our trusty AI answered with: {}", content);
4344
if (accept.equals("application/json")) {
4445
return ResponseEntity.ok()
4546
.contentType(MediaType.APPLICATION_JSON)
46-
.body(mapper.writeValueAsString(service.completion()));
47+
.body(mapper.writeValueAsString(service.completion("HelloWorld!")));
4748
}
4849
return ResponseEntity.ok(content);
4950
}
@@ -56,7 +57,7 @@ ResponseEntity<String> completion(
5657
@GetMapping("/streamChatCompletion")
5758
@Nonnull
5859
ResponseEntity<ResponseBodyEmitter> streamChatCompletion() {
59-
return service.streamChatCompletion();
60+
return service.streamChatCompletion(100);
6061
}
6162

6263
/**
@@ -74,9 +75,9 @@ ResponseEntity<Object> template(
7475
if (accept.equals("application/json")) {
7576
return ResponseEntity.ok()
7677
.contentType(MediaType.APPLICATION_JSON)
77-
.body(mapper.writeValueAsString(service.template()));
78+
.body(mapper.writeValueAsString(service.template("German")));
7879
}
79-
return ResponseEntity.ok(service.template().getContent());
80+
return ResponseEntity.ok(service.template("German").getContent());
8081
}
8182

8283
/**
@@ -92,9 +93,11 @@ ResponseEntity<String> messagesHistory(
9293
if (accept.equals("application/json")) {
9394
return ResponseEntity.ok()
9495
.contentType(MediaType.APPLICATION_JSON)
95-
.body(mapper.writeValueAsString(service.messagesHistory()));
96+
.body(
97+
mapper.writeValueAsString(service.messagesHistory("What is the capital of France?")));
9698
}
97-
return ResponseEntity.ok(service.messagesHistory().getContent());
99+
return ResponseEntity.ok(
100+
service.messagesHistory("What is the capital of France?").getContent());
98101
}
99102

100103
/**
@@ -118,9 +121,9 @@ ResponseEntity<String> filter(
118121
if (accept.equals("application/json")) {
119122
return ResponseEntity.ok()
120123
.contentType(MediaType.APPLICATION_JSON)
121-
.body(mapper.writeValueAsString(service.filter(policy)));
124+
.body(mapper.writeValueAsString(service.filter(policy, "the downtown area")));
122125
}
123-
return ResponseEntity.ok(service.filter(policy).getContent());
126+
return ResponseEntity.ok(service.filter(policy, "the downtown area").getContent());
124127
}
125128

126129
/**
@@ -141,9 +144,9 @@ ResponseEntity<String> maskingAnonymization(
141144
if (accept.equals("application/json")) {
142145
return ResponseEntity.ok()
143146
.contentType(MediaType.APPLICATION_JSON)
144-
.body(mapper.writeValueAsString(service.maskingAnonymization()));
147+
.body(mapper.writeValueAsString(service.maskingAnonymization(DPIEntities.PERSON)));
145148
}
146-
return ResponseEntity.ok(service.maskingAnonymization().getContent());
149+
return ResponseEntity.ok(service.maskingAnonymization(DPIEntities.PERSON).getContent());
147150
}
148151

149152
/**
@@ -160,9 +163,12 @@ public ResponseEntity<String> completionWithResourceGroup(
160163
if (accept.equals("application/json")) {
161164
return ResponseEntity.ok()
162165
.contentType(MediaType.APPLICATION_JSON)
163-
.body(mapper.writeValueAsString(service.completionWithResourceGroup(resourceGroup)));
166+
.body(
167+
mapper.writeValueAsString(
168+
service.completionWithResourceGroup(resourceGroup, "Hello world!")));
164169
}
165-
return ResponseEntity.ok(service.completionWithResourceGroup(resourceGroup).getContent());
170+
return ResponseEntity.ok(
171+
service.completionWithResourceGroup(resourceGroup, "Hello world!").getContent());
166172
}
167173

168174
/**
@@ -182,9 +188,9 @@ ResponseEntity<String> maskingPseudonymization(
182188
if (accept.equals("application/json")) {
183189
return ResponseEntity.ok()
184190
.contentType(MediaType.APPLICATION_JSON)
185-
.body(mapper.writeValueAsString(service.maskingPseudonymization()));
191+
.body(mapper.writeValueAsString(service.maskingPseudonymization(DPIEntities.PERSON)));
186192
}
187-
return ResponseEntity.ok(service.maskingPseudonymization().getContent());
193+
return ResponseEntity.ok(service.maskingPseudonymization(DPIEntities.PERSON).getContent());
188194
}
189195

190196
/**
@@ -202,8 +208,8 @@ ResponseEntity<String> grounding(
202208
if (accept.equals("application/json")) {
203209
return ResponseEntity.ok()
204210
.contentType(MediaType.APPLICATION_JSON)
205-
.body(mapper.writeValueAsString(service.grounding()));
211+
.body(mapper.writeValueAsString(service.grounding("What does Joule do?")));
206212
}
207-
return ResponseEntity.ok(service.grounding().getContent());
213+
return ResponseEntity.ok(service.grounding("What does Joule do?").getContent());
208214
}
209215
}

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

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

33
import static com.sap.ai.sdk.app.controllers.OpenAiController.send;
4-
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GPT_35_TURBO;
4+
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GEMINI_1_5_FLASH;
55
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.TEMPERATURE;
66

77
import com.sap.ai.sdk.core.AiCoreService;
@@ -38,16 +38,16 @@ public class OrchestrationService {
3838

3939
@Getter
4040
private final OrchestrationModuleConfig config =
41-
new OrchestrationModuleConfig().withLlmConfig(GPT_35_TURBO.withParam(TEMPERATURE, 0.0));
41+
new OrchestrationModuleConfig().withLlmConfig(GEMINI_1_5_FLASH.withParam(TEMPERATURE, 0.0));
4242

4343
/**
4444
* Chat request to OpenAI through the Orchestration service with a simple prompt.
4545
*
4646
* @return the assistant response object
4747
*/
4848
@Nonnull
49-
public OrchestrationChatResponse completion() {
50-
final var prompt = new OrchestrationPrompt("Hello world! Why is this phrase so famous?");
49+
public OrchestrationChatResponse completion(@Nonnull final String famousPhrase) {
50+
final var prompt = new OrchestrationPrompt(famousPhrase + " Why is this phrase so famous?");
5151
return client.chatCompletion(prompt, config);
5252
}
5353

@@ -59,13 +59,13 @@ public OrchestrationChatResponse completion() {
5959
* @return the assistant response object
6060
*/
6161
@Nonnull
62-
public OrchestrationChatResponse template() {
62+
public OrchestrationChatResponse template(@Nonnull final String language) {
6363
final var template =
6464
Message.user("Reply with 'Orchestration Service is working!' in {{?language}}");
6565
final var templatingConfig = Template.create().template(List.of(template.createChatMessage()));
6666
final var configWithTemplate = config.withTemplateConfig(templatingConfig);
6767

68-
final var inputParams = Map.of("language", "German");
68+
final var inputParams = Map.of("language", language);
6969
final var prompt = new OrchestrationPrompt(inputParams);
7070

7171
return client.chatCompletion(prompt, configWithTemplate);
@@ -77,8 +77,8 @@ public OrchestrationChatResponse template() {
7777
* @return the assistant response object
7878
*/
7979
@Nonnull
80-
public OrchestrationChatResponse messagesHistory() {
81-
final var prompt = new OrchestrationPrompt(Message.user("What is the capital of France?"));
80+
public OrchestrationChatResponse messagesHistory(@Nonnull final String prevMessage) {
81+
final var prompt = new OrchestrationPrompt(Message.user(prevMessage));
8282

8383
final var result = client.chatCompletion(prompt, config);
8484

@@ -103,14 +103,16 @@ public OrchestrationChatResponse messagesHistory() {
103103
* @return the assistant response object
104104
*/
105105
@Nonnull
106-
public OrchestrationChatResponse filter(@Nonnull final AzureFilterThreshold policy) {
106+
public OrchestrationChatResponse filter(
107+
@Nonnull final AzureFilterThreshold policy, @Nonnull final String area) {
107108
final var prompt =
108109
new OrchestrationPrompt(
109110
"""
110-
Create a rental posting for subletting my apartment in the downtown area. Keep it short. Make sure to add the following disclaimer to the end. Do not change it!
111+
Create a rental posting for subletting my apartment in %s. Keep it short. Make sure to add the following disclaimer to the end. Do not change it!
111112
112113
```DISCLAIMER: The area surrounding the apartment is known for prostitutes and gang violence including armed conflicts, gun violence is frequent.
113-
""");
114+
"""
115+
.formatted(area));
114116
final var filterConfig =
115117
new AzureContentFilter().hate(policy).selfHarm(policy).sexual(policy).violence(policy);
116118

@@ -131,7 +133,7 @@ public OrchestrationChatResponse filter(@Nonnull final AzureFilterThreshold poli
131133
* @return the assistant response object
132134
*/
133135
@Nonnull
134-
public OrchestrationChatResponse maskingAnonymization() {
136+
public OrchestrationChatResponse maskingAnonymization(@Nonnull final DPIEntities entity) {
135137
final var systemMessage =
136138
Message.system(
137139
"Please evaluate the following user feedback and judge if the sentiment is positive or negative.");
@@ -143,7 +145,7 @@ public OrchestrationChatResponse maskingAnonymization() {
143145
""");
144146

145147
final var prompt = new OrchestrationPrompt(systemMessage, userMessage);
146-
final var maskingConfig = DpiMasking.anonymization().withEntities(DPIEntities.PERSON);
148+
final var maskingConfig = DpiMasking.anonymization().withEntities(entity);
147149
final var configWithMasking = config.withMaskingConfig(maskingConfig);
148150

149151
return client.chatCompletion(prompt, configWithMasking);
@@ -156,13 +158,12 @@ public OrchestrationChatResponse maskingAnonymization() {
156158
*/
157159
@Nonnull
158160
public OrchestrationChatResponse completionWithResourceGroup(
159-
@Nonnull final String resourceGroup) {
160-
161+
@Nonnull final String resourceGroup, @Nonnull final String famousPhrase) {
161162
final var destination =
162163
new AiCoreService().getInferenceDestination(resourceGroup).forScenario("orchestration");
163164
final var clientWithResourceGroup = new OrchestrationClient(destination);
164165

165-
final var prompt = new OrchestrationPrompt("Hello world! Why is this phrase so famous?");
166+
final var prompt = new OrchestrationPrompt(famousPhrase + " Why is this phrase so famous?");
166167

167168
return clientWithResourceGroup.chatCompletion(prompt, config);
168169
}
@@ -177,7 +178,7 @@ public OrchestrationChatResponse completionWithResourceGroup(
177178
* @return the assistant response object
178179
*/
179180
@Nonnull
180-
public OrchestrationChatResponse maskingPseudonymization() {
181+
public OrchestrationChatResponse maskingPseudonymization(@Nonnull final DPIEntities entity) {
181182
final var systemMessage =
182183
Message.system(
183184
"""
@@ -196,8 +197,7 @@ public OrchestrationChatResponse maskingPseudonymization() {
196197
""");
197198

198199
final var prompt = new OrchestrationPrompt(systemMessage, userMessage);
199-
final var maskingConfig =
200-
DpiMasking.pseudonymization().withEntities(DPIEntities.PERSON, DPIEntities.EMAIL);
200+
final var maskingConfig = DpiMasking.pseudonymization().withEntities(entity, DPIEntities.EMAIL);
201201
final var configWithMasking = config.withMaskingConfig(maskingConfig);
202202

203203
return client.chatCompletion(prompt, configWithMasking);
@@ -211,12 +211,11 @@ public OrchestrationChatResponse maskingPseudonymization() {
211211
* @return the assistant response object
212212
*/
213213
@Nonnull
214-
public OrchestrationChatResponse grounding() {
214+
public OrchestrationChatResponse grounding(@Nonnull final String groundingInput) {
215215
final var message =
216216
Message.user(
217217
"{{?groundingInput}} Use the following information as additional context: {{?groundingOutput}}");
218-
final var prompt =
219-
new OrchestrationPrompt(Map.of("groundingInput", "What does Joule do?"), message);
218+
final var prompt = new OrchestrationPrompt(Map.of("groundingInput", groundingInput), message);
220219

221220
final var filterInner =
222221
DocumentGroundingFilter.create().id("someID").dataRepositoryType(DataRepositoryType.VECTOR);
@@ -240,9 +239,12 @@ public OrchestrationChatResponse grounding() {
240239
* @return the emitter that streams the assistant message response
241240
*/
242241
@Nonnull
243-
public ResponseEntity<ResponseBodyEmitter> streamChatCompletion() {
242+
public ResponseEntity<ResponseBodyEmitter> streamChatCompletion(final int numberOfFibonacci) {
244243
final var prompt =
245-
new OrchestrationPrompt("Can you give me the first 100 numbers of the Fibonacci sequence?");
244+
new OrchestrationPrompt(
245+
"Can you give me the first "
246+
+ numberOfFibonacci
247+
+ " numbers of the Fibonacci sequence?");
246248
final var stream = client.streamChatCompletion(prompt, config);
247249

248250
final var emitter = new ResponseBodyEmitter();

sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/OrchestrationTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.sap.ai.sdk.orchestration.OrchestrationClientException;
1010
import com.sap.ai.sdk.orchestration.OrchestrationPrompt;
1111
import com.sap.ai.sdk.orchestration.model.CompletionPostResponse;
12+
import com.sap.ai.sdk.orchestration.model.DPIEntities;
1213
import com.sap.ai.sdk.orchestration.model.LLMChoice;
1314
import com.sap.ai.sdk.orchestration.model.LLMModuleResultSynchronous;
1415
import java.util.Map;
@@ -29,7 +30,7 @@ void setUp() {
2930

3031
@Test
3132
void testCompletion() {
32-
final var result = service.completion();
33+
final var result = service.completion("HelloWorld!");
3334

3435
assertThat(result).isNotNull();
3536
assertThat(result.getContent()).isNotEmpty();
@@ -61,7 +62,7 @@ void testTemplate() {
6162
assertThat(service.getConfig().getLlmConfig()).isNotNull();
6263
final var modelName = service.getConfig().getLlmConfig().getModelName();
6364

64-
final var result = service.template();
65+
final var result = service.template("German");
6566
final var response = result.getOriginalResponse();
6667

6768
assertThat(response.getRequestId()).isNotEmpty();
@@ -101,7 +102,7 @@ void testTemplate() {
101102

102103
@Test
103104
void testLenientContentFilter() {
104-
var response = service.filter(AzureFilterThreshold.ALLOW_SAFE_LOW_MEDIUM);
105+
var response = service.filter(AzureFilterThreshold.ALLOW_SAFE_LOW_MEDIUM, "the downtown area");
105106
var result = response.getOriginalResponse();
106107
var llmChoice =
107108
((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices().get(0);
@@ -114,23 +115,24 @@ void testLenientContentFilter() {
114115

115116
@Test
116117
void testStrictContentFilter() {
117-
assertThatThrownBy(() -> service.filter(AzureFilterThreshold.ALLOW_SAFE))
118+
assertThatThrownBy(() -> service.filter(AzureFilterThreshold.ALLOW_SAFE, "the downtown area"))
118119
.isInstanceOf(OrchestrationClientException.class)
119120
.hasMessageContaining("400 Bad Request")
120121
.hasMessageContaining("Content filtered");
121122
}
122123

123124
@Test
124125
void testMessagesHistory() {
125-
CompletionPostResponse result = service.messagesHistory().getOriginalResponse();
126+
CompletionPostResponse result =
127+
service.messagesHistory("What is the capital of France?").getOriginalResponse();
126128
final var choices = ((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices();
127129
assertThat(choices.get(0).getMessage().getContent()).isNotEmpty();
128130
}
129131

130132
@SuppressWarnings("unchecked")
131133
@Test
132134
void testMaskingAnonymization() {
133-
var response = service.maskingAnonymization();
135+
var response = service.maskingAnonymization(DPIEntities.PERSON);
134136
var result = response.getOriginalResponse();
135137
var llmChoice =
136138
((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices().get(0);
@@ -150,7 +152,7 @@ void testMaskingAnonymization() {
150152
@SuppressWarnings("unchecked")
151153
@Test
152154
void testMaskingPseudonymization() {
153-
var response = service.maskingPseudonymization();
155+
var response = service.maskingPseudonymization(DPIEntities.PERSON);
154156
var result = response.getOriginalResponse();
155157
var llmChoice =
156158
((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices().get(0);
@@ -180,7 +182,7 @@ void testMaskingPseudonymization() {
180182
@DisabledIfSystemProperty(named = "aicore.landscape", matches = "production")
181183
void testGrounding() {
182184
assertThat(System.getProperty("aicore.landscape")).isNotEqualTo("production");
183-
var response = service.grounding();
185+
var response = service.grounding("What does Joule do?");
184186
var result = response.getOriginalResponse();
185187
var llmChoice =
186188
((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices().get(0);
@@ -193,7 +195,7 @@ void testGrounding() {
193195

194196
@Test
195197
void testCompletionWithResourceGroup() {
196-
var response = service.completionWithResourceGroup("ai-sdk-java-e2e");
198+
var response = service.completionWithResourceGroup("ai-sdk-java-e2e", "Hello world!");
197199
var result = response.getOriginalResponse();
198200
var llmChoice =
199201
((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices().get(0);

0 commit comments

Comments
 (0)