mirror of
				https://github.com/amithkoujalgi/ollama4j.git
				synced 2025-11-04 10:30:41 +01:00 
			
		
		
		
	- replaced GSON with Jackson
- Updated readme - general cleanup
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core;
 | 
			
		||||
 | 
			
		||||
import com.google.gson.Gson;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.models.*;
 | 
			
		||||
import org.apache.hc.client5.http.classic.methods.HttpDelete;
 | 
			
		||||
@@ -30,10 +30,13 @@ import java.util.stream.Collectors;
 | 
			
		||||
 */
 | 
			
		||||
@SuppressWarnings({"DuplicatedCode", "ExtractMethodRecommender"})
 | 
			
		||||
public class OllamaAPI {
 | 
			
		||||
 | 
			
		||||
    private static final Logger logger = LoggerFactory.getLogger(OllamaAPI.class);
 | 
			
		||||
    private final String host;
 | 
			
		||||
    private boolean verbose = false;
 | 
			
		||||
 | 
			
		||||
    private final ObjectMapper objectMapper = new ObjectMapper();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Instantiates the Ollama API.
 | 
			
		||||
     *
 | 
			
		||||
@@ -76,8 +79,7 @@ public class OllamaAPI {
 | 
			
		||||
                responseString = EntityUtils.toString(responseEntity, "UTF-8");
 | 
			
		||||
            }
 | 
			
		||||
            if (statusCode == 200) {
 | 
			
		||||
                Models m = new Gson().fromJson(responseString, Models.class);
 | 
			
		||||
                return m.getModels();
 | 
			
		||||
                return objectMapper.readValue(responseString, ListModelsResponse.class).getModels();
 | 
			
		||||
            } else {
 | 
			
		||||
                throw new OllamaBaseException(statusCode + " - " + responseString);
 | 
			
		||||
            }
 | 
			
		||||
@@ -109,7 +111,7 @@ public class OllamaAPI {
 | 
			
		||||
                responseString = EntityUtils.toString(responseEntity, "UTF-8");
 | 
			
		||||
            }
 | 
			
		||||
            if (statusCode == 200) {
 | 
			
		||||
                return new Gson().fromJson(responseString, ModelDetail.class);
 | 
			
		||||
                return objectMapper.readValue(responseString, ModelDetail.class);
 | 
			
		||||
            } else {
 | 
			
		||||
                throw new OllamaBaseException(statusCode + " - " + responseString);
 | 
			
		||||
            }
 | 
			
