Skip to content

Commit

Permalink
Merge pull request #12 from DasCanard/master
Browse files Browse the repository at this point in the history
Run request to openai asynchronous
  • Loading branch information
RappyTV authored Aug 28, 2023
2 parents 3a92233 + 95d9d70 commit 33b9a95
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ labyMod {
"1.18.2",
"1.19.2",
"1.19.3",
"1.19.4"
"1.19.4",
"1.20.1"
) { version, provider ->
configureRun(provider, version)
}
Expand Down
59 changes: 33 additions & 26 deletions core/src/main/java/com/rappytv/labygpt/api/GPTRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
import com.google.gson.Gson;
import com.rappytv.labygpt.GPTAddon;
import net.labymod.api.util.I18n;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.concurrent.CompletableFuture;

public class GPTRequest {

private boolean successful;
private String output;
private String error;

public GPTRequest(String query, String key, String username, String model, String behavior) {
public CompletableFuture<Void> sendRequestAsync(String query, String key, String username,
String model, String behavior) {
Gson gson = new Gson();
CompletableFuture<Void> future = new CompletableFuture<>();

try {
if(GPTAddon.queryHistory.isEmpty())
if (GPTAddon.queryHistory.isEmpty()) {
GPTAddon.queryHistory.add(new GPTMessage(behavior, GPTRole.System, "System"));
}
GPTAddon.queryHistory.add(new GPTMessage(query, GPTRole.User, username));
RequestBody apiRequestBody = new RequestBody(model, GPTAddon.queryHistory, username);

Expand All @@ -35,39 +36,45 @@ public GPTRequest(String query, String key, String username, String model, Strin
.build();

HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());

ResponseBody responseBody = gson.fromJson(response.body(), ResponseBody.class);
client.sendAsync(request, BodyHandlers.ofString())
.thenAccept(response -> {
ResponseBody responseBody = gson.fromJson(response.body(), ResponseBody.class);

if(responseBody.error != null) {
error = responseBody.error.message;
if(error.isEmpty() && responseBody.error.code.equals("invalid_api_key"))
error = I18n.translate("labygpt.messages.invalidBearer");
successful = false;
return;
}
if(responseBody.choices.size() < 1) {
successful = false;
return;
}
if (responseBody.error != null) {
error = responseBody.error.message;
if (error.isEmpty() && responseBody.error.code.equals("invalid_api_key")) {
error = I18n.translate("labygpt.messages.invalidBearer");
}
successful = false;
} else if (responseBody.choices.isEmpty()) {
successful = false;
} else {
GPTMessage message = responseBody.choices.get(0).message;
output = message.content.replace("\n\n", "");
GPTAddon.queryHistory.add(
new GPTMessage(message.content, GPTRole.Assistant, "LabyGPT"));
successful = true;
}

GPTMessage message = responseBody.choices.get(0).message;
output = message.content.replace("\n\n", "");
GPTAddon.queryHistory.add(new GPTMessage(message.content, GPTRole.Assistant, "LabyGPT"));
successful = true;
} catch (IOException | InterruptedException | URISyntaxException e) {
future.complete(null);
});
} catch (Exception e) {
e.printStackTrace();
successful = false;
future.completeExceptionally(e);
}

return future;
}

public boolean isSuccessful() {
return successful;
}

public String getOutput() {
return output;
}

public String getError() {
return error;
}
}
}
39 changes: 24 additions & 15 deletions core/src/main/java/com/rappytv/labygpt/commands/GPTCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,41 @@ public GPTCommand(GPTAddon addon) {

@Override
public boolean execute(String prefix, String[] arguments) {
if(addon.configuration().openAI().bearer().isEmpty()) {
displayMessage(Component.empty().append(GPTAddon.prefix).append(Component.translatable("labygpt.messages.noKey", NamedTextColor.RED)));
if (addon.configuration().openAI().bearer().isEmpty()) {
displayMessage(Component.empty().append(GPTAddon.prefix)
.append(Component.translatable("labygpt.messages.noKey", NamedTextColor.RED)));
return true;
}
if(arguments.length < 1) {
displayMessage(Component.empty().append(GPTAddon.prefix).append(Component.translatable("labygpt.messages.noQuery", NamedTextColor.RED)));
if (arguments.length < 1) {
displayMessage(Component.empty().append(GPTAddon.prefix)
.append(Component.translatable("labygpt.messages.noQuery", NamedTextColor.RED)));
return true;
}

if(!addon.configuration().saveHistory())
if (!addon.configuration().saveHistory()) {
GPTAddon.queryHistory.clear();
GPTRequest request = new GPTRequest(
}
GPTRequest request = new GPTRequest();
request.sendRequestAsync(
String.join(" ", arguments),
addon.configuration().openAI().bearer(),
addon.configuration().openAI().shareUsername() ? labyAPI.getName() : "",
addon.configuration().gpt().model().get().toLowerCase(),
addon.configuration().gpt().behavior().get()
);

if(!request.isSuccessful() || request.getOutput() == null) {
GPTAddon.queryHistory.remove(GPTAddon.queryHistory.size() - 1);
displayMessage(Component.empty().append(GPTAddon.prefix).append(Component.text(Objects.requireNonNullElseGet(request.getError(), () -> I18n.translate("labygpt.messages.requestError")), NamedTextColor.RED)));
return true;
}
).thenRun(() -> {
// Callback logic when the request is done:
if (!request.isSuccessful() || request.getOutput() == null) {
GPTAddon.queryHistory.remove(GPTAddon.queryHistory.size() - 1);
displayMessage(Component.empty().append(GPTAddon.prefix).append(Component.text(
Objects.requireNonNullElseGet(request.getError(),
() -> I18n.translate("labygpt.messages.requestError")),
NamedTextColor.RED)));
} else {
displayMessage(Component.empty().append(GPTAddon.prefix)
.append(Component.text(request.getOutput(), NamedTextColor.WHITE)));
}
});

displayMessage(Component.empty().append(GPTAddon.prefix).append(Component.text(request.getOutput(), NamedTextColor.WHITE)));
return true;
}
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
rootProject.name = "LabyGPT"

pluginManagement {
val labyGradlePluginVersion = "0.3.25"
val labyGradlePluginVersion = "0.3.28"
plugins {
id("net.labymod.gradle") version (labyGradlePluginVersion)
}
Expand Down

0 comments on commit 33b9a95

Please sign in to comment.