v1.3.0 - Fixes with new endpoint

This commit is contained in:
ohAlee 2024-02-11 10:49:59 +01:00
parent 7df2a07c62
commit f8eeb5456b
6 changed files with 23 additions and 12 deletions

View File

@ -4,7 +4,7 @@ plugins {
}
group = 'it.ohalee.minecraftgpt'
version = '1.2.8'
version = '1.3.0'
sourceCompatibility = 1.17
targetCompatibility = 1.17

View File

@ -4,6 +4,7 @@ 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 com.theokanning.openai.completion.chat.ChatMessage;
import it.ohalee.minecraftgpt.command.ChatCommand;
import it.ohalee.minecraftgpt.handler.ChatHandler;
import it.ohalee.minecraftgpt.handler.PlayerHandler;
@ -16,11 +17,12 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Main extends JavaPlugin {
public static Cache<Player, StringBuilder> CACHE;
public static Cache<Player, List<ChatMessage>> CACHE;
public static Cache<Player, Type> USER_TYPE = CacheBuilder.newBuilder().build();
@Override
@ -35,7 +37,7 @@ public class Main extends JavaPlugin {
CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(30, TimeUnit.MINUTES)
.removalListener((RemovalListener<Player, StringBuilder>) notification -> {
.removalListener((RemovalListener<Player, List<ChatMessage>>) notification -> {
if (notification.getKey() == null) return;
USER_TYPE.invalidate(notification.getKey());
if (notification.getCause() == RemovalCause.EXPIRED) {

View File

@ -1,13 +1,14 @@
package it.ohalee.minecraftgpt;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.service.OpenAiService;
import org.bukkit.configuration.ConfigurationSection;
import retrofit2.HttpException;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class OpenAI {
@ -18,8 +19,8 @@ public class OpenAI {
return CompletableFuture.runAsync(() -> service = new OpenAiService(key, Duration.ofSeconds(5)));
}
public static CompletableFuture<String> getResponse(ConfigurationSection section, StringBuilder cached, String message) {
cached.append("\nHuman:").append(message).append("\nAI:");
public static CompletableFuture<String> getResponse(ConfigurationSection section, List<ChatMessage> chatMessages, String message) {
chatMessages.add(new ChatMessage("Human", message));
return CompletableFuture.supplyAsync(() -> {
String model = section.getString("model", "text-davinci-003");
@ -29,7 +30,8 @@ public class OpenAI {
double topP = section.getDouble("top-p");
double temperature = section.getDouble("temperature");
return service.createChatCompletion(ChatCompletionRequest.builder()
String reply = service.createChatCompletion(ChatCompletionRequest.builder()
.messages(chatMessages)
.model(model)
.temperature(temperature)
.maxTokens(maxTokens)
@ -39,6 +41,9 @@ public class OpenAI {
.stop(Arrays.asList("Human:", "AI:"))
.build())
.getChoices().get(0).getMessage().getContent();
chatMessages.add(new ChatMessage("AI", reply));
return reply;
}).exceptionally(throwable -> {
if (throwable.getCause() instanceof HttpException e) {
String reason = switch (e.response().code()) {

View File

@ -5,6 +5,8 @@ import it.ohalee.minecraftgpt.Type;
import it.ohalee.minecraftgpt.util.Messages;
import org.bukkit.entity.Player;
import java.util.ArrayList;
public class TypeManager {
public static void startConversation(Main plugin, Player player, Type type) {
@ -15,7 +17,7 @@ public class TypeManager {
}
Main.USER_TYPE.put(player, type);
Main.CACHE.put(player, new StringBuilder());
Main.CACHE.put(player, new ArrayList<>());
player.sendMessage(Messages.format(plugin.getConfig().getString("command.toggle.enabled")));
}

View File

@ -1,5 +1,6 @@
package it.ohalee.minecraftgpt.handler;
import com.theokanning.openai.completion.chat.ChatMessage;
import it.ohalee.minecraftgpt.Main;
import it.ohalee.minecraftgpt.OpenAI;
import it.ohalee.minecraftgpt.Type;
@ -11,6 +12,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@ -42,10 +44,10 @@ public class ChatHandler implements Listener {
sendMessage(format(list.get(0), e.getMessage(), player.getName()), recipients);
}
StringBuilder builder = Main.CACHE.getIfPresent(player);
if (builder == null) builder = new StringBuilder();
List<ChatMessage> messages = Main.CACHE.getIfPresent(player);
if (messages == null) messages = new ArrayList<>();
OpenAI.getResponse(plugin.getConfig().getConfigurationSection("chatgpt"), builder, e.getMessage()).whenComplete((response, throwable) -> {
OpenAI.getResponse(plugin.getConfig().getConfigurationSection("chatgpt"), messages, e.getMessage()).whenComplete((response, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
player.sendMessage(Messages.format(plugin.getConfig().getString("command.error")));

View File

@ -1,6 +1,6 @@
name: MinecraftGPT
main: it.ohalee.minecraftgpt.Main
version: 1.2.7
version: 1.3.0
author: ohAlee
description: A Minecraft plugin that uses ChatGPT
api-version: 1.16