forked from Mirror/ollama4j
		
	Added new createModel API to make it conform to Ollama's new API - https://github.com/ollama/ollama/blob/main/docs/api.md#create-a-model
				
					
				
			This commit is contained in:
		@@ -391,6 +391,7 @@ public class OllamaAPI {
 | 
			
		||||
     * @throws InterruptedException if the operation is interrupted
 | 
			
		||||
     * @throws URISyntaxException   if the URI for the request is malformed
 | 
			
		||||
     */
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public void createModelWithFilePath(String modelName, String modelFilePath) throws IOException, InterruptedException, OllamaBaseException, URISyntaxException {
 | 
			
		||||
        String url = this.host + "/api/create";
 | 
			
		||||
        String jsonData = new CustomModelFilePathRequest(modelName, modelFilePath).toString();
 | 
			
		||||
@@ -423,6 +424,7 @@ public class OllamaAPI {
 | 
			
		||||
     * @throws InterruptedException if the operation is interrupted
 | 
			
		||||
     * @throws URISyntaxException   if the URI for the request is malformed
 | 
			
		||||
     */
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public void createModelWithModelFileContents(String modelName, String modelFileContents) throws IOException, InterruptedException, OllamaBaseException, URISyntaxException {
 | 
			
		||||
        String url = this.host + "/api/create";
 | 
			
		||||
        String jsonData = new CustomModelFileContentsRequest(modelName, modelFileContents).toString();
 | 
			
		||||
@@ -442,6 +444,35 @@ public class OllamaAPI {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a custom model. Read more about custom model creation <a
 | 
			
		||||
     * href="https://github.com/ollama/ollama/blob/main/docs/api.md#create-a-model">here</a>.
 | 
			
		||||
     *
 | 
			
		||||
     * @param customModelRequest custom model spec
 | 
			
		||||
     * @throws OllamaBaseException  if the response indicates an error status
 | 
			
		||||
     * @throws IOException          if an I/O error occurs during the HTTP request
 | 
			
		||||
     * @throws InterruptedException if the operation is interrupted
 | 
			
		||||
     * @throws URISyntaxException   if the URI for the request is malformed
 | 
			
		||||
     */
 | 
			
		||||
    public void createModel(CustomModelRequest customModelRequest) throws IOException, InterruptedException, OllamaBaseException, URISyntaxException {
 | 
			
		||||
        String url = this.host + "/api/create";
 | 
			
		||||
        String jsonData = customModelRequest.toString();
 | 
			
		||||
        HttpRequest request = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-Type", "application/json").POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).build();
 | 
			
		||||
        HttpClient client = HttpClient.newHttpClient();
 | 
			
		||||
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
 | 
			
		||||
        int statusCode = response.statusCode();
 | 
			
		||||
        String responseString = response.body();
 | 
			
		||||
        if (statusCode != 200) {
 | 
			
		||||
            throw new OllamaBaseException(statusCode + " - " + responseString);
 | 
			
		||||
        }
 | 
			
		||||
        if (responseString.contains("error")) {
 | 
			
		||||
            throw new OllamaBaseException(responseString);
 | 
			
		||||
        }
 | 
			
		||||
        if (verbose) {
 | 
			
		||||
            logger.info(responseString);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Delete a model from Ollama server.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ import io.github.ollama4j.utils.Options;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Builderclass to easily create Requests for Embedding models using ollama.
 | 
			
		||||
 * Builder class to easily create Requests for Embedding models using ollama.
 | 
			
		||||
 */
 | 
			
		||||
public class OllamaEmbedRequestBuilder {
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,45 @@
 | 
			
		||||
package io.github.ollama4j.models.request;
 | 
			
		||||
 | 
			
		||||
import static io.github.ollama4j.utils.Utils.getObjectMapper;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import lombok.AllArgsConstructor;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.AllArgsConstructor;
 | 
			
		||||
import lombok.Builder;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
@Builder
 | 
			
		||||
public class CustomModelRequest {
 | 
			
		||||
    private String model;
 | 
			
		||||
    private String from;
 | 
			
		||||
    private Map<String, String> files;
 | 
			
		||||
    private Map<String, String> adapters;
 | 
			
		||||
    private String template;
 | 
			
		||||
    private Object license; // Using Object to handle both String and List<String>
 | 
			
		||||
    private String system;
 | 
			
		||||
    private Map<String, Object> parameters;
 | 
			
		||||
    private List<Object> messages;
 | 
			
		||||
    private Boolean stream;
 | 
			
		||||
    private Boolean quantize;
 | 
			
		||||
 | 
			
		||||
    public CustomModelRequest() {
 | 
			
		||||
        this.stream = true;
 | 
			
		||||
        this.quantize = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        try {
 | 
			
		||||
            return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
 | 
			
		||||
        } catch (JsonProcessingException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user