From d1bb5a7eb1aaecc05a8914194b0198d4d68c58b3 Mon Sep 17 00:00:00 2001 From: Wim Wenigerkind Date: Thu, 20 Feb 2025 13:40:31 +0100 Subject: [PATCH] zuul ollama --- src/de/szut/zuul/CommandWords.java | 2 +- src/de/szut/zuul/Game.java | 11 ++++++ src/de/szut/zuul/Ollama.java | 55 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100755 src/de/szut/zuul/Ollama.java diff --git a/src/de/szut/zuul/CommandWords.java b/src/de/szut/zuul/CommandWords.java index ca71bec..139642c 100644 --- a/src/de/szut/zuul/CommandWords.java +++ b/src/de/szut/zuul/CommandWords.java @@ -15,7 +15,7 @@ public class CommandWords { // a constant array that holds all valid command words private static final String[] validCommands = { - "go", "quit", "help", "look", "status", "take", "drop" + "go", "quit", "help", "look", "status", "take", "drop", "say" }; /** diff --git a/src/de/szut/zuul/Game.java b/src/de/szut/zuul/Game.java index ce6eece..3160bb9 100644 --- a/src/de/szut/zuul/Game.java +++ b/src/de/szut/zuul/Game.java @@ -168,6 +168,17 @@ public class Game else if (commandWord.equals("drop")) { dropItem(command); } + else if (commandWord.equals("say")) { + if (command.hasSecondWord()) { + if (command.getSecondWord().equals("health")) { + Ollama.status(); + } else { + Ollama.chat(command.getSecondWord(), player.getCurrentRoom()); + } + } else { + System.out.println("Chat what?"); + } + } return wantToQuit; } diff --git a/src/de/szut/zuul/Ollama.java b/src/de/szut/zuul/Ollama.java new file mode 100755 index 0000000..5b88da4 --- /dev/null +++ b/src/de/szut/zuul/Ollama.java @@ -0,0 +1,55 @@ +package de.szut.zuul; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +public class Ollama { + public void spit() { + System.out.println("The ollama spits at you."); + } + + public String say() { + return "The ollama says: \"Hello!\""; + } + + public static void chat(String message, Room room) { + + + room.getLongDescription(); + + String model = "llama3.1"; + String system = "You are a NPC in my game World of Zuul and the character kann chat with you please answer short. Here is the room description of the current room: " + room.getLongDescription().replace("\n", " ") + "."; + + String body = "{\"model\": \"" + model + "\", \"messages\": [{\"role\": \"system\", \"content\": \"" + system + "\"}, {\"role\": \"user\", \"content\": \"" + message + "\"}], \"stream\": false}"; + + try { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://localhost:11434/api/chat")) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(body)) + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + String responseBody = response.body(); + String content = responseBody.split("\"content\":\"")[1].split("\"")[0]; + System.out.println(content); + } catch (Exception e) { + System.out.println("Error: " + e.getMessage()); + } + } + public static void status() { + try { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://localhost:11434/")) + .GET() + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + System.out.println(response.body()); + } catch (Exception e) { + System.out.println("Error: " + e.getMessage()); + } + } +}