Skip to content

Commit 334ed8b

Browse files
committed
Combine prompt and config class into request class
- Stop maintaining defaults for request parameters - remove extra serialised data from test
1 parent b946c5c commit 334ed8b

File tree

8 files changed

+215
-302
lines changed

8 files changed

+215
-302
lines changed

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

Lines changed: 0 additions & 72 deletions
This file was deleted.

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

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.sap.ai.sdk.foundationmodels.openai;
2+
3+
import com.sap.ai.sdk.foundationmodels.openai.model2.ChatCompletionStreamOptions;
4+
import com.sap.ai.sdk.foundationmodels.openai.model2.ChatCompletionTool;
5+
import com.sap.ai.sdk.foundationmodels.openai.model2.ChatCompletionToolChoiceOption;
6+
import com.sap.ai.sdk.foundationmodels.openai.model2.CreateChatCompletionRequest;
7+
import com.sap.ai.sdk.foundationmodels.openai.model2.CreateChatCompletionRequestAllOfResponseFormat;
8+
import com.sap.ai.sdk.foundationmodels.openai.model2.CreateChatCompletionRequestAllOfStop;
9+
import java.math.BigDecimal;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.Map;
14+
import javax.annotation.Nonnull;
15+
import javax.annotation.Nullable;
16+
import lombok.Data;
17+
import lombok.experimental.Accessors;
18+
19+
/**
20+
* Represents a request for OpenAI chat completion, including conversation messages and parameters.
21+
*/
22+
@Accessors(fluent = true)
23+
@Data
24+
public class OpenAiChatCompletionRequest {
25+
/** List of messages from the conversation. */
26+
@Nonnull private final List<OpenAiMessage> messages = new ArrayList<>();
27+
28+
/** Stop sequences for the completion. */
29+
@Nullable private List<String> stop;
30+
31+
/** Temperature for the completion. */
32+
@Nullable private BigDecimal temperature;
33+
34+
/** Top-p sampling parameter. */
35+
@Nullable private BigDecimal topP;
36+
37+
/** Whether to stream the completion. */
38+
@Nullable private Boolean stream;
39+
40+
/** Maximum number of tokens for the completion. */
41+
@Nullable private Integer maxTokens;
42+
43+
/** Maximum number of tokens for the completion response. */
44+
@Nullable private Integer maxCompletionTokens;
45+
46+
/** Presence penalty for the completion. */
47+
@Nullable private BigDecimal presencePenalty;
48+
49+
/** Frequency penalty for the completion. */
50+
@Nullable private BigDecimal frequencyPenalty;
51+
52+
/** Logit bias for the completion. */
53+
@Nullable private Map<String, Integer> logitBias;
54+
55+
/** User identifier for the completion. */
56+
@Nullable private String user;
57+
58+
/** Whether to include log probabilities in the response. */
59+
@Nullable private Boolean logprobs;
60+
61+
/** Number of top log probabilities to include. */
62+
@Nullable private Integer topLogprobs;
63+
64+
/** Number of completions to generate. */
65+
@Nullable private Integer n;
66+
67+
/** Whether to allow parallel tool calls. */
68+
@Nullable private Boolean parallelToolCalls;
69+
70+
/** Seed for random number generation. */
71+
@Nullable private Integer seed;
72+
73+
/** Options for streaming the completion. */
74+
@Nullable private ChatCompletionStreamOptions streamOptions;
75+
76+
/** Response format for the completion. */
77+
@Nullable private CreateChatCompletionRequestAllOfResponseFormat responseFormat;
78+
79+
/** List of tools for the completion. */
80+
@Nullable private List<ChatCompletionTool> tools;
81+
82+
/** Tool choice option for the completion. */
83+
@Nullable private ChatCompletionToolChoiceOption toolChoice;
84+
85+
/**
86+
* Creates an OpenAiChatCompletionPrompt with a single message.
87+
*
88+
* @param message the message to be added to the prompt
89+
*/
90+
public OpenAiChatCompletionRequest(@Nonnull final String message) {
91+
messages.add(OpenAiMessage.user(message));
92+
}
93+
94+
/**
95+
* Creates an OpenAiChatCompletionPrompt with a multiple unpacked messages.
96+
*
97+
* @param message the primary message to be added to the prompt
98+
* @param messages additional messages to be added to the prompt
99+
*/
100+
public OpenAiChatCompletionRequest(
101+
@Nonnull final OpenAiMessage message, @Nonnull final OpenAiMessage... messages) {
102+
this.messages.add(message);
103+
this.messages.addAll(Arrays.asList(messages));
104+
}
105+
106+
/**
107+
* Sets the stop sequences for the prompt.
108+
*
109+
* @param values the stop sequences to be set
110+
* @return the current OpenAiChatCompletionPrompt instance
111+
*/
112+
@Nonnull
113+
public OpenAiChatCompletionRequest stop(
114+
@Nonnull final String value, @Nonnull final String... values) {
115+
this.stop = new ArrayList<>();
116+
117+
this.stop.add(value);
118+
this.stop.addAll(Arrays.asList(values));
119+
120+
return this;
121+
}
122+
123+
CreateChatCompletionRequest toCreateChatCompletionRequest() {
124+
final var request = new CreateChatCompletionRequest();
125+
this.messages().forEach(message -> request.addMessagesItem(message.createDTO()));
126+
127+
request.stop(
128+
this.stop() != null ? CreateChatCompletionRequestAllOfStop.create(this.stop()) : null);
129+
130+
request.temperature(this.temperature());
131+
request.topP(this.topP());
132+
request.stream(this.stream());
133+
request.maxTokens(this.maxTokens());
134+
request.maxCompletionTokens(this.maxCompletionTokens());
135+
request.presencePenalty(this.presencePenalty());
136+
request.frequencyPenalty(this.frequencyPenalty());
137+
request.logitBias(this.logitBias());
138+
request.user(this.user());
139+
request.logprobs(this.logprobs());
140+
request.topLogprobs(this.topLogprobs());
141+
request.n(this.n());
142+
request.parallelToolCalls(this.parallelToolCalls());
143+
request.seed(this.seed());
144+
request.streamOptions(this.streamOptions());
145+
request.responseFormat(this.responseFormat());
146+
request.tools(this.tools());
147+
request.toolChoice(this.toolChoice());
148+
request.functionCall(null);
149+
request.functions(null);
150+
return request;
151+
}
152+
}

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiChatCompletionOutput.java renamed to foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiChatCompletionResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/** Represents the output of an OpenAI chat completion. */
1515
@Data
1616
@RequiredArgsConstructor(access = PROTECTED)
17-
public class OpenAiChatCompletionOutput {
17+
public class OpenAiChatCompletionResponse {
1818
/** The original response from the OpenAI API. */
1919
@Nonnull final CreateChatCompletionResponse originalResponse;
2020

0 commit comments

Comments
 (0)