Skip to content

Commit aaff7f0

Browse files
committed
Refactor MoonshotChatOptions builder methods
- Refactor the builder methods to remove `with` as the prefix. - Introduce new methods with updated naming conventions. - Deprecate the existing `with*` methods to maintain backward compatibility. - Update the `copy()` method to use the new builder methods.
1 parent d3d34c9 commit aaff7f0

File tree

10 files changed

+198
-75
lines changed

10 files changed

+198
-75
lines changed

models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/MoonshotChatModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class MoonshotChatModel extends AbstractToolCallSupport implements ChatMo
109109
* Moonshot Chat API.
110110
*/
111111
public MoonshotChatModel(MoonshotApi moonshotApi) {
112-
this(moonshotApi, MoonshotChatOptions.builder().withModel(MoonshotApi.DEFAULT_CHAT_MODEL).build());
112+
this(moonshotApi, MoonshotChatOptions.builder().model(MoonshotApi.DEFAULT_CHAT_MODEL).build());
113113
}
114114

115115
/**
@@ -411,7 +411,7 @@ else if (message.getMessageType() == MessageType.TOOL) {
411411
if (!CollectionUtils.isEmpty(enabledToolsToUse)) {
412412

413413
request = ModelOptionsUtils.merge(
414-
MoonshotChatOptions.builder().withTools(this.getFunctionTools(enabledToolsToUse)).build(), request,
414+
MoonshotChatOptions.builder().tools(this.getFunctionTools(enabledToolsToUse)).build(), request,
415415
ChatCompletionRequest.class);
416416
}
417417

models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/MoonshotChatOptions.java

Lines changed: 163 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,21 @@ public void setToolContext(Map<String, Object> toolContext) {
281281

282282
@Override
283283
public MoonshotChatOptions copy() {
284-
return builder().withModel(this.model)
285-
.withMaxTokens(this.maxTokens)
286-
.withTemperature(this.temperature)
287-
.withTopP(this.topP)
288-
.withN(this.n)
289-
.withPresencePenalty(this.presencePenalty)
290-
.withFrequencyPenalty(this.frequencyPenalty)
291-
.withStop(this.stop)
292-
.withUser(this.user)
293-
.withTools(this.tools)
294-
.withToolChoice(this.toolChoice)
295-
.withFunctionCallbacks(this.functionCallbacks)
296-
.withFunctions(this.functions)
297-
.withProxyToolCalls(this.proxyToolCalls)
298-
.withToolContext(this.toolContext)
284+
return builder().model(this.model)
285+
.maxTokens(this.maxTokens)
286+
.temperature(this.temperature)
287+
.topP(this.topP)
288+
.N(this.n)
289+
.presencePenalty(this.presencePenalty)
290+
.frequencyPenalty(this.frequencyPenalty)
291+
.stop(this.stop)
292+
.user(this.user)
293+
.tools(this.tools)
294+
.toolChoice(this.toolChoice)
295+
.functionCallbacks(this.functionCallbacks)
296+
.functions(this.functions)
297+
.proxyToolCalls(this.proxyToolCalls)
298+
.toolContext(this.toolContext)
299299
.build();
300300
}
301301

@@ -416,94 +416,89 @@ else if (!this.toolContext.equals(other.toolContext)) {
416416

417417
public static class Builder {
418418

419-
protected MoonshotChatOptions options;
419+
private final MoonshotChatOptions options = new MoonshotChatOptions();
420420

421-
public Builder() {
422-
this.options = new MoonshotChatOptions();
423-
}
424-
425-
public Builder(MoonshotChatOptions options) {
426-
this.options = options;
427-
}
428-
429-
public Builder withModel(String model) {
421+
public Builder model(String model) {
430422
this.options.model = model;
431423
return this;
432424
}
433425

434-
public Builder withMaxTokens(Integer maxTokens) {
426+
public Builder maxTokens(Integer maxTokens) {
435427
this.options.maxTokens = maxTokens;
436428
return this;
437429
}
438430

439-
public Builder withTemperature(Double temperature) {
431+
public Builder temperature(Double temperature) {
440432
this.options.temperature = temperature;
441433
return this;
442434
}
443435

444-
public Builder withTopP(Double topP) {
436+
public Builder topP(Double topP) {
445437
this.options.topP = topP;
446438
return this;
447439
}
448440

449-
public Builder withN(Integer n) {
441+
public Builder N(Integer n) {
450442
this.options.n = n;
451443
return this;
452444
}
453445

454-
public Builder withPresencePenalty(Double presencePenalty) {
446+
public Builder presencePenalty(Double presencePenalty) {
455447
this.options.presencePenalty = presencePenalty;
456448
return this;
457449
}
458450

459-
public Builder withFrequencyPenalty(Double frequencyPenalty) {
451+
public Builder frequencyPenalty(Double frequencyPenalty) {
460452
this.options.frequencyPenalty = frequencyPenalty;
461453
return this;
462454
}
463455

464-
public Builder withStop(List<String> stop) {
456+
public Builder stop(List<String> stop) {
465457
this.options.stop = stop;
466458
return this;
467459
}
468460

469-
public Builder withUser(String user) {
461+
public Builder user(String user) {
470462
this.options.user = user;
471463
return this;
472464
}
473465

474-
public Builder withTools(List<MoonshotApi.FunctionTool> tools) {
466+
public Builder tools(List<MoonshotApi.FunctionTool> tools) {
475467
this.options.tools = tools;
476468
return this;
477469
}
478470

479-
public Builder withToolChoice(String toolChoice) {
471+
public Builder toolChoice(String toolChoice) {
480472
this.options.toolChoice = toolChoice;
481473
return this;
482474
}
483475

484-
public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
476+
public Builder functionCallbacks(List<FunctionCallback> functionCallbacks) {
485477
this.options.functionCallbacks = functionCallbacks;
486478
return this;
487479
}
488480

489-
public Builder withFunctions(Set<String> functionNames) {
481+
public Builder functions(Set<String> functionNames) {
490482
Assert.notNull(functionNames, "Function names must not be null");
491483
this.options.functions = functionNames;
492484
return this;
493485
}
494486

495-
public Builder withFunction(String functionName) {
487+
public Builder function(String functionName) {
496488
Assert.hasText(functionName, "Function name must not be empty");
489+
if (this.options.functions == null) {
490+
this.options.functions = new HashSet<>();
491+
}
497492
this.options.functions.add(functionName);
498493
return this;
499494
}
500495

501-
public Builder withProxyToolCalls(Boolean proxyToolCalls) {
496+
public Builder proxyToolCalls(Boolean proxyToolCalls) {
502497
this.options.proxyToolCalls = proxyToolCalls;
503498
return this;
504499
}
505500

506-
public Builder withToolContext(Map<String, Object> toolContext) {
501+
public Builder toolContext(Map<String, Object> toolContext) {
507502
if (this.options.toolContext == null) {
508503
this.options.toolContext = toolContext;
509504
}
@@ -513,6 +508,134 @@ public Builder withToolContext(Map<String, Object> toolContext) {
513508
return this;
514509
}
515510

511+
/**
512+
* @deprecated use {@link #functions(Set)} instead.
513+
*/
514+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
515+
public Builder withFunctions(Set<String> functionNames) {
516+
return functions(functionNames);
517+
}
518+
519+
/**
520+
* @deprecated use {@link #function(String)} instead.
521+
*/
522+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
523+
public Builder withFunction(String functionName) {
524+
return function(functionName);
525+
}
526+
527+
/**
528+
* @deprecated use {@link #model(String)} instead.
529+
*/
530+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
531+
public Builder withModel(String model) {
532+
return model(model);
533+
}
534+
535+
/**
536+
* @deprecated use {@link #maxTokens(Integer)} instead.
537+
*/
538+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
539+
public Builder withMaxTokens(Integer maxTokens) {
540+
return maxTokens(maxTokens);
541+
}
542+
543+
/**
544+
* @deprecated use {@link #temperature(Double)} instead.
545+
*/
546+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
547+
public Builder withTemperature(Double temperature) {
548+
return temperature(temperature);
549+
}
550+
551+
/**
552+
* @deprecated use {@link #topP(Double)} instead.
553+
*/
554+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
555+
public Builder withTopP(Double topP) {
556+
return topP(topP);
557+
}
558+
559+
/**
560+
* @deprecated use {@link #N(Integer)} instead.
561+
*/
562+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
563+
public Builder withN(Integer n) {
564+
return N(n);
565+
}
566+
567+
/**
568+
* @deprecated use {@link #presencePenalty(Double)} instead.
569+
*/
570+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
571+
public Builder withPresencePenalty(Double presencePenalty) {
572+
return presencePenalty(presencePenalty);
573+
}
574+
575+
/**
576+
* @deprecated use {@link #frequencyPenalty(Double)} instead.
577+
*/
578+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
579+
public Builder withFrequencyPenalty(Double frequencyPenalty) {
580+
return frequencyPenalty(frequencyPenalty);
581+
}
582+
583+
/**
584+
* @deprecated use {@link #stop(List)} instead.
585+
*/
586+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
587+
public Builder withStop(List<String> stop) {
588+
return stop(stop);
589+
}
590+
591+
/**
592+
* @deprecated use {@link #user(String)} instead.
593+
*/
594+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
595+
public Builder withUser(String user) {
596+
return user(user);
597+
}
598+
599+
/**
600+
* @deprecated use {@link #tools(List)} instead.
601+
*/
602+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
603+
public Builder withTools(List<MoonshotApi.FunctionTool> tools) {
604+
return tools(tools);
605+
}
606+
607+
/**
608+
* @deprecated use {@link #toolChoice(String)} instead.
609+
*/
610+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
611+
public Builder withToolChoice(String toolChoice) {
612+
return toolChoice(toolChoice);
613+
}
614+
615+
/**
616+
* @deprecated use {@link #functionCallbacks(List)} instead.
617+
*/
618+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
619+
public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
620+
return functionCallbacks(functionCallbacks);
621+
}
622+
623+
/**
624+
* @deprecated use {@link #proxyToolCalls(Boolean)} instead.
625+
*/
626+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
627+
public Builder withProxyToolCalls(Boolean proxyToolCalls) {
628+
return proxyToolCalls(proxyToolCalls);
629+
}
630+
631+
/**
632+
* @deprecated use {@link #toolContext(Map)} instead.
633+
*/
634+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
635+
public Builder withToolContext(Map<String, Object> toolContext) {
636+
return toolContext(toolContext);
637+
}
638+
516639
public MoonshotChatOptions build() {
517640
return this.options;
518641
}

