Compare commits

..

11 Commits

Author SHA1 Message Date
Amith Koujalgi
660a1b937a Added llama3.1
Signed-off-by: Amith Koujalgi <koujalgi.amith@gmail.com>
2024-09-05 01:24:13 +05:30
Amith Koujalgi
fd961d7037 Added BMC
Signed-off-by: Amith Koujalgi <koujalgi.amith@gmail.com>
2024-09-05 00:48:50 +05:30
Amith Koujalgi
b48f9550c3 Update README.md 2024-09-04 00:10:09 +05:30
Amith Koujalgi
363969a275 Update README.md 2024-09-04 00:05:30 +05:30
Amith Koujalgi
992625cf86 Merge pull request #63 from peaster/gradle-dependency-fix
Change Gradle dependency example to use correct declaration
2024-08-10 10:20:09 +05:30
peaster
bbebd26d07 correct Gradle dependency example to use correct declaration 2024-08-09 16:24:43 -04:00
Amith Koujalgi
3aa0fc77cb Updated withMessages method of OllamaChatRequestBuilder to reset the messages 2024-08-09 01:39:21 +05:30
Amith Koujalgi
11a98a72a1 Updated docusaurus.config.js
Signed-off-by: Amith Koujalgi <koujalgi.amith@gmail.com>
2024-08-06 11:22:37 +05:30
Amith Koujalgi
422601c0fc Updated ModelsProcessResponse class to support ps() API
Signed-off-by: Amith Koujalgi <koujalgi.amith@gmail.com>
2024-08-06 11:11:15 +05:30
Amith Koujalgi
75e6576a13 Updated docs
Signed-off-by: Amith Koujalgi <koujalgi.amith@gmail.com>
2024-08-06 11:00:54 +05:30
Amith Koujalgi
51dd3f3e1e Added Json ignore properties for ModelsProcessResponse
Signed-off-by: Amith Koujalgi <koujalgi.amith@gmail.com>
2024-08-06 10:46:35 +05:30
13 changed files with 204 additions and 65 deletions

View File

