Skip to content

Commit 69eab2e

Browse files
committed
Add endpoints for supplying resource groups
- fix bug on resource group with custom destinaiton on openai - update tests
1 parent a319eb6 commit 69eab2e

File tree

7 files changed

+31
-18
lines changed

7 files changed

+31
-18
lines changed

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ public OpenAiClient withApiVersion(@Nonnull final String apiVersion) {
9494
@Beta
9595
@Nonnull
9696
public static OpenAiClient withCustomDestination(@Nonnull final Destination destination) {
97-
return new OpenAiClient(destination);
97+
OpenAiClient client = new OpenAiClient(destination);
98+
99+
if (!destination.get("URL.queries.api-version").isDefined()) {
100+
client = client.withApiVersion(DEFAULT_API_VERSION);
101+
}
102+
103+
return client;
98104
}
99105

100106
/**

foundation-models/openai/src/test/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiClientTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ void apiVersion() {
7474
stubFor(post(anyUrl()).willReturn(okJson("{}")));
7575
Try.of(() -> client.chatCompletion(new OpenAiChatCompletionParameters()));
7676

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

7981
Try.of(
8082
() -> client.withApiVersion("fooBar").chatCompletion(new OpenAiChatCompletionParameters()));
@@ -85,7 +87,9 @@ void apiVersion() {
8587
"withApiVersion should return a new object, the sut object should remain unchanged")
8688
.isNotSameAs(client.withApiVersion("fooBar"));
8789
Try.of(() -> client.chatCompletion(new OpenAiChatCompletionParameters()));
88-
verify(exactly(2), postRequestedFor(anyUrl()).withoutQueryParam("api-version"));
90+
verify(
91+
exactly(2),
92+
postRequestedFor(anyUrl()).withQueryParam("api-version", equalTo("2024-02-01")));
8993
}
9094

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

199203
final String response = new String(inputStream.readAllBytes());
200-
stubFor(post("/chat/completions").willReturn(okJson(response)));
204+
// with query parameter api-version=2024-02-01
205+
stubFor(
206+
post(urlPathEqualTo("/chat/completions"))
207+
.withQueryParam("api-version", equalTo("2024-02-01"))
208+
.willReturn(okJson(response)));
201209

202210
final OpenAiChatCompletionOutput result = request.call();
203211

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

265273
verify(
266274
postRequestedFor(urlPathEqualTo("/chat/completions"))
275+
.withQueryParam("api-version", equalTo("2024-02-01"))
267276
.withRequestBody(
268277
equalToJson(
269278
"""

sample-code/spring-app/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@
157157
<artifactId>assertj-core</artifactId>
158158
<scope>test</scope>
159159
</dependency>
160+
<dependency>
161+
<groupId>com.sap.cloud.sdk.cloudplatform</groupId>
162+
<artifactId>cloudplatform-connectivity</artifactId>
163+
</dependency>
160164
</dependencies>
161165

162166
<build>

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OpenAiController.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ OpenAiEmbeddingOutput embedding() {
193193
}
194194

195195
/**
196-
* Chat request to OpenAI with filtering by resource group
196+
* Chat request to OpenAI filtering by resource group
197197
*
198198
* @param resourceGroup The resource group to use
199199
* @return the assistant message response
@@ -203,13 +203,10 @@ OpenAiEmbeddingOutput embedding() {
203203
public static OpenAiChatCompletionOutput chatCompletionWithResource(
204204
@Nonnull @PathVariable("resourceGroup") final String resourceGroup) {
205205

206-
final var destinationWithResource =
207-
new AiCoreService()
208-
.forDeploymentByModel(GPT_4O)
209-
.withResourceGroup(resourceGroup)
210-
.destination();
206+
final var destination =
207+
new AiCoreService().getInferenceDestination(resourceGroup).forModel(GPT_4O);
211208

212-
return OpenAiClient.withCustomDestination(destinationWithResource)
209+
return OpenAiClient.withCustomDestination(destination)
213210
.chatCompletion("Where is the nearest coffee shop?");
214211
}
215212
}

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,9 @@ OrchestrationChatResponse maskingAnonymization() {
156156
public OrchestrationChatResponse completionWithResourceGroup(
157157
@PathVariable("resourceGroup") @Nonnull final String resourceGroup) {
158158

159-
var deployment =
160-
new AiCoreService()
161-
.forDeploymentByScenario("orchestration")
162-
.withResourceGroup(resourceGroup);
163-
var clientWithResourceGroup = new OrchestrationClient(deployment);
159+
final var destination =
160+
new AiCoreService().getInferenceDestination(resourceGroup).forScenario("orchestration");
161+
final var clientWithResourceGroup = new OrchestrationClient(destination);
164162

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

sample-code/spring-app/src/main/resources/static/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ <h2>Endpoints</h2>
8585
<li><a href="/chatCompletionTool">/chatCompletionTool</a></li>
8686
<li><a href="/chatCompletionImage">/chatCompletionImage</a></li>
8787
<li><a href="/embedding">/embedding</a></li>
88+
<li><a href="/chatCompletion/ai-sdk-java-e2e">/chatCompletion/resourceGroup</a></li>
8889
</ul>
8990
</ul>
9091
</li>

sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/OpenAiTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatMessage.OpenAiChatUserMessage;
1010
import java.util.concurrent.atomic.AtomicInteger;
1111
import lombok.extern.slf4j.Slf4j;
12-
import org.junit.jupiter.api.Disabled;
1312
import org.junit.jupiter.api.BeforeEach;
1413
import org.junit.jupiter.api.Test;
1514

@@ -91,7 +90,6 @@ void embedding() {
9190
}
9291

9392
@Test
94-
@Disabled
9593
void chatCompletionWithResource() {
9694
final var completion = OpenAiController.chatCompletionWithResource("ai-sdk-java-e2e");
9795

0 commit comments

Comments
 (0)