Skip to content

Commit 5804e93

Browse files
committed
Refactor MistralAiChatOptions 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 MistralAiChatOptions builder documentation
1 parent 627fb79 commit 5804e93

File tree

14 files changed

+211
-75
lines changed

14 files changed

+211
-75
lines changed

models/spring-ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/MistralAiChatModel.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
* @author Thomas Vitale
7878
* @author luocongqiu
7979
* @author Ilayaperumal Gopinathan
80+
* @author Alexandros Pappas
8081
* @since 1.0.0
8182
*/
8283
public class MistralAiChatModel extends AbstractToolCallSupport implements ChatModel {
@@ -110,10 +111,10 @@ public class MistralAiChatModel extends AbstractToolCallSupport implements ChatM
110111
public MistralAiChatModel(MistralAiApi mistralAiApi) {
111112
this(mistralAiApi,
112113
MistralAiChatOptions.builder()
113-
.withTemperature(0.7)
114-
.withTopP(1.0)
115-
.withSafePrompt(false)
116-
.withModel(MistralAiApi.ChatModel.OPEN_MISTRAL_7B.getValue())
114+
.temperature(0.7)
115+
.topP(1.0)
116+
.safePrompt(false)
117+
.model(MistralAiApi.ChatModel.OPEN_MISTRAL_7B.getValue())
117118
.build());
118119
}
119120

@@ -416,7 +417,7 @@ else if (message instanceof ToolResponseMessage toolResponseMessage) {
416417
if (!CollectionUtils.isEmpty(functionsForThisRequest)) {
417418

418419
request = ModelOptionsUtils.merge(
419-
MistralAiChatOptions.builder().withTools(this.getFunctionTools(functionsForThisRequest)).build(),
420+
MistralAiChatOptions.builder().tools(this.getFunctionTools(functionsForThisRequest)).build(),
420421
request, ChatCompletionRequest.class);
421422
}
422423

models/spring-ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/MistralAiChatOptions.java

Lines changed: 159 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Ricken Bazolo
4242
* @author Christian Tzolov
4343
* @author Thomas Vitale
44+
* @author Alexandros Pappas
4445
* @since 0.8.1
4546
*/
4647
@JsonInclude(JsonInclude.Include.NON_NULL)
@@ -145,20 +146,20 @@ public static Builder builder() {
145146
}
146147

147148
public static MistralAiChatOptions fromOptions(MistralAiChatOptions fromOptions) {
148-
return builder().withModel(fromOptions.getModel())
149-
.withMaxTokens(fromOptions.getMaxTokens())
150-
.withSafePrompt(fromOptions.getSafePrompt())
151-
.withRandomSeed(fromOptions.getRandomSeed())
152-
.withTemperature(fromOptions.getTemperature())
153-
.withTopP(fromOptions.getTopP())
154-
.withResponseFormat(fromOptions.getResponseFormat())
155-
.withStop(fromOptions.getStop())
156-
.withTools(fromOptions.getTools())
157-
.withToolChoice(fromOptions.getToolChoice())
158-
.withFunctionCallbacks(fromOptions.getFunctionCallbacks())
159-
.withFunctions(fromOptions.getFunctions())
160-
.withProxyToolCalls(fromOptions.getProxyToolCalls())
161-
.withToolContext(fromOptions.getToolContext())
149+
return builder().model(fromOptions.getModel())
150+
.maxTokens(fromOptions.getMaxTokens())
151+
.safePrompt(fromOptions.getSafePrompt())
152+
.randomSeed(fromOptions.getRandomSeed())
153+
.temperature(fromOptions.getTemperature())
154+
.topP(fromOptions.getTopP())
155+
.responseFormat(fromOptions.getResponseFormat())
156+
.stop(fromOptions.getStop())
157+
.tools(fromOptions.getTools())
158+
.toolChoice(fromOptions.getToolChoice())
159+
.functionCallbacks(fromOptions.getFunctionCallbacks())
160+
.functions(fromOptions.getFunctions())
161+
.proxyToolCalls(fromOptions.getProxyToolCalls())
162+
.toolContext(fromOptions.getToolContext())
162163
.build();
163164
}
164165

@@ -357,84 +358,84 @@ public static class Builder {
357358

358359
private final MistralAiChatOptions options = new MistralAiChatOptions();
359360

360-
public Builder withModel(String model) {
361+
public Builder model(String model) {
361362
this.options.setModel(model);
362363
return this;
363364
}
364365

365-
public Builder withModel(MistralAiApi.ChatModel chatModel) {
366+
public Builder model(MistralAiApi.ChatModel chatModel) {
366367
this.options.setModel(chatModel.getName());
367368
return this;
368369
}
369370

370-
public Builder withMaxTokens(Integer maxTokens) {
371+
public Builder maxTokens(Integer maxTokens) {
371372
this.options.setMaxTokens(maxTokens);
372373
return this;
373374
}
374375

375-
public Builder withSafePrompt(Boolean safePrompt) {
376+
public Builder safePrompt(Boolean safePrompt) {
376377
this.options.setSafePrompt(safePrompt);
377378
return this;
378379
}
379380

380-
public Builder withRandomSeed(Integer randomSeed) {
381+
public Builder randomSeed(Integer randomSeed) {
381382
this.options.setRandomSeed(randomSeed);
382383
return this;
383384
}
384385

385-
public Builder withStop(List<String> stop) {
386+
public Builder stop(List<String> stop) {
386387
this.options.setStop(stop);
387388
return this;
388389
}
389390

390-
public Builder withTemperature(Double temperature) {
391+
public Builder temperature(Double temperature) {
391392
this.options.setTemperature(temperature);
392393
return this;
393394
}
394395

395-
public Builder withTopP(Double topP) {
396+
public Builder topP(Double topP) {
396397
this.options.setTopP(topP);
397398
return this;
398399
}
399400

400-
public Builder withResponseFormat(ResponseFormat responseFormat) {
401+
public Builder responseFormat(ResponseFormat responseFormat) {
401402
this.options.responseFormat = responseFormat;
402403
return this;
403404
}
404405

405-
public Builder withTools(List<FunctionTool> tools) {
406+
public Builder tools(List<FunctionTool> tools) {
406407
this.options.tools = tools;
407408
return this;
408409
}
409410

410-
public Builder withToolChoice(ToolChoice toolChoice) {
411+
public Builder toolChoice(ToolChoice toolChoice) {
411412
this.options.toolChoice = toolChoice;
412413
return this;
413414
}
414415

415-
public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
416+
public Builder functionCallbacks(List<FunctionCallback> functionCallbacks) {
416417
this.options.functionCallbacks = functionCallbacks;
417418
return this;
418419
}
419420

420-
public Builder withFunctions(Set<String> functionNames) {
421+
public Builder functions(Set<String> functionNames) {
421422
Assert.notNull(functionNames, "Function names must not be null");
422423
this.options.functions = functionNames;
423424
return this;
424425
}
425426

426-
public Builder withFunction(String functionName) {
427+
public Builder function(String functionName) {
427428
Assert.hasText(functionName, "Function name must not be empty");
428429
this.options.functions.add(functionName);
429430
return this;
430431
}
431432

432-
public Builder withProxyToolCalls(Boolean proxyToolCalls) {
433+
public Builder proxyToolCalls(Boolean proxyToolCalls) {
433434
this.options.proxyToolCalls = proxyToolCalls;
434435
return this;
435436
}
436437

437-
public Builder withToolContext(Map<String, Object> toolContext) {
438+
public Builder toolContext(Map<String, Object> toolContext) {
438439
if (this.options.toolContext == null) {
439440
this.options.toolContext = toolContext;
440441
}
@@ -444,6 +445,134 @@ public Builder withToolContext(Map<String, Object> toolContext) {
444445
return this;
445446
}
446447

448+
/**
449+
* @deprecated use {@link #model(String)} instead.
450+
*/
451+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
452+
public Builder withModel(String model) {
453+
return model(model);
454+
}
455+
456+
/**
457+
* @deprecated use {@link #model(MistralAiApi.ChatModel)} instead.
458+
*/
459+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
460+
public Builder withModel(MistralAiApi.ChatModel chatModel) {
461+
return model(chatModel);
462+
}
463+
464+
/**
465+
* @deprecated use {@link #maxTokens(Integer)} instead.
466+
*/
467+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
468+
public Builder withMaxTokens(Integer maxTokens) {
469+
return maxTokens(maxTokens);
470+
}
471+
472+
/**
473+
* @deprecated use {@link #safePrompt(Boolean)} instead.
474+
*/
475+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
476+
public Builder withSafePrompt(Boolean safePrompt) {
477+
return safePrompt(safePrompt);
478+
}
479+
480+
/**
481+
* @deprecated use {@link #randomSeed(Integer)} instead.
482+
*/
483+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
484+
public Builder withRandomSeed(Integer randomSeed) {
485+
return randomSeed(randomSeed);
486+
}
487+
488+
/**
489+
* @deprecated use {@link #stop(List)} instead.
490+
*/
491+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
492+
public Builder withStop(List<String> stop) {
493+
return stop(stop);
494+
}
495+
496+
/**
497+
* @deprecated use {@link #temperature(Double)} instead.
498+
*/
499+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
500+
public Builder withTemperature(Double temperature) {
501+
return temperature(temperature);
502+
}
503+
504+
/**
505+
* @deprecated use {@link #topP(Double)} instead.
506+
*/
507+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
508+
public Builder withTopP(Double topP) {
509+
return topP(topP);
510+
}
511+
512+
/**
513+
* @deprecated use {@link #responseFormat(ResponseFormat)} instead.
514+
*/
515+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
516+
public Builder withResponseFormat(ResponseFormat responseFormat) {
517+
return responseFormat(responseFormat);
518+
}
519+
520+
/**
521+
* @deprecated use {@link #tools(List)} instead.
522+
*/
523+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
524+
public Builder withTools(List<FunctionTool> tools) {
525+
return tools(tools);
526+
}
527+
528+
/**
529+
* @deprecated use {@link #toolChoice(ToolChoice)} instead.
530+
*/
531+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
532+
public Builder withToolChoice(ToolChoice toolChoice) {
533+
return toolChoice(toolChoice);
534+
}
535+
536+
/**
537+
* @deprecated use {@link #functionCallbacks(List)} instead.
538+
*/
539+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
540+
public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
541+
return functionCallbacks(functionCallbacks);
542+
}
543+
544+
/**
545+
* @deprecated use {@link #functions(Set)} instead.
546+
*/
547+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
548+
public Builder withFunctions(Set<String> functionNames) {
549+
return functions(functionNames);
550+
}
551+
552+
/**
553+
* @deprecated use {@link #function(String)} instead.
554+
*/
555+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
556+
public Builder withFunction(String functionName) {
557+
return function(functionName);
558+
}
559+
560+
/**
561+
* @deprecated use {@link #proxyToolCalls(Boolean)} instead.
562+
*/
563+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
564+
public Builder withProxyToolCalls(Boolean proxyToolCalls) {
565+
return proxyToolCalls(proxyToolCalls);
566+
}
567+
568+
/**
569+
* @deprecated use {@link #toolContext(Map)} instead.
570+
*/
571+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
572+
public Builder withToolContext(Map<String, Object> toolContext) {
573+
return toolContext(toolContext);
574+
}
575+
447576
public MistralAiChatOptions build() {
448577
return this.options;
449578
}

models/spring-ai-mistral-ai/src/test/java/org/springframework/ai/mistralai/MistralAiChatClientIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void functionCallTest() {
223223

224224
// @formatter:off
225225
String response = ChatClient.create(this.chatModel).prompt()
226-
.options(MistralAiChatOptions.builder().withModel(MistralAiApi.ChatModel.SMALL).withToolChoice(ToolChoice.AUTO).build())
226+
.options(MistralAiChatOptions.builder().model(MistralAiApi.ChatModel.SMALL).toolChoice(ToolChoice.AUTO).build())
227227
.user(u -> u.text("What's the weather like in San Francisco, Tokyo, and Paris? Use parallel function calling if required. Response should be in Celsius."))
228228
.functions(FunctionCallback.builder()
229229
.function("getCurrentWeather", new MockWeatherService())
@@ -246,7 +246,7 @@ void defaultFunctionCallTest() {
246246

247247
// @formatter:off
248248
String response = ChatClient.builder(this.chatModel)
249-
.defaultOptions(MistralAiChatOptions.builder().withModel(MistralAiApi.ChatModel.SMALL).build())
249+
.defaultOptions(MistralAiChatOptions.builder().model(MistralAiApi.ChatModel.SMALL).build())
250250
.defaultFunctions(FunctionCallback.builder()
251251
.function("getCurrentWeather", new MockWeatherService())
252252
.description("Get the weather in location")
@@ -269,7 +269,7 @@ void streamFunctionCallTest() {
269269

270270
// @formatter:off
271271
Flux<String> response = ChatClient.create(this.chatModel).prompt()
272-
.options(MistralAiChatOptions.builder().withModel(MistralAiApi.ChatModel.SMALL).build())
272+
.options(MistralAiChatOptions.builder().model(MistralAiApi.ChatModel.SMALL).build())
273273
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use parallel function calling if required. Response should be in Celsius.")
274274
.functions(FunctionCallback.builder()
275275
.function("getCurrentWeather", new MockWeatherService())
@@ -295,7 +295,7 @@ void validateCallResponseMetadata() {
295295
// String model = MistralAiApi.ChatModel.PIXTRAL_LARGE.getName();
296296
// @formatter:off
297297
ChatResponse response = ChatClient.create(this.chatModel).prompt()
298-
.options(MistralAiChatOptions.builder().withModel(model).build())
298+
.options(MistralAiChatOptions.builder().model(model).build())
299299
.user("Tell me about 3 famous pirates from the Golden Age of Piracy and what they did")
300300
.call()
301301
.chatResponse();

models/spring-ai-mistral-ai/src/test/java/org/springframework/ai/mistralai/MistralAiChatCompletionRequestTest.java

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

2828
/**
2929
* @author Ricken Bazolo
30+
* @author Alexandros Pappas
3031
* @since 0.8.1
3132
*/
3233
@SpringBootTest(classes = MistralAiTestConfiguration.class)
@@ -51,7 +52,7 @@ void chatCompletionDefaultRequestTest() {
5152
@Test
5253
void chatCompletionRequestWithOptionsTest() {
5354

54-
var options = MistralAiChatOptions.builder().withTemperature(0.5).withTopP(0.8).build();
55+
var options = MistralAiChatOptions.builder().temperature(0.5).topP(0.8).build();
5556

5657
var request = this.chatModel.createRequest(new Prompt("test content", options), true);
5758

models/spring-ai-mistral-ai/src/test/java/org/springframework/ai/mistralai/MistralAiChatModelIT.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
/**
5555
* @author Christian Tzolov
56+
* @author Alexandros Pappas
5657
* @since 0.8.1
5758
*/
5859
@SpringBootTest(classes = MistralAiTestConfiguration.class)
@@ -192,8 +193,8 @@ void functionCallTest() {
192193
List<Message> messages = new ArrayList<>(List.of(userMessage));
193194

194195
var promptOptions = MistralAiChatOptions.builder()
195-
.withModel(MistralAiApi.ChatModel.SMALL.getValue())
196-
.withFunctionCallbacks(List.of(FunctionCallback.builder()
196+
.model(MistralAiApi.ChatModel.SMALL.getValue())
197+
.functionCallbacks(List.of(FunctionCallback.builder()
197198
.function("getCurrentWeather", new MockWeatherService())
198199
.description("Get the weather in location")
199200
.inputType(MockWeatherService.Request.class)
@@ -218,8 +219,8 @@ void streamFunctionCallTest() {
218219
List<Message> messages = new ArrayList<>(List.of(userMessage));
219220

220221
var promptOptions = MistralAiChatOptions.builder()
221-
.withModel(MistralAiApi.ChatModel.SMALL.getValue())
222-
.withFunctionCallbacks(List.of(FunctionCallback.builder()
222+
.model(MistralAiApi.ChatModel.SMALL.getValue())
223+
.functionCallbacks(List.of(FunctionCallback.builder()
223224
.function("getCurrentWeather", new MockWeatherService())
224225
.description("Get the weather in location")
225226
.inputType(MockWeatherService.Request.class)

0 commit comments

Comments
 (0)