diff --git a/.env b/.env new file mode 100644 index 0000000..b4accff --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +API_BASE_URL="ollama.cametendo.org" +MODEL="gpt-oss:20b" \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e69de29 diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/java/AiCompanion/aicompanion2_0/Aicompanion2_0.java b/src/main/java/AiCompanion/aicompanion2_0/Aicompanion2_0.java index 899d9e4..1dcb78e 100644 --- a/src/main/java/AiCompanion/aicompanion2_0/Aicompanion2_0.java +++ b/src/main/java/AiCompanion/aicompanion2_0/Aicompanion2_0.java @@ -12,11 +12,16 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Properties; + public class Aicompanion2_0 implements ModInitializer { public static final String MOD_ID = "aicompanion2_0"; - private static String API_BASE_URL = "http://localhost:11434"; + private static String API_BASE_URL = "http://ollama.cametendo.org"; private static String API_PATH = "/api/generate"; @Override @@ -73,20 +78,21 @@ public class Aicompanion2_0 implements ModInitializer { System.out.println("[" + MOD_ID + "] MOD GELADEN!"); } + // Innerhalb deiner Klasse Aicompanion2_0 + private String callOllama(String prompt) throws Exception { - // URL deines Ollama‑Servers, ggf. anpassen URL url = new URL(API_BASE_URL + API_PATH); - - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); conn.setDoOutput(true); + // WICHTIG: "stream": false hinzugefügt String json = """ { - "model": "llama3", - "prompt": "%s" + "model": "gpt-oss:20b", + "prompt": "%s", + "stream": false } """.formatted(prompt.replace("\"", "\\\"")); @@ -95,28 +101,28 @@ public class Aicompanion2_0 implements ModInitializer { os.write(input, 0, input.length); } - int status = conn.getResponseCode(); - if (status != 200) { - System.out.println("[" + MOD_ID + "] Ollama HTTP Status: " + status); - return null; - } + if (conn.getResponseCode() != 200) return "Fehler: " + conn.getResponseCode(); StringBuilder resp = new StringBuilder(); - try (BufferedReader br = new BufferedReader( - new InputStreamReader(conn.getInputStream(), "utf-8"))) { + try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) { String line; while ((line = br.readLine()) != null) { resp.append(line); } } - conn.disconnect(); - // Sehr einfach: komplette JSON‑Antwort zurückgeben - // Später können wir das noch sauber parsen - return resp.toString(); + // Primitives Parsing der Antwort (ohne externe Library) + String fullResponse = resp.toString(); + if (fullResponse.contains("\"response\":\"")) { + int start = fullResponse.indexOf("\"response\":\"") + 12; + int end = fullResponse.indexOf("\"", start); + return fullResponse.substring(start, end); + } + + return fullResponse; } -} + private void loadConfig() { try { Path configPath = Path.of("config", "aicompanion2_0.properties"); @@ -130,5 +136,4 @@ private void loadConfig() { System.out.println("[" + MOD_ID + "] Keine Config gefunden, benutze Default-API."); } } - - +} \ No newline at end of file