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
This commit is contained in:
Ilayaperumal Gopinathan
2024-12-16 23:05:51 +00:00
committed by Mark Pollack
parent 26fab03c2c
commit 7e4a187f0d
33 changed files with 959 additions and 204 deletions

View File

@@ -155,7 +155,7 @@ OllamaChatModel chatModel = ...
UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
ChatResponse response = this.chatModel.call(new Prompt(this.userMessage,
OllamaOptions.builder().withFunction("CurrentWeather").build())); // Enable the function
OllamaOptions.builder().function("CurrentWeather").build())); // Enable the function
logger.info("Response: {}", response);
----
@@ -184,7 +184,7 @@ OllamaChatModel chatModel = ...
UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
var promptOptions = OllamaOptions.builder()
.withFunctionCallbacks(List.of(FunctionCallback.builder()
.functionCallbacks(List.of(FunctionCallback.builder()
.function("CurrentWeather", new MockWeatherService()) // (1) function name and instance
.description("Get the weather in location") // (2) function description
.inputType(MockWeatherService.Request.class) // (3) function signature

View File

@@ -152,8 +152,8 @@ ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
OllamaOptions.builder()
.withModel(OllamaModel.LLAMA3_1)
.withTemperature(0.4)
.model(OllamaModel.LLAMA3_1)
.temperature(0.4)
.build()
));
----
@@ -252,7 +252,7 @@ var userMessage = new UserMessage("Explain what do you see on this picture?",
new Media(MimeTypeUtils.IMAGE_PNG, this.imageResource));
ChatResponse response = chatModel.call(new Prompt(this.userMessage,
OllamaOptions.builder().withModel(OllamaModel.LLAVA)).build());
OllamaOptions.builder().model(OllamaModel.LLAVA)).build());
----
The example shows a model taking as an input the `multimodal.test.png` image:
@@ -468,8 +468,8 @@ var ollamaApi = new OllamaApi();
var chatModel = new OllamaChatModel(this.ollamaApi,
OllamaOptions.create()
.withModel(OllamaOptions.DEFAULT_MODEL)
.withTemperature(0.9));
.model(OllamaOptions.DEFAULT_MODEL)
.temperature(0.9));
ChatResponse response = this.chatModel.call(
new Prompt("Generate the names of 5 famous pirates."));
@@ -499,27 +499,27 @@ OllamaApi ollamaApi = new OllamaApi("YOUR_HOST:YOUR_PORT");
// Sync request
var request = ChatRequest.builder("orca-mini")
.withStream(false) // not streaming
.withMessages(List.of(
.stream(false) // not streaming
.messages(List.of(
Message.builder(Role.SYSTEM)
.withContent("You are a geography teacher. You are talking to a student.")
.content("You are a geography teacher. You are talking to a student.")
.build(),
Message.builder(Role.USER)
.withContent("What is the capital of Bulgaria and what is the size? "
.content("What is the capital of Bulgaria and what is the size? "
+ "What is the national anthem?")
.build()))
.withOptions(OllamaOptions.create().withTemperature(0.9))
.options(OllamaOptions.create().temperature(0.9))
.build();
ChatResponse response = this.ollamaApi.chat(this.request);
// Streaming request
var request2 = ChatRequest.builder("orca-mini")
.withStream(true) // streaming
.withMessages(List.of(Message.builder(Role.USER)
.withContent("What is the capital of Bulgaria and what is the size? " + "What is the national anthem?")
.ttream(true) // streaming
.messages(List.of(Message.builder(Role.USER)
.content("What is the capital of Bulgaria and what is the size? " + "What is the national anthem?")
.build()))
.withOptions(OllamaOptions.create().withTemperature(0.9).toMap())
.options(OllamaOptions.create().temperature(0.9).toMap())
.build();
Flux<ChatResponse> streamingResponse = this.ollamaApi.streamingChat(this.request2);

View File

@@ -156,8 +156,8 @@ For example to override the default model name for a specific request:
EmbeddingResponse embeddingResponse = embeddingModel.call(
new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
OllamaOptions.builder()
.withModel("Different-Embedding-Model-Deployment-Name"))
.withtTuncates(false)
.model("Different-Embedding-Model-Deployment-Name"))
.truncates(false)
.build());
----
@@ -303,14 +303,14 @@ var ollamaApi = new OllamaApi();
var embeddingModel = new OllamaEmbeddingModel(this.ollamaApi,
OllamaOptions.builder()
.withModel(OllamaModel.MISTRAL.id())
.model(OllamaModel.MISTRAL.id())
.build());
EmbeddingResponse embeddingResponse = this.embeddingModel.call(
new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
OllamaOptions.builder()
.withModel("chroma/all-minilm-l6-v2-f32"))
.withTruncate(false)
.model("chroma/all-minilm-l6-v2-f32"))
.truncate(false)
.build());
----

View File

@@ -129,7 +129,7 @@ void testFactChecking() {
OllamaApi ollamaApi = new OllamaApi("http://localhost:11434");
ChatModel chatModel = new OllamaChatModel(ollamaApi,
OllamaOptions.builder().withModel(BESPOKE_MINICHECK).withNumPredict(2).withTemperature(0.0d).build())
OllamaOptions.builder().model(BESPOKE_MINICHECK).numPredict(2).temperature(0.0d).build())
// Create the FactCheckingEvaluator