Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Response Convenience #173

Merged
merged 19 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import static lombok.AccessLevel.PACKAGE;

import com.sap.ai.sdk.orchestration.client.model.ChatMessage;
import com.sap.ai.sdk.orchestration.client.model.CompletionPostResponse;
import com.sap.ai.sdk.orchestration.client.model.LLMChoice;
import com.sap.ai.sdk.orchestration.client.model.LLMModuleResultSynchronous;
import com.sap.ai.sdk.orchestration.client.model.TokenUsage;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
Expand All @@ -24,18 +30,47 @@ public class OrchestrationChatResponse {
*/
@Nonnull
public String getContent() throws OrchestrationClientException {
final var choices =
((LLMModuleResultSynchronous) originalResponse.getOrchestrationResult()).getChoices();

if (choices.isEmpty()) {
return "";
}

final var choice = choices.get(0);
final var choice = getCurrentChoice();

if ("content_filter".equals(choice.getFinishReason())) {
throw new OrchestrationClientException("Content filter filtered the output.");
}
return choice.getMessage().getContent();
}

/**
* Get the token usage.
*
* @return The token usage.
*/
@Nonnull
public TokenUsage getTokenUsage() {
return ((LLMModuleResultSynchronous) originalResponse.getOrchestrationResult()).getUsage();
}

/**
* Get all messages. This can be used for subsequent prompts as a message history.
*
* @return A list of all messages.
*/
@Nonnull
public List<ChatMessage> getAllMessages() {
Jonas-Isr marked this conversation as resolved.
Show resolved Hide resolved
final var items = Objects.requireNonNull(originalResponse.getModuleResults().getTemplating());
final var messages = new ArrayList<>(items);
messages.add(getCurrentChoice().getMessage());
return messages;
}

/**
* Get current choice.
*
* @return The current choice.
*/
@Nonnull
private LLMChoice getCurrentChoice() {
// We expect choices to be defined and never empty.
return ((LLMModuleResultSynchronous) originalResponse.getOrchestrationResult())
.getChoices()
.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ void testTemplating() throws IOException {

final var response = result.getOriginalResponse();
assertThat(response.getRequestId()).isEqualTo("26ea36b5-c196-4806-a9a6-a686f0c6ad91");
assertThat(response.getModuleResults().getTemplating().get(0).getContent())
assertThat(result.getAllMessages().get(0).getContent())
.isEqualTo("Reply with 'Orchestration Service is working!' in German");
assertThat(response.getModuleResults().getTemplating().get(0).getRole()).isEqualTo("user");
assertThat(result.getAllMessages().get(0).getRole()).isEqualTo("user");
var llm = (LLMModuleResultSynchronous) response.getModuleResults().getLlm();
assertThat(llm).isNotNull();
assertThat(llm.getId()).isEqualTo("chatcmpl-9lzPV4kLrXjFckOp2yY454wksWBoj");
Expand All @@ -144,7 +144,7 @@ void testTemplating() throws IOException {
.isEqualTo("Orchestration Service funktioniert!");
assertThat(choices.get(0).getMessage().getRole()).isEqualTo("assistant");
assertThat(choices.get(0).getFinishReason()).isEqualTo("stop");
var usage = llm.getUsage();
var usage = result.getTokenUsage();
assertThat(usage.getCompletionTokens()).isEqualTo(7);
assertThat(usage.getPromptTokens()).isEqualTo(19);
assertThat(usage.getTotalTokens()).isEqualTo(26);
Expand All @@ -159,7 +159,7 @@ void testTemplating() throws IOException {
.isEqualTo("Orchestration Service funktioniert!");
assertThat(choices.get(0).getMessage().getRole()).isEqualTo("assistant");
assertThat(choices.get(0).getFinishReason()).isEqualTo("stop");
usage = orchestrationResult.getUsage();
usage = result.getTokenUsage();
assertThat(usage.getCompletionTokens()).isEqualTo(7);
assertThat(usage.getPromptTokens()).isEqualTo(19);
assertThat(usage.getTotalTokens()).isEqualTo(26);
Expand Down Expand Up @@ -380,17 +380,4 @@ void testErrorHandling() {

softly.assertAll();
}

@Test
void testEmptyChoicesResponse() {
Jonas-Isr marked this conversation as resolved.
Show resolved Hide resolved
stubFor(
post(urlPathEqualTo("/v2/inference/deployments/abcdef0123456789/completion"))
.willReturn(
aResponse()
.withBodyFile("emptyChoicesResponse.json")
.withHeader("Content-Type", "application/json")));
final var result = client.chatCompletion(prompt, config);

assertThat(result.getContent()).isEmpty();
}
}
35 changes: 0 additions & 35 deletions orchestration/src/test/resources/__files/emptyChoicesResponse.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ void testTemplate() {
assertThat(controller.config.getLlmConfig()).isNotNull();
final var modelName = controller.config.getLlmConfig().getModelName();

final var response = controller.template();
final var result = response.getOriginalResponse();
final var result = controller.template();
final var response = result.getOriginalResponse();

assertThat(result.getRequestId()).isNotEmpty();
assertThat(result.getModuleResults().getTemplating().get(0).getContent())
assertThat(response.getRequestId()).isNotEmpty();
assertThat(result.getAllMessages().get(0).getContent())
.isEqualTo("Reply with 'Orchestration Service is working!' in German");
assertThat(result.getModuleResults().getTemplating().get(0).getRole()).isEqualTo("user");
var llm = (LLMModuleResultSynchronous) result.getModuleResults().getLlm();
assertThat(result.getAllMessages().get(0).getRole()).isEqualTo("user");
var llm = (LLMModuleResultSynchronous) response.getModuleResults().getLlm();
assertThat(llm.getId()).isNotEmpty();
assertThat(llm.getObject()).isEqualTo("chat.completion");
assertThat(llm.getCreated()).isGreaterThan(1);
Expand All @@ -54,12 +54,12 @@ void testTemplate() {
assertThat(choices.get(0).getMessage().getContent()).isNotEmpty();
assertThat(choices.get(0).getMessage().getRole()).isEqualTo("assistant");
assertThat(choices.get(0).getFinishReason()).isEqualTo("stop");
var usage = llm.getUsage();
var usage = result.getTokenUsage();
assertThat(usage.getCompletionTokens()).isGreaterThan(1);
assertThat(usage.getPromptTokens()).isGreaterThan(1);
assertThat(usage.getTotalTokens()).isGreaterThan(1);

var orchestrationResult = ((LLMModuleResultSynchronous) result.getOrchestrationResult());
var orchestrationResult = ((LLMModuleResultSynchronous) response.getOrchestrationResult());
assertThat(orchestrationResult.getObject()).isEqualTo("chat.completion");
assertThat(orchestrationResult.getCreated()).isGreaterThan(1);
assertThat(orchestrationResult.getModel()).isEqualTo(modelName);
Expand All @@ -68,7 +68,7 @@ void testTemplate() {
assertThat(choices.get(0).getMessage().getContent()).isNotEmpty();
assertThat(choices.get(0).getMessage().getRole()).isEqualTo("assistant");
assertThat(choices.get(0).getFinishReason()).isEqualTo("stop");
usage = orchestrationResult.getUsage();
usage = result.getTokenUsage();
assertThat(usage.getCompletionTokens()).isGreaterThan(1);
assertThat(usage.getPromptTokens()).isGreaterThan(1);
assertThat(usage.getTotalTokens()).isGreaterThan(1);
Expand Down