forked from Mirror/ollama4j
		
	Compare commits
	
		
			9 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | 11201bc7c7 | ||
|   | 3a8b5257c0 | ||
|   | 7481c2ba0e | ||
|   | 9d336e257c | ||
|   | 2027171cb9 | ||
|   | e06baf0d29 | ||
|   | 5d6a68a5bb | ||
|   | 41a5bb70bf | ||
|   | c2ec62ba08 | 
| @@ -60,7 +60,7 @@ In your Maven project, add this dependency: | |||||||
| <dependency> | <dependency> | ||||||
|     <groupId>io.github.amithkoujalgi</groupId> |     <groupId>io.github.amithkoujalgi</groupId> | ||||||
|     <artifactId>ollama4j</artifactId> |     <artifactId>ollama4j</artifactId> | ||||||
|     <version>1.0.20</version> |     <version>1.0.29</version> | ||||||
| </dependency> | </dependency> | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,6 +3,6 @@ | |||||||
|   "position": 10, |   "position": 10, | ||||||
|   "link": { |   "link": { | ||||||
|     "type": "generated-index", |     "type": "generated-index", | ||||||
|     "description": "Specs of APIs and their usage." |     "description": "Details of APIs to interact with LLMs." | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										8
									
								
								docs/docs/apis-extras/_category_.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								docs/docs/apis-extras/_category_.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | { | ||||||
|  |   "label": "APIs - Extras", | ||||||
|  |   "position": 10, | ||||||
|  |   "link": { | ||||||
|  |     "type": "generated-index", | ||||||
|  |     "description": "Details of APIs to handle bunch of extra stuff." | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										20
									
								
								docs/docs/apis-extras/ping.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								docs/docs/apis-extras/ping.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | --- | ||||||
|  | sidebar_position: 3 | ||||||
|  | --- | ||||||
|  |  | ||||||
|  | # Ping | ||||||
|  |  | ||||||
|  | This API lets you check the reachability of Ollama server. | ||||||
|  |  | ||||||
|  | ```java | ||||||
|  | public class Main { | ||||||
|  |  | ||||||
|  |     public static void main(String[] args) { | ||||||
|  |         String host = "http://localhost:11434/"; | ||||||
|  |          | ||||||
|  |         OllamaAPI ollamaAPI = new OllamaAPI(host); | ||||||
|  |          | ||||||
|  |         ollamaAPI.ping(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | ``` | ||||||
							
								
								
									
										21
									
								
								docs/docs/apis-extras/request-timeout.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								docs/docs/apis-extras/request-timeout.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | |||||||
|  | --- | ||||||
|  | sidebar_position: 2 | ||||||
|  | --- | ||||||
|  |  | ||||||
|  | # Set Request Timeout | ||||||
|  |  | ||||||
|  | This API lets you set the request timeout for the Ollama client. | ||||||
|  |  | ||||||
|  | ```java | ||||||
|  | public class Main { | ||||||
|  |  | ||||||
|  |   public static void main(String[] args) { | ||||||
|  |  | ||||||
|  |     String host = "http://localhost:11434/"; | ||||||
|  |  | ||||||
|  |     OllamaAPI ollamaAPI = new OllamaAPI(host); | ||||||
|  |  | ||||||
|  |     ollamaAPI.setRequestTimeoutSeconds(10); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | ``` | ||||||
							
								
								
									
										23
									
								
								docs/docs/apis-extras/verbosity.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								docs/docs/apis-extras/verbosity.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | --- | ||||||
|  | sidebar_position: 1 | ||||||
|  | --- | ||||||
|  |  | ||||||
|  | # Set Verbosity | ||||||
|  |  | ||||||
|  | This API lets you set the verbosity of the Ollama client. | ||||||
|  |  | ||||||
|  | ## Try asking a question about the model. | ||||||
|  |  | ||||||
|  | ```java | ||||||
|  | public class Main { | ||||||
|  |  | ||||||
|  |     public static void main(String[] args) { | ||||||
|  |  | ||||||
|  |         String host = "http://localhost:11434/"; | ||||||
|  |  | ||||||
|  |         OllamaAPI ollamaAPI = new OllamaAPI(host); | ||||||
|  |  | ||||||
|  |         ollamaAPI.setVerbose(true); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | ``` | ||||||
| @@ -3,6 +3,6 @@ | |||||||
|   "position": 4, |   "position": 4, | ||||||
|   "link": { |   "link": { | ||||||
|     "type": "generated-index", |     "type": "generated-index", | ||||||
|     "description": "Specs of APIs and their usage." |     "description": "Details of APIs to manage LLMs." | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								pom.xml
									
									
									
									
									
								
							| @@ -4,7 +4,7 @@ | |||||||
|  |  | ||||||
|     <groupId>io.github.amithkoujalgi</groupId> |     <groupId>io.github.amithkoujalgi</groupId> | ||||||
|     <artifactId>ollama4j</artifactId> |     <artifactId>ollama4j</artifactId> | ||||||
|     <version>1.0.28</version> |     <version>1.0.30</version> | ||||||
|  |  | ||||||
|     <name>Ollama4j</name> |     <name>Ollama4j</name> | ||||||
|     <description>Java library for interacting with Ollama API.</description> |     <description>Java library for interacting with Ollama API.</description> | ||||||
| @@ -39,7 +39,7 @@ | |||||||
|         <connection>scm:git:git@github.com:amithkoujalgi/ollama4j.git</connection> |         <connection>scm:git:git@github.com:amithkoujalgi/ollama4j.git</connection> | ||||||
|         <developerConnection>scm:git:https://github.com/amithkoujalgi/ollama4j.git</developerConnection> |         <developerConnection>scm:git:https://github.com/amithkoujalgi/ollama4j.git</developerConnection> | ||||||
|         <url>https://github.com/amithkoujalgi/ollama4j</url> |         <url>https://github.com/amithkoujalgi/ollama4j</url> | ||||||
|         <tag>v1.0.28</tag> |         <tag>v1.0.30</tag> | ||||||
|     </scm> |     </scm> | ||||||
|  |  | ||||||
|     <build> |     <build> | ||||||
|   | |||||||
| @@ -37,6 +37,8 @@ public class OllamaAPI { | |||||||
|   private final String host; |   private final String host; | ||||||
|   private long requestTimeoutSeconds = 3; |   private long requestTimeoutSeconds = 3; | ||||||
|   private boolean verbose = true; |   private boolean verbose = true; | ||||||
|  |   private String username; | ||||||
|  |   private String password; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Instantiates the Ollama API. |    * Instantiates the Ollama API. | ||||||
| @@ -64,6 +66,14 @@ public class OllamaAPI { | |||||||
|     this.verbose = verbose; |     this.verbose = verbose; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * | ||||||
|  |    */ | ||||||
|  |   public void setBasicAuth(String username, String password) { | ||||||
|  |     this.username = username; | ||||||
|  |     this.password = password; | ||||||
|  |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * API to check the reachability of Ollama server. |    * API to check the reachability of Ollama server. | ||||||
|    * |    * | ||||||
| @@ -306,17 +316,13 @@ public class OllamaAPI { | |||||||
|    */ |    */ | ||||||
|   public List<Double> generateEmbeddings(String model, String prompt) |   public List<Double> generateEmbeddings(String model, String prompt) | ||||||
|       throws IOException, InterruptedException, OllamaBaseException { |       throws IOException, InterruptedException, OllamaBaseException { | ||||||
|     String url = this.host + "/api/embeddings"; |     URI uri = URI.create(this.host + "/api/embeddings"); | ||||||
|     String jsonData = new ModelEmbeddingsRequest(model, prompt).toString(); |     String jsonData = new ModelEmbeddingsRequest(model, prompt).toString(); | ||||||
|     HttpClient httpClient = HttpClient.newHttpClient(); |     HttpClient httpClient = HttpClient.newHttpClient(); | ||||||
|     HttpRequest request = |     HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri) | ||||||
|         HttpRequest.newBuilder() |  | ||||||
|             .uri(URI.create(url)) |  | ||||||
|             .header("Accept", "application/json") |             .header("Accept", "application/json") | ||||||
|             .header("Content-type", "application/json") |             .POST(HttpRequest.BodyPublishers.ofString(jsonData)); | ||||||
|             .timeout(Duration.ofSeconds(requestTimeoutSeconds)) |     HttpRequest request = requestBuilder.build(); | ||||||
|             .POST(HttpRequest.BodyPublishers.ofString(jsonData)) |  | ||||||
|             .build(); |  | ||||||
|     HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |     HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||||||
|     int statusCode = response.statusCode(); |     int statusCode = response.statusCode(); | ||||||
|     String responseBody = response.body(); |     String responseBody = response.body(); | ||||||
| @@ -426,14 +432,12 @@ public class OllamaAPI { | |||||||
|     long startTime = System.currentTimeMillis(); |     long startTime = System.currentTimeMillis(); | ||||||
|     HttpClient httpClient = HttpClient.newHttpClient(); |     HttpClient httpClient = HttpClient.newHttpClient(); | ||||||
|     URI uri = URI.create(this.host + "/api/generate"); |     URI uri = URI.create(this.host + "/api/generate"); | ||||||
|     HttpRequest request = |     HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri) | ||||||
|         HttpRequest.newBuilder(uri) |  | ||||||
|             .POST( |             .POST( | ||||||
|                 HttpRequest.BodyPublishers.ofString( |                 HttpRequest.BodyPublishers.ofString( | ||||||
|                     Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))) |                     Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))); | ||||||
|             .header("Content-Type", "application/json") |     HttpRequest request = requestBuilder.build(); | ||||||
|             .timeout(Duration.ofSeconds(requestTimeoutSeconds)) |     logger.debug("Ask model '" + ollamaRequestModel + "' ..."); | ||||||
|             .build(); |  | ||||||
|     HttpResponse<InputStream> response = |     HttpResponse<InputStream> response = | ||||||
|         httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); |         httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); | ||||||
|     int statusCode = response.statusCode(); |     int statusCode = response.statusCode(); | ||||||
| @@ -444,10 +448,16 @@ public class OllamaAPI { | |||||||
|       String line; |       String line; | ||||||
|       while ((line = reader.readLine()) != null) { |       while ((line = reader.readLine()) != null) { | ||||||
|         if (statusCode == 404) { |         if (statusCode == 404) { | ||||||
|  |           logger.warn("Status code: 404 (Not Found)"); | ||||||
|           OllamaErrorResponseModel ollamaResponseModel = |           OllamaErrorResponseModel ollamaResponseModel = | ||||||
|               Utils.getObjectMapper().readValue(line, OllamaErrorResponseModel.class); |               Utils.getObjectMapper().readValue(line, OllamaErrorResponseModel.class); | ||||||
|           responseBuffer.append(ollamaResponseModel.getError()); |           responseBuffer.append(ollamaResponseModel.getError()); | ||||||
|         } else { |         } else if (statusCode == 401) { | ||||||
|  |           logger.warn("Status code: 401 (Unauthorized)"); | ||||||
|  |           OllamaErrorResponseModel ollamaResponseModel = | ||||||
|  |                   Utils.getObjectMapper().readValue("{\"error\":\"Unauthorized\"}", OllamaErrorResponseModel.class); | ||||||
|  |           responseBuffer.append(ollamaResponseModel.getError()); | ||||||
|  |         }else { | ||||||
|           OllamaResponseModel ollamaResponseModel = |           OllamaResponseModel ollamaResponseModel = | ||||||
|               Utils.getObjectMapper().readValue(line, OllamaResponseModel.class); |               Utils.getObjectMapper().readValue(line, OllamaResponseModel.class); | ||||||
|           if (!ollamaResponseModel.isDone()) { |           if (!ollamaResponseModel.isDone()) { | ||||||
| @@ -457,10 +467,44 @@ public class OllamaAPI { | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     if (statusCode != 200) { |     if (statusCode != 200) { | ||||||
|  |       logger.error("Status code " + statusCode + " instead 200"); | ||||||
|       throw new OllamaBaseException(responseBuffer.toString()); |       throw new OllamaBaseException(responseBuffer.toString()); | ||||||
|     } else { |     } else { | ||||||
|       long endTime = System.currentTimeMillis(); |       long endTime = System.currentTimeMillis(); | ||||||
|       return new OllamaResult(responseBuffer.toString().trim(), endTime - startTime, statusCode); |       return new OllamaResult(responseBuffer.toString().trim(), endTime - startTime, statusCode); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * | ||||||
|  |    */ | ||||||
|  |   private HttpRequest.Builder getRequestBuilderDefault(URI uri) { | ||||||
|  |     HttpRequest.Builder requestBuilder = | ||||||
|  |             HttpRequest.newBuilder(uri) | ||||||
|  |                     .header("Content-Type", "application/json") | ||||||
|  |                     .timeout(Duration.ofSeconds(requestTimeoutSeconds)); | ||||||
|  |     if (basicAuthCredentialsSet()) { | ||||||
|  |       requestBuilder.header("Authorization", getBasicAuthHeaderValue()); | ||||||
|  |     } | ||||||
|  |     return requestBuilder; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * @return basic authentication header value (encoded credentials) | ||||||
|  |    */ | ||||||
|  |   private String getBasicAuthHeaderValue() { | ||||||
|  |     String credentialsToEncode = username + ":" + password; | ||||||
|  |     return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes()); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * @return true when Basic Auth credentials set | ||||||
|  |    */ | ||||||
|  |   private boolean basicAuthCredentialsSet() { | ||||||
|  |     if (username != null && password !=  null) { | ||||||
|  |       return true; | ||||||
|  |     } else { | ||||||
|  |       return false; | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user