Skip to content

Commit b927a6b

Browse files
committed
javadoc, release notes, etc
1 parent caf61d2 commit b927a6b

File tree

6 files changed

+62
-27
lines changed

6 files changed

+62
-27
lines changed

docs/release_notes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
- [Orchestration] Deprecated `OrchestrationAiModel.IBM_GRANITE_13B_CHAT` with no replacement.
2626
- [OpenAI] [Introduced SpringAI integration with our OpenAI client.](https://sap.github.io/ai-sdk/docs/java/spring-ai/openai)
2727
- Added `OpenAiChatModel`
28+
- [Orchestration] [Added convenience to add custom headers to individual orchestration calls.](add link here)
29+
- [OpenAI] [Added convenience to add custom headers to individual LLM calls.](add link here)
2830

2931
### 📈 Improvements
3032

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ public OpenAiClient withSystemPrompt(@Nonnull final String systemPrompt) {
139139
@Nonnull
140140
public OpenAiClient withHeader(@Nonnull final Header customHeader) {
141141
final var newDestination =
142-
DefaultHttpDestination.fromDestination(this.destination)
143-
.header(customHeader)
144-
.build();
142+
DefaultHttpDestination.fromDestination(this.destination).header(customHeader).build();
145143
return new OpenAiClient(newDestination);
146144
}
147145

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionTool;
1717
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiContentFilterPromptResults;
1818
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiEmbeddingParameters;
19+
import com.sap.cloud.sdk.cloudplatform.connectivity.Header;
1920
import java.io.IOException;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.concurrent.Callable;
2324
import java.util.stream.Stream;
2425
import javax.annotation.Nonnull;
25-
26-
import com.sap.cloud.sdk.cloudplatform.connectivity.Header;
2726
import lombok.SneakyThrows;
2827
import org.junit.jupiter.api.DisplayName;
2928
import org.junit.jupiter.api.Test;
@@ -487,14 +486,20 @@ void chatCompletionTool() {
487486
@Test
488487
void testCustomHeaders() {
489488
stubForChatCompletion();
490-
final var request = new OpenAiChatCompletionRequest("Hello World! Why is this phrase so famous?");
489+
final var request =
490+
new OpenAiChatCompletionRequest("Hello World! Why is this phrase so famous?");
491491

492492
var customHeader = new Header("foo", "bar");
493-
final var result = client.withHeader("footoo", "barzar").withHeader(customHeader).chatCompletion(request);
493+
final var result =
494+
client.withHeader("footoo", "barzar").withHeader(customHeader).chatCompletion(request);
494495
assertThat(result).isNotNull();
495496

496497
var newCustomHeader = new Header("foo", "baz");
497-
var streamResult = client.withHeader("footoo", "barz").withHeader(newCustomHeader).streamChatCompletion("Hello World! Why is this phrase so famous?");
498+
var streamResult =
499+
client
500+
.withHeader("footoo", "barz")
501+
.withHeader(newCustomHeader)
502+
.streamChatCompletion("Hello World! Why is this phrase so famous?");
498503
assertThat(streamResult).isNotNull();
499504

500505
verify(

orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationClient.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
import com.sap.cloud.sdk.cloudplatform.connectivity.Header;
1919
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;
2020
import io.vavr.control.Try;
21-
2221
import java.util.ArrayList;
2322
import java.util.List;
2423
import java.util.Map;
2524
import java.util.function.Supplier;
2625
import java.util.stream.Stream;
2726
import javax.annotation.Nonnull;
2827
import javax.annotation.Nullable;
29-
3028
import lombok.extern.slf4j.Slf4j;
3129
import lombok.val;
3230

@@ -68,7 +66,9 @@ public OrchestrationClient(@Nonnull final HttpDestination destination) {
6866
this.executor = new OrchestrationHttpExecutor(() -> destination);
6967
}
7068

71-
private OrchestrationClient(@Nonnull OrchestrationHttpExecutor executor, @Nullable List<Header> customHeaders) {
69+
private OrchestrationClient(
70+
@Nonnull final OrchestrationHttpExecutor executor,
71+
@Nullable final List<Header> customHeaders) {
7272
this.executor = executor;
7373
if (customHeaders != null) {
7474
this.customHeaders.addAll(customHeaders);
@@ -169,7 +169,8 @@ private static Map<String, Object> getOutputFilteringChoices(
169169
@Nonnull
170170
public CompletionPostResponse executeRequest(@Nonnull final CompletionPostRequest request)
171171
throws OrchestrationClientException {
172-
return executor.execute(COMPLETION_ENDPOINT, request, CompletionPostResponse.class, customHeaders);
172+
return executor.execute(
173+
COMPLETION_ENDPOINT, request, CompletionPostResponse.class, customHeaders);
173174
}
174175

175176
/**
@@ -211,7 +212,8 @@ public OrchestrationChatResponse executeRequestFromJsonModuleConfig(
211212
requestJson.set("orchestration_config", moduleConfigJson);
212213

213214
return new OrchestrationChatResponse(
214-
executor.execute(COMPLETION_ENDPOINT, requestJson, CompletionPostResponse.class, customHeaders));
215+
executor.execute(
216+
COMPLETION_ENDPOINT, requestJson, CompletionPostResponse.class, customHeaders));
215217
}
216218

217219
/**
@@ -244,16 +246,32 @@ EmbeddingsPostResponse embed(@Nonnull final EmbeddingsPostRequest request)
244246
return executor.execute("/v2/embeddings", request, EmbeddingsPostResponse.class, customHeaders);
245247
}
246248

247-
private OrchestrationClient addHeader(Header header) {
248-
this.customHeaders.add(header);
249-
return this;
250-
}
251-
252-
public OrchestrationClient withHeader(String key, String value) {
249+
/**
250+
* Create a new orchestration client with a custom header added to every call made with this
251+
* client
252+
*
253+
* @param key the key of the custom header to add
254+
* @param value the value of the custom header to add
255+
* @return a new client.
256+
*/
257+
@Beta
258+
@Nonnull
259+
public OrchestrationClient withHeader(@Nonnull final String key, @Nonnull final String value) {
253260
return this.withHeader(new Header(key, value));
254261
}
255262

256-
public OrchestrationClient withHeader(Header header) {
257-
return new OrchestrationClient(this.executor, this.customHeaders).addHeader(header);
263+
/**
264+
* Create a new orchestration client with a custom header added to every call made with this
265+
* client
266+
*
267+
* @param customHeader the custom header to add
268+
* @return a new client.
269+
*/
270+
@Beta
271+
@Nonnull
272+
public OrchestrationClient withHeader(@Nonnull final Header customHeader) {
273+
final var newClient = new OrchestrationClient(this.executor, this.customHeaders);
274+
newClient.customHeaders.add(customHeader);
275+
return newClient;
258276
}
259277
}

orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationHttpExecutor.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ <T> T execute(
7171

7272
@Nonnull
7373
Stream<OrchestrationChatCompletionDelta> stream(
74-
@Nonnull final String path, @Nonnull final Object payload, @Nonnull final List<Header> customHeaders) {
74+
@Nonnull final String path,
75+
@Nonnull final Object payload,
76+
@Nonnull final List<Header> customHeaders) {
7577
try {
7678

7779
val json = JACKSON.writeValueAsString(payload);
@@ -95,7 +97,10 @@ Stream<OrchestrationChatCompletionDelta> stream(
9597

9698
@Nonnull
9799
private HttpClient getHttpClient(@Nonnull final List<Header> customHeaders) {
98-
val destination = DefaultHttpDestination.fromDestination(destinationSupplier.get()).headers(customHeaders).build();
100+
val destination =
101+
DefaultHttpDestination.fromDestination(destinationSupplier.get())
102+
.headers(customHeaders)
103+
.build();
99104
log.debug("Using destination {} to connect to orchestration service", destination);
100105
return ApacheHttpClient5Accessor.getHttpClient(destination);
101106
}

orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Accessor;
7979
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Cache;
8080
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
81+
import com.sap.cloud.sdk.cloudplatform.connectivity.Header;
8182
import java.io.IOException;
8283
import java.io.InputStream;
8384
import java.math.BigDecimal;
@@ -89,8 +90,6 @@
8990
import java.util.function.Function;
9091
import java.util.stream.Stream;
9192
import javax.annotation.Nonnull;
92-
93-
import com.sap.cloud.sdk.cloudplatform.connectivity.Header;
9493
import lombok.val;
9594
import org.apache.hc.client5.http.classic.HttpClient;
9695
import org.apache.hc.core5.http.ContentType;
@@ -181,11 +180,19 @@ void testCustomHeaders() {
181180
.withHeader("Content-Type", "application/json")));
182181

183182
var customHeader = new Header("foo", "bar");
184-
final var result = client.withHeader("footoo", "barzar").withHeader(customHeader).chatCompletion(prompt, config);
183+
final var result =
184+
client
185+
.withHeader("footoo", "barzar")
186+
.withHeader(customHeader)
187+
.chatCompletion(prompt, config);
185188
assertThat(result).isNotNull();
186189

187190
var newCustomHeader = new Header("foo", "baz");
188-
var streamResult = client.withHeader("footoo", "barz").withHeader(newCustomHeader).streamChatCompletion(prompt, config);
191+
var streamResult =
192+
client
193+
.withHeader("footoo", "barz")
194+
.withHeader(newCustomHeader)
195+
.streamChatCompletion(prompt, config);
189196
assertThat(streamResult).isNotNull();
190197

191198
verify(

0 commit comments

Comments
 (0)