From fd289fa39a772e737014750b1d27fe9291bd4689 Mon Sep 17 00:00:00 2001 From: Amith Koujalgi Date: Fri, 27 Oct 2023 12:12:47 +0530 Subject: [PATCH] init --- README.md | 27 ++++++++++++++++++- .../github/amithkoujalgi/ollama4j/Main.java | 10 ++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2e913a5..b48ea7e 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Start Ollama Container: docker run -v ~/ollama:/root/.ollama -p 11434:11434 ollama/ollama ``` -Post a question to Ollama using Ollama4j: +Pull a model: ```java public class Main { @@ -15,6 +15,31 @@ public class Main { String host = "http://localhost:11434/"; OllamaAPI ollamaAPI = new OllamaAPI(host); ollamaAPI.pullModel(OllamaModel.LLAMA2); + } +} +``` +Post a question to Ollama using Ollama4j: + +Using sync API: + +```java +public class Main { + public static void main(String[] args) throws Exception { + String host = "http://localhost:11434/"; + OllamaAPI ollamaAPI = new OllamaAPI(host); + String response = ollamaAPI.runSync(OllamaModel.LLAMA2, "Who are you?"); + System.out.println(response); + } +} +``` + +Using async API: + +```java +public class Main { + public static void main(String[] args) throws Exception { + String host = "http://localhost:11434/"; + OllamaAPI ollamaAPI = new OllamaAPI(host); OllamaAsyncResultCallback ollamaAsyncResultCallback = ollamaAPI.runAsync(OllamaModel.LLAMA2, "Who are you?"); while (true) { if (ollamaAsyncResultCallback.isComplete()) { diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/Main.java b/src/main/java/io/github/amithkoujalgi/ollama4j/Main.java index 62d4e3a..ee5997b 100644 --- a/src/main/java/io/github/amithkoujalgi/ollama4j/Main.java +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/Main.java @@ -5,14 +5,8 @@ public class Main { String host = "http://localhost:11434/"; OllamaAPI ollamaAPI = new OllamaAPI(host); ollamaAPI.pullModel(OllamaModel.LLAMA2); - OllamaAsyncResultCallback ollamaAsyncResultCallback = ollamaAPI.runAsync(OllamaModel.LLAMA2, "Who are you?"); - while (true) { - if (ollamaAsyncResultCallback.isComplete()) { - System.out.println(ollamaAsyncResultCallback.getResponse()); - break; - } - Thread.sleep(1000); - } + String response = ollamaAPI.runSync(OllamaModel.LLAMA2, "Who are you?"); + System.out.println(response); } }