Commit a3ce5aab by Jonathan Thomas

- Adding random error messages if an API failure happens, directing users to our discord.

- Load API url, key, model, and other settings from config file (creaturechat.json)
- Expanded our Config class to include other API settings, such as context length and output length.
- Include 20 generated funny error messages
parent 4dab7efe
Pipeline #11984 passed with stage
in 22 seconds
...@@ -205,7 +205,7 @@ public class ChatDataManager { ...@@ -205,7 +205,7 @@ public class ChatDataManager {
// fetch HTTP response from ChatGPT // fetch HTTP response from ChatGPT
ChatGPTRequest.fetchMessageFromChatGPT(systemPrompt, contextData, previousMessages, false).thenAccept(output_message -> { ChatGPTRequest.fetchMessageFromChatGPT(systemPrompt, contextData, previousMessages, false).thenAccept(output_message -> {
if (output_message != null && systemPrompt == "system-character") { if (output_message != null && systemPrompt == "system-character") {
// Remove system-character message from previous messages // Character Sheet: Remove system-character message from previous messages
previousMessages.clear(); previousMessages.clear();
// Add NEW CHARACTER sheet & greeting // Add NEW CHARACTER sheet & greeting
...@@ -214,7 +214,7 @@ public class ChatDataManager { ...@@ -214,7 +214,7 @@ public class ChatDataManager {
this.addMessage(shortGreeting.replace("\n", " "), ChatSender.ASSISTANT); this.addMessage(shortGreeting.replace("\n", " "), ChatSender.ASSISTANT);
} else if (output_message != null && systemPrompt == "system-chat") { } else if (output_message != null && systemPrompt == "system-chat") {
// Parse message for behaviors // Chat Message: Parse message for behaviors
ParsedMessage result = MessageParser.parseMessage(output_message.replace("\n", " ")); ParsedMessage result = MessageParser.parseMessage(output_message.replace("\n", " "));
// Apply behaviors (if any) // Apply behaviors (if any)
...@@ -278,10 +278,15 @@ public class ChatDataManager { ...@@ -278,10 +278,15 @@ public class ChatDataManager {
// Get cleaned message (i.e. no <BEHAVIOR> strings) // Get cleaned message (i.e. no <BEHAVIOR> strings)
String cleanedMessage = result.getCleanedMessage(); String cleanedMessage = result.getCleanedMessage();
if (cleanedMessage.isEmpty()) { if (cleanedMessage.isEmpty()) {
cleanedMessage = result.getRandomNoResponseMessage(); cleanedMessage = ParsedMessage.getRandomNoResponseMessage();
} }
// Update the current message to a 'cleaned version' // Update the current message to a 'cleaned version'
this.currentMessage = cleanedMessage; this.currentMessage = cleanedMessage;
} else {
// Error / No Chat Message (Failure)
String randomErrorMessage = ParsedMessage.getRandomErrorMessage();
this.addMessage(randomErrorMessage, ChatSender.ASSISTANT);
} }
// Broadcast to all players // Broadcast to all players
......
...@@ -2,6 +2,7 @@ package com.owlmaddie.chat; ...@@ -2,6 +2,7 @@ package com.owlmaddie.chat;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.owlmaddie.ModInit; import com.owlmaddie.ModInit;
import com.owlmaddie.commands.ConfigurationHandler;
import com.owlmaddie.json.ChatGPTResponse; import com.owlmaddie.json.ChatGPTResponse;
import net.minecraft.resource.ResourceManager; import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
...@@ -26,14 +27,6 @@ import java.util.regex.Pattern; ...@@ -26,14 +27,6 @@ import java.util.regex.Pattern;
public class ChatGPTRequest { public class ChatGPTRequest {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt"); public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
// Init API & LLM details
private static final String apiUrl = "https://api.openai.com/v1/chat/completions";
private static final String apiKey = "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";
private static final String modelName = "gpt-3.5-turbo";
private static final int maxContextTokens = 16385;
private static final int maxOutputTokens = 200;
private static final double percentOfContext = 0.75;
static class ChatGPTRequestMessage { static class ChatGPTRequestMessage {
String role; String role;
String content; String content;
...@@ -105,6 +98,17 @@ public class ChatGPTRequest { ...@@ -105,6 +98,17 @@ public class ChatGPTRequest {
} }
public static CompletableFuture<String> fetchMessageFromChatGPT(String systemPrompt, Map<String, String> context, List<ChatDataManager.ChatMessage> messageHistory, Boolean jsonMode) { public static CompletableFuture<String> fetchMessageFromChatGPT(String systemPrompt, Map<String, String> context, List<ChatDataManager.ChatMessage> messageHistory, Boolean jsonMode) {
// Get config (api key, url, settings)
ConfigurationHandler.Config config = new ConfigurationHandler(ModInit.serverInstance).loadConfig();
// Init API & LLM details
String apiUrl = config.getUrl();
String apiKey = config.getApiKey();
String modelName = config.getModel();
int maxContextTokens = config.getMaxContextTokens();
int maxOutputTokens = config.getMaxOutputTokens();
double percentOfContext = config.getPercentOfContext();
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
String systemMessage = ""; String systemMessage = "";
......
...@@ -15,7 +15,7 @@ import java.nio.file.Paths; ...@@ -15,7 +15,7 @@ import java.nio.file.Paths;
/** /**
* The {@code ConfigurationHandler} class loads and saves configuration settings for this mod. It first * The {@code ConfigurationHandler} class loads and saves configuration settings for this mod. It first
* checks for a config file in the world save folder, and if not found, falls back to the root folder. * checks for a config file in the world save folder, and if not found, falls back to the root folder.
* This allows for global/default settings, or server-specific settings. * This allows for global/default settings, or optional server-specific settings.
*/ */
public class ConfigurationHandler { public class ConfigurationHandler {
...@@ -54,16 +54,32 @@ public class ConfigurationHandler { ...@@ -54,16 +54,32 @@ public class ConfigurationHandler {
} }
public static class Config { public static class Config {
private String apiKey; private String apiKey = "";
private String url; private String url = "https://api.openai.com/v1/chat/completions";
private String model; private String model = "gpt-3.5-turbo";
private int maxContextTokens = 16385;
private int maxOutputTokens = 200;
private double percentOfContext = 0.75;
// getters and setters // Getters and setters for existing fields
public String getApiKey() { return apiKey; } public String getApiKey() { return apiKey; }
public void setApiKey(String apiKey) { this.apiKey = apiKey; } public void setApiKey(String apiKey) { this.apiKey = apiKey; }
public String getUrl() { return url; } public String getUrl() { return url; }
public void setUrl(String url) { this.url = url; } public void setUrl(String url) { this.url = url; }
public String getModel() { return model; } public String getModel() { return model; }
public void setModel(String model) { this.model = model; } public void setModel(String model) { this.model = model; }
// Getters and setters for new fields
public int getMaxContextTokens() { return maxContextTokens; }
public void setMaxContextTokens(int maxContextTokens) { this.maxContextTokens = maxContextTokens; }
public int getMaxOutputTokens() { return maxOutputTokens; }
public void setMaxOutputTokens(int maxOutputTokens) { this.maxOutputTokens = maxOutputTokens; }
public double getPercentOfContext() { return percentOfContext; }
public void setPercentOfContext(double percentOfContext) { this.percentOfContext = percentOfContext; }
} }
} }
...@@ -11,7 +11,7 @@ public class ParsedMessage { ...@@ -11,7 +11,7 @@ public class ParsedMessage {
private String cleanedMessage; private String cleanedMessage;
private String originalMessage; private String originalMessage;
private List<Behavior> behaviors; private List<Behavior> behaviors;
private List<String> noResponseMessages = Arrays.asList( private static List<String> noResponseMessages = Arrays.asList(
"<no response>", "<no response>",
"<silence>", "<silence>",
"<stares>", "<stares>",
...@@ -41,6 +41,29 @@ public class ParsedMessage { ...@@ -41,6 +41,29 @@ public class ParsedMessage {
"<counts imaginary stars>", "<counts imaginary stars>",
"<plays with a nonexistent pet>" "<plays with a nonexistent pet>"
); );
private static List<String> errorResponseMessages = Arrays.asList(
"Seems like my words got lost in the End. Check out https://discord.gg/m9dvPFmN3e for clues!",
"Oops! My speech bubble popped. Need help? Visit https://discord.gg/m9dvPFmN3e.",
"I might've eaten a bad Command Block. Help me out at https://discord.gg/m9dvPFmN3e!",
"My words are on strike. More info? https://discord.gg/m9dvPFmN3e.",
"I think a Creeper blew up my script. Instructions? https://discord.gg/m9dvPFmN3e.",
"BRB, asking a villager for directions to https://discord.gg/m9dvPFmN3e.",
"It’s not you, it’s my API key. Let's regroup at https://discord.gg/m9dvPFmN3e.",
"I tried to speak, but it was a critical miss. Help at https://discord.gg/m9dvPFmN3e.",
"Words are hard. Come chat at https://discord.gg/m9dvPFmN3e.",
"I must've left my responses in my other pants. See https://discord.gg/m9dvPFmN3e.",
"Shh... I’m hiding from an invalid API key. Join the hunt at https://discord.gg/m9dvPFmN3e.",
"I’d tell you, but then I’d have to respawn. Meet me at https://discord.gg/m9dvPFmN3e.",
"Error 404: Response not found. Maybe it’s at https://discord.gg/m9dvPFmN3e?",
"I'm speechless, literally. Let's troubleshoot at https://discord.gg/m9dvPFmN3e.",
"Looks like my connection got lost in the Nether. Can you help? https://discord.gg/m9dvPFmN3e.",
"I forgot what I was saying, but https://discord.gg/m9dvPFmN3e remembers.",
"Are my words mining without a pickaxe? Dig up some help at https://discord.gg/m9dvPFmN3e.",
"Sorry, my parrot ate the response. Teach it better at https://discord.gg/m9dvPFmN3e.",
"My magic mirror says: 'Better answers found at https://discord.gg/m9dvPFmN3e.'",
"This message is temporarily out of order. Order yours at https://discord.gg/m9dvPFmN3e."
);
public ParsedMessage(String cleanedMessage, String originalMessage, List<Behavior> behaviors) { public ParsedMessage(String cleanedMessage, String originalMessage, List<Behavior> behaviors) {
this.cleanedMessage = cleanedMessage; this.cleanedMessage = cleanedMessage;
...@@ -54,12 +77,19 @@ public class ParsedMessage { ...@@ -54,12 +77,19 @@ public class ParsedMessage {
} }
// Get random no response message // Get random no response message
public String getRandomNoResponseMessage() { public static String getRandomNoResponseMessage() {
Random random = new Random(); Random random = new Random();
int index = random.nextInt(noResponseMessages.size()); int index = random.nextInt(noResponseMessages.size());
return noResponseMessages.get(index).trim(); return noResponseMessages.get(index).trim();
} }
// Get random error message
public static String getRandomErrorMessage() {
Random random = new Random();
int index = random.nextInt(errorResponseMessages.size());
return errorResponseMessages.get(index).trim();
}
// Get original message // Get original message
public String getOriginalMessage() { public String getOriginalMessage() {
return originalMessage.trim(); return originalMessage.trim();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment