Skip to content

Commit 5f4cedb

Browse files
committed
Refactor Zhipu AI options builder methods
- Deprecate builder methods with the prefix `with` - Update docs and references
1 parent d77c950 commit 5f4cedb

File tree

19 files changed

+253
-85
lines changed

19 files changed

+253
-85
lines changed

models/spring-ai-zhipuai/src/main/java/org/springframework/ai/zhipuai/ZhiPuAiChatModel.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ public class ZhiPuAiChatModel extends AbstractToolCallSupport implements ChatMod
123123
* @throws IllegalArgumentException if zhiPuAiApi is null
124124
*/
125125
public ZhiPuAiChatModel(ZhiPuAiApi zhiPuAiApi) {
126-
this(zhiPuAiApi,
127-
ZhiPuAiChatOptions.builder().withModel(ZhiPuAiApi.DEFAULT_CHAT_MODEL).withTemperature(0.7).build());
126+
this(zhiPuAiApi, ZhiPuAiChatOptions.builder().model(ZhiPuAiApi.DEFAULT_CHAT_MODEL).temperature(0.7).build());
128127
}
129128

130129
/**
@@ -434,7 +433,7 @@ else if (message.getMessageType() == MessageType.TOOL) {
434433
if (!CollectionUtils.isEmpty(enabledToolsToUse)) {
435434

436435
request = ModelOptionsUtils.merge(
437-
ZhiPuAiChatOptions.builder().withTools(this.getFunctionTools(enabledToolsToUse)).build(), request,
436+
ZhiPuAiChatOptions.builder().tools(this.getFunctionTools(enabledToolsToUse)).build(), request,
438437
ChatCompletionRequest.class);
439438
}
440439

models/spring-ai-zhipuai/src/main/java/org/springframework/ai/zhipuai/ZhiPuAiChatOptions.java

Lines changed: 157 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*
3838
* @author Geng Rong
3939
* @author Thomas Vitale
40+
* @author Ilayaperumal Gopinathan
4041
* @since 1.0.0 M1
4142
*/
4243
@JsonInclude(Include.NON_NULL)
@@ -136,20 +137,20 @@ public static Builder builder() {
136137

137138
public static ZhiPuAiChatOptions fromOptions(ZhiPuAiChatOptions fromOptions) {
138139
return ZhiPuAiChatOptions.builder()
139-
.withModel(fromOptions.getModel())
140-
.withMaxTokens(fromOptions.getMaxTokens())
141-
.withStop(fromOptions.getStop())
142-
.withTemperature(fromOptions.getTemperature())
143-
.withTopP(fromOptions.getTopP())
144-
.withTools(fromOptions.getTools())
145-
.withToolChoice(fromOptions.getToolChoice())
146-
.withUser(fromOptions.getUser())
147-
.withRequestId(fromOptions.getRequestId())
148-
.withDoSample(fromOptions.getDoSample())
149-
.withFunctionCallbacks(fromOptions.getFunctionCallbacks())
150-
.withFunctions(fromOptions.getFunctions())
151-
.withProxyToolCalls(fromOptions.getProxyToolCalls())
152-
.withToolContext(fromOptions.getToolContext())
140+
.model(fromOptions.getModel())
141+
.maxTokens(fromOptions.getMaxTokens())
142+
.stop(fromOptions.getStop())
143+
.temperature(fromOptions.getTemperature())
144+
.topP(fromOptions.getTopP())
145+
.tools(fromOptions.getTools())
146+
.toolChoice(fromOptions.getToolChoice())
147+
.user(fromOptions.getUser())
148+
.requestId(fromOptions.getRequestId())
149+
.doSample(fromOptions.getDoSample())
150+
.functionCallbacks(fromOptions.getFunctionCallbacks())
151+
.functions(fromOptions.getFunctions())
152+
.proxyToolCalls(fromOptions.getProxyToolCalls())
153+
.toolContext(fromOptions.getToolContext())
153154
.build();
154155
}
155156

@@ -449,78 +450,220 @@ public Builder(ZhiPuAiChatOptions options) {
449450
this.options = options;
450451
}
451452

453+
public Builder model(String model) {
454+
this.options.model = model;
455+
return this;
456+
}
457+
458+
public Builder maxTokens(Integer maxTokens) {
459+
this.options.maxTokens = maxTokens;
460+
return this;
461+
}
462+
463+
public Builder stop(List<String> stop) {
464+
this.options.stop = stop;
465+
return this;
466+
}
467+
468+
public Builder temperature(Double temperature) {
469+
this.options.temperature = temperature;
470+
return this;
471+
}
472+
473+
public Builder topP(Double topP) {
474+
this.options.topP = topP;
475+
return this;
476+
}
477+
478+
public Builder tools(List<ZhiPuAiApi.FunctionTool> tools) {
479+
this.options.tools = tools;
480+
return this;
481+
}
482+
483+
public Builder toolChoice(String toolChoice) {
484+
this.options.toolChoice = toolChoice;
485+
return this;
486+
}
487+
488+
public Builder user(String user) {
489+
this.options.user = user;
490+
return this;
491+
}
492+
493+
public Builder requestId(String requestId) {
494+
this.options.requestId = requestId;
495+
return this;
496+
}
497+
498+
public Builder doSample(Boolean doSample) {
499+
this.options.doSample = doSample;
500+
return this;
501+
}
502+
503+
public Builder functionCallbacks(List<FunctionCallback> functionCallbacks) {
504+
this.options.functionCallbacks = functionCallbacks;
505+
return this;
506+
}
507+
508+
public Builder functions(Set<String> functionNames) {
509+
Assert.notNull(functionNames, "Function names must not be null");
510+
this.options.functions = functionNames;
511+
return this;
512+
}
513+
514+
public Builder function(String functionName) {
515+
Assert.hasText(functionName, "Function name must not be empty");
516+
this.options.functions.add(functionName);
517+
return this;
518+
}
519+
520+
public Builder proxyToolCalls(Boolean proxyToolCalls) {
521+
this.options.proxyToolCalls = proxyToolCalls;
522+
return this;
523+
}
524+
525+
public Builder toolContext(Map<String, Object> toolContext) {
526+
if (this.options.toolContext == null) {
527+
this.options.toolContext = toolContext;
528+
}
529+
else {
530+
this.options.toolContext.putAll(toolContext);
531+
}
532+
return this;
533+
}
534+
535+
/**
536+
* @deprecated use {@link #model(String)} instead.
537+
*/
538+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
452539
public Builder withModel(String model) {
453540
this.options.model = model;
454541
return this;
455542
}
456543

544+
/**
545+
* @deprecated use {@link #maxTokens(Integer)} instead.
546+
*/
547+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
457548
public Builder withMaxTokens(Integer maxTokens) {
458549
this.options.maxTokens = maxTokens;
459550
return this;
460551
}
461552

553+
/**
554+
* @deprecated use {@link #stop(List)} instead.
555+
*/
556+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
462557
public Builder withStop(List<String> stop) {
463558
this.options.stop = stop;
464559
return this;
465560
}
466561

562+
/**
563+
* @deprecated use {@link #temperature(Double)} instead.
564+
*/
565+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
467566
public Builder withTemperature(Double temperature) {
468567
this.options.temperature = temperature;
469568
return this;
470569
}
471570

571+
/**
572+
* @deprecated use {@link #topP(Double)} instead.
573+
*/
574+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
472575
public Builder withTopP(Double topP) {
473576
this.options.topP = topP;
474577
return this;
475578
}
476579

580+
/**
581+
* @deprecated use {@link #tools(List)} instead.
582+
*/
583+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
477584
public Builder withTools(List<ZhiPuAiApi.FunctionTool> tools) {
478585
this.options.tools = tools;
479586
return this;
480587
}
481588

589+
/**
590+
* @deprecated use {@link #toolChoice(String)} instead.
591+
*/
592+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
482593
public Builder withToolChoice(String toolChoice) {
483594
this.options.toolChoice = toolChoice;
484595
return this;
485596
}
486597

598+
/**
599+
* @deprecated use {@link #user(String)} instead.
600+
*/
601+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
487602
public Builder withUser(String user) {
488603
this.options.user = user;
489604
return this;
490605
}
491606

607+
/**
608+
* @deprecated use {@link #requestId(String)} instead.
609+
*/
610+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
492611
public Builder withRequestId(String requestId) {
493612
this.options.requestId = requestId;
494613
return this;
495614
}
496615

616+
/**
617+
* @deprecated use {@link #doSample(Boolean)} instead.
618+
*/
619+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
497620
public Builder withDoSample(Boolean doSample) {
498621
this.options.doSample = doSample;
499622
return this;
500623
}
501624

625+
/**
626+
* @deprecated use {@link #functionCallbacks(List)} instead.
627+
*/
628+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
502629
public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
503630
this.options.functionCallbacks = functionCallbacks;
504631
return this;
505632
}
506633

