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; 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.command.ChatCommand;
import it.ohalee.minecraftgpt.handler.PlayerHandlers; import it.ohalee.minecraftgpt.handler.PlayerHandlers;
import it.ohalee.minecraftgpt.util.Messages; 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.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.CompletionRequest;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import retrofit2.HttpException;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -18,8 +19,7 @@ public class OpenAI {
public static CompletableFuture<String> getResponse(ConfigurationSection section, StringBuilder cached, String message) { public static CompletableFuture<String> getResponse(ConfigurationSection section, StringBuilder cached, String message) {
cached.append("\nHuman:").append(message).append("\nAI:"); cached.append("\nHuman:").append(message).append("\nAI:");
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> service.createCompletion(CompletionRequest.builder()
CompletionRequest request = CompletionRequest.builder()
.prompt(cached.toString()) .prompt(cached.toString())
.model(section.getString("model")) .model(section.getString("model"))
.temperature(section.getDouble("temperature")) .temperature(section.getDouble("temperature"))
@ -28,8 +28,17 @@ public class OpenAI {
.frequencyPenalty(section.getDouble("frequency-penalty")) .frequencyPenalty(section.getDouble("frequency-penalty"))
.presencePenalty(section.getDouble("presence-penalty")) .presencePenalty(section.getDouble("presence-penalty"))
.stop(Arrays.asList("Human:", "AI:")) .stop(Arrays.asList("Human:", "AI:"))
.build(); .build())
return service.createCompletion(request).getChoices().get(0).getText(); .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

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