mirror of
https://github.com/ddaodan/minechatgpt.git
synced 2025-11-03 13:14:13 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b2e38e09d0 | |||
| f021715cec | |||
| fa5d79e252 | |||
| acdfcda8db |
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = 'com'
|
group = 'com'
|
||||||
version = '2.3'
|
version = '2.4'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
# 更新日志
|
# 更新日志
|
||||||
|
## 2.4
|
||||||
|
- 添加自定义prompt
|
||||||
|
- 移除了没有任何效果的配置文件自动更新功能
|
||||||
## 2.3
|
## 2.3
|
||||||
- 修复 #3:[BUG]提问乱码/答非所问
|
- 修复 #3:[BUG]提问乱码/答非所问
|
||||||
## 2.2
|
## 2.2
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import java.util.logging.Logger;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
|
|
||||||
import jodd.http.HttpRequest;
|
import jodd.http.HttpRequest;
|
||||||
import jodd.http.HttpResponse;
|
import jodd.http.HttpResponse;
|
||||||
@@ -36,7 +36,7 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
|
|
||||||
if (!userContexts.containsKey(userId)) {
|
if (!userContexts.containsKey(userId)) {
|
||||||
userContexts.put(userId, new ConversationContext(configManager.getMaxHistorySize()));
|
userContexts.put(userId, new ConversationContext(configManager.getMaxHistorySize()));
|
||||||
userContextEnabled.put(userId, configManager.isContextEnabled()); // 使用配置文件中的默认值
|
userContextEnabled.put(userId, configManager.isContextEnabled());
|
||||||
}
|
}
|
||||||
|
|
||||||
ConversationContext conversationContext = userContexts.get(userId);
|
ConversationContext conversationContext = userContexts.get(userId);
|
||||||
@@ -69,7 +69,6 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
String model = args[1];
|
String model = args[1];
|
||||||
List<String> models = configManager.getModels();
|
List<String> models = configManager.getModels();
|
||||||
if (models.contains(model)) {
|
if (models.contains(model)) {
|
||||||
// Logic to switch model
|
|
||||||
configManager.setCurrentModel(model);
|
configManager.setCurrentModel(model);
|
||||||
sender.sendMessage(configManager.getModelSwitchMessage().replace("%s", model));
|
sender.sendMessage(configManager.getModelSwitchMessage().replace("%s", model));
|
||||||
} else {
|
} else {
|
||||||
@@ -88,7 +87,7 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} else if (args.length > 0 && args[0].equalsIgnoreCase("context")) {
|
} else if (args.length > 0 && args[0].equalsIgnoreCase("context")) {
|
||||||
contextEnabled = !contextEnabled; // 切换上下文开关状态
|
contextEnabled = !contextEnabled;
|
||||||
userContextEnabled.put(userId, contextEnabled);
|
userContextEnabled.put(userId, contextEnabled);
|
||||||
String status = contextEnabled ? configManager.getContextToggleEnabledMessage() : configManager.getContextToggleDisabledMessage();
|
String status = contextEnabled ? configManager.getContextToggleEnabledMessage() : configManager.getContextToggleDisabledMessage();
|
||||||
sender.sendMessage(configManager.getContextToggleMessage().replace("%s", status));
|
sender.sendMessage(configManager.getContextToggleMessage().replace("%s", status));
|
||||||
@@ -107,9 +106,8 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
String question = String.join(" ", args);
|
String question = String.join(" ", args);
|
||||||
// Logic to send question to ChatGPT
|
|
||||||
if (contextEnabled) {
|
if (contextEnabled) {
|
||||||
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);
|
||||||
@@ -120,18 +118,21 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void askChatGPT(CommandSender sender, String question, ConversationContext conversationContext, boolean contextEnabled) {
|
private void askChatGPT(CommandSender sender, String question, ConversationContext conversationContext, boolean contextEnabled) {
|
||||||
|
|
||||||
logger.info("Original question: " + question);
|
|
||||||
// 尝试将问题转换为 UTF-8 编码
|
|
||||||
String utf8Question = convertToUTF8(question);
|
String utf8Question = convertToUTF8(question);
|
||||||
logger.info("Converted question: " + utf8Question);
|
|
||||||
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
|
||||||
|
String customPrompt = configManager.getCustomPrompt();
|
||||||
|
if (!customPrompt.isEmpty()) {
|
||||||
|
JSONObject promptMessage = new JSONObject();
|
||||||
|
promptMessage.put("role", "system");
|
||||||
|
promptMessage.put("content", customPrompt);
|
||||||
|
messages.put(promptMessage);
|
||||||
|
}
|
||||||
JSONObject message = new JSONObject();
|
JSONObject message = new JSONObject();
|
||||||
message.put("role", "user");
|
message.put("role", "user");
|
||||||
message.put("content", utf8Question);
|
message.put("content", utf8Question);
|
||||||
//message.put("content", question);
|
|
||||||
messages.put(message);
|
messages.put(message);
|
||||||
if (contextEnabled) {
|
if (contextEnabled) {
|
||||||
String history = conversationContext.getConversationHistory();
|
String history = conversationContext.getConversationHistory();
|
||||||
@@ -144,8 +145,9 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
}
|
}
|
||||||
json.put("messages", messages);
|
json.put("messages", messages);
|
||||||
json.put("model", configManager.getCurrentModel());
|
json.put("model", configManager.getCurrentModel());
|
||||||
// 记录构建的请求
|
if (configManager.isDebugMode()) {
|
||||||
logger.info("Built request: " + json.toString());
|
logger.info("Built request: " + json.toString());
|
||||||
|
}
|
||||||
|
|
||||||
HttpRequest request = HttpRequest.post(configManager.getBaseUrl() + "/chat/completions")
|
HttpRequest request = HttpRequest.post(configManager.getBaseUrl() + "/chat/completions")
|
||||||
.header("Content-Type", "application/json; charset=UTF-8")
|
.header("Content-Type", "application/json; charset=UTF-8")
|
||||||
@@ -156,26 +158,33 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
logger.info("Sending request to ChatGPT: " + request.toString());
|
logger.info("Sending request to ChatGPT: " + request.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponse response = request.send();
|
//HttpResponse response = request.send();
|
||||||
|
CompletableFuture.supplyAsync(() -> request.send())
|
||||||
if (configManager.isDebugMode()) {
|
.thenAccept(response -> {
|
||||||
logger.info("Received response from ChatGPT: " + response.toString());
|
if (configManager.isDebugMode()) {
|
||||||
}
|
logger.info("Received response from ChatGPT: " + response.toString());
|
||||||
|
}
|
||||||
if (response.statusCode() == 200) {
|
if (response.statusCode() == 200) {
|
||||||
String responseBody = response.bodyText();
|
String responseBody = response.bodyText();
|
||||||
JSONObject jsonResponse = new JSONObject(responseBody);
|
JSONObject jsonResponse = new JSONObject(responseBody);
|
||||||
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().replace("%s", answer));
|
||||||
if (contextEnabled) {
|
if (contextEnabled) {
|
||||||
conversationContext.addMessage(answer); // 仅在启用上下文时添加AI响应到历史记录
|
conversationContext.addMessage(answer); // 仅在启用上下文时添加AI响应到历史记录
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
String errorBody = response.bodyText();
|
String errorBody = response.bodyText();
|
||||||
logger.log(Level.SEVERE, "Failed to get a response from ChatGPT: " + errorBody);
|
logger.log(Level.SEVERE, "Failed to get a response from ChatGPT: " + errorBody);
|
||||||
sender.sendMessage(configManager.getChatGPTErrorMessage());
|
sender.sendMessage(configManager.getChatGPTErrorMessage());
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
.exceptionally(e -> {
|
||||||
|
logger.log(Level.SEVERE, "Exception occurred while processing request: " + e.getMessage(), e);
|
||||||
|
sender.sendMessage(configManager.getChatGPTErrorMessage());
|
||||||
|
return null;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private String convertToUTF8(String input) {
|
private String convertToUTF8(String input) {
|
||||||
try {
|
try {
|
||||||
// 尝试将输入字符串转换为 UTF-8 编码
|
// 尝试将输入字符串转换为 UTF-8 编码
|
||||||
|
|||||||
@@ -25,117 +25,90 @@ public class ConfigManager {
|
|||||||
private String translateColorCodes(String message) {
|
private String translateColorCodes(String message) {
|
||||||
return ChatColor.translateAlternateColorCodes('&', message);
|
return ChatColor.translateAlternateColorCodes('&', message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCurrentModel() {
|
public String getCurrentModel() {
|
||||||
return currentModel;
|
return currentModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCurrentModel(String model) {
|
public void setCurrentModel(String model) {
|
||||||
currentModel = model;
|
currentModel = model;
|
||||||
}
|
}
|
||||||
public String getConfigVersion() {
|
|
||||||
return config.getString("version", "1.0");
|
|
||||||
}
|
|
||||||
public String getApiKey() {
|
public String getApiKey() {
|
||||||
return config.getString("api.key");
|
return config.getString("api.key");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBaseUrl() {
|
public String getBaseUrl() {
|
||||||
return config.getString("api.base_url");
|
return config.getString("api.base_url");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDefaultModel() {
|
public String getDefaultModel() {
|
||||||
return config.getString("default_model");
|
return config.getString("default_model");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getReloadMessage() {
|
public String getReloadMessage() {
|
||||||
return translateColorCodes(config.getString("messages.reload"));
|
return translateColorCodes(config.getString("messages.reload"));
|
||||||
}
|
}
|
||||||
|
|
||||||
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"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHelpAskMessage() {
|
public String getHelpAskMessage() {
|
||||||
return translateColorCodes(config.getString("messages.help_ask"));
|
return translateColorCodes(config.getString("messages.help_ask"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHelpReloadMessage() {
|
public String getHelpReloadMessage() {
|
||||||
return translateColorCodes(config.getString("messages.help_reload"));
|
return translateColorCodes(config.getString("messages.help_reload"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHelpModelMessage() {
|
public String getHelpModelMessage() {
|
||||||
return translateColorCodes(config.getString("messages.help_model"));
|
return translateColorCodes(config.getString("messages.help_model"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHelpModelListMessage() {
|
public String getHelpModelListMessage() {
|
||||||
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", "/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", "/chatgpt clear - Clear conversation history."));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getModelSwitchMessage() {
|
public String getModelSwitchMessage() {
|
||||||
return translateColorCodes(config.getString("messages.model_switch"));
|
return translateColorCodes(config.getString("messages.model_switch"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getChatGPTErrorMessage() {
|
public String getChatGPTErrorMessage() {
|
||||||
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"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getQuestionMessage() {
|
public String getQuestionMessage() {
|
||||||
return translateColorCodes(config.getString("messages.question"));
|
return translateColorCodes(config.getString("messages.question"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getInvalidModelMessage() {
|
public String getInvalidModelMessage() {
|
||||||
return translateColorCodes(config.getString("messages.invalid_model"));
|
return translateColorCodes(config.getString("messages.invalid_model"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAvailableModelsMessage() {
|
public String getAvailableModelsMessage() {
|
||||||
return translateColorCodes(config.getString("messages.available_models"));
|
return translateColorCodes(config.getString("messages.available_models"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getNoPermissionMessage() {
|
public String getNoPermissionMessage() {
|
||||||
return translateColorCodes(config.getString("messages.no_permission"));
|
return translateColorCodes(config.getString("messages.no_permission"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCurrentModelInfoMessage() {
|
public String getCurrentModelInfoMessage() {
|
||||||
return translateColorCodes(config.getString("messages.current_model_info"));
|
return translateColorCodes(config.getString("messages.current_model_info"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMaxHistorySize() {
|
public int getMaxHistorySize() {
|
||||||
return config.getInt("conversation.max_history_size", 10);
|
return config.getInt("conversation.max_history_size", 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isContextEnabled() {
|
public boolean isContextEnabled() {
|
||||||
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", "Context 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", "enabled"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getContextToggleDisabledMessage() {
|
public String getContextToggleDisabledMessage() {
|
||||||
return translateColorCodes(config.getString("messages.context_toggle_disabled", "disabled"));
|
return translateColorCodes(config.getString("messages.context_toggle_disabled", "disabled"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getClearMessage() {
|
public String getClearMessage() {
|
||||||
return translateColorCodes(config.getString("messages.clear", "Conversation history has been cleared."));
|
return translateColorCodes(config.getString("messages.clear", "Conversation history has been cleared."));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.ddaodan.MineChatGPT;
|
package com.ddaodan.MineChatGPT;
|
||||||
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.bstats.bukkit.Metrics;
|
import org.bstats.bukkit.Metrics;
|
||||||
|
|
||||||
@@ -19,7 +18,6 @@ public final class Main extends JavaPlugin {
|
|||||||
tabCompleter = new MineChatGPTTabCompleter(configManager);
|
tabCompleter = new MineChatGPTTabCompleter(configManager);
|
||||||
Objects.requireNonNull(getCommand("chatgpt")).setExecutor(commandHandler);
|
Objects.requireNonNull(getCommand("chatgpt")).setExecutor(commandHandler);
|
||||||
Objects.requireNonNull(getCommand("chatgpt")).setTabCompleter(tabCompleter);
|
Objects.requireNonNull(getCommand("chatgpt")).setTabCompleter(tabCompleter);
|
||||||
checkAndUpdateConfig();
|
|
||||||
if (configManager.isDebugMode()) {
|
if (configManager.isDebugMode()) {
|
||||||
getLogger().info( "DEBUG MODE IS TRUE!!!!!");
|
getLogger().info( "DEBUG MODE IS TRUE!!!!!");
|
||||||
}
|
}
|
||||||
@@ -32,27 +30,4 @@ public final class Main extends JavaPlugin {
|
|||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
saveConfig();
|
saveConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkAndUpdateConfig() {
|
|
||||||
String currentVersion = getConfig().getString("version", "1.0");
|
|
||||||
String pluginVersion = getDescription().getVersion();
|
|
||||||
|
|
||||||
if (!currentVersion.equals(pluginVersion)) {
|
|
||||||
// 加载默认配置文件
|
|
||||||
FileConfiguration defaultConfig = getConfig();
|
|
||||||
reloadConfig();
|
|
||||||
FileConfiguration newConfig = getConfig();
|
|
||||||
|
|
||||||
// 合并配置文件
|
|
||||||
for (String key : defaultConfig.getKeys(true)) {
|
|
||||||
if (!newConfig.contains(key)) {
|
|
||||||
newConfig.set(key, defaultConfig.get(key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新版本号
|
|
||||||
newConfig.set("version", pluginVersion);
|
|
||||||
saveConfig();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -28,6 +28,7 @@ conversation:
|
|||||||
# Continuous conversation switch
|
# Continuous conversation switch
|
||||||
context_enabled: false
|
context_enabled: false
|
||||||
max_history_size: 10
|
max_history_size: 10
|
||||||
|
prompt: "You are a helpful assistant."
|
||||||
# Message settings
|
# Message settings
|
||||||
messages:
|
messages:
|
||||||
reload: "&aConfiguration reloaded successfully!"
|
reload: "&aConfiguration reloaded successfully!"
|
||||||
@@ -53,4 +54,4 @@ messages:
|
|||||||
# If you don't know what this is, don't change it
|
# If you don't know what this is, don't change it
|
||||||
debug: false
|
debug: false
|
||||||
# DO NOT EDIT!!!!!
|
# DO NOT EDIT!!!!!
|
||||||
version: 2.3
|
version: 2.4
|
||||||
Reference in New Issue
Block a user