Skip to content

Commit abd7acb

Browse files
committed
Fix @with equality check for Boolean
1 parent f7b7717 commit abd7acb

File tree

2 files changed

+80
-26
lines changed

2 files changed

+80
-26
lines changed

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

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Arrays;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.Objects;
1516
import javax.annotation.Nonnull;
1617
import javax.annotation.Nullable;
1718
import lombok.AccessLevel;
@@ -44,9 +45,6 @@ public class OpenAiChatCompletionRequest {
4445
/** Top-p sampling parameter. */
4546
@Nullable BigDecimal topP;
4647

47-
/** Whether to stream the completion. */
48-
boolean stream;
49-
5048
/** Maximum number of tokens for the completion. */
5149
@Nullable Integer maxTokens;
5250

@@ -66,7 +64,9 @@ public class OpenAiChatCompletionRequest {
6664
@Nullable String user;
6765

6866
/** Whether to include log probabilities in the response. */
69-
boolean logprobs;
67+
@With(AccessLevel.NONE)
68+
@Nullable
69+
Boolean logprobs;
7070

7171
/** Number of top log probabilities to include. */
7272
@Nullable Integer topLogprobs;
@@ -75,7 +75,9 @@ public class OpenAiChatCompletionRequest {
7575
@Nullable Integer n;
7676

7777
/** Whether to allow parallel tool calls. */
78-
boolean parallelToolCalls;
78+
@With(AccessLevel.NONE)
79+
@Nullable
80+
Boolean parallelToolCalls;
7981

8082
/** Seed for random number generation. */
8183
@Nullable Integer seed;
@@ -113,21 +115,20 @@ public OpenAiChatCompletionRequest(
113115
@Nonnull final OpenAiMessage message, @Nonnull final OpenAiMessage... messages) {
114116
// Keeps default values for boolean fields. @With introduces bug comparison of Boolean
115117
this(
116-
new ArrayList<>(),
118+
new ArrayList<OpenAiMessage>(),
119+
null,
120+
null,
117121
null,
118122
null,
119123
null,
120-
false,
121124
null,
122125
null,
123126
null,
124127
null,
125128
null,
126129
null,
127-
false,
128130
null,
129131
null,
130-
true,
131132
null,
132133
null,
133134
null,
@@ -157,6 +158,71 @@ public OpenAiChatCompletionRequest withStop(
157158
return this.withStop(allSequences);
158159
}
159160

161+
/**
162+
* Sets the parallel tool calls option.
163+
*
164+
* @param parallelToolCalls Whether to allow parallel tool calls.
165+
* @return A new instance with the specified option.
166+
*/
167+
@Nonnull
168+
public OpenAiChatCompletionRequest withParallelToolCalls(
169+
@Nonnull final Boolean parallelToolCalls) {
170+
return Objects.equals(this.parallelToolCalls, parallelToolCalls)
171+
? this
172+
: new OpenAiChatCompletionRequest(
173+
this.messages,
174+
this.stop,
175+
this.temperature,
176+
this.topP,
177+
this.maxTokens,
178+
this.maxCompletionTokens,
179+
this.presencePenalty,
180+
this.frequencyPenalty,
181+
this.logitBias,
182+
this.user,
183+
this.logprobs,
184+
this.topLogprobs,
185+
this.n,
186+
parallelToolCalls,
187+
this.seed,
188+
this.streamOptions,
189+
this.responseFormat,
190+
this.tools,
191+
this.toolChoice);
192+
}
193+
194+
/**
195+
* Sets the log probabilities option.
196+
*
197+
* @param logprobs Whether to include log probabilities in the response.
198+
* @return A new instance with the specified option.
199+
*/
200+
@Nonnull
201+
public OpenAiChatCompletionRequest withLogprobs(@Nonnull final Boolean logprobs) {
202+
return Objects.equals(this.logprobs, logprobs)
203+
? this
204+
: new OpenAiChatCompletionRequest(
205+
this.messages,
206+
this.stop,
207+
this.temperature,
208+
this.topP,
209+
this.maxTokens,
210+
this.maxCompletionTokens,
211+
this.presencePenalty,
212+
this.frequencyPenalty,
213+
this.logitBias,
214+
this.user,
215+
logprobs,
216+
this.topLogprobs,
217+
this.n,
218+
this.parallelToolCalls,
219+
this.seed,
220+
this.streamOptions,
221+
this.responseFormat,
222+
this.tools,
223+
this.toolChoice);
224+
}
225+
160226
/**
161227
* Converts the request to a generated model class CreateChatCompletionRequest.
162228
*
@@ -171,7 +237,7 @@ CreateChatCompletionRequest toCreateChatCompletionRequest() {
171237
request.temperature(this.temperature);
172238
request.topP(this.topP);
173239

174-
request.stream(this.stream);
240+
request.stream(null);
175241
request.maxTokens(this.maxTokens);
176242
request.maxCompletionTokens(this.maxCompletionTokens);
177243
request.presencePenalty(this.presencePenalty);

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,13 @@ void chatCompletion() {
181181
equalToJson(
182182
"""
183183
{
184-
"stream": false,
185184
"messages" : [ {
186185
"content" : "You are a helpful AI",
187186
"role" : "system"
188187
}, {
189188
"content" : "Hello World! Why is this phrase so famous?",
190189
"role" : "user"
191-
} ],
192-
"logprobs" : false,
193-
"parallel_tool_calls" : true
190+
} ]
194191
}""")));
195192
}
196193

@@ -213,13 +210,10 @@ void history() {
213210
equalToJson(
214211
"""
215212
{
216-
"stream" : false,
217213
"messages" : [{
218214
"content" : "First message",
219215
"role" : "user"
220-
}],
221-
"logprobs" : false,
222-
"parallel_tool_calls" : true
216+
}]
223217
}""")));
224218

225219
var response = client.chatCompletion(new OpenAiChatCompletionRequest("Second message"));
@@ -236,13 +230,10 @@ void history() {
236230
equalToJson(
237231
"""
238232
{
239-
"stream" : false,
240233
"messages" : [{
241234
"content" : "Second message",
242235
"role" : "user"
243-
}],
244-
"logprobs" : false,
245-
"parallel_tool_calls" : true
236+
}]
246237
}""")));
247238
}
248239

@@ -544,7 +535,6 @@ void chatCompletionTool() {
544535
equalToJson(
545536
"""
546537
{
547-
"stream" : false,
548538
"messages" : [ {
549539
"content" : "A pair of rabbits is placed in a field. Each month, every pair produces one new pair, starting from the second month. How many rabbits will there be after 12 months?",
550540
"role" : "user"
@@ -569,9 +559,7 @@ void chatCompletionTool() {
569559
"function" : {
570560
"name" : "fibonacci"
571561
}
572-
},
573-
"logprobs" : false,
574-
"parallel_tool_calls" : true
562+
}
575563
}
576564
""")));
577565
}

0 commit comments

Comments
 (0)