Skip to content

Commit

Permalink
WiP
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesDuboisSAP committed Oct 11, 2024
1 parent 31a6090 commit e217da9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 38 deletions.
26 changes: 6 additions & 20 deletions core/src/main/java/com/sap/ai/sdk/core/AiCoreDeployment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import static com.sap.ai.sdk.core.DestinationResolver.AI_CLIENT_TYPE_KEY;
import static com.sap.ai.sdk.core.DestinationResolver.AI_CLIENT_TYPE_VALUE;

import com.sap.ai.sdk.core.client.DeploymentApi;
import com.sap.ai.sdk.core.client.model.AiDeployment;
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;
import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import lombok.AccessLevel;
Expand All @@ -24,9 +21,6 @@
public class AiCoreDeployment implements AiCoreDestination {
private static final String AI_RESOURCE_GROUP = "URL.headers.AI-Resource-Group";

// the deployment id handler to be used, based on resource group
@Nonnull private final Function<String, String> deploymentId;

// the base destination handler to be used
@Nonnull private final Supplier<Destination> destination;

Expand All @@ -38,13 +32,11 @@ public class AiCoreDeployment implements AiCoreDestination {
/**
* Create a new instance of the AI Core service with a specific deployment id and destination.
*
* @param deploymentId The deployment id handler, based on resource group.
* @param destination The destination handler.
*/
public AiCoreDeployment(
@Nonnull final Function<String, String> deploymentId,
@Nonnull final Supplier<Destination> destination) {
this(deploymentId, destination, "default");
this( destination, "default");
}

@Nonnull
Expand All @@ -64,7 +56,7 @@ public Destination destination() {
*/
@Nonnull
public AiCoreDeployment withResourceGroup(@Nonnull final String resourceGroup) {
return new AiCoreDeployment(deploymentId, destination, resourceGroup);
return new AiCoreDeployment(destination, resourceGroup);
}

/**
Expand All @@ -75,7 +67,7 @@ public AiCoreDeployment withResourceGroup(@Nonnull final String resourceGroup) {
*/
@Nonnull
public AiCoreDeployment withDestination(@Nonnull final Destination destination) {
return new AiCoreDeployment(deploymentId, () -> destination, resourceGroup);
return new AiCoreDeployment(() -> destination, resourceGroup);
}

/**
Expand Down Expand Up @@ -145,16 +137,10 @@ protected static boolean isDeploymentOfModel(
* @throws NoSuchElementException if no deployment is found for the scenario id.
*/
@Nonnull
protected static String getDeploymentId(
protected String getDeploymentId(
@Nonnull final ApiClient client,
@Nonnull final String resourceGroup,
@Nonnull final Predicate<AiDeployment> predicate)
@Nonnull final String name)
throws NoSuchElementException {
final var deployments = new DeploymentApi(client).query(resourceGroup);

final var first =
deployments.getResources().stream().filter(predicate).map(AiDeployment::getId).findFirst();
return first.orElseThrow(
() -> new NoSuchElementException("No deployment found with scenario id orchestration"));
return DeploymentCache.getDeploymentId(resourceGroup, name);
}
}
15 changes: 8 additions & 7 deletions core/src/main/java/com/sap/ai/sdk/core/AiCoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationNotFoundException;
import java.util.function.Predicate;

import java.util.NoSuchElementException;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import lombok.AccessLevel;
Expand Down Expand Up @@ -62,9 +63,9 @@ public AiCoreDeployment forDeployment(@Nonnull final String deploymentId) {
* @return A new instance of the AI Core service.
*/
@Nonnull
public AiCoreDeployment forDeploymentByModel(@Nonnull final String modelName) {
final Predicate<AiDeployment> p = deployment -> isDeploymentOfModel(modelName, deployment);
return new AiCoreDeployment(res -> getDeploymentId(client(), res, p), this::destination);
public AiCoreDeployment forDeploymentByModel(@Nonnull final String modelName) throws NoSuchElementException
{
return new AiCoreDeployment(this::destination).getDeploymentId(client(), modelName);
}

/**
Expand All @@ -74,9 +75,9 @@ public AiCoreDeployment forDeploymentByModel(@Nonnull final String modelName) {
* @return A new instance of the AI Core service.
*/
@Nonnull
public AiCoreDeployment forDeploymentByScenario(@Nonnull final String scenarioId) {
final Predicate<AiDeployment> p = deployment -> scenarioId.equals(deployment.getScenarioId());
return new AiCoreDeployment(res -> getDeploymentId(client(), res, p), this::destination);
public AiCoreDeployment forDeploymentByScenario(@Nonnull final String scenarioId) throws NoSuchElementException
{
return new AiCoreDeployment(this::destination).getDeploymentId(client(), scenarioId);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions core/src/main/java/com/sap/ai/sdk/core/DeploymentCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void clearCache() {
*/
public static void loadCache() {
try {
final var deployments = API.deploymentQuery("default").getResources();
final var deployments = API.query("default").getResources();
deployments.forEach(deployment -> CACHE.put(getModelName(deployment), deployment.getId()));
} catch (final OpenApiRequestException e) {
log.error("Failed to load deployments into cache", e);
Expand Down Expand Up @@ -102,8 +102,7 @@ public static String getDeploymentId(
private static String getOrchestrationDeployment(@Nonnull final String resourceGroup)
throws NoSuchElementException {
final var deployments =
API.deploymentQuery(
resourceGroup, null, null, "orchestration", "RUNNING", null, null, null);
API.query(resourceGroup, null, null, "orchestration", "RUNNING", null, null, null);

return deployments.getResources().stream()
.map(AiDeployment::getId)
Expand All @@ -127,8 +126,7 @@ private static String getDeploymentForModel(
@Nonnull final String resourceGroup, @Nonnull final String modelName)
throws NoSuchElementException {
final var deployments =
API.deploymentQuery(
resourceGroup, null, null, "foundation-models", "RUNNING", null, null, null);
API.query(resourceGroup, null, null, "foundation-models", "RUNNING", null, null, null);

return deployments.getResources().stream()
.filter(deployment -> modelName.equals(getModelName(deployment)))
Expand Down
3 changes: 2 additions & 1 deletion core/src/test/java/com/sap/ai/sdk/core/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class CacheTest extends WireMockTestServer {
class CacheTest extends WireMockTestServer
{

@BeforeEach
void setupCache() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.sap.ai.sdk.core.client;
package com.sap.ai.sdk.core;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.sap.ai.sdk.core.AiCoreService;
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient;
Expand All @@ -16,9 +15,9 @@ public abstract class WireMockTestServer {
private static final WireMockConfiguration WIREMOCK_CONFIGURATION =
wireMockConfig().dynamicPort();

static WireMockServer wireMockServer;
static Destination destination;
static ApiClient client;
public static WireMockServer wireMockServer;
public static Destination destination;
public static ApiClient client;

@BeforeAll
static void setup() {
Expand Down

0 comments on commit e217da9

Please sign in to comment.