Skip to content

Commit

Permalink
E2E Different Resource Group (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanackal authored Dec 3, 2024
1 parent 81b5036 commit 8527ee6
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ public OpenAiClient withApiVersion(@Nonnull final String apiVersion) {
@Beta
@Nonnull
public static OpenAiClient withCustomDestination(@Nonnull final Destination destination) {
return new OpenAiClient(destination);
final OpenAiClient client = new OpenAiClient(destination);

if (destination.get("URL.queries.api-version").isDefined()) {
return client;
}

return client.withApiVersion(DEFAULT_API_VERSION);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ void apiVersion() {
stubFor(post(anyUrl()).willReturn(okJson("{}")));
Try.of(() -> client.chatCompletion(new OpenAiChatCompletionParameters()));

verify(exactly(1), postRequestedFor(anyUrl()).withoutQueryParam("api-version"));
verify(
exactly(1),
postRequestedFor(anyUrl()).withQueryParam("api-version", equalTo("2024-02-01")));

Try.of(
() -> client.withApiVersion("fooBar").chatCompletion(new OpenAiChatCompletionParameters()));
Expand All @@ -85,7 +87,9 @@ void apiVersion() {
"withApiVersion should return a new object, the sut object should remain unchanged")
.isNotSameAs(client.withApiVersion("fooBar"));
Try.of(() -> client.chatCompletion(new OpenAiChatCompletionParameters()));
verify(exactly(2), postRequestedFor(anyUrl()).withoutQueryParam("api-version"));
verify(
exactly(2),
postRequestedFor(anyUrl()).withQueryParam("api-version", equalTo("2024-02-01")));
}

private static Runnable[] errorHandlingCalls() {
Expand Down Expand Up @@ -197,7 +201,11 @@ void chatCompletion(@Nonnull final Callable<OpenAiChatCompletionOutput> request)
try (var inputStream = fileLoader.apply("__files/chatCompletionResponse.json")) {

final String response = new String(inputStream.readAllBytes());
stubFor(post("/chat/completions").willReturn(okJson(response)));
// with query parameter api-version=2024-02-01
stubFor(
post(urlPathEqualTo("/chat/completions"))
.withQueryParam("api-version", equalTo("2024-02-01"))
.willReturn(okJson(response)));

final OpenAiChatCompletionOutput result = request.call();

Expand Down Expand Up @@ -264,6 +272,7 @@ void chatCompletion(@Nonnull final Callable<OpenAiChatCompletionOutput> request)

verify(
postRequestedFor(urlPathEqualTo("/chat/completions"))
.withQueryParam("api-version", equalTo("2024-02-01"))
.withRequestBody(
equalToJson(
"""
Expand Down
4 changes: 4 additions & 0 deletions sample-code/spring-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sap.cloud.sdk.cloudplatform</groupId>
<artifactId>cloudplatform-connectivity</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sap.ai.sdk.core.AiCoreService;
import com.sap.ai.sdk.foundationmodels.openai.OpenAiClient;
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionFunction;
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionOutput;
Expand All @@ -26,6 +27,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

Expand Down Expand Up @@ -189,4 +191,22 @@ OpenAiEmbeddingOutput embedding() {

return OpenAiClient.forModel(TEXT_EMBEDDING_ADA_002).embedding(request);
}

/**
* Chat request to OpenAI filtering by resource group
*
* @param resourceGroup The resource group to use
* @return the assistant message response
*/
@GetMapping("/chatCompletion/{resourceGroup}")
@Nonnull
public static OpenAiChatCompletionOutput chatCompletionWithResource(
@Nonnull @PathVariable("resourceGroup") final String resourceGroup) {

final var destination =
new AiCoreService().getInferenceDestination(resourceGroup).forModel(GPT_4O);

return OpenAiClient.withCustomDestination(destination)
.chatCompletion("Where is the nearest coffee shop?");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GPT_35_TURBO;
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.TEMPERATURE;

import com.sap.ai.sdk.core.AiCoreService;
import com.sap.ai.sdk.orchestration.AzureContentFilter;
import com.sap.ai.sdk.orchestration.AzureFilterThreshold;
import com.sap.ai.sdk.orchestration.DpiMasking;
Expand Down Expand Up @@ -151,6 +152,25 @@ OrchestrationChatResponse maskingAnonymization() {
return client.chatCompletion(prompt, configWithMasking);
}

/**
* Chat request to OpenAI through the Orchestration deployment under a specific resource group.
*
* @return the result object
*/
@GetMapping("/completion/{resourceGroup}")
@Nonnull
public OrchestrationChatResponse completionWithResourceGroup(
@PathVariable("resourceGroup") @Nonnull final String resourceGroup) {

final var destination =
new AiCoreService().getInferenceDestination(resourceGroup).forScenario("orchestration");
final var clientWithResourceGroup = new OrchestrationClient(destination);

final var prompt = new OrchestrationPrompt("Hello world! Why is this phrase so famous?");

return clientWithResourceGroup.chatCompletion(prompt, config);
}

/**
* Let the orchestration service a response to a hypothetical user who provided feedback on the AI
* SDK. Pseudonymize the user's name and location to protect their privacy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ <h2>Endpoints</h2>
<li><a href="/chatCompletionTool">/chatCompletionTool</a></li>
<li><a href="/chatCompletionImage">/chatCompletionImage</a></li>
<li><a href="/embedding">/embedding</a></li>
<li><a href="/chatCompletion/ai-sdk-java-e2e">/chatCompletion/resourceGroup</a></li>
</ul>
</ul>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,13 @@ void embedding() {
assertThat(embedding.getModel()).isEqualTo("ada");
assertThat(embedding.getObject()).isEqualTo("list");
}

@Test
void chatCompletionWithResource() {
final var completion = OpenAiController.chatCompletionWithResource("ai-sdk-java-e2e");

final var message = completion.getChoices().get(0).getMessage();
assertThat(message.getRole()).isEqualTo("assistant");
assertThat(message.getContent()).isNotEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,14 @@ void testGrounding() {
// Placeholder for grounding test
assertThat(System.getProperty("aicore.landscape")).isNotEqualTo("production");
}

@Test
void testCompletionWithResourceGroup() {
var response = controller.completionWithResourceGroup("ai-sdk-java-e2e");
var result = response.getOriginalResponse();
var llmChoice =
((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices().get(0);
assertThat(llmChoice.getFinishReason()).isEqualTo("stop");
assertThat(llmChoice.getMessage().getContent()).isNotEmpty();
}
}

0 comments on commit 8527ee6

Please sign in to comment.