Made Core#qurryOllama asynchronous instead to make so it doesn't lock up the calling thread, issue discovered in the (Ollama-chat)[https://git.server.4zellen.se/neurodock/Ollama-chat] Proof of Concept
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@ plugins {
|
||||
id 'java-library'
|
||||
}
|
||||
|
||||
version = '1.6.6'
|
||||
version = '1.7'
|
||||
|
||||
dependencies {
|
||||
implementation project(":Plugin-API")
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -601,65 +602,65 @@ public class Core {
|
||||
* Sends the OllamaObject to Ollama
|
||||
* @return The response from Ollama
|
||||
*/
|
||||
public JSONObject qurryOllama()
|
||||
public CompletableFuture<JSONObject> qurryOllama()
|
||||
{
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setDoOutput(true);
|
||||
connection.setConnectTimeout(80*1000);
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setDoOutput(true);
|
||||
connection.setConnectTimeout(80 * 1000);
|
||||
|
||||
String ollamaObjectString = ollamaObject.toString();
|
||||
String ollamaObjectString = ollamaObject.toString();
|
||||
|
||||
ollamaObjectString = ollamaObjectString.replace("\n", "\\n");
|
||||
ollamaObjectString = ollamaObjectString.replace("\n", "\\n");
|
||||
|
||||
try(DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
|
||||
wr.write(ollamaObjectString.getBytes(StandardCharsets.UTF_8));
|
||||
wr.flush();
|
||||
}
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
|
||||
// HTTP_OK or 200 response code generally means that the server ran successfully without any errors
|
||||
StringBuilder response = new StringBuilder();
|
||||
|
||||
// Read response content
|
||||
// connection.getInputStream() purpose is to obtain an input stream for reading the server's response.
|
||||
try (
|
||||
BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line); // Adds every line to response till the end of file.
|
||||
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
|
||||
wr.write(ollamaObjectString.getBytes(StandardCharsets.UTF_8));
|
||||
wr.flush();
|
||||
}
|
||||
}catch (Exception ex)
|
||||
{
|
||||
// If the server returns an error, we read the error stream instead
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
|
||||
// HTTP_OK or 200 response code generally means that the server ran successfully without any errors
|
||||
StringBuilder response = new StringBuilder();
|
||||
|
||||
// Read response content
|
||||
// connection.getInputStream() purpose is to obtain an input stream for reading the server's response.
|
||||
try (
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
response.append(line); // Adds every line to response till the end of file.
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
// If the server returns an error, we read the error stream instead
|
||||
InputStream errorStream = connection.getErrorStream();
|
||||
if (errorStream != null) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error reading error stream: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
|
||||
|
||||
connection.disconnect();
|
||||
return new JSONObject(response.toString());
|
||||
connection.disconnect();
|
||||
return new JSONObject(response.toString());
|
||||
} else {
|
||||
connection.disconnect();
|
||||
printMessageHandler.printErrorMessage(new OllamaMessage(OllamaMessageRole.SYSTEM,"Error: HTTP Response code - " + responseCode + "\n" + response.toString()));
|
||||
throw new RuntimeException("HTTP Response code - " + responseCode);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
else {
|
||||
connection.disconnect();
|
||||
System.err.println("Error: HTTP Response code - " + responseCode + "\n"+response.toString());
|
||||
throw new RuntimeException("HTTP Response code - " + responseCode);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// System.err.println("Error: JSON: "+);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -693,7 +694,7 @@ public class Core {
|
||||
}
|
||||
|
||||
checkIfResponceMessage(response);
|
||||
handleResponse(qurryOllama());
|
||||
qurryOllama().thenAccept(this::handleResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -169,7 +169,7 @@ public class Display {
|
||||
writeLog("User: " + message);
|
||||
core.getOllamaObject().addMessage(new OllamaMessage(OllamaMessageRole.USER, message.toString()));
|
||||
//System.out.println(ollamaObject.toString());
|
||||
core.handleResponse(core.qurryOllama());
|
||||
core.qurryOllama().thenAccept(core::handleResponse);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user