Skip to content

Commit 4b0933b

Browse files
committed
Refactor QianFan model option builder methods
- Deprecate the builder methods with the prefix `with` and add the new methods - Update docs and references
1 parent d77c950 commit 4b0933b

File tree

16 files changed

+205
-59
lines changed

16 files changed

+205
-59
lines changed

models/spring-ai-qianfan/src/main/java/org/springframework/ai/qianfan/QianFanChatModel.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ public class QianFanChatModel implements ChatModel, StreamingChatModel {
104104
* @throws IllegalArgumentException if QianFanApi is null
105105
*/
106106
public QianFanChatModel(QianFanApi qianFanApi) {
107-
this(qianFanApi,
108-
QianFanChatOptions.builder().withModel(QianFanApi.DEFAULT_CHAT_MODEL).withTemperature(0.7).build());
107+
this(qianFanApi, QianFanChatOptions.builder().model(QianFanApi.DEFAULT_CHAT_MODEL).temperature(0.7).build());
109108
}
110109

111110
/**

models/spring-ai-qianfan/src/main/java/org/springframework/ai/qianfan/QianFanChatOptions.java

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* frequency penalty, max tokens, etc.
3333
*
3434
* @author Geng Rong
35+
* @author Ilayaperumal Gopinathan
3536
* @since 1.0
3637
* @see ChatOptions
3738
*/
@@ -87,14 +88,14 @@ public static Builder builder() {
8788

8889
public static QianFanChatOptions fromOptions(QianFanChatOptions fromOptions) {
8990
return QianFanChatOptions.builder()
90-
.withModel(fromOptions.getModel())
91-
.withFrequencyPenalty(fromOptions.getFrequencyPenalty())
92-
.withMaxTokens(fromOptions.getMaxTokens())
93-
.withPresencePenalty(fromOptions.getPresencePenalty())
94-
.withResponseFormat(fromOptions.getResponseFormat())
95-
.withStop(fromOptions.getStop())
96-
.withTemperature(fromOptions.getTemperature())
97-
.withTopP(fromOptions.getTopP())
91+
.model(fromOptions.getModel())
92+
.frequencyPenalty(fromOptions.getFrequencyPenalty())
93+
.maxTokens(fromOptions.getMaxTokens())
94+
.presencePenalty(fromOptions.getPresencePenalty())
95+
.responseFormat(fromOptions.getResponseFormat())
96+
.stop(fromOptions.getStop())
97+
.temperature(fromOptions.getTemperature())
98+
.topP(fromOptions.getTopP())
9899
.build();
99100
}
100101

@@ -296,41 +297,115 @@ public Builder(QianFanChatOptions options) {
296297
this.options = options;
297298
}
298299

300+
public Builder model(String model) {
301+
this.options.model = model;
302+
return this;
303+
}
304+
305+
public Builder frequencyPenalty(Double frequencyPenalty) {
306+
this.options.frequencyPenalty = frequencyPenalty;
307+
return this;
308+
}
309+
310+
public Builder maxTokens(Integer maxTokens) {
311+
this.options.maxTokens = maxTokens;
312+
return this;
313+
}
314+
315+
public Builder presencePenalty(Double presencePenalty) {
316+
this.options.presencePenalty = presencePenalty;
317+
return this;
318+
}
319+
320+
public Builder responseFormat(QianFanApi.ChatCompletionRequest.ResponseFormat responseFormat) {
321+
this.options.responseFormat = responseFormat;
322+
return this;
323+
}
324+
325+
public Builder stop(List<String> stop) {
326+
this.options.stop = stop;
327+
return this;
328+
}
329+
330+
public Builder temperature(Double temperature) {
331+
this.options.temperature = temperature;
332+
return this;
333+
}
334+
335+
public Builder topP(Double topP) {
336+
this.options.topP = topP;
337+
return this;
338+
}
339+
340+
/**
341+
* @deprecated use {@link #model(String)} instead.
342+
*/
343+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
299344
public Builder withModel(String model) {
300345
this.options.model = model;
301346
return this;
302347
}
303348

349+
/**
350+
* @deprecated use {@link #frequencyPenalty(Double)} instead.
351+
*/
352+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
304353
public Builder withFrequencyPenalty(Double frequencyPenalty) {
305354
this.options.frequencyPenalty = frequencyPenalty;
306355
return this;
307356
}
308357

358+
/**
359+
* @deprecated use {@link #maxTokens(Integer)} instead.
360+
*/
361+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
309362
public Builder withMaxTokens(Integer maxTokens) {
310363
this.options.maxTokens = maxTokens;
311364
return this;
312365
}
313366

367+
/**
368+
* @deprecated use {@link #presencePenalty(Double)} instead.
369+
*/
370+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
314371
public Builder withPresencePenalty(Double presencePenalty) {
315372
this.options.presencePenalty = presencePenalty;
316373
return this;
317374
}
318375

376+
/**
377+
* @deprecated use
378+
* {@link #responseFormat(QianFanApi.ChatCompletionRequest.ResponseFormat)}
379+
* instead.
380+
*/
381+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
319382
public Builder withResponseFormat(QianFanApi.ChatCompletionRequest.ResponseFormat responseFormat) {
320383
this.options.responseFormat = responseFormat;
321384
return this;
322385
}
323386

387+
/**
388+
* @deprecated use {@link #stop(List)} instead.
389+
*/
390+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
324391
public Builder withStop(List<String> stop) {
325392
this.options.stop = stop;
326393
return this;
327394
}
328395

396+
/**
397+
* @deprecated use {@link #temperature(Double)} instead.
398+
*/
399+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
329400
public Builder withTemperature(Double temperature) {
330401
this.options.temperature = temperature;
331402
return this;
332403
}
333404

405+
/**
406+
* @deprecated use {@link #topP(Double)} instead.
407+
*/
408+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
334409
public Builder withTopP(Double topP) {
335410
this.options.topP = topP;
336411
return this;

models/spring-ai-qianfan/src/main/java/org/springframework/ai/qianfan/QianFanEmbeddingModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public QianFanEmbeddingModel(QianFanApi qianFanApi) {
9090
*/
9191
public QianFanEmbeddingModel(QianFanApi qianFanApi, MetadataMode metadataMode) {
9292
this(qianFanApi, metadataMode,
93-
QianFanEmbeddingOptions.builder().withModel(QianFanApi.DEFAULT_EMBEDDING_MODEL).build());
93+
QianFanEmbeddingOptions.builder().model(QianFanApi.DEFAULT_EMBEDDING_MODEL).build());
9494
}
9595

9696
/**
@@ -206,8 +206,8 @@ private QianFanEmbeddingOptions mergeOptions(@Nullable EmbeddingOptions runtimeO
206206
}
207207

208208
return QianFanEmbeddingOptions.builder()
209-
.withModel(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel()))
210-
.withUser(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getUser(), defaultOptions.getUser()))
209+
.model(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel()))
210+
.user(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getUser(), defaultOptions.getUser()))
211211
.build();
212212
}
213213

models/spring-ai-qianfan/src/main/java/org/springframework/ai/qianfan/QianFanEmbeddingOptions.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*
2929
* @author Geng Rong
3030
* @author Thomas Vitale
31+
* @author Ilayaperumal Gopinathan
3132
* @since 1.0
3233
*/
3334
@JsonInclude(Include.NON_NULL)
@@ -81,11 +82,29 @@ public Builder() {
8182
this.options = new QianFanEmbeddingOptions();
8283
}
8384

85+
public Builder model(String model) {
86+
this.options.setModel(model);
87+
return this;
88+
}
89+
90+
public Builder user(String user) {
91+
this.options.setUser(user);
92+
return this;
93+
}
94+
95+
/**
96+
* @deprecated use {@link #model(String)} instead.
97+
*/
98+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
8499
public Builder withModel(String model) {
85100
this.options.setModel(model);
86101
return this;
87102
}
88103

104+
/**
105+
* @deprecated use {@link #user(String)} instead.
106+
*/
107+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
89108
public Builder withUser(String user) {
90109
this.options.setUser(user);
91110
return this;

models/spring-ai-qianfan/src/main/java/org/springframework/ai/qianfan/QianFanImageModel.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,13 @@ private QianFanImageOptions mergeOptions(@Nullable ImageOptions runtimeImageOpti
203203
}
204204

205205
return QianFanImageOptions.builder()
206-
.withModel(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel()))
207-
.withN(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getN(), defaultOptions.getN()))
208-
.withModel(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel()))
209-
.withWidth(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getWidth(), defaultOptions.getWidth()))
210-
.withHeight(
211-
ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getHeight(), defaultOptions.getHeight()))
212-
.withStyle(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getStyle(), defaultOptions.getStyle()))
213-
.withUser(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getUser(), defaultOptions.getUser()))
206+
.model(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel()))
207+
.N(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getN(), defaultOptions.getN()))
208+
.model(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel()))
209+
.width(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getWidth(), defaultOptions.getWidth()))
210+
.height(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getHeight(), defaultOptions.getHeight()))
211+
.style(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getStyle(), defaultOptions.getStyle()))
212+
.user(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getUser(), defaultOptions.getUser()))
214213
.build();
215214
}
216215

