56 lines
2.2 KiB
Java
Executable File
56 lines
2.2 KiB
Java
Executable File
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());
|
|
}
|
|
}
|
|
}
|