models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotChatCompletionRequestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void chatCompletionDefaultRequestTest() {
4747

4848
@Test
4949
void chatCompletionRequestWithOptionsTest() {
50-
var options = MoonshotChatOptions.builder().withTemperature(0.5).withTopP(0.8).build();
50+
var options = MoonshotChatOptions.builder().temperature(0.5).topP(0.8).build();
5151
var request = this.chatModel.createRequest(new Prompt("test content", options), true);
5252

5353
assertThat(request.messages().size()).isEqualTo(1);

models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotRetryTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public void beforeEach() {
6868

6969
this.chatModel = new MoonshotChatModel(this.moonshotApi,
7070
MoonshotChatOptions.builder()
71-
.withTemperature(0.7)
72-
.withTopP(1.0)
73-
.withModel(MoonshotApi.ChatModel.MOONSHOT_V1_32K.getValue())
71+
.temperature(0.7)
72+
.topP(1.0)
73+
.model(MoonshotApi.ChatModel.MOONSHOT_V1_32K.getValue())
7474
.build(),
7575
null, retryTemplate);
7676
}

models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/chat/MoonshotChatModelFunctionCallingIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ void functionCallTest() {
6262
List<Message> messages = new ArrayList<>(List.of(userMessage));
6363

6464
var promptOptions = MoonshotChatOptions.builder()
65-
.withModel(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue())
66-
.withFunctionCallbacks(List.of(FunctionCallback.builder()
65+
.model(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue())
66+
.functionCallbacks(List.of(FunctionCallback.builder()
6767
.function("getCurrentWeather", new MockWeatherService())
6868
.description("Get the weather in location")
6969
.inputType(MockWeatherService.Request.class)
@@ -86,7 +86,7 @@ void streamFunctionCallTest() {
8686
List<Message> messages = new ArrayList<>(List.of(userMessage));
8787

8888
var promptOptions = MoonshotChatOptions.builder()
89-
.withFunctionCallbacks(List.of(FunctionCallback.builder()
89+
.functionCallbacks(List.of(FunctionCallback.builder()
9090
.function("getCurrentWeather", new MockWeatherService())
9191
.description("Get the weather in location")
9292
.build()))

models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/chat/MoonshotChatModelObservationIT.java

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

7272
var options = MoonshotChatOptions.builder()
73-
.withModel(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue())
74-
.withFrequencyPenalty(0.0)
75-
.withMaxTokens(2048)
76-
.withPresencePenalty(0.0)
77-
.withStop(List.of("this-is-the-end"))
78-
.withTemperature(0.7)
79-
.withTopP(1.0)
73+
.model(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue())
74+
.frequencyPenalty(0.0)
75+
.maxTokens(2048)
76+
.presencePenalty(0.0)
77+
.stop(List.of("this-is-the-end"))
78+
.temperature(0.7)
79+
.topP(1.0)
8080
.build();
8181

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

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

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotChatProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public class MoonshotChatProperties extends MoonshotParentProperties {
4242

4343
@NestedConfigurationProperty
4444
private MoonshotChatOptions options = MoonshotChatOptions.builder()
45-
.withModel(DEFAULT_CHAT_MODEL)
46-
.withTemperature(DEFAULT_TEMPERATURE)
45+
.model(DEFAULT_CHAT_MODEL)
46+
.temperature(DEFAULT_TEMPERATURE)
4747
.build();
4848

4949
public MoonshotChatOptions getOptions() {

0 commit comments

Comments
 (0)