Skip to content

Commit aca8ffd

Browse files
scionalteratzolov
authored andcommitted
Support keep-alive and format options in Ollama chat requests
- add support for the advanced parameter 'template'. - add docs for the advanced parameters.
1 parent ae6a019 commit aca8ffd

File tree

4 files changed

+134
-43
lines changed

4 files changed

+134
-43
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,22 @@ OllamaApi.ChatRequest ollamaChatRequest(Prompt prompt, boolean stream) {
161161
}
162162

163163
String model = mergedOptions.getModel();
164-
return OllamaApi.ChatRequest.builder(model)
164+
OllamaApi.ChatRequest.Builder requestBuilder = OllamaApi.ChatRequest.builder(model)
165165
.withStream(stream)
166166
.withMessages(ollamaMessages)
167-
.withOptions(mergedOptions)
168-
.build();
167+
.withOptions(mergedOptions);
168+
169+
if (mergedOptions.getFormat() != null) {
170+
requestBuilder.withFormat(mergedOptions.getFormat());
171+
}
172+
173+
if (mergedOptions.getKeepAlive() != null) {
174+
requestBuilder.withKeepAlive(mergedOptions.getKeepAlive());
175+
}
176+
177+
if (mergedOptions.getTemp)
178+
179+
return requestBuilder.build();
169180
}
170181

171182
private String fromMediaData(Object mediaData) {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,9 @@ public Message build() {
399399
* @param model The model to use for completion.
400400
* @param messages The list of messages to chat with.
401401
* @param stream Whether to stream the response.
402-
* @param format The format to return the response in. Currently the only accepted
402+
* @param format The format to return the response in. Currently, the only accepted
403403
* value is "json".
404+
* @param keepAlive The duration to keep the model loaded in ollama while idle. https://pkg.go.dev/time#ParseDuration
404405
* @param options Additional model parameters. You can use the {@link OllamaOptions} builder
405406
* to create the options then {@link OllamaOptions#toMap()} to convert the options into a
406407
* map.
@@ -411,6 +412,7 @@ public record ChatRequest(
411412
@JsonProperty("messages") List<Message> messages,
412413
@JsonProperty("stream") Boolean stream,
413414
@JsonProperty("format") String format,
415+
@JsonProperty("keep_alive") String keepAlive,
414416
@JsonProperty("options") Map<String, Object> options) {
415417

416418
public static Builder builder(String model) {
@@ -423,6 +425,7 @@ public static class Builder {
423425
private List<Message> messages = List.of();
424426
private boolean stream = false;
425427
private String format;
428+
private String keepAlive;
426429
private Map<String, Object> options = Map.of();
427430

428431
public Builder(String model) {
@@ -445,6 +448,11 @@ public Builder withFormat(String format) {
445448
return this;
446449
}
447450

451+
public Builder withKeepAlive(String keepAlive) {
452+
this.keepAlive = keepAlive;
453+
return this;
454+
}
455+
448456
public Builder withOptions(Map<String, Object> options) {
449457
Objects.requireNonNullElse(options, "The options can not be null.");
450458

@@ -459,7 +467,7 @@ public Builder withOptions(OllamaOptions options) {
459467
}
460468

461469
public ChatRequest build() {
462-
return new ChatRequest(model, messages, stream, format, options);
470+
return new ChatRequest(model, messages, stream, format, keepAlive, options);
463471
}
464472
}
465473
}

0 commit comments

Comments
 (0)