Skip to content

Commit 390712e

Browse files
committed
Initial
1 parent 7ec7dff commit 390712e

File tree

2 files changed

+53
-43
lines changed

2 files changed

+53
-43
lines changed

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

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
/** Client for interacting with OpenAI models. */
3838
@Slf4j
3939
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
40-
public final class OpenAiClient {
40+
public final class OpenAiClient implements OpenAiClientWithSystemPrompt {
4141
private static final String DEFAULT_API_VERSION = "2024-02-01";
4242
static final ObjectMapper JACKSON = getDefaultObjectMapper();
4343
@Nullable private String systemPrompt = null;
@@ -113,18 +113,12 @@ public static OpenAiClient withCustomDestination(@Nonnull final Destination dest
113113
* @return the client
114114
*/
115115
@Nonnull
116-
public OpenAiClient withSystemPrompt(@Nonnull final String systemPrompt) {
116+
public OpenAiClientWithSystemPrompt withSystemPrompt(@Nonnull final String systemPrompt) {
117117
this.systemPrompt = systemPrompt;
118118
return this;
119119
}
120120

121-
/**
122-
* Generate a completion for the given user prompt.
123-
*
124-
* @param prompt a text message.
125-
* @return the completion output
126-
* @throws OpenAiClientException if the request fails
127-
*/
121+
@Override
128122
@Nonnull
129123
public OpenAiChatCompletionOutput chatCompletion(@Nonnull final String prompt)
130124
throws OpenAiClientException {
@@ -146,35 +140,10 @@ public OpenAiChatCompletionOutput chatCompletion(@Nonnull final String prompt)
146140
@Nonnull
147141
public OpenAiChatCompletionOutput chatCompletion(
148142
@Nonnull final OpenAiChatCompletionParameters parameters) throws OpenAiClientException {
149-
warnIfUnsupportedUsage();
150143
return execute("/chat/completions", parameters, OpenAiChatCompletionOutput.class);
151144
}
152145

153-
/**
154-
* Stream a completion for the given prompt. Returns a <b>lazily</b> populated stream of text
155-
* chunks. To access more details about the individual chunks, use {@link
156-
* #streamChatCompletionDeltas(OpenAiChatCompletionParameters)}.
157-
*
158-
* <p>The stream should be consumed using a try-with-resources block to ensure that the underlying
159-
* HTTP connection is closed.
160-
*
161-
* <p>Example:
162-
*
163-
* <pre>{@code
164-
* try (var stream = client.streamChatCompletion("...")) {
165-
* stream.forEach(System.out::println);
166-
* }
167-
* }</pre>
168-
*
169-
* <p>Please keep in mind that using a terminal stream operation like {@link Stream#forEach} will
170-
* block until all chunks are consumed. Also, for obvious reasons, invoking {@link
171-
* Stream#parallel()} on this stream is not supported.
172-
*
173-
* @param prompt a text message.
174-
* @return A stream of message deltas
175-
* @throws OpenAiClientException if the request fails or if the finish reason is content_filter
176-
* @see #streamChatCompletionDeltas(OpenAiChatCompletionParameters)
177-
*/
146+
@Override
178147
@Nonnull
179148
public Stream<String> streamChatCompletion(@Nonnull final String prompt)
180149
throws OpenAiClientException {
@@ -225,18 +194,10 @@ private static void throwOnContentFilter(@Nonnull final OpenAiChatCompletionDelt
225194
@Nonnull
226195
public Stream<OpenAiChatCompletionDelta> streamChatCompletionDeltas(
227196
@Nonnull final OpenAiChatCompletionParameters parameters) throws OpenAiClientException {
228-
warnIfUnsupportedUsage();
229197
parameters.enableStreaming();
230198
return executeStream("/chat/completions", parameters, OpenAiChatCompletionDelta.class);
231199
}
232200

233-
private void warnIfUnsupportedUsage() {
234-
if (systemPrompt != null) {
235-
log.warn(
236-
"Previously set messages will be ignored, set it as an argument of this method instead.");
237-
}
238-
}
239-
240201
/**
241202
* Get a vector representation of a given input that can be easily consumed by machine learning
242203
* models and algorithms.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.sap.ai.sdk.foundationmodels.openai;
2+
3+
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionOutput;
4+
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionParameters;
5+
import java.util.stream.Stream;
6+
import javax.annotation.Nonnull;
7+
8+
/** Client for interacting with OpenAI models. Allows for convenient string prompts only. */
9+
public interface OpenAiClientWithSystemPrompt {
10+
11+
/**
12+
* Generate a completion for the given user prompt.
13+
*
14+
* @param prompt a text message.
15+
* @return the completion output
16+
* @throws OpenAiClientException if the request fails
17+
*/
18+
@Nonnull
19+
OpenAiChatCompletionOutput chatCompletion(@Nonnull final String prompt)
20+
throws OpenAiClientException;
21+
22+
/**
23+
* Stream a completion for the given prompt. Returns a <b>lazily</b> populated stream of text
24+
* chunks. To access more details about the individual chunks, use {@link
25+
* OpenAiClient#streamChatCompletionDeltas(OpenAiChatCompletionParameters)}.
26+
*
27+
* <p>The stream should be consumed using a try-with-resources block to ensure that the underlying
28+
* HTTP connection is closed.
29+
*
30+
* <p>Example:
31+
*
32+
* <pre>{@code
33+
* try (var stream = client.streamChatCompletion("...")) {
34+
* stream.forEach(System.out::println);
35+
* }
36+
* }</pre>
37+
*
38+
* <p>Please keep in mind that using a terminal stream operation like {@link Stream#forEach} will
39+
* block until all chunks are consumed. Also, for obvious reasons, invoking {@link
40+
* Stream#parallel()} on this stream is not supported.
41+
*
42+
* @param prompt a text message.
43+
* @return A stream of message deltas
44+
* @throws OpenAiClientException if the request fails or if the finish reason is content_filter
45+
* @see OpenAiClient#streamChatCompletionDeltas(OpenAiChatCompletionParameters)
46+
*/
47+
@Nonnull
48+
Stream<String> streamChatCompletion(@Nonnull final String prompt) throws OpenAiClientException;
49+
}

0 commit comments

Comments
 (0)