mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-11-03 18:10:42 +01:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 893e5dd763 | |||
| c520604f4b | |||
| a85c23d64a | |||
| d32a8b7d88 | |||
|
|
992625cf86 | ||
|
|
bbebd26d07 | ||
|
|
3aa0fc77cb | ||
|
|
11a98a72a1 | ||
|
|
422601c0fc | ||
|
|
75e6576a13 | ||
|
|
51dd3f3e1e |
32
.gitea/workflows/publish.yaml
Normal file
32
.gitea/workflows/publish.yaml
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
name: Build and Publish
|
||||||
|
on: push
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: standard-22.04
|
||||||
|
steps:
|
||||||
|
- name: Check out
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set Up Java
|
||||||
|
uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
distribution: 'temurin'
|
||||||
|
java-version: '21'
|
||||||
|
#cache: 'maven'
|
||||||
|
#server-id: 'gitea'
|
||||||
|
|
||||||
|
- name: Set up Maven
|
||||||
|
uses: stCarolas/setup-maven@v5
|
||||||
|
with:
|
||||||
|
maven-version: 3.8.2
|
||||||
|
|
||||||
|
- run: cat /root/.m2/toolchains.xml
|
||||||
|
- run: cat /root/.m2/settings.xml
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: mvn -B package --file pom.xml
|
||||||
|
|
||||||
|
- name: Publish
|
||||||
|
run: mvn deploy
|
||||||
|
|
||||||
@@ -221,7 +221,7 @@ In your Maven project, add this dependency:
|
|||||||
|
|
||||||
```groovy
|
```groovy
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.github.ollama4j:ollama4j:1.0.79'
|
implementation 'io.github.ollama4j:ollama4j:1.0.79'
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -82,6 +82,33 @@ You will get a response similar to:
|
|||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Conversational loop
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
OllamaAPI ollamaAPI = new OllamaAPI();
|
||||||
|
ollamaAPI.setRequestTimeoutSeconds(60);
|
||||||
|
|
||||||
|
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance("<your-model>");
|
||||||
|
|
||||||
|
OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.USER, "<your-first-message>").build();
|
||||||
|
OllamaChatResult initialChatResult = ollamaAPI.chat(requestModel);
|
||||||
|
System.out.println(initialChatResult.getResponse());
|
||||||
|
|
||||||
|
List<OllamaChatMessage> history = initialChatResult.getChatHistory();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
OllamaChatResult chatResult = ollamaAPI.chat(builder.withMessages(history).withMessage(OllamaChatMessageRole.USER, "<your-new-message").build());
|
||||||
|
System.out.println(chatResult.getResponse());
|
||||||
|
history = chatResult.getChatHistory();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Create a conversation where the answer is streamed
|
## Create a conversation where the answer is streamed
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
|||||||
@@ -118,6 +118,24 @@ Create a new Java class in your project and add this code.
|
|||||||
```java
|
```java
|
||||||
import io.github.ollama4j.OllamaAPI;
|
import io.github.ollama4j.OllamaAPI;
|
||||||
|
|
||||||
|
public class OllamaAPITest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
OllamaAPI ollamaAPI = new OllamaAPI();
|
||||||
|
|
||||||
|
boolean isOllamaServerReachable = ollamaAPI.ping();
|
||||||
|
|
||||||
|
System.out.println("Is Ollama server running: " + isOllamaServerReachable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
This uses the default Ollama host as `http://localhost:11434`.
|
||||||
|
|
||||||
|
Specify a different Ollama host that you want to connect to.
|
||||||
|
|
||||||
|
```java
|
||||||
|
import io.github.ollama4j.OllamaAPI;
|
||||||
|
|
||||||
public class OllamaAPITest {
|
public class OllamaAPITest {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -129,7 +147,7 @@ public class OllamaAPITest {
|
|||||||
|
|
||||||
boolean isOllamaServerReachable = ollamaAPI.ping();
|
boolean isOllamaServerReachable = ollamaAPI.ping();
|
||||||
|
|
||||||
System.out.println("Is Ollama server alive: " + isOllamaServerReachable);
|
System.out.println("Is Ollama server running: " + isOllamaServerReachable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -58,6 +58,10 @@ const config = {
|
|||||||
theme: {
|
theme: {
|
||||||
customCss: './src/css/custom.css',
|
customCss: './src/css/custom.css',
|
||||||
},
|
},
|
||||||
|
gtag: {
|
||||||
|
trackingID: 'G-G7FLH6FNDC',
|
||||||
|
anonymizeIP: false,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|||||||
1
docs/package-lock.json
generated
1
docs/package-lock.json
generated
@@ -9,6 +9,7 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@docusaurus/core": "^3.4.0",
|
"@docusaurus/core": "^3.4.0",
|
||||||
|
"@docusaurus/plugin-google-gtag": "^3.4.0",
|
||||||
"@docusaurus/preset-classic": "^3.4.0",
|
"@docusaurus/preset-classic": "^3.4.0",
|
||||||
"@docusaurus/theme-mermaid": "^3.4.0",
|
"@docusaurus/theme-mermaid": "^3.4.0",
|
||||||
"@mdx-js/react": "^3.0.0",
|
"@mdx-js/react": "^3.0.0",
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@docusaurus/core": "^3.4.0",
|
"@docusaurus/core": "^3.4.0",
|
||||||
|
"@docusaurus/plugin-google-gtag": "^3.4.0",
|
||||||
"@docusaurus/preset-classic": "^3.4.0",
|
"@docusaurus/preset-classic": "^3.4.0",
|
||||||
"@docusaurus/theme-mermaid": "^3.4.0",
|
"@docusaurus/theme-mermaid": "^3.4.0",
|
||||||
"@mdx-js/react": "^3.0.0",
|
"@mdx-js/react": "^3.0.0",
|
||||||
|
|||||||
14
pom.xml
14
pom.xml
@@ -129,6 +129,13 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>gitea</id>
|
||||||
|
<url>https://gitea.seeseepuff.be/api/packages/seeseemelk/maven</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
@@ -178,8 +185,13 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
|
<snapshotRepository>
|
||||||
|
<id>gitea</id>
|
||||||
|
<url>https://gitea.seeseepuff.be/api/packages/seeseemelk/maven</url>
|
||||||
|
</snapshotRepository>
|
||||||
<repository>
|
<repository>
|
||||||
<id>mvn-repo-id</id>
|
<id>gitea</id>
|
||||||
|
<url>https://gitea.seeseepuff.be/api/packages/seeseemelk/maven</url>
|
||||||
</repository>
|
</repository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,14 @@ public class OllamaAPI {
|
|||||||
private final ToolRegistry toolRegistry = new ToolRegistry();
|
private final ToolRegistry toolRegistry = new ToolRegistry();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates the Ollama API.
|
* Instantiates the Ollama API with default Ollama host: <a href="http://localhost:11434">http://localhost:11434</a>
|
||||||
|
**/
|
||||||
|
public OllamaAPI() {
|
||||||
|
this.host = "http://localhost:11434";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates the Ollama API with specified Ollama host address.
|
||||||
*
|
*
|
||||||
* @param host the host address of Ollama server
|
* @param host the host address of Ollama server
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
package io.github.ollama4j.models.chat;
|
package io.github.ollama4j.models.chat;
|
||||||
|
|
||||||
|
import io.github.ollama4j.utils.Options;
|
||||||
|
import io.github.ollama4j.utils.Utils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
@@ -8,12 +13,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import io.github.ollama4j.utils.Options;
|
|
||||||
import io.github.ollama4j.utils.Utils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class for creating {@link OllamaChatRequest} objects using the builder-pattern.
|
* Helper class for creating {@link OllamaChatRequest} objects using the builder-pattern.
|
||||||
*/
|
*/
|
||||||
@@ -21,88 +20,85 @@ public class OllamaChatRequestBuilder {
|
|||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(OllamaChatRequestBuilder.class);
|
private static final Logger LOG = LoggerFactory.getLogger(OllamaChatRequestBuilder.class);
|
||||||
|
|
||||||
private OllamaChatRequestBuilder(String model, List<OllamaChatMessage> messages){
|
private OllamaChatRequestBuilder(String model, List<OllamaChatMessage> messages) {
|
||||||
request = new OllamaChatRequest(model, messages);
|
request = new OllamaChatRequest(model, messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
private OllamaChatRequest request;
|
private OllamaChatRequest request;
|
||||||
|
|
||||||
public static OllamaChatRequestBuilder getInstance(String model){
|
public static OllamaChatRequestBuilder getInstance(String model) {
|
||||||
return new OllamaChatRequestBuilder(model, new ArrayList<>());
|
return new OllamaChatRequestBuilder(model, new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaChatRequest build(){
|
public OllamaChatRequest build() {
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset(){
|
public void reset() {
|
||||||
request = new OllamaChatRequest(request.getModel(), new ArrayList<>());
|
request = new OllamaChatRequest(request.getModel(), new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List<File> images){
|
public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List<File> images) {
|
||||||
List<OllamaChatMessage> messages = this.request.getMessages();
|
List<OllamaChatMessage> messages = this.request.getMessages();
|
||||||
|
|
||||||
List<byte[]> binaryImages = images.stream().map(file -> {
|
List<byte[]> binaryImages = images.stream().map(file -> {
|
||||||
try {
|
try {
|
||||||
return Files.readAllBytes(file.toPath());
|
return Files.readAllBytes(file.toPath());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOG.warn(String.format("File '%s' could not be accessed, will not add to message!",file.toPath()), e);
|
LOG.warn(String.format("File '%s' could not be accessed, will not add to message!", file.toPath()), e);
|
||||||
return new byte[0];
|
return new byte[0];
|
||||||
}
|
}
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
|
|
||||||
messages.add(new OllamaChatMessage(role,content,binaryImages));
|
messages.add(new OllamaChatMessage(role, content, binaryImages));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, String... imageUrls){
|
public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, String... imageUrls) {
|
||||||
List<OllamaChatMessage> messages = this.request.getMessages();
|
List<OllamaChatMessage> messages = this.request.getMessages();
|
||||||
List<byte[]> binaryImages = null;
|
List<byte[]> binaryImages = null;
|
||||||
if(imageUrls.length>0){
|
if (imageUrls.length > 0) {
|
||||||
binaryImages = new ArrayList<>();
|
binaryImages = new ArrayList<>();
|
||||||
for (String imageUrl : imageUrls) {
|
for (String imageUrl : imageUrls) {
|
||||||
try{
|
try {
|
||||||
binaryImages.add(Utils.loadImageBytesFromUrl(imageUrl));
|
binaryImages.add(Utils.loadImageBytesFromUrl(imageUrl));
|
||||||
}
|
} catch (URISyntaxException e) {
|
||||||
catch (URISyntaxException e){
|
LOG.warn(String.format("URL '%s' could not be accessed, will not add to message!", imageUrl), e);
|
||||||
LOG.warn(String.format("URL '%s' could not be accessed, will not add to message!",imageUrl), e);
|
} catch (IOException e) {
|
||||||
}
|
LOG.warn(String.format("Content of URL '%s' could not be read, will not add to message!", imageUrl), e);
|
||||||
catch (IOException e){
|
|
||||||
LOG.warn(String.format("Content of URL '%s' could not be read, will not add to message!",imageUrl), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
messages.add(new OllamaChatMessage(role,content,binaryImages));
|
messages.add(new OllamaChatMessage(role, content, binaryImages));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaChatRequestBuilder withMessages(List<OllamaChatMessage> messages){
|
public OllamaChatRequestBuilder withMessages(List<OllamaChatMessage> messages) {
|
||||||
this.request.getMessages().addAll(messages);
|
return new OllamaChatRequestBuilder(request.getModel(), messages);
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaChatRequestBuilder withOptions(Options options){
|
public OllamaChatRequestBuilder withOptions(Options options) {
|
||||||
this.request.setOptions(options.getOptionsMap());
|
this.request.setOptions(options.getOptionsMap());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaChatRequestBuilder withGetJsonResponse(){
|
public OllamaChatRequestBuilder withGetJsonResponse() {
|
||||||
this.request.setReturnFormatJson(true);
|
this.request.setReturnFormatJson(true);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaChatRequestBuilder withTemplate(String template){
|
public OllamaChatRequestBuilder withTemplate(String template) {
|
||||||
this.request.setTemplate(template);
|
this.request.setTemplate(template);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaChatRequestBuilder withStreaming(){
|
public OllamaChatRequestBuilder withStreaming() {
|
||||||
this.request.setStream(true);
|
this.request.setStream(true);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaChatRequestBuilder withKeepAlive(String keepAlive){
|
public OllamaChatRequestBuilder withKeepAlive(String keepAlive) {
|
||||||
this.request.setKeepAlive(keepAlive);
|
this.request.setKeepAlive(keepAlive);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,17 +16,11 @@ public class OllamaChatResult extends OllamaResult{
|
|||||||
List<OllamaChatMessage> chatHistory) {
|
List<OllamaChatMessage> chatHistory) {
|
||||||
super(response, responseTime, httpStatusCode);
|
super(response, responseTime, httpStatusCode);
|
||||||
this.chatHistory = chatHistory;
|
this.chatHistory = chatHistory;
|
||||||
appendAnswerToChatHistory(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<OllamaChatMessage> getChatHistory() {
|
public List<OllamaChatMessage> getChatHistory() {
|
||||||
return chatHistory;
|
return chatHistory;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void appendAnswerToChatHistory(String answer){
|
|
||||||
OllamaChatMessage assistantMessage = new OllamaChatMessage(OllamaChatMessageRole.ASSISTANT, answer);
|
|
||||||
this.chatHistory.add(assistantMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ public class OllamaChatStreamObserver {
|
|||||||
|
|
||||||
private List<OllamaChatResponseModel> responseParts = new ArrayList<>();
|
private List<OllamaChatResponseModel> responseParts = new ArrayList<>();
|
||||||
|
|
||||||
private String message = "";
|
|
||||||
|
|
||||||
public OllamaChatStreamObserver(OllamaStreamHandler streamHandler) {
|
public OllamaChatStreamObserver(OllamaStreamHandler streamHandler) {
|
||||||
this.streamHandler = streamHandler;
|
this.streamHandler = streamHandler;
|
||||||
}
|
}
|
||||||
@@ -23,8 +21,7 @@ public class OllamaChatStreamObserver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void handleCurrentResponsePart(OllamaChatResponseModel currentResponsePart) {
|
protected void handleCurrentResponsePart(OllamaChatResponseModel currentResponsePart) {
|
||||||
message = message + currentResponsePart.getMessage().getContent();
|
streamHandler.accept(currentResponsePart.getMessage().getContent());
|
||||||
streamHandler.accept(message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package io.github.ollama4j.models.ps;
|
package io.github.ollama4j.models.ps;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@@ -7,29 +9,55 @@ import java.util.List;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class ModelsProcessResponse {
|
public class ModelsProcessResponse {
|
||||||
|
@JsonProperty("models")
|
||||||
private List<ModelProcess> models;
|
private List<ModelProcess> models;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public static class ModelProcess {
|
public static class ModelProcess {
|
||||||
|
@JsonProperty("name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@JsonProperty("model")
|
||||||
private String model;
|
private String model;
|
||||||
|
|
||||||
|
@JsonProperty("size")
|
||||||
private long size;
|
private long size;
|
||||||
|
|
||||||
|
@JsonProperty("digest")
|
||||||
private String digest;
|
private String digest;
|
||||||
|
|
||||||
|
@JsonProperty("details")
|
||||||
private ModelDetails details;
|
private ModelDetails details;
|
||||||
private String expiresAt;
|
|
||||||
|
@JsonProperty("expires_at")
|
||||||
|
private String expiresAt; // Consider using LocalDateTime if you need to process date/time
|
||||||
|
|
||||||
|
@JsonProperty("size_vram")
|
||||||
private long sizeVram;
|
private long sizeVram;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public static class ModelDetails {
|
public static class ModelDetails {
|
||||||
|
@JsonProperty("parent_model")
|
||||||
private String parentModel;
|
private String parentModel;
|
||||||
|
|
||||||
|
@JsonProperty("format")
|
||||||
private String format;
|
private String format;
|
||||||
|
|
||||||
|
@JsonProperty("family")
|
||||||
private String family;
|
private String family;
|
||||||
|
|
||||||
|
@JsonProperty("families")
|
||||||
private List<String> families;
|
private List<String> families;
|
||||||
|
|
||||||
|
@JsonProperty("parameter_size")
|
||||||
private String parameterSize;
|
private String parameterSize;
|
||||||
|
|
||||||
|
@JsonProperty("quantization_level")
|
||||||
private String quantizationLevel;
|
private String quantizationLevel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user