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