Skip to content

Commit 3b18f2f

Browse files
committed
Refactor Ollama options builder methods
- Refactor Ollama related options builder methods - Deprecate builder methods with the prefix `with` - Update docs and references
1 parent d77c950 commit 3b18f2f

File tree

32 files changed

+725
-194
lines changed

32 files changed

+725
-194
lines changed

models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaChatModel.java

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
* @author luocongqiu
7777
* @author Thomas Vitale
7878
* @author Jihoon Kim
79+
* @author Ilayaperumal Gopinathan
7980
* @since 1.0.0
8081
*/
8182
public class OllamaChatModel extends AbstractToolCallSupport implements ChatModel {
@@ -316,15 +317,15 @@ OllamaApi.ChatRequest ollamaChatRequest(Prompt prompt, boolean stream) {
316317

317318
List<OllamaApi.Message> ollamaMessages = prompt.getInstructions().stream().map(message -> {
318319
if (message instanceof UserMessage userMessage) {
319-
var messageBuilder = OllamaApi.Message.builder(Role.USER).withContent(message.getText());
320+
var messageBuilder = OllamaApi.Message.builder(Role.USER).content(message.getText());
320321
if (!CollectionUtils.isEmpty(userMessage.getMedia())) {
321322
messageBuilder.images(
322323
userMessage.getMedia().stream().map(media -> this.fromMediaData(media.getData())).toList());
323324
}
324325
return List.of(messageBuilder.build());
325326
}
326327
else if (message instanceof SystemMessage systemMessage) {
327-
return List.of(OllamaApi.Message.builder(Role.SYSTEM).withContent(systemMessage.getText()).build());
328+
return List.of(OllamaApi.Message.builder(Role.SYSTEM).content(systemMessage.getText()).build());
328329
}
329330
else if (message instanceof AssistantMessage assistantMessage) {
330331
List<ToolCall> toolCalls = null;
@@ -336,8 +337,8 @@ else if (message instanceof AssistantMessage assistantMessage) {
336337
}).toList();
337338
}
338339
return List.of(OllamaApi.Message.builder(Role.ASSISTANT)
339-
.withContent(assistantMessage.getText())
340-
.withToolCalls(toolCalls)
340+
.content(assistantMessage.getText())
341+
.toolCalls(toolCalls)
341342
.build());
342343
}
343344
else if (message instanceof ToolResponseMessage toolMessage) {
@@ -377,21 +378,21 @@ else if (message instanceof ToolResponseMessage toolMessage) {
377378

378379
String model = mergedOptions.getModel();
379380
OllamaApi.ChatRequest.Builder requestBuilder = OllamaApi.ChatRequest.builder(model)
380-
.withStream(stream)
381-
.withMessages(ollamaMessages)
382-
.withOptions(mergedOptions);
381+
.stream(stream)
382+
.messages(ollamaMessages)
383+
.options(mergedOptions);
383384

384385
if (mergedOptions.getFormat() != null) {
385-
requestBuilder.withFormat(mergedOptions.getFormat());
386+
requestBuilder.format(mergedOptions.getFormat());
386387
}
387388

388389
if (mergedOptions.getKeepAlive() != null) {
389-
requestBuilder.withKeepAlive(mergedOptions.getKeepAlive());
390+
requestBuilder.keepAlive(mergedOptions.getKeepAlive());
390391
}
391392

392393
// Add the enabled functions definitions to the request's tools parameter.
393394
if (!CollectionUtils.isEmpty(functionsForThisRequest)) {
394-
requestBuilder.withTools(this.getFunctionTools(functionsForThisRequest));
395+
requestBuilder.tools(this.getFunctionTools(functionsForThisRequest));
395396
}
396397

397398
return requestBuilder.build();
@@ -459,7 +460,7 @@ public static final class Builder {
459460

460461
private OllamaApi ollamaApi;
461462

462-
private OllamaOptions defaultOptions = OllamaOptions.create().withModel(OllamaModel.MISTRAL.id());
463+
private OllamaOptions defaultOptions = OllamaOptions.create().model(OllamaModel.MISTRAL.id());
463464

464465
private FunctionCallbackResolver functionCallbackResolver;
465466

@@ -472,18 +473,18 @@ public static final class Builder {
472473
private Builder() {
473474
}
474475

475-
public Builder withOllamaApi(OllamaApi ollamaApi) {
476+
public Builder ollamaApi(OllamaApi ollamaApi) {
476477
this.ollamaApi = ollamaApi;
477478
return this;
478479
}
479480

480-
public Builder withDefaultOptions(OllamaOptions defaultOptions) {
481+
public Builder defaultOptions(OllamaOptions defaultOptions) {
481482
this.defaultOptions = defaultOptions;
482483
return this;
483484
}
484485

485486
/**
486-
* @deprecated use the {@link functionCallbackResolver(FunctionCallbackResolver)}
487+
* @deprecated use the {@link #functionCallbackResolver(FunctionCallbackResolver)}
487488
* instead
488489
*/
489490
@Deprecated
@@ -497,16 +498,62 @@ public Builder functionCallbackResolver(FunctionCallbackResolver functionCallbac
497498
return this;
498499
}
499500

501+
public Builder toolFunctionCallbacks(List<FunctionCallback> toolFunctionCallbacks) {
502+
this.toolFunctionCallbacks = toolFunctionCallbacks;
503+
return this;
504+
}
505+
506+
public Builder observationRegistry(ObservationRegistry observationRegistry) {
507+
this.observationRegistry = observationRegistry;
508+
return this;
509+
}
510+
511+
public Builder modelManagementOptions(ModelManagementOptions modelManagementOptions) {
512+
this.modelManagementOptions = modelManagementOptions;
513+
return this;
514+
}
515+
516+
/**
517+
* @deprecated use {@link #ollamaApi(OllamaApi)} instead.
518+
*/
519+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
520+
public Builder withOllamaApi(OllamaApi ollamaApi) {
521+
this.ollamaApi = ollamaApi;
522+
return this;
523+
}
524+
525+
/**
526+
* @deprecated use {@link #defaultOptions(OllamaOptions)} instead.
527+
*/
528+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
529+
public Builder withDefaultOptions(OllamaOptions defaultOptions) {
530+
this.defaultOptions = defaultOptions;
531+
return this;
532+
}
533+
534+
/**
535+
* @deprecated use {@link #toolFunctionCallbacks(List)} instead.
536+
*/
537+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
500538
public Builder withToolFunctionCallbacks(List<FunctionCallback> toolFunctionCallbacks) {
501539
this.toolFunctionCallbacks = toolFunctionCallbacks;
502540
return this;
503541
}
504542

543+
/**
544+
* @deprecated use {@link #observationRegistry(ObservationRegistry)} instead.
545+
*/
546+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
505547
public Builder withObservationRegistry(ObservationRegistry observationRegistry) {
506548
this.observationRegistry = observationRegistry;
507549
return this;
508550
}
509551

552+
/**
553+
* @deprecated use {@link #modelManagementOptions(ModelManagementOptions)}
554+
* instead.
555+
*/
556+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
510557
public Builder withModelManagementOptions(ModelManagementOptions modelManagementOptions) {
511558
this.modelManagementOptions = modelManagementOptions;
512559
return this;

models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaEmbeddingModel.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
*
5959
* @author Christian Tzolov
6060
* @author Thomas Vitale
61+
* @author Ilayaperumal Gopinathan
6162
* @since 0.8.0
6263
*/
6364
public class OllamaEmbeddingModel extends AbstractEmbeddingModel {
@@ -215,7 +216,7 @@ public static final class Builder {
215216

216217
private OllamaApi ollamaApi;
217218

218-
private OllamaOptions defaultOptions = OllamaOptions.create().withModel(OllamaModel.MXBAI_EMBED_LARGE.id());
219+
private OllamaOptions defaultOptions = OllamaOptions.create().model(OllamaModel.MXBAI_EMBED_LARGE.id());
219220

220221
private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
221222

@@ -224,21 +225,58 @@ public static final class Builder {
224225
private Builder() {
225226
}
226227

228+
public Builder ollamaApi(OllamaApi ollamaApi) {
229+
this.ollamaApi = ollamaApi;
230+
return this;
231+
}
232+
233+
public Builder defaultOptions(OllamaOptions defaultOptions) {
234+
this.defaultOptions = defaultOptions;
235+
return this;
236+
}
237+
238+
public Builder observationRegistry(ObservationRegistry observationRegistry) {
239+
this.observationRegistry = observationRegistry;
240+
return this;
241+
}
242+
243+
public Builder modelManagementOptions(ModelManagementOptions modelManagementOptions) {
244+
this.modelManagementOptions = modelManagementOptions;
245+
return this;
246+
}
247+
248+
/**
249+
* @deprecated use {@link #ollamaApi(OllamaApi)} instead.
250+
*/
251+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
227252
public Builder withOllamaApi(OllamaApi ollamaApi) {
228253
this.ollamaApi = ollamaApi;
229254
return this;
230255
}
231256

257+
/**
258+
* @deprecated use {@link #defaultOptions(OllamaOptions)} instead.
259+
*/
260+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
232261
public Builder withDefaultOptions(OllamaOptions defaultOptions) {
233262
this.defaultOptions = defaultOptions;
234263
return this;
235264
}
236265

266+
/**
267+
* @deprecated use {@link #observationRegistry(ObservationRegistry)} instead.
268+
*/
269+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
237270
public Builder withObservationRegistry(ObservationRegistry observationRegistry) {
238271
this.observationRegistry = observationRegistry;
239272
return this;
240273
}
241274

275+
/**
276+
* @deprecated use {@link #modelManagementOptions(ModelManagementOptions)}
277+
* instead.
278+
*/
279+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
242280
public Builder withModelManagementOptions(ModelManagementOptions modelManagementOptions) {
243281
this.modelManagementOptions = modelManagementOptions;
244282
return this;

models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/api/OllamaApi.java

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import java.util.concurrent.atomic.AtomicBoolean;
2626
import java.util.function.Consumer;
2727

28-
import com.fasterxml.jackson.annotation.JsonFormat;
2928
import com.fasterxml.jackson.annotation.JsonInclude;
3029
import com.fasterxml.jackson.annotation.JsonInclude.Include;
3130
import com.fasterxml.jackson.annotation.JsonProperty;
32-
import com.fasterxml.jackson.annotation.JsonFormat.Feature;
3331
import org.apache.commons.logging.Log;
3432
import org.apache.commons.logging.LogFactory;
3533
import reactor.core.publisher.Flux;
@@ -517,38 +515,104 @@ public Builder(String model) {
517515
this.model = model;
518516
}
519517

518+
public Builder messages(List<Message> messages) {
519+
this.messages = messages;
520+
return this;
521+
}
522+
523+
public Builder stream(boolean stream) {
524+
this.stream = stream;
525+
return this;
526+
}
527+
528+
public Builder format(String format) {
529+
this.format = format;
530+
return this;
531+
}
532+
533+
public Builder keepAlive(String keepAlive) {
534+
this.keepAlive = keepAlive;
535+
return this;
536+
}
537+
538+
public Builder tools(List<Tool> tools) {
539+
this.tools = tools;
540+
return this;
541+
}
542+
543+
public Builder options(Map<String, Object> options) {
544+
Objects.requireNonNull(options, "The options can not be null.");
545+
546+
this.options = OllamaOptions.filterNonSupportedFields(options);
547+
return this;
548+
}
549+
550+
public Builder options(OllamaOptions options) {
551+
Objects.requireNonNull(options, "The options can not be null.");
552+
this.options = OllamaOptions.filterNonSupportedFields(options.toMap());
553+
return this;
554+
}
555+
556+
/**
557+
* @deprecated use {@link #messages( List)} instead.
558+
*/
559+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
520560
public Builder withMessages(List<Message> messages) {
521561
this.messages = messages;
522562
return this;
523563
}
524564

565+
/**
566+
* @deprecated use {@link #stream(boolean)} instead.
567+
*/
568+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
525569
public Builder withStream(boolean stream) {
526570
this.stream = stream;
527571
return this;
528572
}
529573

574+
/**
575+
* @deprecated use {@link #format( String)} instead.
576+
*/
577+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
530578
public Builder withFormat(String format) {
531579
this.format = format;
532580
return this;
533581
}
534582

583+
/**
584+
* @deprecated use {@link #keepAlive( String)} instead.
585+
*/
586+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
535587
public Builder withKeepAlive(String keepAlive) {
536588
this.keepAlive = keepAlive;
537589
return this;
538590
}
539591

592+
/**
593+
* @deprecated use {@link #tools( List)} instead.
594+
*/
595+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
540596
public Builder withTools(List<Tool> tools) {
541597
this.tools = tools;
542598
return this;
543599
}
544600

601+
/**
602+
* @deprecated use {@link #options( Map)} instead.
603+
*/
604+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
545605
public Builder withOptions(Map<String, Object> options) {
546606
Objects.requireNonNull(options, "The options can not be null.");
547607

548608
this.options = OllamaOptions.filterNonSupportedFields(options);
549609
return this;
550610
}
551611

612+
/**
613+
* @deprecated use {@link #options( OllamaOptions)} instead.
614+
*/
615+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
552616
public Builder withOptions(OllamaOptions options) {
553617
Objects.requireNonNull(options, "The options can not be null.");
554618
this.options = OllamaOptions.filterNonSupportedFields(options.toMap());

0 commit comments

Comments
 (0)