diff --git a/src/main/java/io/github/ollama4j/OllamaAPI.java b/src/main/java/io/github/ollama4j/OllamaAPI.java index a8e0304..d2b15cf 100644 --- a/src/main/java/io/github/ollama4j/OllamaAPI.java +++ b/src/main/java/io/github/ollama4j/OllamaAPI.java @@ -22,6 +22,7 @@ import io.github.ollama4j.tools.*; import io.github.ollama4j.tools.annotations.OllamaToolService; import io.github.ollama4j.tools.annotations.ToolProperty; import io.github.ollama4j.tools.annotations.ToolSpec; +import io.github.ollama4j.utils.Constants; import io.github.ollama4j.utils.Options; import io.github.ollama4j.utils.Utils; import lombok.Setter; @@ -102,7 +103,7 @@ public class OllamaAPI { this.host = host; } if (this.verbose) { - logger.info("Ollama API initialized with host: " + this.host); + logger.info("Ollama API initialized with host: {}", this.host); } } @@ -135,13 +136,17 @@ public class OllamaAPI { public boolean ping() { String url = this.host + "/api/tags"; HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest httpRequest = null; + HttpRequest httpRequest; try { - httpRequest = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-type", "application/json").GET().build(); + httpRequest = getRequestBuilderDefault(new URI(url)) + .header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON) + .header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON) + .GET() + .build(); } catch (URISyntaxException e) { throw new RuntimeException(e); } - HttpResponse response = null; + HttpResponse response; try { response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); } catch (HttpConnectTimeoutException e) { @@ -167,7 +172,7 @@ public class OllamaAPI { HttpClient httpClient = HttpClient.newHttpClient(); HttpRequest httpRequest = null; try { - httpRequest = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-type", "application/json").GET().build(); + httpRequest = getRequestBuilderDefault(new URI(url)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).GET().build(); } catch (URISyntaxException e) { throw new RuntimeException(e); } @@ -194,7 +199,7 @@ public class OllamaAPI { public List listModels() throws OllamaBaseException, IOException, InterruptedException, URISyntaxException { String url = this.host + "/api/tags"; HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-type", "application/json").GET().build(); + HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).GET().build(); HttpResponse response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); String responseString = response.body(); @@ -225,7 +230,7 @@ public class OllamaAPI { public List listModelsFromLibrary() throws OllamaBaseException, IOException, InterruptedException, URISyntaxException { String url = "https://ollama.com/library"; HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-type", "application/json").GET().build(); + HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).GET().build(); HttpResponse response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); String responseString = response.body(); @@ -286,7 +291,7 @@ public class OllamaAPI { public LibraryModelDetail getLibraryModelDetails(LibraryModel libraryModel) throws OllamaBaseException, IOException, InterruptedException, URISyntaxException { String url = String.format("https://ollama.com/library/%s/tags", libraryModel.getName()); HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-type", "application/json").GET().build(); + HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).GET().build(); HttpResponse response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); String responseString = response.body(); @@ -380,7 +385,7 @@ public class OllamaAPI { private void doPullModel(String modelName) throws OllamaBaseException, IOException, URISyntaxException, InterruptedException { String url = this.host + "/api/pull"; String jsonData = new ModelRequest(modelName).toString(); - HttpRequest request = getRequestBuilderDefault(new URI(url)).POST(HttpRequest.BodyPublishers.ofString(jsonData)).header("Accept", "application/json").header("Content-type", "application/json").build(); + HttpRequest request = getRequestBuilderDefault(new URI(url)).POST(HttpRequest.BodyPublishers.ofString(jsonData)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).build(); HttpClient client = HttpClient.newHttpClient(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()); int statusCode = response.statusCode(); @@ -399,7 +404,7 @@ public class OllamaAPI { if (modelPullResponse.getStatus() != null) { if (verbose) { - logger.info(modelName + ": " + modelPullResponse.getStatus()); + logger.info("{}: {}", modelName, modelPullResponse.getStatus()); } // Check if status is "success" and set success flag to true. if ("success".equalsIgnoreCase(modelPullResponse.getStatus())) { @@ -423,7 +428,7 @@ public class OllamaAPI { public String getVersion() throws URISyntaxException, IOException, InterruptedException, OllamaBaseException { String url = this.host + "/api/version"; HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-type", "application/json").GET().build(); + HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).GET().build(); HttpResponse response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); String responseString = response.body(); @@ -466,7 +471,7 @@ public class OllamaAPI { public ModelDetail getModelDetails(String modelName) throws IOException, OllamaBaseException, InterruptedException, URISyntaxException { String url = this.host + "/api/show"; String jsonData = new ModelRequest(modelName).toString(); - HttpRequest request = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-type", "application/json").POST(HttpRequest.BodyPublishers.ofString(jsonData)).build(); + HttpRequest request = getRequestBuilderDefault(new URI(url)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).POST(HttpRequest.BodyPublishers.ofString(jsonData)).build(); HttpClient client = HttpClient.newHttpClient(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); @@ -495,7 +500,7 @@ public class OllamaAPI { 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(); - HttpRequest request = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-Type", "application/json").POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).build(); + HttpRequest request = getRequestBuilderDefault(new URI(url)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).build(); HttpClient client = HttpClient.newHttpClient(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); @@ -532,7 +537,7 @@ public class OllamaAPI { 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(); - HttpRequest request = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-Type", "application/json").POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).build(); + HttpRequest request = getRequestBuilderDefault(new URI(url)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).build(); HttpClient client = HttpClient.newHttpClient(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); @@ -562,7 +567,7 @@ public class OllamaAPI { 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(); + HttpRequest request = getRequestBuilderDefault(new URI(url)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).build(); HttpClient client = HttpClient.newHttpClient(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); @@ -592,7 +597,7 @@ public class OllamaAPI { public void deleteModel(String modelName, boolean ignoreIfNotPresent) throws IOException, InterruptedException, OllamaBaseException, URISyntaxException { String url = this.host + "/api/delete"; String jsonData = new ModelRequest(modelName).toString(); - HttpRequest request = getRequestBuilderDefault(new URI(url)).method("DELETE", HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).header("Accept", "application/json").header("Content-type", "application/json").build(); + HttpRequest request = getRequestBuilderDefault(new URI(url)).method("DELETE", HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).build(); HttpClient client = HttpClient.newHttpClient(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); @@ -636,7 +641,7 @@ public class OllamaAPI { URI uri = URI.create(this.host + "/api/embeddings"); String jsonData = modelRequest.toString(); HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri).header("Accept", "application/json").POST(HttpRequest.BodyPublishers.ofString(jsonData)); + HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).POST(HttpRequest.BodyPublishers.ofString(jsonData)); HttpRequest request = requestBuilder.build(); HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); @@ -677,7 +682,7 @@ public class OllamaAPI { String jsonData = Utils.getObjectMapper().writeValueAsString(modelRequest); HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder(uri).header("Accept", "application/json").POST(HttpRequest.BodyPublishers.ofString(jsonData)).build(); + HttpRequest request = HttpRequest.newBuilder(uri).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).POST(HttpRequest.BodyPublishers.ofString(jsonData)).build(); HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); @@ -774,7 +779,7 @@ public class OllamaAPI { String jsonData = Utils.getObjectMapper().writeValueAsString(requestBody); HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest request = getRequestBuilderDefault(uri).header("Accept", "application/json").header("Content-type", "application/json").POST(HttpRequest.BodyPublishers.ofString(jsonData)).build(); + HttpRequest request = getRequestBuilderDefault(uri).header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).POST(HttpRequest.BodyPublishers.ofString(jsonData)).build(); if (verbose) { try { @@ -1370,7 +1375,7 @@ public class OllamaAPI { * @return HttpRequest.Builder */ private HttpRequest.Builder getRequestBuilderDefault(URI uri) { - HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri).header("Content-Type", "application/json").timeout(Duration.ofSeconds(requestTimeoutSeconds)); + HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri).header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).timeout(Duration.ofSeconds(requestTimeoutSeconds)); if (isBasicAuthCredentialsSet()) { requestBuilder.header("Authorization", auth.getAuthHeaderValue()); } diff --git a/src/main/java/io/github/ollama4j/models/request/BasicAuth.java b/src/main/java/io/github/ollama4j/models/request/BasicAuth.java index c58b240..b560d39 100644 --- a/src/main/java/io/github/ollama4j/models/request/BasicAuth.java +++ b/src/main/java/io/github/ollama4j/models/request/BasicAuth.java @@ -4,10 +4,11 @@ import java.util.Base64; import lombok.AllArgsConstructor; import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.EqualsAndHashCode; @Data @AllArgsConstructor +@EqualsAndHashCode(callSuper = false) public class BasicAuth extends Auth { private String username; private String password; diff --git a/src/main/java/io/github/ollama4j/models/request/OllamaChatEndpointCaller.java b/src/main/java/io/github/ollama4j/models/request/OllamaChatEndpointCaller.java index 65db860..724e028 100644 --- a/src/main/java/io/github/ollama4j/models/request/OllamaChatEndpointCaller.java +++ b/src/main/java/io/github/ollama4j/models/request/OllamaChatEndpointCaller.java @@ -24,6 +24,7 @@ import java.util.List; /** * Specialization class for requests */ +@SuppressWarnings("resource") public class OllamaChatEndpointCaller extends OllamaEndpointCaller { private static final Logger LOG = LoggerFactory.getLogger(OllamaChatEndpointCaller.class); diff --git a/src/main/java/io/github/ollama4j/models/request/OllamaEndpointCaller.java b/src/main/java/io/github/ollama4j/models/request/OllamaEndpointCaller.java index 04d7fd9..ae91322 100644 --- a/src/main/java/io/github/ollama4j/models/request/OllamaEndpointCaller.java +++ b/src/main/java/io/github/ollama4j/models/request/OllamaEndpointCaller.java @@ -4,6 +4,7 @@ import java.net.URI; import java.net.http.HttpRequest; import java.time.Duration; +import io.github.ollama4j.utils.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,7 +45,7 @@ public abstract class OllamaEndpointCaller { protected HttpRequest.Builder getRequestBuilderDefault(URI uri) { HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri) - .header("Content-Type", "application/json") + .header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON) .timeout(Duration.ofSeconds(this.requestTimeoutSeconds)); if (isAuthCredentialsSet()) { requestBuilder.header("Authorization", this.auth.getAuthHeaderValue()); diff --git a/src/main/java/io/github/ollama4j/models/request/OllamaGenerateEndpointCaller.java b/src/main/java/io/github/ollama4j/models/request/OllamaGenerateEndpointCaller.java index 55d6fdf..b500060 100644 --- a/src/main/java/io/github/ollama4j/models/request/OllamaGenerateEndpointCaller.java +++ b/src/main/java/io/github/ollama4j/models/request/OllamaGenerateEndpointCaller.java @@ -22,6 +22,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; +@SuppressWarnings("resource") public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller { private static final Logger LOG = LoggerFactory.getLogger(OllamaGenerateEndpointCaller.class); diff --git a/src/main/java/io/github/ollama4j/models/response/OllamaAsyncResultStreamer.java b/src/main/java/io/github/ollama4j/models/response/OllamaAsyncResultStreamer.java index fd43696..dc7b363 100644 --- a/src/main/java/io/github/ollama4j/models/response/OllamaAsyncResultStreamer.java +++ b/src/main/java/io/github/ollama4j/models/response/OllamaAsyncResultStreamer.java @@ -8,6 +8,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; +import io.github.ollama4j.utils.Constants; import java.io.BufferedReader; import java.io.IOException; @@ -68,14 +69,14 @@ public class OllamaAsyncResultStreamer extends Thread { public void run() { ollamaRequestModel.setStream(true); HttpClient httpClient = HttpClient.newHttpClient(); + long startTime = System.currentTimeMillis(); try { - long startTime = System.currentTimeMillis(); HttpRequest request = requestBuilder .POST( HttpRequest.BodyPublishers.ofString( Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))) - .header("Content-Type", "application/json") + .header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON) .timeout(Duration.ofSeconds(requestTimeoutSeconds)) .build(); HttpResponse response = @@ -84,8 +85,9 @@ public class OllamaAsyncResultStreamer extends Thread { this.httpStatusCode = statusCode; InputStream responseBodyStream = response.body(); - try (BufferedReader reader = - new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) { + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8)); String line; StringBuilder responseBuffer = new StringBuilder(); while ((line = reader.readLine()) != null) { @@ -109,6 +111,21 @@ public class OllamaAsyncResultStreamer extends Thread { this.completeResponse = responseBuffer.toString(); long endTime = System.currentTimeMillis(); responseTime = endTime - startTime; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + // Optionally log or handle + } + } + if (responseBodyStream != null) { + try { + responseBodyStream.close(); + } catch (IOException e) { + // Optionally log or handle + } + } } if (statusCode != 200) { throw new OllamaBaseException(this.completeResponse); diff --git a/src/main/java/io/github/ollama4j/tools/sampletools/WeatherTool.java b/src/main/java/io/github/ollama4j/tools/sampletools/WeatherTool.java index eb0ba72..e1bf483 100644 --- a/src/main/java/io/github/ollama4j/tools/sampletools/WeatherTool.java +++ b/src/main/java/io/github/ollama4j/tools/sampletools/WeatherTool.java @@ -12,15 +12,17 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.github.ollama4j.tools.Tools; +@SuppressWarnings("resource") public class WeatherTool { private String openWeatherMapAPIKey = null; - + private String paramCityName = "cityName"; public WeatherTool(String openWeatherMapAPIKey) { this.openWeatherMapAPIKey = openWeatherMapAPIKey; } public String getCurrentWeather(Map arguments) { - String city = (String) arguments.get("cityName"); + + String city = (String) arguments.get(paramCityName); System.out.println("Finding weather for city: " + city); String url = String.format("https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric", @@ -45,7 +47,6 @@ public class WeatherTool { + response.statusCode(); } } catch (IOException | InterruptedException e) { - e.printStackTrace(); return "Error retrieving weather data: " + e.getMessage(); } } @@ -70,7 +71,7 @@ public class WeatherTool { .type("object") .properties( Map.of( - "cityName", + paramCityName, Tools.PromptFuncDefinition.Property .builder() .type("string") @@ -79,7 +80,7 @@ public class WeatherTool { .required(true) .build())) .required(java.util.List - .of("cityName")) + .of(paramCityName)) .build()) .build()) .build()) diff --git a/src/main/java/io/github/ollama4j/utils/Constants.java b/src/main/java/io/github/ollama4j/utils/Constants.java new file mode 100644 index 0000000..dfe5377 --- /dev/null +++ b/src/main/java/io/github/ollama4j/utils/Constants.java @@ -0,0 +1,14 @@ +package io.github.ollama4j.utils; + +public final class Constants { + public static final class HttpConstants { + private HttpConstants() { + } + + public static final String APPLICATION_JSON = "application/json"; + public static final String APPLICATION_XML = "application/xml"; + public static final String TEXT_PLAIN = "text/plain"; + public static final String HEADER_KEY_CONTENT_TYPE = "Content-Type"; + public static final String HEADER_KEY_ACCEPT = "Accept"; + } +} diff --git a/src/main/java/io/github/ollama4j/utils/SamplePrompts.java b/src/main/java/io/github/ollama4j/utils/SamplePrompts.java index 89a7f83..37b1245 100644 --- a/src/main/java/io/github/ollama4j/utils/SamplePrompts.java +++ b/src/main/java/io/github/ollama4j/utils/SamplePrompts.java @@ -16,7 +16,7 @@ public class SamplePrompts { stringBuffer.append(scanner.nextLine()).append("\n"); } scanner.close(); - return stringBuffer.toString().replaceAll("", question); + return stringBuffer.toString().replace("", question); } else { throw new Exception("Sample database question file not found."); } diff --git a/src/test/java/io/github/ollama4j/integrationtests/OllamaAPIIntegrationTest.java b/src/test/java/io/github/ollama4j/integrationtests/OllamaAPIIntegrationTest.java index 2ea8977..13d2d5a 100644 --- a/src/test/java/io/github/ollama4j/integrationtests/OllamaAPIIntegrationTest.java +++ b/src/test/java/io/github/ollama4j/integrationtests/OllamaAPIIntegrationTest.java @@ -98,6 +98,13 @@ public class OllamaAPIIntegrationTest { // image version"); } + @Test + @Order(1) + public void testPing() throws URISyntaxException, IOException, OllamaBaseException, InterruptedException { + boolean pingResponse = api.ping(); + assertTrue(pingResponse, "Ping should return true"); + } + @Test @Order(2) public void testListModelsAPI() diff --git a/src/test/java/io/github/ollama4j/unittests/jackson/TestModelRequestSerialization.java b/src/test/java/io/github/ollama4j/unittests/jackson/TestModelRequestSerialization.java index 5bc44f3..961dd43 100644 --- a/src/test/java/io/github/ollama4j/unittests/jackson/TestModelRequestSerialization.java +++ b/src/test/java/io/github/ollama4j/unittests/jackson/TestModelRequestSerialization.java @@ -3,40 +3,66 @@ package io.github.ollama4j.unittests.jackson; import io.github.ollama4j.models.response.Model; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + public class TestModelRequestSerialization extends AbstractSerializationTest { @Test - public void testDeserializationOfModelResponseWithOffsetTime(){ - String serializedTestStringWithOffsetTime = "{\n" - + "\"name\": \"codellama:13b\",\n" - + "\"modified_at\": \"2023-11-04T14:56:49.277302595-07:00\",\n" - + "\"size\": 7365960935,\n" - + "\"digest\": \"9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697\",\n" - + "\"details\": {\n" - + "\"format\": \"gguf\",\n" - + "\"family\": \"llama\",\n" - + "\"families\": null,\n" - + "\"parameter_size\": \"13B\",\n" - + "\"quantization_level\": \"Q4_0\"\n" - + "}}"; - deserialize(serializedTestStringWithOffsetTime,Model.class); + public void testDeserializationOfModelResponseWithOffsetTime() { + String serializedTestStringWithOffsetTime = "{\n" + + " \"name\": \"codellama:13b\",\n" + + " \"modified_at\": \"2023-11-04T14:56:49.277302595-07:00\",\n" + + " \"size\": 7365960935,\n" + + " \"digest\": \"9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697\",\n" + + " \"details\": {\n" + + " \"format\": \"gguf\",\n" + + " \"family\": \"llama\",\n" + + " \"families\": null,\n" + + " \"parameter_size\": \"13B\",\n" + + " \"quantization_level\": \"Q4_0\"\n" + + " }\n" + + "}"; + Model model = deserialize(serializedTestStringWithOffsetTime, Model.class); + assertNotNull(model); + assertEquals("codellama:13b", model.getName()); + assertEquals("2023-11-04T21:56:49.277302595Z", model.getModifiedAt().toString()); + assertEquals(7365960935L, model.getSize()); + assertEquals("9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697", model.getDigest()); + assertNotNull(model.getModelMeta()); + assertEquals("gguf", model.getModelMeta().getFormat()); + assertEquals("llama", model.getModelMeta().getFamily()); + assertNull(model.getModelMeta().getFamilies()); + assertEquals("13B", model.getModelMeta().getParameterSize()); + assertEquals("Q4_0", model.getModelMeta().getQuantizationLevel()); } @Test - public void testDeserializationOfModelResponseWithZuluTime(){ - String serializedTestStringWithZuluTimezone = "{\n" - + "\"name\": \"codellama:13b\",\n" - + "\"modified_at\": \"2023-11-04T14:56:49.277302595Z\",\n" - + "\"size\": 7365960935,\n" - + "\"digest\": \"9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697\",\n" - + "\"details\": {\n" - + "\"format\": \"gguf\",\n" - + "\"family\": \"llama\",\n" - + "\"families\": null,\n" - + "\"parameter_size\": \"13B\",\n" - + "\"quantization_level\": \"Q4_0\"\n" - + "}}"; - deserialize(serializedTestStringWithZuluTimezone,Model.class); + public void testDeserializationOfModelResponseWithZuluTime() { + String serializedTestStringWithZuluTimezone = "{\n" + + " \"name\": \"codellama:13b\",\n" + + " \"modified_at\": \"2023-11-04T14:56:49.277302595Z\",\n" + + " \"size\": 7365960935,\n" + + " \"digest\": \"9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697\",\n" + + " \"details\": {\n" + + " \"format\": \"gguf\",\n" + + " \"family\": \"llama\",\n" + + " \"families\": null,\n" + + " \"parameter_size\": \"13B\",\n" + + " \"quantization_level\": \"Q4_0\"\n" + + " }\n" + + "}"; + Model model = deserialize(serializedTestStringWithZuluTimezone, Model.class); + assertNotNull(model); + assertEquals("codellama:13b", model.getName()); + assertEquals("2023-11-04T14:56:49.277302595Z", model.getModifiedAt().toString()); + assertEquals(7365960935L, model.getSize()); + assertEquals("9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697", model.getDigest()); + assertNotNull(model.getModelMeta()); + assertEquals("gguf", model.getModelMeta().getFormat()); + assertEquals("llama", model.getModelMeta().getFamily()); + assertNull(model.getModelMeta().getFamilies()); + assertEquals("13B", model.getModelMeta().getParameterSize()); + assertEquals("Q4_0", model.getModelMeta().getQuantizationLevel()); } }