Skip to content

Commit

Permalink
Switch to deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
MatKuhr committed Oct 29, 2024
1 parent 1335c6f commit 66fe024
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
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;
Expand All @@ -18,9 +19,9 @@
import io.vavr.NotImplementedError;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.function.Supplier;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
Expand All @@ -36,7 +37,6 @@
* OrchestrationPrompt} will take precedence upon execution.
*/
@Slf4j
@RequiredArgsConstructor
public class OrchestrationClient implements OrchestrationConfig<OrchestrationClient> {
static final ObjectMapper JACKSON;

Expand All @@ -55,13 +55,23 @@ public class OrchestrationClient implements OrchestrationConfig<OrchestrationCli
private final DefaultOrchestrationConfig<OrchestrationClient> clientConfig =
DefaultOrchestrationConfig.asDelegateFor(this);

@Nonnull private final AiCoreService service;
@Nonnull private final Supplier<AiCoreDeployment> deployment;

private interface IDelegate extends OrchestrationConfig<OrchestrationClient> {}

/** Default constructor. */
public OrchestrationClient() {
service = new AiCoreService();
deployment = () -> new AiCoreService().forDeploymentByScenario("orchestration");
}

/**
* Constructor with a custom deployment, allowing for a custom resource group or otherwise
* specific deployment ID.
*
* @param deployment The specific {@link AiCoreDeployment} to use.
*/
public OrchestrationClient(@Nonnull final AiCoreDeployment deployment) {
this.deployment = () -> deployment;
}

/**
Expand Down Expand Up @@ -149,12 +159,14 @@ public Stream<String> streamChatCompletionDelta(@Nonnull final OrchestrationProm
* }
* }</pre>
*
* <p>Alternatively, you can call this method directly with a fully custom request object.
*
* @param request The request DTO to send to orchestration.
* @return The response DTO from orchestration.
* @throws OrchestrationClientException If the request fails.
*/
@Nonnull
protected CompletionPostResponse executeRequest(@Nonnull final CompletionPostRequest request)
public CompletionPostResponse executeRequest(@Nonnull final CompletionPostRequest request)
throws OrchestrationClientException {
final BasicClassicHttpRequest postRequest = new HttpPost("/completion");
try {
Expand All @@ -172,7 +184,7 @@ protected CompletionPostResponse executeRequest(@Nonnull final CompletionPostReq
@Nonnull
CompletionPostResponse executeRequest(@Nonnull final BasicClassicHttpRequest request) {
try {
val destination = service.forDeploymentByScenario("orchestration").destination();
val destination = deployment.get().destination();
log.debug("Using destination {} to connect to orchestration service", destination);
val client = ApacheHttpClient5Accessor.getHttpClient(destination);
return client.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.sap.ai.sdk.core.AiCoreService;
import com.sap.ai.sdk.core.AiCoreDeployment;
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;
Expand All @@ -28,7 +28,7 @@ class OrchestrationClientTest {

@BeforeEach
void setup() {
var mock = mock(AiCoreService.class);
var mock = mock(AiCoreDeployment.class);
client = spy(new OrchestrationClient(mock).withLlmConfig(LLM_CONFIG));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import com.sap.ai.sdk.core.AiCoreDeployment;
import com.sap.ai.sdk.core.AiCoreService;
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Accessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Cache;
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
Expand All @@ -37,11 +35,9 @@ class OrchestrationResponseHandlerTest {
@BeforeEach
void setup(WireMockRuntimeInfo server) {
var destination = DefaultHttpDestination.builder(server.getHttpBaseUrl()).build();
var mockService = mock(AiCoreService.class);
var mockDeployment = mock(AiCoreDeployment.class);
when(mockService.forDeploymentByScenario(any())).thenReturn(mockDeployment);
when(mockDeployment.destination()).thenReturn(destination);
client = new OrchestrationClient(mockService).withLlmConfig(LLM_CONFIG);
client = new OrchestrationClient(mockDeployment).withLlmConfig(LLM_CONFIG);
ApacheHttpClient5Accessor.setHttpClientCache(ApacheHttpClient5Cache.DISABLED);
}

Expand Down

0 comments on commit 66fe024

Please sign in to comment.