634+
/**
635+
* @deprecated use {@link #functions(Set)} instead.
636+
*/
637+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
507638
public Builder withFunctions(Set<String> functionNames) {
508639
Assert.notNull(functionNames, "Function names must not be null");
509640
this.options.functions = functionNames;
510641
return this;
511642
}
512643

644+
/**
645+
* @deprecated use {@link #function(String)} instead.
646+
*/
647+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
513648
public Builder withFunction(String functionName) {
514649
Assert.hasText(functionName, "Function name must not be empty");
515650
this.options.functions.add(functionName);
516651
return this;
517652
}
518653

654+
/**
655+
* @deprecated use {@link #proxyToolCalls(Boolean)} instead.
656+
*/
657+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
519658
public Builder withProxyToolCalls(Boolean proxyToolCalls) {
520659
this.options.proxyToolCalls = proxyToolCalls;
521660
return this;
522661
}
523662

663+
/**
664+
* @deprecated use {@link #toolContext(Map)} instead.
665+
*/
666+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
524667
public Builder withToolContext(Map<String, Object> toolContext) {
525668
if (this.options.toolContext == null) {
526669
this.options.toolContext = toolContext;

models/spring-ai-zhipuai/src/main/java/org/springframework/ai/zhipuai/ZhiPuAiEmbeddingModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ZhiPuAiEmbeddingModel(ZhiPuAiApi zhiPuAiApi) {
9090
*/
9191
public ZhiPuAiEmbeddingModel(ZhiPuAiApi zhiPuAiApi, MetadataMode metadataMode) {
9292
this(zhiPuAiApi, metadataMode,
93-
ZhiPuAiEmbeddingOptions.builder().withModel(ZhiPuAiApi.DEFAULT_EMBEDDING_MODEL).build(),
93+
ZhiPuAiEmbeddingOptions.builder().model(ZhiPuAiApi.DEFAULT_EMBEDDING_MODEL).build(),
9494
RetryUtils.DEFAULT_RETRY_TEMPLATE);
9595
}
9696

@@ -220,7 +220,7 @@ private ZhiPuAiEmbeddingOptions mergeOptions(@Nullable EmbeddingOptions runtimeO
220220
}
221221

222222
return ZhiPuAiEmbeddingOptions.builder()
223-
.withModel(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel()))
223+
.model(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel()))
224224
.build();
225225
}
226226

