Error message based on http error code.

This commit is contained in:
ohAlee 2023-03-15 16:31:07 +01:00
parent fc05e5f1fe
commit 4388494ab6
4 changed files with 27 additions and 18 deletions

View File

@ -1,6 +1,9 @@
package it.ohalee.minecraftgpt;
import com.google.common.cache.*;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalCause;
import com.google.common.cache.RemovalListener;
import it.ohalee.minecraftgpt.command.ChatCommand;
import it.ohalee.minecraftgpt.handler.PlayerHandlers;
import it.ohalee.minecraftgpt.util.Messages;

View File

@ -3,6 +3,7 @@ package it.ohalee.minecraftgpt;
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import org.bukkit.configuration.ConfigurationSection;
import retrofit2.HttpException;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
@ -18,18 +19,26 @@ public class OpenAI {
public static CompletableFuture<String> getResponse(ConfigurationSection section, StringBuilder cached, String message) {
cached.append("\nHuman:").append(message).append("\nAI:");
return CompletableFuture.supplyAsync(() -> {
CompletionRequest request = CompletionRequest.builder()
.prompt(cached.toString())
.model(section.getString("model"))
.temperature(section.getDouble("temperature"))
.maxTokens(section.getInt("max-tokens"))
.topP(section.getDouble("top-p"))
.frequencyPenalty(section.getDouble("frequency-penalty"))
.presencePenalty(section.getDouble("presence-penalty"))
.stop(Arrays.asList("Human:", "AI:"))
.build();
return service.createCompletion(request).getChoices().get(0).getText();
return CompletableFuture.supplyAsync(() -> service.createCompletion(CompletionRequest.builder()
.prompt(cached.toString())
.model(section.getString("model"))
.temperature(section.getDouble("temperature"))
.maxTokens(section.getInt("max-tokens"))
.topP(section.getDouble("top-p"))
.frequencyPenalty(section.getDouble("frequency-penalty"))
.presencePenalty(section.getDouble("presence-penalty"))
.stop(Arrays.asList("Human:", "AI:"))
.build())
.getChoices().get(0).getText()).exceptionally(throwable -> {
if (throwable.getCause() instanceof HttpException e) {
return switch (e.response().code()) {
case 401 -> "Invalid API key! Please check your configuration.";
case 429 -> "Too many requests! Please wait a few seconds and try again.";
case 500 -> "OpenAI service is currently unavailable. Please try again later.";
default -> "Unknown error! Please try again later. If this error persists, contact the plugin developer.";
};
}
throw new RuntimeException(throwable);
});
}

View File

@ -28,7 +28,7 @@ public class ChatCommand implements TabExecutor {
sender.sendMessage(ChatColor.GREEN + "Config reloaded!");
return true;
}
if (!(sender instanceof Player player)) {
sender.sendMessage(ChatColor.RED + "Only players can use this command!");
return true;

View File

@ -11,8 +11,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
@ -54,8 +52,7 @@ public class PlayerHandlers implements Listener {
if (builder == null) builder = new StringBuilder();
OpenAI.getResponse(plugin.getConfig().getConfigurationSection("chatgpt"), builder, e.getMessage()).whenComplete((response, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
if (response == null) {
player.sendMessage(Messages.format(plugin.getConfig().getString("command.error")));
return;
}