@@ -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'
} }
``` ```
@@ -277,8 +277,12 @@ Newer artifacts are published via GitHub Actions CI workflow when a new release
- `ollama-translator`: Minecraft 1.20.6 spigot plugin allows to easily break language barriers by using ollama on the - `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. server to translate all messages into a specfic target language.
- https://github.com/liebki/ollama-translator - https://github.com/liebki/ollama-translator
- https://www.reddit.com/r/fabricmc/comments/1e65x5s/comment/ldr2vcf/
- `Ollama4j Web UI`: A web UI for Ollama written in Java using Spring Boot and Vaadin framework and - `Ollama4j Web UI`: A web UI for Ollama written in Java using Spring Boot and Vaadin framework and
Ollama4j. https://github.com/ollama4j/ollama4j-web-ui Ollama4j.
- https://github.com/ollama4j/ollama4j-web-ui
- `JnsCLI`: A command-line tool for Jenkins that manages jobs, builds, and configurations directly from the terminal while offering AI-powered error analysis for quick troubleshooting.
- https://github.com/mirum8/jnscli
#### Traction #### Traction

View File

@@ -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

View File

@@ -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);
} }
} }
``` ```

View File

@@ -58,6 +58,10 @@ const config = {
theme: { theme: {
customCss: './src/css/custom.css', customCss: './src/css/custom.css',
}, },
gtag: {
trackingID: 'G-G7FLH6FNDC',
anonymizeIP: false,
},
}), }),
], ],
], ],

View File

@@ -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",

View File

@@ -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",

View File

@@ -0,0 +1,41 @@
import React from "react";
class BuyMeACoffee extends React.Component {
constructor(props) {
super(props)
let script = document.createElement("script");
script.src = 'https://cdnjs.buymeacoffee.com/1.0.0/widget.prod.min.js';
script.dataset.name = 'BMC-Widget';
script.dataset.cfasync = 'false';
script.dataset.id = 'amithkoujalgi';
script.dataset.description = 'Support me on Buy me a coffee!';
script.dataset.message = 'If you like my work and want to say thanks, or encourage me to do more, you can buy me a coffee! 😊';
script.dataset.color = '#2e8555';
script.dataset.position = 'Right';
script.dataset.x_margin = '18';
script.dataset.y_margin = '18';
script.async = true
script.onload = function () {
let evt = document.createEvent('Event');
evt.initEvent('DOMContentLoaded', false, false);
window.dispatchEvent(evt);
}
this.script = script
}
componentDidMount() {
document.head.appendChild(this.script)
}
// componentWillUnmount() {
// document.head.removeChild(this.script);
// document.body.removeChild(document.getElementById("bmc-wbtn"))
// }
render() {
return null
}
}
export default BuyMeACoffee;

View File

@@ -37,4 +37,12 @@ div > h1,
header > h1, header > h1,
h2 > a { h2 > a {
font-size: 2rem !important; font-size: 2rem !important;
}
#bmc-wbtn{
bottom: 15px;
}
#bmc-wbtn + div{
bottom:15px;
} }

View File

@@ -3,38 +3,40 @@ import Link from '@docusaurus/Link';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import Layout from '@theme/Layout'; import Layout from '@theme/Layout';
import HomepageFeatures from '@site/src/components/HomepageFeatures'; import HomepageFeatures from '@site/src/components/HomepageFeatures';
import BuyMeACoffee from '@site/src/components/BuyMeACoffee';
import Heading from '@theme/Heading'; import Heading from '@theme/Heading';
import styles from './index.module.css'; import styles from './index.module.css';
function HomepageHeader() { function HomepageHeader() {
const {siteConfig} = useDocusaurusContext(); const {siteConfig} = useDocusaurusContext();
return (<header className={clsx('hero hero--primary', styles.heroBanner)}> return (<header className={clsx('hero hero--primary', styles.heroBanner)}>
<div className="container"> <div className="container">
<Heading as="h1" className="hero__title"> <Heading as="h1" className="hero__title">
{siteConfig.title} {siteConfig.title}
</Heading> </Heading>
<img src="img/logo.svg" alt="Ollama4j Logo" className={styles.logo} style={{maxWidth: '20vh'}}/> <img src="img/logo.svg" alt="Ollama4j Logo" className={styles.logo}
<p className="hero__subtitle">{siteConfig.tagline}</p> style={{maxWidth: '20vh'}}/>
<div className={styles.buttons}> <p className="hero__subtitle">{siteConfig.tagline}</p>
<Link <div className={styles.buttons}>
className="button button--secondary button--lg" <Link
to="/intro"> className="button button--secondary button--lg"
Getting Started to="/intro">
</Link> Getting Started
</div> </Link>
</div> </div>
</header>); </div>
</header>);
} }
export default function Home() { export default function Home() {
const {siteConfig} = useDocusaurusContext(); const {siteConfig} = useDocusaurusContext();
return (<Layout return (<Layout
title={`Hello from ${siteConfig.title}`} title={`Hello from ${siteConfig.title}`}
description="Description will go into a meta tag in <head />"> description="Description will go into a meta tag in <head />">
<HomepageHeader/> <HomepageHeader/>
<main> <main>
<HomepageFeatures/> <HomepageFeatures/>
</main> <BuyMeACoffee/>
</Layout>); </main>
} </Layout>);
}

View File

@@ -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
*/ */

View File

@@ -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;
} }

View File

@@ -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;
} }
} }

View File

@@ -14,6 +14,8 @@ public class OllamaModelType {
public static final String LLAMA2 = "llama2"; public static final String LLAMA2 = "llama2";
public static final String LLAMA3 = "llama3"; public static final String LLAMA3 = "llama3";
public static final String LLAMA3_1 = "llama3.1";
public static final String MISTRAL = "mistral"; public static final String MISTRAL = "mistral";
public static final String MIXTRAL = "mixtral"; public static final String MIXTRAL = "mixtral";
public static final String LLAVA = "llava"; public static final String LLAVA = "llava";