models/spring-ai-zhipuai/src/main/java/org/springframework/ai/zhipuai/ZhiPuAiEmbeddingOptions.java

Lines changed: 10 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.0 M1
3233
*/
3334
@JsonInclude(Include.NON_NULL)
@@ -67,6 +68,15 @@ public Builder() {
6768
this.options = new ZhiPuAiEmbeddingOptions();
6869
}
6970

71+
public Builder model(String model) {
72+
this.options.setModel(model);
73+
return this;
74+
}
75+
76+
/**
77+
* @deprecated use {@link #model(String)} instead.
78+
*/
79+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
7080
public Builder withModel(String model) {
7181
this.options.setModel(model);
7282
return this;

models/spring-ai-zhipuai/src/main/java/org/springframework/ai/zhipuai/ZhiPuAiImageModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ private ZhiPuAiImageOptions toZhiPuAiImageOptions(ImageOptions runtimeImageOptio
122122
ZhiPuAiImageOptions.Builder zhiPuAiImageOptionsBuilder = ZhiPuAiImageOptions.builder();
123123
if (runtimeImageOptions != null) {
124124
if (runtimeImageOptions.getModel() != null) {
125-
zhiPuAiImageOptionsBuilder.withModel(runtimeImageOptions.getModel());
125+
zhiPuAiImageOptionsBuilder.model(runtimeImageOptions.getModel());
126126
}
127127
if (runtimeImageOptions instanceof ZhiPuAiImageOptions runtimeZhiPuAiImageOptions) {
128128
if (runtimeZhiPuAiImageOptions.getUser() != null) {
129-
zhiPuAiImageOptionsBuilder.withUser(runtimeZhiPuAiImageOptions.getUser());
129+
zhiPuAiImageOptionsBuilder.user(runtimeZhiPuAiImageOptions.getUser());
130130
}
131131
}
132132
}

models/spring-ai-zhipuai/src/main/java/org/springframework/ai/zhipuai/ZhiPuAiImageOptions.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* </ul>
4444
*
4545
* @author Geng Rong
46+
* @author Ilayaperumal Gopinathan
4647
* @since 1.0.0 M1
4748
*/
4849
@JsonInclude(JsonInclude.Include.NON_NULL)
@@ -142,11 +143,29 @@ private Builder() {
142143
this.options = new ZhiPuAiImageOptions();
143144
}
144145

146+
public Builder model(String model) {
147+
this.options.setModel(model);
148+
return this;
149+
}
150+
151+
public Builder user(String user) {
152+
this.options.setUser(user);
153+
return this;
154+
}
155+
156+
/**
157+
* @deprecated use {@link #model(String)} instead.
158+
*/
159+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
145160
public Builder withModel(String model) {
146161
this.options.setModel(model);
147162
return this;
148163
}
149164

165+
/**
166+
* @deprecated use {@link #user(String)} instead.
167+
*/
168+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
150169
public Builder withUser(String user) {
151170
this.options.setUser(user);
152171
return this;

0 commit comments

Comments
 (0)