models/spring-ai-qianfan/src/main/java/org/springframework/ai/qianfan/QianFanImageOptions.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* QianFan Image API options. QianFanImageOptions.java
2929
*
3030
* @author Geng Rong
31+
* @author Ilayaperumal Gopinathan
3132
* @since 1.0
3233
*/
3334
@JsonInclude(JsonInclude.Include.NON_NULL)
@@ -196,31 +197,85 @@ private Builder() {
196197
this.options = new QianFanImageOptions();
197198
}
198199

200+
public Builder N(Integer n) {
201+
this.options.setN(n);
202+
return this;
203+
}
204+
205+
public Builder model(String model) {
206+
this.options.setModel(model);
207+
return this;
208+
}
209+
210+
public Builder width(Integer width) {
211+
this.options.setWidth(width);
212+
return this;
213+
}
214+
215+
public Builder height(Integer height) {
216+
this.options.setHeight(height);
217+
return this;
218+
}
219+
220+
public Builder style(String style) {
221+
this.options.setStyle(style);
222+
return this;
223+
}
224+
225+
public Builder user(String user) {
226+
this.options.setUser(user);
227+
return this;
228+
}
229+
230+
/**
231+
* @deprecated use {@link #N(Integer)} instead.
232+
*/
233+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
199234
public Builder withN(Integer n) {
200235
this.options.setN(n);
201236
return this;
202237
}
203238

