Skip to content

Commit 9dd3dda

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 9dd3dda

File tree

10 files changed

+207
-75
lines changed

10 files changed

+207
-75
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
* MoonshotChatModel is a {@link ChatModel} implementation that uses the Moonshot
7575
*
7676
* @author Geng Rong
77+
* @author Alexandros Pappas
7778
*/
7879
public class MoonshotChatModel extends AbstractToolCallSupport implements ChatModel, StreamingChatModel {
7980

@@ -109,7 +110,7 @@ public class MoonshotChatModel extends AbstractToolCallSupport implements ChatMo
109110
* Moonshot Chat API.
110111
*/
111112
public MoonshotChatModel(MoonshotApi moonshotApi) {
112-
this(moonshotApi, MoonshotChatOptions.builder().withModel(MoonshotApi.DEFAULT_CHAT_MODEL).build());
113+
this(moonshotApi, MoonshotChatOptions.builder().model(MoonshotApi.DEFAULT_CHAT_MODEL).build());
113114
}
114115

115116
/**
@@ -411,7 +412,7 @@ else if (message.getMessageType() == MessageType.TOOL) {
411412
if (!CollectionUtils.isEmpty(enabledToolsToUse)) {
412413

413414
request = ModelOptionsUtils.merge(
414-
MoonshotChatOptions.builder().withTools(this.getFunctionTools(enabledToolsToUse)).build(), request,
415+
MoonshotChatOptions.builder().tools(this.getFunctionTools(enabledToolsToUse)).build(), request,
415416
ChatCompletionRequest.class);
416417
}
417418

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

Lines changed: 164 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* @author Geng Rong
3838
* @author Thomas Vitale
39+
* @author Alexandros Pappas
3940
*/
4041
@JsonInclude(JsonInclude.Include.NON_NULL)
4142
public class MoonshotChatOptions implements FunctionCallingOptions {
@@ -281,21 +282,21 @@ public void setToolContext(Map<String, Object> toolContext) {
281282

282283
@Override
283284
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)
285+
return builder().model(this.model)
286+
.maxTokens(this.maxTokens)
287+
.temperature(this.temperature)
288+
.topP(this.topP)
289+
.N(this.n)
290+
.presencePenalty(this.presencePenalty)
291+
.frequencyPenalty(this.frequencyPenalty)
292+
.stop(this.stop)
293+
.user(this.user)
294+
.tools(this.tools)
295+
.toolChoice(this.toolChoice)
296+
.functionCallbacks(this.functionCallbacks)
297+
.functions(this.functions)
298+
.proxyToolCalls(this.proxyToolCalls)
299+
.toolContext(this.toolContext)
299300
.build();
300301
}
301302

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

417418
public static class Builder {
418419

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

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) {
422+
public Builder model(String model) {
430423
this.options.model = model;
431424
return this;
432425
}
433426

434-
public Builder withMaxTokens(Integer maxTokens) {
427+
public Builder maxTokens(Integer maxTokens) {
435428
this.options.maxTokens = maxTokens;
436429
return this;
437430
}
438431

439-
public Builder withTemperature(Double temperature) {
432+
public Builder temperature(Double temperature) {
440433
this.options.temperature = temperature;
441434
return this;
442435
}
443436

444-
public Builder withTopP(Double topP) {
437+
public Builder topP(Double topP) {
445438
this.options.topP = topP;
446439
return this;
447440
}
448441

449-
public Builder withN(Integer n) {
442+
public Builder N(Integer n) {
450443
this.options.n = n;
451444
return this;
452445
}
453446

454-
public Builder withPresencePenalty(Double presencePenalty) {
447+
public Builder presencePenalty(Double presencePenalty) {
455448
this.options.presencePenalty = presencePenalty;
456449
return this;
457450
}
458451

459-
public Builder withFrequencyPenalty(Double frequencyPenalty) {
452+
public Builder frequencyPenalty(Double frequencyPenalty) {
460453
this.options.frequencyPenalty = frequencyPenalty;
461454
return this;
462455
}
463456

464-
public Builder withStop(List<String> stop) {
457+
public Builder stop(List<String> stop) {
465458
this.options.stop = stop;
466459
return this;
467460
}
468461

469-
public Builder withUser(String user) {
462+
public Builder user(String user) {
470463
this.options.user = user;
471464
return this;
472465
}
473466

474-
public Builder withTools(List<MoonshotApi.FunctionTool> tools) {
467+
public Builder tools(List<MoonshotApi.FunctionTool> tools) {
475468
this.options.tools = tools;
476469
return this;
477470
}
478471

479-
public Builder withToolChoice(String toolChoice) {
472+
public Builder toolChoice(String toolChoice) {
480473
this.options.toolChoice = toolChoice;
481474
return this;
482475
}
483476

484-
public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
477+
public Builder functionCallbacks(List<FunctionCallback> functionCallbacks) {
485478
this.options.functionCallbacks = functionCallbacks;
486479
return this;
487480
}
488481

489-
public Builder withFunctions(Set<String> functionNames) {
482+
public Builder functions(Set<String> functionNames) {
490483
Assert.notNull(functionNames, "Function names must not be null");
491484
this.options.functions = functionNames;
492485
return this;
493486
}
494487

495-
public Builder withFunction(String functionName) {
488+
public Builder function(String functionName) {
496489
Assert.hasText(functionName, "Function name must not be empty");
490+
if (this.options.functions == null) {
491+
this.options.functions = new HashSet<>();
492+
}
497493
this.options.functions.add(functionName);
498494
return this;
499495
}
500496

501-
public Builder withProxyToolCalls(Boolean proxyToolCalls) {
497+
public Builder proxyToolCalls(Boolean proxyToolCalls) {
502498
this.options.proxyToolCalls = proxyToolCalls;
503499
return this;
504500
}
505501

506-
public Builder withToolContext(Map<String, Object> toolContext) {
502+
public Builder toolContext(Map<String, Object> toolContext) {
507503
if (this.options.toolContext == null) {
508504
this.options.toolContext = toolContext;
509505
}
@@ -513,6 +509,134 @@ public Builder withToolContext(Map<String, Object> toolContext) {
513509
return this;
514510
}
515511

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @author Geng Rong
30+
* @author Alexandros Pappas
3031
*/
3132
@SpringBootTest
3233
@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".+")
@@ -47,7 +48,7 @@ void chatCompletionDefaultRequestTest() {
4748

4849
@Test
4950
void chatCompletionRequestWithOptionsTest() {
50-
var options = MoonshotChatOptions.builder().withTemperature(0.5).withTopP(0.8).build();
51+
var options = MoonshotChatOptions.builder().temperature(0.5).topP(0.8).build();
5152
var request = this.chatModel.createRequest(new Prompt("test content", options), true);
5253

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

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
/**
5151
* @author Geng Rong
52+
* @author Alexandros Pappas
5253
*/
5354
@SuppressWarnings("unchecked")
5455
@ExtendWith(MockitoExtension.class)
@@ -68,9 +69,9 @@ public void beforeEach() {
6869

6970
this.chatModel = new MoonshotChatModel(this.moonshotApi,
7071
MoonshotChatOptions.builder()
71-
.withTemperature(0.7)
72-
.withTopP(1.0)
73-
.withModel(MoonshotApi.ChatModel.MOONSHOT_V1_32K.getValue())
72+
.temperature(0.7)
73+
.topP(1.0)
74+
.model(MoonshotApi.ChatModel.MOONSHOT_V1_32K.getValue())
7475
.build(),
7576
null, retryTemplate);
7677
}

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()))

0 commit comments

Comments
 (0)