- 添加角色功能
- 移除prompt功能
This commit is contained in:
ddaodan 2024-08-27 17:24:14 +08:00
parent 6901130708
commit ea0466947c
9 changed files with 115 additions and 35 deletions

View File

@ -4,7 +4,7 @@ plugins {
} }
group = 'com' group = 'com'
version = '2.5' version = '2.6'
repositories { repositories {
mavenCentral() mavenCentral()

View File

@ -1,4 +1,7 @@
# 更新日志 # 更新日志
## 2.6
- 添加角色功能
- 移除prompt功能
## 2.5 ## 2.5
- 允许插件在Folia加载 - 允许插件在Folia加载
- 修复 #4[BUG]部分情况下回复乱码 - 修复 #4[BUG]部分情况下回复乱码

View File

@ -1,6 +1,6 @@
# MineChatGPT # MineChatGPT
![minechatgpt](https://socialify.git.ci/ddaodan/minechatgpt/image?description=1&descriptionEditable=%E5%9C%A8Minecraft%E4%B8%AD%E4%B8%8EChatGPT%E4%BA%A4%E6%B5%81&font=Inter&issues=1&language=1&name=1&pattern=Solid&stargazers=1&theme=Auto) ![minechatgpt](https://socialify.git.ci/ddaodan/minechatgpt/image?description=1&descriptionEditable=%E5%9C%A8Minecraft%E4%B8%AD%E4%B8%8EChatGPT%E4%BA%A4%E6%B5%81&font=Inter&issues=1&language=1&name=1&pattern=Solid&stargazers=1&theme=Auto)
![modrinth-gallery](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy-minimal/documentation/modrinth-gallery_vector.svg) ![spigot](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy-minimal/supported/spigot_vector.svg) ![paper](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy-minimal/supported/paper_vector.svg) ![java8](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy-minimal/built-with/java8_vector.svg) ![modrinth-gallery](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy-minimal/documentation/modrinth-gallery_vector.svg) ![spigot](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy-minimal/supported/spigot_vector.svg) ![paper](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy-minimal/supported/paper_vector.svg) ![java8](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy-minimal/built-with/java8_vector.svg)
![GitHub Release](https://img.shields.io/github/v/release/ddaodan/minechatgpt?label=version) ![bStats Servers](https://img.shields.io/bstats/servers/22635) ![bStats Players](https://img.shields.io/bstats/players/22635) ![Modrinth Downloads](https://img.shields.io/modrinth/dt/Op2X2eDG?logo=modrinth) ![Spiget Downloads](https://img.shields.io/spiget/downloads/118963?logo=spigotmc) ![GitHub Release](https://img.shields.io/github/v/release/ddaodan/minechatgpt?label=version) ![bStats Servers](https://img.shields.io/bstats/servers/22635) ![bStats Players](https://img.shields.io/bstats/players/22635) ![Modrinth Downloads](https://img.shields.io/modrinth/dt/Op2X2eDG?logo=modrinth) ![Spiget Downloads](https://img.shields.io/spiget/downloads/118963?logo=spigotmc)
在Minecraft中与ChatGPT交流 在Minecraft中与ChatGPT交流

View File

@ -100,6 +100,27 @@ public class CommandHandler implements CommandExecutor {
conversationContext.clearHistory(); conversationContext.clearHistory();
sender.sendMessage(configManager.getClearMessage()); sender.sendMessage(configManager.getClearMessage());
return true; return true;
} else if (subCommand.equalsIgnoreCase("character")) {
if (!sender.hasPermission("minechatgpt.character")) {
sender.sendMessage(configManager.getNoPermissionMessage().replace("%s", "minechatgpt.character"));
return true;
}
Map<String, String> characters = configManager.getCharacters();
if (args.length < 2) {
sender.sendMessage(configManager.getAvailableCharactersMessage());
for (String character : characters.keySet()) {
sender.sendMessage("- " + character);
}
return true;
}
String character = args[1];
if (characters.containsKey(character)) {
configManager.setCurrentCharacter(userId, character);
sender.sendMessage(configManager.getCharacterSwitchedMessage().replace("%s", character));
} else {
sender.sendMessage(configManager.getInvalidCharacterMessage());
}
return true;
} else { } else {
if (!sender.hasPermission("minechatgpt.use")) { if (!sender.hasPermission("minechatgpt.use")) {
sender.sendMessage(configManager.getNoPermissionMessage().replace("%s", "minechatgpt.use")); sender.sendMessage(configManager.getNoPermissionMessage().replace("%s", "minechatgpt.use"));
@ -110,21 +131,22 @@ public class CommandHandler implements CommandExecutor {
conversationContext.addMessage(question); conversationContext.addMessage(question);
} }
sender.sendMessage(configManager.getQuestionMessage().replace("%s", question)); sender.sendMessage(configManager.getQuestionMessage().replace("%s", question));
askChatGPT(sender, question, conversationContext, contextEnabled); askChatGPT(sender, question, conversationContext, contextEnabled, userId);
return true; return true;
} }
} }
return false; return false;
} }
private void askChatGPT(CommandSender sender, String question, ConversationContext conversationContext, boolean contextEnabled) { private void askChatGPT(CommandSender sender, String question, ConversationContext conversationContext, boolean contextEnabled, String userId) {
String utf8Question = convertToUTF8(question); String utf8Question = convertToUTF8(question);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("model", configManager.getDefaultModel()); json.put("model", configManager.getDefaultModel());
JSONArray messages = new JSONArray(); JSONArray messages = new JSONArray();
// 添加自定义 prompt // 添加自定义 prompt
String customPrompt = configManager.getCustomPrompt(); String currentCharacter = configManager.getCurrentCharacter(userId);
if (!customPrompt.isEmpty()) { String customPrompt = configManager.getCharacters().get(currentCharacter);
if (customPrompt != null && !customPrompt.isEmpty()) {
JSONObject promptMessage = new JSONObject(); JSONObject promptMessage = new JSONObject();
promptMessage.put("role", "system"); promptMessage.put("role", "system");
promptMessage.put("content", customPrompt); promptMessage.put("content", customPrompt);
@ -169,7 +191,7 @@ public class CommandHandler implements CommandExecutor {
String utf8ResponseBody = new String(responseBody.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); String utf8ResponseBody = new String(responseBody.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
JSONObject jsonResponse = new JSONObject(utf8ResponseBody); JSONObject jsonResponse = new JSONObject(utf8ResponseBody);
String answer = jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content"); String answer = jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
sender.sendMessage(configManager.getChatGPTResponseMessage().replace("%s", answer)); sender.sendMessage(configManager.getChatGPTResponseMessage().replaceFirst("%s", currentCharacter).replaceFirst("%s", answer));
if (contextEnabled) { if (contextEnabled) {
conversationContext.addMessage(answer); // 仅在启用上下文时添加AI响应到历史记录 conversationContext.addMessage(answer); // 仅在启用上下文时添加AI响应到历史记录
} }
@ -204,5 +226,6 @@ public class CommandHandler implements CommandExecutor {
sender.sendMessage(configManager.getHelpModelListMessage()); sender.sendMessage(configManager.getHelpModelListMessage());
sender.sendMessage(configManager.getHelpContextMessage()); sender.sendMessage(configManager.getHelpContextMessage());
sender.sendMessage(configManager.getHelpClearMessage()); sender.sendMessage(configManager.getHelpClearMessage());
sender.sendMessage(configManager.getHelpCharacterMessage());
} }
} }

View File

@ -2,7 +2,10 @@ package com.ddaodan.MineChatGPT;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class ConfigManager { public class ConfigManager {
private final Main plugin; private final Main plugin;
@ -46,9 +49,6 @@ public class ConfigManager {
public List<String> getModels() { public List<String> getModels() {
return config.getStringList("models"); return config.getStringList("models");
} }
public String getCustomPrompt() {
return config.getString("prompt", "You are a helpful assistant.");
}
public String getHelpMessage() { public String getHelpMessage() {
return translateColorCodes(config.getString("messages.help")); return translateColorCodes(config.getString("messages.help"));
} }
@ -65,10 +65,13 @@ public class ConfigManager {
return translateColorCodes(config.getString("messages.help_modellist")); return translateColorCodes(config.getString("messages.help_modellist"));
} }
public String getHelpContextMessage() { public String getHelpContextMessage() {
return translateColorCodes(config.getString("messages.help_context", "/chatgpt context - Toggle context mode.")); return translateColorCodes(config.getString("messages.help_context", "&e/chatgpt context - Toggle context mode."));
} }
public String getHelpClearMessage() { public String getHelpClearMessage() {
return translateColorCodes(config.getString("messages.help_clear", "/chatgpt clear - Clear conversation history.")); return translateColorCodes(config.getString("messages.help_clear", "&e/chatgpt clear - Clear conversation history."));
}
public String getHelpCharacterMessage() {
return translateColorCodes(config.getString("messages.help_character", "&e/chatgpt character [character_name] - List or switch to a character."));
} }
public String getModelSwitchMessage() { public String getModelSwitchMessage() {
return translateColorCodes(config.getString("messages.model_switch")); return translateColorCodes(config.getString("messages.model_switch"));
@ -77,7 +80,7 @@ public class ConfigManager {
return translateColorCodes(config.getString("messages.chatgpt_error")); return translateColorCodes(config.getString("messages.chatgpt_error"));
} }
public String getChatGPTResponseMessage() { public String getChatGPTResponseMessage() {
return translateColorCodes(config.getString("messages.chatgpt_response")); return translateColorCodes(config.getString("messages.chatgpt_response", "&b%s: %s"));
} }
public String getQuestionMessage() { public String getQuestionMessage() {
return translateColorCodes(config.getString("messages.question")); return translateColorCodes(config.getString("messages.question"));
@ -101,15 +104,38 @@ public class ConfigManager {
return config.getBoolean("conversation.context_enabled", false); return config.getBoolean("conversation.context_enabled", false);
} }
public String getContextToggleMessage() { public String getContextToggleMessage() {
return translateColorCodes(config.getString("messages.context_toggle", "Context is now %s.")); return translateColorCodes(config.getString("messages.context_toggle", "&eContext is now %s."));
} }
public String getContextToggleEnabledMessage() { public String getContextToggleEnabledMessage() {
return translateColorCodes(config.getString("messages.context_toggle_enabled", "enabled")); return translateColorCodes(config.getString("messages.context_toggle_enabled", "&aenabled"));
} }
public String getContextToggleDisabledMessage() { public String getContextToggleDisabledMessage() {
return translateColorCodes(config.getString("messages.context_toggle_disabled", "disabled")); return translateColorCodes(config.getString("messages.context_toggle_disabled", "&edisabled"));
} }
public String getClearMessage() { public String getClearMessage() {
return translateColorCodes(config.getString("messages.clear", "Conversation history has been cleared.")); return translateColorCodes(config.getString("messages.clear", "&aConversation history has been cleared."));
}
public String getCharacterSwitchedMessage() {
return translateColorCodes(config.getString("messages.character_switched", "&aSwitched to character: %s"));
}
public String getAvailableCharactersMessage() {
return translateColorCodes(config.getString("messages.available_characters", "&eAvailable characters:"));
}
public String getInvalidCharacterMessage() {
return translateColorCodes(config.getString("messages.invalid_character", "&cInvalid character. Use /chatgpt character to list available characters."));
}
public Map<String, String> getCharacters() {
Map<String, String> characters = new HashMap<>();
config.getConfigurationSection("characters").getKeys(false).forEach(key -> {
characters.put(key, config.getString("characters." + key));
});
return characters;
}
public String getCurrentCharacter(String userId) {
return config.getString("users." + userId + ".character", "ChatGPT");
}
public void setCurrentCharacter(String userId, String character) {
config.set("users." + userId + ".character", character);
plugin.saveConfig();
} }
} }

View File

@ -25,9 +25,14 @@ public class MineChatGPTTabCompleter implements TabCompleter {
completions.add("modellist"); completions.add("modellist");
completions.add("context"); completions.add("context");
completions.add("clear"); completions.add("clear");
} else if (args.length == 2 && args[0].equalsIgnoreCase("model")) { completions.add("character");
// 补全模型名称 } else if (args.length == 2) {
completions.addAll(configManager.getModels()); String subCommand = args[0];
if (subCommand.equalsIgnoreCase("model")) {
completions.addAll(configManager.getModels());
} else if (subCommand.equalsIgnoreCase("character")) {
completions.addAll(configManager.getCharacters().keySet());
}
} }
// 过滤补全列表以匹配输入 // 过滤补全列表以匹配输入

View File

@ -24,30 +24,40 @@ models:
# And more... # And more...
# The default model to use # The default model to use
default_model: "gpt-3.5-turbo" default_model: "gpt-3.5-turbo"
# Conversation setting
conversation: conversation:
# Continuous conversation switch # Continuous conversation switch
context_enabled: false context_enabled: false
# Maximum number of historical records retained
max_history_size: 10 max_history_size: 10
prompt: "You are a helpful assistant." # Characters setting
characters:
# Format:
# Character Name: "Character Prompt"
ChatGPT: "You are a helpful assistant."
# Message settings # Message settings
messages: messages:
reload: "&aConfiguration reloaded successfully!" reload: "&aConfiguration reloaded successfully!"
clear: "Conversation history has been cleared!" clear: "&aConversation history has been cleared!"
help: "&e===== MineChatGPT Help =====" help: "&e===== MineChatGPT Help ====="
help_ask: "&e/chatgpt <text> - Ask ChatGPT a question." help_ask: "&e/chatgpt <text> - Ask ChatGPT a question."
help_reload: "&e/chatgpt reload - Reload the configuration file." help_reload: "&e/chatgpt reload - Reload the configuration file."
help_model: "&e/chatgpt model <model_name> - Switch to a different model." help_model: "&e/chatgpt model <model_name> - Switch to a different model."
help_modellist: "&e/chatgpt modellist - List available models." help_modellist: "&e/chatgpt modellist - List available models."
help_context: "/chatgpt context - Toggle context mode." help_context: "&e/chatgpt context - Toggle context mode."
help_clear: "/chatgpt clear - Clear conversation history." help_clear: "&e/chatgpt clear - Clear conversation history."
context_toggle: "Context is now %s." help_character: "&e/chatgpt character [character_name] - List or switch to a character."
context_toggle_enabled: "enabled" context_toggle: "&eContext is now %s."
context_toggle_disabled: "disabled" context_toggle_enabled: "&aenabled"
context_toggle_disabled: "&cdisabled"
current_model_info: "&eCurrent model: %s. Use /chatgpt model <model_name> to switch models." current_model_info: "&eCurrent model: %s. Use /chatgpt model <model_name> to switch models."
model_switch: "&aModel switched to %s" model_switch: "&aModel switched to %s"
chatgpt_error: "&cFailed to contact ChatGPT." chatgpt_error: "&cFailed to contact ChatGPT."
chatgpt_response: "&bChatGPT: %s" chatgpt_response: "&b%s: %s"
question: "&bYou: %s" question: "&bYou: %s"
character_switched: "&aSwitched to character: %s"
available_characters: "&eAvailable characters:"
invalid_character: "&cInvalid character. Use /chatgpt character to list available characters."
invalid_model: "&cInvalid model. Use /chatgpt modellist to see available models." invalid_model: "&cInvalid model. Use /chatgpt modellist to see available models."
available_models: "&eAvailable models:" available_models: "&eAvailable models:"
no_permission: "&cYou do not have permission to use this command. Required permission: %s" no_permission: "&cYou do not have permission to use this command. Required permission: %s"

View File

@ -24,11 +24,17 @@ models:
# 以及更多... # 以及更多...
# 默认使用的模型 # 默认使用的模型
default_model: "gpt-3.5-turbo" default_model: "gpt-3.5-turbo"
# 连续对话设置
conversation: conversation:
# 连续对话开关 # 连续对话开关
context_enabled: false context_enabled: false
# 最大历史记录保留数量
max_history_size: 10 max_history_size: 10
prompt: "You are a helpful assistant.use Chinese." # 角色设置
characters:
# 格式:
# 角色名称: "角色提示词"
ChatGPT: "You are a helpful assistant."
# 消息相关设置 # 消息相关设置
messages: messages:
reload: "&a已重新加载配置文件" reload: "&a已重新加载配置文件"
@ -39,15 +45,19 @@ messages:
help_model: "&e/chatgpt model <model_name> - 切换至其他模型" help_model: "&e/chatgpt model <model_name> - 切换至其他模型"
help_modellist: "&e/chatgpt modellist - 可用的模型列表" help_modellist: "&e/chatgpt modellist - 可用的模型列表"
help_context: "&e/chatgpt context - 切换连续对话模式" help_context: "&e/chatgpt context - 切换连续对话模式"
help_clear: "/chatgpt clear - 清空对话历史" help_clear: "&e/chatgpt clear - 清空对话历史"
context_toggle: "&a连续对话模式已%s。" help_character: "&e/chatgpt character [character_name] - 列出或切换角色"
context_toggle_enabled: "开启" context_toggle: "&e连续对话模式已%s。"
context_toggle_disabled: "关闭" context_toggle_enabled: "&a开启"
context_toggle_disabled: "&c关闭"
current_model_info: "&e当前模型%s输入 /chatgpt model <model_name> 来切换模型。" current_model_info: "&e当前模型%s输入 /chatgpt model <model_name> 来切换模型。"
model_switch: "&a已切换至模型 %s" model_switch: "&a已切换至模型 %s"
chatgpt_error: "&c无法联系ChatGPT。" chatgpt_error: "&c无法联系ChatGPT。"
chatgpt_response: "&bChatGPT: %s" chatgpt_response: "&b%s: %s"
question: "&b你: %s" question: "&b你: %s"
character_switched: "&a已切换至角色 %s"
available_characters: "&e可用的角色列表"
invalid_character: "&c无效的角色。使用 /chatgpt character 查看所有可用的角色。"
invalid_model: "&c模型无效。使用 /chatgpt modellist 查看可用模型。" invalid_model: "&c模型无效。使用 /chatgpt modellist 查看可用模型。"
available_models: "&e可用模型列表" available_models: "&e可用模型列表"
no_permission: "&c你没有权限使用这个指令。需要的权限%s" no_permission: "&c你没有权限使用这个指令。需要的权限%s"

View File

@ -27,4 +27,7 @@ permissions:
default: true default: true
minechatgpt.clear: minechatgpt.clear:
description: Allows clearing conversation history description: Allows clearing conversation history
default: true
minechatgpt.character:
description: Allows switching characters
default: true default: true