239+
/**
240+
* @deprecated use {@link #model(String)} instead.
241+
*/
242+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
204243
public Builder withModel(String model) {
205244
this.options.setModel(model);
206245
return this;
207246
}
208247

248+
/**
249+
* @deprecated use {@link #width(Integer)} instead.
250+
*/
251+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
209252
public Builder withWidth(Integer width) {
210253
this.options.setWidth(width);
211254
return this;
212255
}
213256

257+
/**
258+
* @deprecated use {@link #height(Integer)} instead.
259+
*/
260+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
214261
public Builder withHeight(Integer height) {
215262
this.options.setHeight(height);
216263
return this;
217264
}
218265

266+
/**
267+
* @deprecated use {@link #style(String)} instead.
268+
*/
269+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
219270
public Builder withStyle(String style) {
220271
this.options.setStyle(style);
221272
return this;
222273
}
223274

275+
/**
276+
* @deprecated use {@link #user(String)} instead.
277+
*/
278+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
224279
public Builder withUser(String user) {
225280
this.options.setUser(user);
226281
return this;

models/spring-ai-qianfan/src/test/java/org/springframework/ai/qianfan/ChatCompletionRequestTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ChatCompletionRequestTests {
3232
public void createRequestWithChatOptions() {
3333

3434
var client = new QianFanChatModel(new QianFanApi("TEST", "TEST"),
35-
QianFanChatOptions.builder().withModel("DEFAULT_MODEL").withTemperature(66.6).build());
35+
QianFanChatOptions.builder().model("DEFAULT_MODEL").temperature(66.6).build());
3636

3737
var request = client.createRequest(new Prompt("Test message content"), false);
3838

@@ -43,7 +43,7 @@ public void createRequestWithChatOptions() {
4343
assertThat(request.temperature()).isEqualTo(66.6);
4444

4545
request = client.createRequest(new Prompt("Test message content",
46-
QianFanChatOptions.builder().withModel("PROMPT_MODEL").withTemperature(99.9).build()), true);
46+
QianFanChatOptions.builder().model("PROMPT_MODEL").temperature(99.9).build()), true);
4747

4848
assertThat(request.messages()).hasSize(1);
4949
assertThat(request.stream()).isTrue();

models/spring-ai-qianfan/src/test/java/org/springframework/ai/qianfan/chat/QianFanChatModelObservationIT.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ void beforeEach() {
7171
void observationForChatOperation() {
7272

7373
var options = QianFanChatOptions.builder()
74-
.withModel(QianFanApi.ChatModel.ERNIE_Speed_8K.getValue())
75-
.withFrequencyPenalty(0.0)
76-
.withMaxTokens(2048)
77-
.withPresencePenalty(0.0)
78-
.withStop(List.of("this-is-the-end"))
79-
.withTemperature(0.7)
80-
.withTopP(1.0)
74+
.model(QianFanApi.ChatModel.ERNIE_Speed_8K.getValue())
75+
.frequencyPenalty(0.0)
76+
.maxTokens(2048)
77+
.presencePenalty(0.0)
78+
.stop(List.of("this-is-the-end"))
79+
.temperature(0.7)
80+
.topP(1.0)
8181
.build();
8282

8383
Prompt prompt = new Prompt("Why does a raven look like a desk?", options);
@@ -94,13 +94,13 @@ void observationForChatOperation() {
9494
@Test
9595
void observationForStreamingChatOperation() {
9696
var options = QianFanChatOptions.builder()
97-
.withModel(QianFanApi.ChatModel.ERNIE_Speed_8K.getValue())
98-
.withFrequencyPenalty(0.0)
99-
.withMaxTokens(2048)
100-
.withPresencePenalty(0.0)
101-
.withStop(List.of("this-is-the-end"))
102-
.withTemperature(0.7)
103-
.withTopP(1.0)
97+
.model(QianFanApi.ChatModel.ERNIE_Speed_8K.getValue())
98+
.frequencyPenalty(0.0)
99+
.maxTokens(2048)
100+
.presencePenalty(0.0)
101+
.stop(List.of("this-is-the-end"))
102+
.temperature(0.7)
103+
.topP(1.0)
104104
.build();
105105

106106
Prompt prompt = new Prompt("Why does a raven look like a desk?", options);

models/spring-ai-qianfan/src/test/java/org/springframework/ai/qianfan/embedding/QianFanEmbeddingModelObservationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class QianFanEmbeddingModelObservationIT {
6363
@Test
6464
void observationForEmbeddingOperation() {
6565
var options = QianFanEmbeddingOptions.builder()
66-
.withModel(QianFanApi.EmbeddingModel.BGE_LARGE_ZH.getValue())
66+
.model(QianFanApi.EmbeddingModel.BGE_LARGE_ZH.getValue())
6767
.build();
6868

6969
EmbeddingRequest embeddingRequest = new EmbeddingRequest(List.of("Here comes the sun"), options);

0 commit comments

Comments
 (0)