Skip to content

Commit 7e4a187

Browse files
ilayaperumalgmarkpollack
authored andcommitted
Refactor Ollama builder API
The Ollama options builder API has been refactored to follow standard Java builder pattern conventions. This change deprecates all builder methods prefixed with 'with' in favor of more concise method names, improving API consistency and usability. The deprecated methods are marked for removal in version 1.0.0-M5, giving users time to migrate to the new builder pattern. This change aligns with our goal of providing a more intuitive and maintainable API surface. Breaking Changes: * builder() method now returns Builder instead of OllamaOptions * Clients using the old fluent API will need to migrate to the new builder pattern Refactor Ollama options builder methods
1 parent 26fab03 commit 7e4a187

File tree

33 files changed

+959
-204
lines changed

33 files changed

+959
-204
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
@@ -77,6 +77,7 @@
7777
* @author Thomas Vitale
7878
* @author Jihoon Kim
7979
* @author Alexandros Pappas
80+
* @author Ilayaperumal Gopinathan
8081
* @since 1.0.0
8182
*/
8283
public class OllamaChatModel extends AbstractToolCallSupport implements ChatModel {
@@ -317,15 +318,15 @@ OllamaApi.ChatRequest ollamaChatRequest(Prompt prompt, boolean stream) {
317318

318319
List<OllamaApi.Message> ollamaMessages = prompt.getInstructions().stream().map(message -> {
319320
if (message instanceof UserMessage userMessage) {
320-
var messageBuilder = OllamaApi.Message.builder(Role.USER).withContent(message.getText());
321+
var messageBuilder = OllamaApi.Message.builder(Role.USER).content(message.getText());
321322
if (!CollectionUtils.isEmpty(userMessage.getMedia())) {
322323
messageBuilder.images(
323324
userMessage.getMedia().stream().map(media -> this.fromMediaData(media.getData())).toList());
324325
}
325326
return List.of(messageBuilder.build());
326327
}
327328
else if (message instanceof SystemMessage systemMessage) {
328-
return List.of(OllamaApi.Message.builder(Role.SYSTEM).withContent(systemMessage.getText()).build());
329+
return List.of(OllamaApi.Message.builder(Role.SYSTEM).content(systemMessage.getText()).build());
329330
}
330331
else if (message instanceof AssistantMessage assistantMessage) {
331332
List<ToolCall> toolCalls = null;
@@ -337,8 +338,8 @@ else if (message instanceof AssistantMessage assistantMessage) {
337338
}).toList();
338339
}
339340
return List.of(OllamaApi.Message.builder(Role.ASSISTANT)
340-
.withContent(assistantMessage.getText())
341-
.withToolCalls(toolCalls)
341+
.content(assistantMessage.getText())
342+
.toolCalls(toolCalls)
342343
.build());
343344
}
344345
else if (message instanceof ToolResponseMessage toolMessage) {
@@ -378,21 +379,21 @@ else if (message instanceof ToolResponseMessage toolMessage) {
378379

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

385386
if (mergedOptions.getFormat() != null) {
386-
requestBuilder.withFormat(mergedOptions.getFormat());
387+
requestBuilder.format(mergedOptions.getFormat());
387388
}
388389

389390
if (mergedOptions.getKeepAlive() != null) {
390-
requestBuilder.withKeepAlive(mergedOptions.getKeepAlive());
391+
requestBuilder.keepAlive(mergedOptions.getKeepAlive());
391392
}
392393

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

398399
return requestBuilder.build();
@@ -460,7 +461,7 @@ public static final class Builder {
460461

461462
private OllamaApi ollamaApi;
462463

463-
private OllamaOptions defaultOptions = OllamaOptions.create().withModel(OllamaModel.MISTRAL.id());
464+
private OllamaOptions defaultOptions = OllamaOptions.builder().model(OllamaModel.MISTRAL.id()).build();
464465

465466
private FunctionCallbackResolver functionCallbackResolver;
466467

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

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

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

486487
/**
487-
* @deprecated use the {@link functionCallbackResolver(FunctionCallbackResolver)}
488+
* @deprecated use the {@link #functionCallbackResolver(FunctionCallbackResolver)}
488489
* instead
489490
*/
490491
@Deprecated
@@ -498,16 +499,62 @@ public Builder functionCallbackResolver(FunctionCallbackResolver functionCallbac
498499
return this;
499500
}
500501

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

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

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

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

Lines changed: 41 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,9 @@ 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.builder()
220+
.model(OllamaModel.MXBAI_EMBED_LARGE.id())
221+
.build();
219222

220223
private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
221224

@@ -224,21 +227,58 @@ public static final class Builder {
224227
private Builder() {
225228
}
226229

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

259+
/**
260+
* @deprecated use {@link #defaultOptions(OllamaOptions)} instead.
261+
*/
262+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
232263
public Builder withDefaultOptions(OllamaOptions defaultOptions) {
233264
this.defaultOptions = defaultOptions;
234265
return this;
235266
}
236267

268+
/**
269+
* @deprecated use {@link #observationRegistry(ObservationRegistry)} instead.
270+
*/
271+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
237272
public Builder withObservationRegistry(ObservationRegistry observationRegistry) {
238273
this.observationRegistry = observationRegistry;
239274
return this;
240275
}
241276

277+
/**
278+
* @deprecated use {@link #modelManagementOptions(ModelManagementOptions)}
279+
* instead.
280+
*/
281+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
242282
public Builder withModelManagementOptions(ModelManagementOptions modelManagementOptions) {
243283
this.modelManagementOptions = modelManagementOptions;
244284
return this;

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,38 +515,104 @@ public Builder(String model) {
515515
this.model = model;
516516
}
517517

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(Object 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")
518560
public Builder withMessages(List<Message> messages) {
519561
this.messages = messages;
520562
return this;
521563
}
522564

565+
/**
566+
* @deprecated use {@link #stream(boolean)} instead.
567+
*/
568+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
523569
public Builder withStream(boolean stream) {
524570
this.stream = stream;
525571
return this;
526572
}
527573

574+
/**
575+
* @deprecated use {@link #format( String)} instead.
576+
*/
577+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
528578
public Builder withFormat(Object format) {
529579
this.format = format;
530580
return this;
531581
}
532582

583+
/**
584+
* @deprecated use {@link #keepAlive( String)} instead.
585+
*/
586+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
533587
public Builder withKeepAlive(String keepAlive) {
534588
this.keepAlive = keepAlive;
535589
return this;
536590
}
537591

592+
/**
593+
* @deprecated use {@link #tools( List)} instead.
594+
*/
595+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
538596
public Builder withTools(List<Tool> tools) {
539597
this.tools = tools;
540598
return this;
541599
}
542600

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

546608
this.options = OllamaOptions.filterNonSupportedFields(options);
547609
return this;
548610
}
549611

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

0 commit comments

Comments
 (0)