zuul ollama
This commit is contained in:
@@ -15,7 +15,7 @@ public class CommandWords
|
|||||||
{
|
{
|
||||||
// a constant array that holds all valid command words
|
// a constant array that holds all valid command words
|
||||||
private static final String[] validCommands = {
|
private static final String[] validCommands = {
|
||||||
"go", "quit", "help", "look", "status", "take", "drop"
|
"go", "quit", "help", "look", "status", "take", "drop", "say"
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -168,6 +168,17 @@ public class Game
|
|||||||
else if (commandWord.equals("drop")) {
|
else if (commandWord.equals("drop")) {
|
||||||
dropItem(command);
|
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;
|
return wantToQuit;
|
||||||
}
|
}
|
||||||
|
|||||||
55
src/de/szut/zuul/Ollama.java
Executable file
55
src/de/szut/zuul/Ollama.java
Executable file
@@ -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<String> 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<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
System.out.println(response.body());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user