		||||
@@ -234,7 +236,6 @@ public class OllamaAPI {
 | 
			
		||||
     * @throws IOException
 | 
			
		||||
     */
 | 
			
		||||
    public String ask(String ollamaModelType, String promptText) throws OllamaBaseException, IOException {
 | 
			
		||||
        Gson gson = new Gson();
 | 
			
		||||
        OllamaRequestModel ollamaRequestModel = new OllamaRequestModel(ollamaModelType, promptText);
 | 
			
		||||
        URL obj = new URL(this.host + "/api/generate");
 | 
			
		||||
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 | 
			
		||||
@@ -250,7 +251,7 @@ public class OllamaAPI {
 | 
			
		||||
                String inputLine;
 | 
			
		||||
                StringBuilder response = new StringBuilder();
 | 
			
		||||
                while ((inputLine = in.readLine()) != null) {
 | 
			
		||||
                    OllamaResponseModel ollamaResponseModel = gson.fromJson(inputLine, OllamaResponseModel.class);
 | 
			
		||||
                    OllamaResponseModel ollamaResponseModel = objectMapper.readValue(inputLine, OllamaResponseModel.class);
 | 
			
		||||
                    if (!ollamaResponseModel.getDone()) {
 | 
			
		||||
                        response.append(ollamaResponseModel.getResponse());
 | 
			
		||||
                    }
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ package io.github.amithkoujalgi.ollama4j.core.models;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public class Models {
 | 
			
		||||
public class ListModelsResponse {
 | 
			
		||||
    private List<Model> models;
 | 
			
		||||
 | 
			
		||||
    public List<Model> getModels() {
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models;
 | 
			
		||||
 | 
			
		||||
import com.google.gson.GsonBuilder;
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
 | 
			
		||||
public class ModelDetail {
 | 
			
		||||
    private String license, modelfile, parameters, template;
 | 
			
		||||
@@ -39,6 +40,13 @@ public class ModelDetail {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        return new GsonBuilder().setPrettyPrinting().create().toJson(this);
 | 
			
		||||
        try {
 | 
			
		||||
            return new ObjectMapper()
 | 
			
		||||
                    .writer()
 | 
			
		||||
                    .withDefaultPrettyPrinter()
 | 
			
		||||
                    .writeValueAsString(this);
 | 
			
		||||
        } catch (JsonProcessingException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,38 +1,42 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models;
 | 
			
		||||
 | 
			
		||||
import com.google.gson.Gson;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
 | 
			
		||||
 | 
			
		||||
import java.io.BufferedReader;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStreamReader;
 | 
			
		||||
import java.net.HttpURLConnection;
 | 
			
		||||
import java.util.LinkedList;
 | 
			
		||||
import java.util.Queue;
 | 
			
		||||
 | 
			
		||||
@SuppressWarnings("DuplicatedCode")
 | 
			
		||||
public class OllamaAsyncResultCallback extends Thread {
 | 
			
		||||
    private final HttpURLConnection connection;
 | 
			
		||||
    private String result;
 | 
			
		||||
    private boolean isDone;
 | 
			
		||||
    private final ObjectMapper objectMapper = new ObjectMapper();
 | 
			
		||||
    private final Queue<String> queue = new LinkedList<>();
 | 
			
		||||
 | 
			
		||||
    public OllamaAsyncResultCallback(HttpURLConnection connection) {
 | 
			
		||||
        this.connection = connection;
 | 
			
		||||
        this.isDone = false;
 | 
			
		||||
        this.result = "";
 | 
			
		||||
        this.queue.add("");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void run() {
 | 
			
		||||
        Gson gson = new Gson();
 | 
			
		||||
        int responseCode = 0;
 | 
			
		||||
        try {
 | 
			
		||||
            responseCode = this.connection.getResponseCode();
 | 
			
		||||
            if (responseCode == HttpURLConnection.HTTP_OK) {
 | 
			
		||||
                try (BufferedReader in =
 | 
			
		||||
                             new BufferedReader(new InputStreamReader(this.connection.getInputStream()))) {
 | 
			
		||||
                try (BufferedReader in = new BufferedReader(new InputStreamReader(this.connection.getInputStream()))) {
 | 
			
		||||
                    String inputLine;
 | 
			
		||||
                    StringBuilder response = new StringBuilder();
 | 
			
		||||
                    while ((inputLine = in.readLine()) != null) {
 | 
			
		||||
                        OllamaResponseModel ollamaResponseModel =
 | 
			
		||||
                                gson.fromJson(inputLine, OllamaResponseModel.class);
 | 
			
		||||
                        OllamaResponseModel ollamaResponseModel = objectMapper.readValue(inputLine, OllamaResponseModel.class);
 | 
			
		||||
                        queue.add(ollamaResponseModel.getResponse());
 | 
			
		||||
                        if (!ollamaResponseModel.getDone()) {
 | 
			
		||||
                            response.append(ollamaResponseModel.getResponse());
 | 
			
		||||
                        }
 | 
			
		||||
@@ -42,8 +46,7 @@ public class OllamaAsyncResultCallback extends Thread {
 | 
			
		||||
                    this.result = response.toString();
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                throw new OllamaBaseException(
 | 
			
		||||
                        connection.getResponseCode() + " - " + connection.getResponseMessage());
 | 
			
		||||
                throw new OllamaBaseException(connection.getResponseCode() + " - " + connection.getResponseMessage());
 | 
			
		||||
            }
 | 
			
		||||
        } catch (IOException | OllamaBaseException e) {
 | 
			
		||||
            this.isDone = true;
 | 
			
		||||
@@ -55,7 +58,15 @@ public class OllamaAsyncResultCallback extends Thread {
 | 
			
		||||
        return isDone;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the final response when the execution completes. Does not return intermediate results.
 | 
			
		||||
     * @return response text
 | 
			
		||||
     */
 | 
			
		||||
    public String getResponse() {
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Queue<String> getStream() {
 | 
			
		||||
        return queue;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models;
 | 
			
		||||
 | 
			
		||||
import com.google.gson.Gson;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
 | 
			
		||||
public class OllamaRequestModel {
 | 
			
		||||
    private String model;
 | 
			
		||||
@@ -29,6 +31,13 @@ public class OllamaRequestModel {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        return new Gson().toJson(this);
 | 
			
		||||
        try {
 | 
			
		||||
            return new ObjectMapper()
 | 
			
		||||
                    .writer()
 | 
			
		||||
                    .withDefaultPrettyPrinter()
 | 
			
		||||
                    .writeValueAsString(this);
 | 
			
		||||
        } catch (JsonProcessingException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user