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 14 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,11 @@

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.LLMModuleResultSynchronous;
import com.sap.ai.sdk.orchestration.client.model.TokenUsage;
import java.util.List;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
Expand Down Expand Up @@ -38,4 +41,29 @@ public String getContent() throws OrchestrationClientException {
}
return choice.getMessage().getContent();
}

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

/**
* Get all messages.
*
* @return A list of all messages.
*/
@Nonnull
public List<ChatMessage> getAllMessages() {
Jonas-Isr marked this conversation as resolved.
Show resolved Hide resolved
final var allMessages = originalResponse.getModuleResults().getTemplating();

if (allMessages == null) {
return List.of();
}
return allMessages;
Jonas-Isr marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,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 @@ -155,7 +155,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 @@ -170,7 +170,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
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