Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ public Flux<ProgressResponse> pullModel(PullModelRequest pullModelRequest) {
.bodyToFlux(ProgressResponse.class);
}

public Flux<ProgressResponse> createModel(CreateModelRequest createModelRequest) {
Assert.notNull(createModelRequest, "createModelRequest must not be null");
Assert.isTrue(createModelRequest.stream(), "Request must set the stream property to true.");

return this.webClient.post()
.uri("/api/create")
.bodyValue(createModelRequest)
.retrieve()
.bodyToFlux(ProgressResponse.class);
}

/**
* Chat message object.
*
Expand Down Expand Up @@ -741,6 +752,32 @@ public PullModelRequest(String model) {
}
}

@JsonInclude(Include.NON_NULL)
public record CreateModelRequest(
@JsonProperty("model") String model,
@JsonProperty("from") String from,
@JsonProperty("files") Map<String, Object> files,
@JsonProperty("adapters") Map<String, Object> adapters,
@JsonProperty("template") String template,
@JsonProperty("license") List<String> license,
@JsonProperty("system") String system,
@JsonProperty("parameters") Map<String, Object> parameters,
@JsonProperty("messages") List<Message> messages,
@JsonProperty("stream") boolean stream,
@JsonProperty("quantize") String quantize
) {
public CreateModelRequest {
if (!stream) {
logger.warn("Enforcing streaming of the model creation request");
}
stream = true;
}

public CreateModelRequest(String model, String from) {
this(model, from, null, null, null, null, null, null, null, true, null);
}
}

@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public record ProgressResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.ai.ollama.api.OllamaApi.DeleteModelRequest;
import org.springframework.ai.ollama.api.OllamaApi.ListModelResponse;
import org.springframework.ai.ollama.api.OllamaApi.PullModelRequest;
import org.springframework.ai.ollama.api.OllamaApi.CreateModelRequest;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

Expand Down Expand Up @@ -127,4 +128,26 @@ public void pullModel(String modelName, PullModelStrategy pullModelStrategy) {
// @formatter:on
}

public void createModel(String newModelName, String originalModelName) {
// @formatter:off

logger.info("Start creating model {} from {}", newModelName, originalModelName);
this.ollamaApi.createModel(new CreateModelRequest(newModelName, originalModelName))
.bufferUntilChanged(OllamaApi.ProgressResponse::status)
.doOnEach(signal -> {
var progressResponses = signal.get();
if (!CollectionUtils.isEmpty(progressResponses) && progressResponses.get(progressResponses.size() - 1) != null) {
logger.info("Creating the '{}' model - Status: {}", newModelName, progressResponses.get(progressResponses.size() - 1).status());
}
})
.takeUntil(progressResponses ->
progressResponses.get(0) != null && "success".equals(progressResponses.get(0).status()))
.timeout(this.options.timeout())
.retryWhen(Retry.backoff(this.options.maxRetries(), Duration.ofSeconds(5)))
.blockLast();
logger.info("Completed creating model {} from {}", newModelName, originalModelName);

// @formatter:on
}

}