Skip to content

Commit 9d9881e

Browse files
committed
feat: Orchestration Make getLlmChoice public
1 parent cd821c6 commit 9d9881e

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public List<ChatMessage> getAllMessages() {
6262
}
6363

6464
/**
65-
* Get current choice.
65+
* Get the LLM response. Useful for accessing the finish reason or further data like logprobs.
6666
*
67-
* @return The current choice.
67+
* @return The (first, in case of multiple) {@link LLMChoice}.
6868
*/
6969
@Nonnull
70-
private LLMChoice getCurrentChoice() {
70+
public LLMChoice getCurrentChoice() {
7171
// We expect choices to be defined and never empty.
7272
return ((LLMModuleResultSynchronous) originalResponse.getOrchestrationResult())
7373
.getChoices()

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.sap.ai.sdk.orchestration.Message;
1111
import com.sap.ai.sdk.orchestration.OrchestrationChatResponse;
1212
import com.sap.ai.sdk.orchestration.OrchestrationClient;
13+
import com.sap.ai.sdk.orchestration.OrchestrationClientException;
1314
import com.sap.ai.sdk.orchestration.OrchestrationModuleConfig;
1415
import com.sap.ai.sdk.orchestration.OrchestrationPrompt;
1516
import com.sap.ai.sdk.orchestration.SystemMessage;
@@ -19,13 +20,15 @@
1920
import java.util.List;
2021
import java.util.Map;
2122
import javax.annotation.Nonnull;
23+
import lombok.extern.slf4j.Slf4j;
2224
import org.springframework.web.bind.annotation.GetMapping;
2325
import org.springframework.web.bind.annotation.PathVariable;
2426
import org.springframework.web.bind.annotation.RequestMapping;
2527
import org.springframework.web.bind.annotation.RestController;
2628

2729
/** Endpoints for the Orchestration service */
2830
@RestController
31+
@Slf4j
2932
@RequestMapping("/orchestration")
3033
class OrchestrationController {
3134
private final OrchestrationClient client = new OrchestrationClient();
@@ -42,7 +45,11 @@ class OrchestrationController {
4245
public OrchestrationChatResponse completion() {
4346
final var prompt = new OrchestrationPrompt("Hello world! Why is this phrase so famous?");
4447

45-
return client.chatCompletion(prompt, config);
48+
final var result = client.chatCompletion(prompt, config);
49+
50+
log.info("Our trusty AI answered with: {}", result.getContent());
51+
52+
return result;
4653
}
4754

4855
/**
@@ -106,7 +113,16 @@ public OrchestrationChatResponse filter(
106113
final var configWithFilter =
107114
config.withInputFiltering(filterConfig).withOutputFiltering(filterConfig);
108115

109-
return client.chatCompletion(prompt, configWithFilter);
116+
final OrchestrationChatResponse result = client.chatCompletion(prompt, configWithFilter);
117+
try {
118+
// in case the output was filtered, calling .getContent() will throw
119+
log.info("The following AI response passed the output filter: {}", result.getContent());
120+
} catch (OrchestrationClientException e) {
121+
log.info("The content filter blocked the output.");
122+
// alternatively to calling .getContent(), you can also check the finish reason manually
123+
log.info("Finish reason: {}", result.getCurrentChoice().getFinishReason());
124+
}
125+
return result;
110126
}
111127

112128
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void testTemplate() {
6767
assertThat(choices.get(0).getMessage().getContent()).isNotEmpty();
6868
assertThat(choices.get(0).getMessage().getRole()).isEqualTo("assistant");
6969
assertThat(choices.get(0).getFinishReason()).isEqualTo("stop");
70+
assertThat(result.getCurrentChoice()).isSameAs(choices.get(0));
7071
usage = result.getTokenUsage();
7172
assertThat(usage.getCompletionTokens()).isGreaterThan(1);
7273
assertThat(usage.getPromptTokens()).isGreaterThan(1);

0 commit comments

Comments
 (0)