Skip to content

Commit 884fad5

Browse files
authored
feat: [Orchestration] Make getLlmChoice public (#204)
* feat: Orchestration Make getLlmChoice public * Revert filtering change
1 parent 5f4d5f8 commit 884fad5

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class OrchestrationChatResponse {
2929
*/
3030
@Nonnull
3131
public String getContent() throws OrchestrationClientException {
32-
final var choice = getCurrentChoice();
32+
final var choice = getChoice();
3333

3434
if ("content_filter".equals(choice.getFinishReason())) {
3535
throw new OrchestrationClientException("Content filter filtered the output.");
@@ -67,17 +67,17 @@ public List<Message> getAllMessages() {
6767
messages.add(message);
6868
}
6969

70-
messages.add(new AssistantMessage(getCurrentChoice().getMessage().getContent()));
70+
messages.add(new AssistantMessage(getChoice().getMessage().getContent()));
7171
return messages;
7272
}
7373

7474
/**
75-
* Get current choice.
75+
* Get the LLM response. Useful for accessing the finish reason or further data like logprobs.
7676
*
77-
* @return The current choice.
77+
* @return The (first, in case of multiple) {@link LLMChoice}.
7878
*/
7979
@Nonnull
80-
private LLMChoice getCurrentChoice() {
80+
public LLMChoice getChoice() {
8181
// We expect choices to be defined and never empty.
8282
return ((LLMModuleResultSynchronous) originalResponse.getOrchestrationResult())
8383
.getChoices()

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
import java.util.List;
1717
import java.util.Map;
1818
import javax.annotation.Nonnull;
19+
import lombok.extern.slf4j.Slf4j;
1920
import org.springframework.web.bind.annotation.GetMapping;
2021
import org.springframework.web.bind.annotation.PathVariable;
2122
import org.springframework.web.bind.annotation.RequestMapping;
2223
import org.springframework.web.bind.annotation.RestController;
2324

2425
/** Endpoints for the Orchestration service */
2526
@RestController
27+
@Slf4j
2628
@RequestMapping("/orchestration")
2729
class OrchestrationController {
2830
private final OrchestrationClient client = new OrchestrationClient();
@@ -39,7 +41,11 @@ class OrchestrationController {
3941
OrchestrationChatResponse completion() {
4042
final var prompt = new OrchestrationPrompt("Hello world! Why is this phrase so famous?");
4143

42-
return client.chatCompletion(prompt, config);
44+
final var result = client.chatCompletion(prompt, config);
45+
46+
log.info("Our trusty AI answered with: {}", result.getContent());
47+
48+
return result;
4349
}
4450

4551
/**

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.getChoice()).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)