forked from Mirror/ollama4j
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e750c2d7f9 | ||
|
|
62f16131f3 | ||
|
|
2cbaf12d7c | ||
|
|
e2d555d404 | ||
|
|
c296b34174 | ||
|
|
e8f99f28ec | ||
|
|
250b1abc79 | ||
|
|
42b15ad93f | ||
|
|
6f7a714bae | ||
|
|
92618e5084 | ||
|
|
391a9242c3 | ||
|
|
e1b6dc3b54 | ||
|
|
04124cf978 | ||
|
|
e4e717b747 |
10
README.md
10
README.md
@@ -110,6 +110,16 @@ make it
|
|||||||
Releases (newer artifact versions) are done automatically on pushing the code to the `main` branch through GitHub
|
Releases (newer artifact versions) are done automatically on pushing the code to the `main` branch through GitHub
|
||||||
Actions CI workflow.
|
Actions CI workflow.
|
||||||
|
|
||||||
|
#### Who's using Ollama4j?
|
||||||
|
|
||||||
|
- `Datafaker`: a library to generate fake data
|
||||||
|
- https://github.com/datafaker-net/datafaker-experimental/tree/main/ollama-api
|
||||||
|
- `Vaadin Web UI`: UI-Tester for Interactions with Ollama via ollama4j
|
||||||
|
- https://github.com/TEAMPB/ollama4j-vaadin-ui
|
||||||
|
- `ollama-translator`: Minecraft 1.20.6 spigot plugin allows to easily break language barriers by using ollama on the
|
||||||
|
server to translate all messages into a specfic target language.
|
||||||
|
- https://github.com/liebki/ollama-translator
|
||||||
|
|
||||||
#### Traction
|
#### Traction
|
||||||
|
|
||||||
[](https://star-history.com/#amithkoujalgi/ollama4j&Date)
|
[](https://star-history.com/#amithkoujalgi/ollama4j&Date)
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ You will get a response similar to:
|
|||||||
|
|
||||||
## Use a simple Console Output Stream Handler
|
## Use a simple Console Output Stream Handler
|
||||||
|
|
||||||
```
|
```java
|
||||||
import io.github.amithkoujalgi.ollama4j.core.impl.ConsoleOutputStreamHandler;
|
import io.github.amithkoujalgi.ollama4j.core.impl.ConsoleOutputStreamHandler;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|||||||
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.65</version>
|
<version>1.0.69</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.65</tag>
|
<tag>v1.0.69</tag>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -9,18 +9,13 @@ import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatResult;
|
|||||||
import io.github.amithkoujalgi.ollama4j.core.models.embeddings.OllamaEmbeddingResponseModel;
|
import io.github.amithkoujalgi.ollama4j.core.models.embeddings.OllamaEmbeddingResponseModel;
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.embeddings.OllamaEmbeddingsRequestModel;
|
import io.github.amithkoujalgi.ollama4j.core.models.embeddings.OllamaEmbeddingsRequestModel;
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.generate.OllamaGenerateRequestModel;
|
import io.github.amithkoujalgi.ollama4j.core.models.generate.OllamaGenerateRequestModel;
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.request.CustomModelFileContentsRequest;
|
import io.github.amithkoujalgi.ollama4j.core.models.request.*;
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.request.CustomModelFilePathRequest;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.request.ModelRequest;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.request.OllamaChatEndpointCaller;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.request.OllamaGenerateEndpointCaller;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.utils.Options;
|
import io.github.amithkoujalgi.ollama4j.core.utils.Options;
|
||||||
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
|
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
|
||||||
import java.io.BufferedReader;
|
import org.slf4j.Logger;
|
||||||
import java.io.File;
|
import org.slf4j.LoggerFactory;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.*;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
@@ -33,16 +28,16 @@ import java.time.Duration;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/** The base Ollama API class. */
|
/**
|
||||||
|
* The base Ollama API class.
|
||||||
|
*/
|
||||||
@SuppressWarnings("DuplicatedCode")
|
@SuppressWarnings("DuplicatedCode")
|
||||||
public class OllamaAPI {
|
public class OllamaAPI {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(OllamaAPI.class);
|
private static final Logger logger = LoggerFactory.getLogger(OllamaAPI.class);
|
||||||
private final String host;
|
private final String host;
|
||||||
private long requestTimeoutSeconds = 3;
|
private long requestTimeoutSeconds = 10;
|
||||||
private boolean verbose = true;
|
private boolean verbose = true;
|
||||||
private BasicAuth basicAuth;
|
private BasicAuth basicAuth;
|
||||||
|
|
||||||
@@ -365,7 +360,7 @@ public class OllamaAPI {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience method to call Ollama API without streaming responses.
|
* Convenience method to call Ollama API without streaming responses.
|
||||||
*
|
* <p>
|
||||||
* Uses {@link #generate(String, String, Options, OllamaStreamHandler)}
|
* Uses {@link #generate(String, String, Options, OllamaStreamHandler)}
|
||||||
*/
|
*/
|
||||||
public OllamaResult generate(String model, String prompt, Options options)
|
public OllamaResult generate(String model, String prompt, Options options)
|
||||||
@@ -420,7 +415,7 @@ public class OllamaAPI {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience method to call Ollama API without streaming responses.
|
* Convenience method to call Ollama API without streaming responses.
|
||||||
*
|
* <p>
|
||||||
* Uses {@link #generateWithImageFiles(String, String, List, Options, OllamaStreamHandler)}
|
* Uses {@link #generateWithImageFiles(String, String, List, Options, OllamaStreamHandler)}
|
||||||
*/
|
*/
|
||||||
public OllamaResult generateWithImageFiles(
|
public OllamaResult generateWithImageFiles(
|
||||||
@@ -456,7 +451,7 @@ public class OllamaAPI {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience method to call Ollama API without streaming responses.
|
* Convenience method to call Ollama API without streaming responses.
|
||||||
*
|
* <p>
|
||||||
* Uses {@link #generateWithImageURLs(String, String, List, Options, OllamaStreamHandler)}
|
* Uses {@link #generateWithImageURLs(String, String, List, Options, OllamaStreamHandler)}
|
||||||
*/
|
*/
|
||||||
public OllamaResult generateWithImageURLs(String model, String prompt, List<String> imageURLs,
|
public OllamaResult generateWithImageURLs(String model, String prompt, List<String> imageURLs,
|
||||||
@@ -466,7 +461,6 @@ public class OllamaAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ask a question to a model based on a given message stack (i.e. a chat history). Creates a synchronous call to the api
|
* Ask a question to a model based on a given message stack (i.e. a chat history). Creates a synchronous call to the api
|
||||||
* 'api/chat'.
|
* 'api/chat'.
|
||||||
@@ -485,7 +479,7 @@ public class OllamaAPI {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Ask a question to a model using an {@link OllamaChatRequestModel}. This can be constructed using an {@link OllamaChatRequestBuilder}.
|
* Ask a question to a model using an {@link OllamaChatRequestModel}. This can be constructed using an {@link OllamaChatRequestBuilder}.
|
||||||
*
|
* <p>
|
||||||
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
|
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
|
||||||
*
|
*
|
||||||
* @param request request object to be sent to the server
|
* @param request request object to be sent to the server
|
||||||
@@ -500,7 +494,7 @@ public class OllamaAPI {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Ask a question to a model using an {@link OllamaChatRequestModel}. This can be constructed using an {@link OllamaChatRequestBuilder}.
|
* Ask a question to a model using an {@link OllamaChatRequestModel}. This can be constructed using an {@link OllamaChatRequestBuilder}.
|
||||||
*
|
* <p>
|
||||||
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
|
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
|
||||||
*
|
*
|
||||||
* @param request request object to be sent to the server
|
* @param request request object to be sent to the server
|
||||||
@@ -516,8 +510,7 @@ public class OllamaAPI {
|
|||||||
if (streamHandler != null) {
|
if (streamHandler != null) {
|
||||||
request.setStream(true);
|
request.setStream(true);
|
||||||
result = requestCaller.call(request, streamHandler);
|
result = requestCaller.call(request, streamHandler);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
result = requestCaller.callSync(request);
|
result = requestCaller.callSync(request);
|
||||||
}
|
}
|
||||||
return new OllamaChatResult(result.getResponse(), result.getResponseTime(), result.getHttpStatusCode(), request.getMessages());
|
return new OllamaChatResult(result.getResponse(), result.getResponseTime(), result.getHttpStatusCode(), request.getMessages());
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
package io.github.amithkoujalgi.ollama4j.core.models.chat;
|
package io.github.amithkoujalgi.ollama4j.core.models.chat;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class OllamaChatResponseModel {
|
public class OllamaChatResponseModel {
|
||||||
private String model;
|
private String model;
|
||||||
private @JsonProperty("created_at") String createdAt;
|
private @JsonProperty("created_at") String createdAt;
|
||||||
|
private @JsonProperty("done_reason") String doneReason;
|
||||||
private OllamaChatMessage message;
|
private OllamaChatMessage message;
|
||||||
private boolean done;
|
private boolean done;
|
||||||
private String error;
|
private String error;
|
||||||
|
|||||||
@@ -1,12 +1,6 @@
|
|||||||
package io.github.amithkoujalgi.ollama4j.core.models.request;
|
package io.github.amithkoujalgi.ollama4j.core.models.request;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.OllamaStreamHandler;
|
import io.github.amithkoujalgi.ollama4j.core.OllamaStreamHandler;
|
||||||
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
|
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
|
import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
|
||||||
@@ -15,6 +9,10 @@ import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatResponseModel
|
|||||||
import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatStreamObserver;
|
import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatStreamObserver;
|
||||||
import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody;
|
import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody;
|
||||||
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
|
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specialization class for requests
|
* Specialization class for requests
|
||||||
@@ -54,7 +52,4 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller{
|
|||||||
streamObserver = new OllamaChatStreamObserver(streamHandler);
|
streamObserver = new OllamaChatStreamObserver(streamHandler);
|
||||||
return super.callSync(body);
|
return super.callSync(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
package io.github.amithkoujalgi.ollama4j.core.models.request;
|
package io.github.amithkoujalgi.ollama4j.core.models.request;
|
||||||
|
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.models.OllamaErrorResponseModel;
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody;
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -12,17 +22,6 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.OllamaErrorResponseModel;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody;
|
|
||||||
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract helperclass to call the ollama api server.
|
* Abstract helperclass to call the ollama api server.
|
||||||
*/
|
*/
|
||||||
@@ -57,7 +56,6 @@ public abstract class OllamaEndpointCaller {
|
|||||||
* @throws InterruptedException in case the server is not reachable or network issues happen
|
* @throws InterruptedException in case the server is not reachable or network issues happen
|
||||||
*/
|
*/
|
||||||
public OllamaResult callSync(OllamaRequestBody body) throws OllamaBaseException, IOException, InterruptedException {
|
public OllamaResult callSync(OllamaRequestBody body) throws OllamaBaseException, IOException, InterruptedException {
|
||||||
|
|
||||||
// Create Request
|
// Create Request
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
HttpClient httpClient = HttpClient.newHttpClient();
|
HttpClient httpClient = HttpClient.newHttpClient();
|
||||||
@@ -71,7 +69,6 @@ public abstract class OllamaEndpointCaller {
|
|||||||
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();
|
||||||
InputStream responseBodyStream = response.body();
|
InputStream responseBodyStream = response.body();
|
||||||
StringBuilder responseBuffer = new StringBuilder();
|
StringBuilder responseBuffer = new StringBuilder();
|
||||||
|
|||||||
Reference in New Issue
Block a user