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'
|
id 'java-library'
|
||||||
}
|
}
|
||||||
|
|
||||||
version = '1.6.6'
|
version = '1.7'
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation project(":Plugin-API")
|
implementation project(":Plugin-API")
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import java.nio.file.Path;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -601,20 +602,21 @@ public class Core {
|
|||||||
* Sends the OllamaObject to Ollama
|
* Sends the OllamaObject to Ollama
|
||||||
* @return The response from Ollama
|
* @return The response from Ollama
|
||||||
*/
|
*/
|
||||||
public JSONObject qurryOllama()
|
public CompletableFuture<JSONObject> qurryOllama()
|
||||||
{
|
{
|
||||||
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
try {
|
try {
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setRequestMethod("POST");
|
connection.setRequestMethod("POST");
|
||||||
connection.setRequestProperty("Content-Type", "application/json");
|
connection.setRequestProperty("Content-Type", "application/json");
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
connection.setConnectTimeout(80*1000);
|
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())) {
|
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
|
||||||
wr.write(ollamaObjectString.getBytes(StandardCharsets.UTF_8));
|
wr.write(ollamaObjectString.getBytes(StandardCharsets.UTF_8));
|
||||||
wr.flush();
|
wr.flush();
|
||||||
}
|
}
|
||||||
@@ -627,21 +629,21 @@ public class Core {
|
|||||||
// Read response content
|
// Read response content
|
||||||
// connection.getInputStream() purpose is to obtain an input stream for reading the server's response.
|
// connection.getInputStream() purpose is to obtain an input stream for reading the server's response.
|
||||||
try (
|
try (
|
||||||
BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream()))) {
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
response.append(line); // Adds every line to response till the end of file.
|
response.append(line); // Adds every line to response till the end of file.
|
||||||
}
|
}
|
||||||
}catch (Exception ex)
|
} catch (Exception ex) {
|
||||||
{
|
|
||||||
// If the server returns an error, we read the error stream instead
|
// If the server returns an error, we read the error stream instead
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
|
InputStream errorStream = connection.getErrorStream();
|
||||||
|
if (errorStream != null) {
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream))) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
response.append(line);
|
response.append(line);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
}
|
||||||
System.err.println("Error reading error stream: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -650,16 +652,15 @@ public class Core {
|
|||||||
|
|
||||||
connection.disconnect();
|
connection.disconnect();
|
||||||
return new JSONObject(response.toString());
|
return new JSONObject(response.toString());
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
connection.disconnect();
|
connection.disconnect();
|
||||||
System.err.println("Error: HTTP Response code - " + responseCode + "\n"+response.toString());
|
printMessageHandler.printErrorMessage(new OllamaMessage(OllamaMessageRole.SYSTEM,"Error: HTTP Response code - " + responseCode + "\n" + response.toString()));
|
||||||
throw new RuntimeException("HTTP Response code - " + responseCode);
|
throw new RuntimeException("HTTP Response code - " + responseCode);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// System.err.println("Error: JSON: "+);
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -693,7 +694,7 @@ public class Core {
|
|||||||
}
|
}
|
||||||
|
|
||||||
checkIfResponceMessage(response);
|
checkIfResponceMessage(response);
|
||||||
handleResponse(qurryOllama());
|
qurryOllama().thenAccept(this::handleResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ public class Display {
|
|||||||
writeLog("User: " + message);
|
writeLog("User: " + message);
|
||||||
core.getOllamaObject().addMessage(new OllamaMessage(OllamaMessageRole.USER, message.toString()));
|
core.getOllamaObject().addMessage(new OllamaMessage(OllamaMessageRole.USER, message.toString()));
|
||||||
//System.out.println(ollamaObject.toString());
|
//System.out.println(ollamaObject.toString());
|
||||||
core.handleResponse(core.qurryOllama());
|
core.qurryOllama().thenAccept(core::handleResponse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user