mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-10-27 06:30:42 +01:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac00bb9029 | ||
|
|
67cb444d82 | ||
|
|
1914a29163 | ||
|
|
11201bc7c7 | ||
|
|
3a8b5257c0 | ||
|
|
00bb4e92dc | ||
|
|
7481c2ba0e | ||
|
|
9d336e257c | ||
|
|
2027171cb9 | ||
|
|
e06baf0d29 |
16
README.md
16
README.md
@@ -39,17 +39,17 @@ for [Ollama](https://github.com/jmorganca/ollama/blob/main/docs/api.md) APIs.
|
||||
|
||||
#### Requirements
|
||||
|
||||

|
||||

|
||||
|
||||
[![][ollama-shield]][ollama] Or [![][ollama-docker-shield]][ollama-docker]
|
||||
[![][ollama-shield]][ollama] **Or** [![][ollama-docker-shield]][ollama-docker]
|
||||
|
||||
[ollama]: https://ollama.ai/
|
||||
|
||||
[ollama-shield]: https://img.shields.io/badge/Ollama-Local_Installation-blue.svg?style=for-the-badge&labelColor=gray
|
||||
[ollama-shield]: https://img.shields.io/badge/Ollama-Local_Installation-blue.svg?style=just-the-message&labelColor=gray
|
||||
|
||||
[ollama-docker]: https://hub.docker.com/r/ollama/ollama
|
||||
|
||||
[ollama-docker-shield]: https://img.shields.io/badge/Ollama-Docker-blue.svg?style=for-the-badge&labelColor=gray
|
||||
[ollama-docker-shield]: https://img.shields.io/badge/Ollama-Docker-blue.svg?style=just-the-message&labelColor=gray
|
||||
|
||||
#### Installation
|
||||
|
||||
@@ -60,17 +60,19 @@ In your Maven project, add this dependency:
|
||||
<dependency>
|
||||
<groupId>io.github.amithkoujalgi</groupId>
|
||||
<artifactId>ollama4j</artifactId>
|
||||
<version>1.0.20</version>
|
||||
<version>1.0.29</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
Latest release: 
|
||||
Latest release:
|
||||
|
||||

|
||||
|
||||
[![][lib-shield]][lib]
|
||||
|
||||
[lib]: https://central.sonatype.com/artifact/io.github.amithkoujalgi/ollama4j
|
||||
|
||||
[lib-shield]: https://img.shields.io/badge/ollama4j-get_latest_version-blue.svg?style=for-the-badge&labelColor=gray
|
||||
[lib-shield]: https://img.shields.io/badge/ollama4j-get_latest_version-blue.svg?style=just-the-message&labelColor=gray
|
||||
|
||||
#### API Spec
|
||||
|
||||
|
||||
4
pom.xml
4
pom.xml
@@ -4,7 +4,7 @@
|
||||
|
||||
<groupId>io.github.amithkoujalgi</groupId>
|
||||
<artifactId>ollama4j</artifactId>
|
||||
<version>1.0.29</version>
|
||||
<version>1.0.31</version>
|
||||
|
||||
<name>Ollama4j</name>
|
||||
<description>Java library for interacting with Ollama API.</description>
|
||||
@@ -39,7 +39,7 @@
|
||||
<connection>scm:git:git@github.com:amithkoujalgi/ollama4j.git</connection>
|
||||
<developerConnection>scm:git:https://github.com/amithkoujalgi/ollama4j.git</developerConnection>
|
||||
<url>https://github.com/amithkoujalgi/ollama4j</url>
|
||||
<tag>v1.0.29</tag>
|
||||
<tag>v1.0.31</tag>
|
||||
</scm>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -37,6 +37,8 @@ public class OllamaAPI {
|
||||
private final String host;
|
||||
private long requestTimeoutSeconds = 3;
|
||||
private boolean verbose = true;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Instantiates the Ollama API.
|
||||
@@ -64,6 +66,14 @@ public class OllamaAPI {
|
||||
this.verbose = verbose;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setBasicAuth(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* API to check the reachability of Ollama server.
|
||||
*
|
||||
@@ -306,17 +316,13 @@ public class OllamaAPI {
|
||||
*/
|
||||
public List<Double> generateEmbeddings(String model, String prompt)
|
||||
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();
|
||||
HttpClient httpClient = HttpClient.newHttpClient();
|
||||
HttpRequest request =
|
||||
HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri)
|
||||
.header("Accept", "application/json")
|
||||
.header("Content-type", "application/json")
|
||||
.timeout(Duration.ofSeconds(requestTimeoutSeconds))
|
||||
.POST(HttpRequest.BodyPublishers.ofString(jsonData))
|
||||
.build();
|
||||
.POST(HttpRequest.BodyPublishers.ofString(jsonData));
|
||||
HttpRequest request = requestBuilder.build();
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
int statusCode = response.statusCode();
|
||||
String responseBody = response.body();
|
||||
@@ -426,14 +432,12 @@ public class OllamaAPI {
|
||||
long startTime = System.currentTimeMillis();
|
||||
HttpClient httpClient = HttpClient.newHttpClient();
|
||||
URI uri = URI.create(this.host + "/api/generate");
|
||||
HttpRequest request =
|
||||
HttpRequest.newBuilder(uri)
|
||||
HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri)
|
||||
.POST(
|
||||
HttpRequest.BodyPublishers.ofString(
|
||||
Utils.getObjectMapper().writeValueAsString(ollamaRequestModel)))
|
||||
.header("Content-Type", "application/json")
|
||||
.timeout(Duration.ofSeconds(requestTimeoutSeconds))
|
||||
.build();
|
||||
Utils.getObjectMapper().writeValueAsString(ollamaRequestModel)));
|
||||
HttpRequest request = requestBuilder.build();
|
||||
logger.debug("Ask model '" + ollamaRequestModel + "' ...");
|
||||
HttpResponse<InputStream> response =
|
||||
httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||
int statusCode = response.statusCode();
|
||||
@@ -444,10 +448,16 @@ public class OllamaAPI {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (statusCode == 404) {
|
||||
logger.warn("Status code: 404 (Not Found)");
|
||||
OllamaErrorResponseModel ollamaResponseModel =
|
||||
Utils.getObjectMapper().readValue(line, OllamaErrorResponseModel.class);
|
||||
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 =
|
||||
Utils.getObjectMapper().readValue(line, OllamaResponseModel.class);
|
||||
if (!ollamaResponseModel.isDone()) {
|
||||
@@ -457,10 +467,44 @@ public class OllamaAPI {
|
||||
}
|
||||
}
|
||||
if (statusCode != 200) {
|
||||
logger.error("Status code " + statusCode + " instead 200");
|
||||
throw new OllamaBaseException(responseBuffer.toString());
|
||||
} else {
|
||||
long endTime = System.currentTimeMillis();
|
||||
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