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

Added Response Convenience #157

Merged
merged 10 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions docs/guides/ORCHESTRATION_CHAT_COMPLETION.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ var prompt = new OrchestrationPrompt("Hello world! Why is this phrase so famous?

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

String messageResult =
result.getOrchestrationResult().getChoices().get(0).getMessage().getContent();
String messageResult = result.getContent();
```

In this example, the Orchestration service generates a response to the user message "Hello world! Why is this phrase so famous?".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.sap.ai.sdk.core.AiCoreDeployment;
import com.sap.ai.sdk.core.AiCoreService;
import com.sap.ai.sdk.orchestration.client.model.CompletionPostRequest;
import com.sap.ai.sdk.orchestration.client.model.CompletionPostResponse;
import com.sap.ai.sdk.orchestration.client.model.ModuleConfigs;
import com.sap.ai.sdk.orchestration.client.model.OrchestrationConfig;
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Accessor;
Expand Down Expand Up @@ -75,7 +74,7 @@ public OrchestrationClient(@Nonnull final AiCoreDeployment deployment) {
* @throws OrchestrationClientException if the request fails.
*/
@Nonnull
public CompletionPostResponse chatCompletion(
public OrchestrationResponse chatCompletion(
@Nonnull final OrchestrationPrompt prompt, @Nonnull final OrchestrationModuleConfig config)
throws OrchestrationClientException {

Expand Down Expand Up @@ -104,7 +103,7 @@ public CompletionPostResponse chatCompletion(
* @throws OrchestrationClientException If the request fails.
*/
@Nonnull
public CompletionPostResponse executeRequest(@Nonnull final CompletionPostRequest request)
public OrchestrationResponse executeRequest(@Nonnull final CompletionPostRequest request)
throws OrchestrationClientException {
final BasicClassicHttpRequest postRequest = new HttpPost("/completion");
try {
Expand Down Expand Up @@ -133,13 +132,13 @@ public static CompletionPostRequest toCompletionPostRequest(
}

@Nonnull
CompletionPostResponse executeRequest(@Nonnull final BasicClassicHttpRequest request) {
OrchestrationResponse executeRequest(@Nonnull final BasicClassicHttpRequest request) {
try {
val destination = deployment.get().destination();
log.debug("Using destination {} to connect to orchestration service", destination);
val client = ApacheHttpClient5Accessor.getHttpClient(destination);
return client.execute(
request, new OrchestrationResponseHandler<>(CompletionPostResponse.class));
request, new OrchestrationResponseHandler<>(OrchestrationResponse.class));
} catch (NoSuchElementException
| DestinationAccessException
| DestinationNotFoundException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.sap.ai.sdk.orchestration;

import com.sap.ai.sdk.orchestration.client.model.CompletionPostResponse;
import javax.annotation.Nonnull;

/** Orchestration chat completion output. */
public class OrchestrationResponse extends CompletionPostResponse {
Jonas-Isr marked this conversation as resolved.
Show resolved Hide resolved
/**
* Get the message content from the output.
*
* <p>Note: If there are multiple choices only the first one is returned
*
* @return the message content or empty string.
* @throws OrchestrationClientException if the content filter filtered the output.
Jonas-Isr marked this conversation as resolved.
Show resolved Hide resolved
*/
@Nonnull
public String getContent() throws OrchestrationClientException {
if (getOrchestrationResult().getChoices().isEmpty()) {
Jonas-Isr marked this conversation as resolved.
Show resolved Hide resolved
return "";
}
if ("content_filter".equals(getOrchestrationResult().getChoices().get(0).getFinishReason())) {
throw new OrchestrationClientException("Content filter filtered the output.");
}
return getOrchestrationResult().getChoices().get(0).getMessage().getContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ void testCompletion() {
final var result = client.chatCompletion(prompt, config);

assertThat(result).isNotNull();
assertThat(result.getOrchestrationResult().getChoices().get(0).getMessage().getContent())
.isNotEmpty();
assertThat(result.getContent()).isNotEmpty();
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<enforcer.skipBanGeneratedModulesReference>false</enforcer.skipBanGeneratedModulesReference>
<!-- Test coverage -->
<coverage.instruction>74%</coverage.instruction>
<coverage.branch>62%</coverage.branch>
<coverage.branch>60%</coverage.branch>
Jonas-Isr marked this conversation as resolved.
Show resolved Hide resolved
<coverage.complexity>67%</coverage.complexity>
<coverage.line>75%</coverage.line>
<coverage.method>80%</coverage.method>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.sap.ai.sdk.orchestration.OrchestrationClient;
import com.sap.ai.sdk.orchestration.OrchestrationModuleConfig;
import com.sap.ai.sdk.orchestration.OrchestrationPrompt;
import com.sap.ai.sdk.orchestration.OrchestrationResponse;
import com.sap.ai.sdk.orchestration.client.model.AzureContentSafety;
import com.sap.ai.sdk.orchestration.client.model.AzureThreshold;
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.DPIEntities;
import com.sap.ai.sdk.orchestration.client.model.DPIEntityConfig;
import com.sap.ai.sdk.orchestration.client.model.FilterConfig;
Expand Down Expand Up @@ -44,7 +44,7 @@ class OrchestrationController {
*/
@GetMapping("/completion")
@Nonnull
public CompletionPostResponse completion() {
public OrchestrationResponse completion() {
final var prompt = new OrchestrationPrompt("Hello world! Why is this phrase so famous?");

return client.chatCompletion(prompt, config);
Expand All @@ -57,7 +57,7 @@ public CompletionPostResponse completion() {
*/
@GetMapping("/template")
@Nonnull
public CompletionPostResponse template() {
public OrchestrationResponse template() {
final var template =
ChatMessage.create()
.role("user")
Expand All @@ -78,7 +78,7 @@ public CompletionPostResponse template() {
*/
@GetMapping("/messagesHistory")
@Nonnull
public CompletionPostResponse messagesHistory() {
public OrchestrationResponse messagesHistory() {
final List<ChatMessage> messagesHistory =
List.of(
ChatMessage.create().role("user").content("What is the capital of France?"),
Expand All @@ -99,7 +99,7 @@ public CompletionPostResponse messagesHistory() {
*/
@GetMapping("/filter/{threshold}")
@Nonnull
public CompletionPostResponse filter(
public OrchestrationResponse filter(
@Nonnull @PathVariable("threshold") final AzureThreshold threshold) {
final var prompt =
new OrchestrationPrompt(
Expand Down Expand Up @@ -146,7 +146,7 @@ private static FilteringModuleConfig createAzureContentFilter(
*/
@GetMapping("/maskingAnonymization")
@Nonnull
public CompletionPostResponse maskingAnonymization() {
public OrchestrationResponse maskingAnonymization() {
final var systemMessage =
ChatMessage.create()
.role("system")
Expand Down Expand Up @@ -177,7 +177,7 @@ public CompletionPostResponse maskingAnonymization() {
*/
@GetMapping("/maskingPseudonymization")
@Nonnull
public CompletionPostResponse maskingPseudonymization() {
public OrchestrationResponse maskingPseudonymization() {
final var systemMessage =
ChatMessage.create()
.role("system")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ void testCompletion() {
final var result = controller.completion();

assertThat(result).isNotNull();
assertThat(result.getOrchestrationResult().getChoices().get(0).getMessage().getContent())
.isNotEmpty();
assertThat(result.getContent()).isNotEmpty();
}

@Test
Expand Down