mirror of
https://github.com/ddaodan/minechatgpt.git
synced 2025-11-04 05:34:14 +08:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b2e38e09d0 | |||
| f021715cec | |||
| fa5d79e252 | |||
| acdfcda8db | |||
| aa8aadac49 | |||
| 0944aa1a85 | |||
| b8aaaaf6a3 |
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = 'com'
|
group = 'com'
|
||||||
version = '2.1'
|
version = '2.4'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
@@ -23,7 +23,7 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly "org.spigotmc:spigot-api:1.13-R0.1-SNAPSHOT"
|
compileOnly "org.spigotmc:spigot-api:1.13-R0.1-SNAPSHOT"
|
||||||
compileOnly 'org.bstats:bstats-bukkit:3.0.2'
|
implementation 'org.bstats:bstats-bukkit:3.0.2'
|
||||||
implementation 'org.jodd:jodd-http:6.3.0'
|
implementation 'org.jodd:jodd-http:6.3.0'
|
||||||
implementation 'org.json:json:20231013'
|
implementation 'org.json:json:20231013'
|
||||||
}
|
}
|
||||||
@@ -32,6 +32,7 @@ shadowJar {
|
|||||||
archiveFileName = "MineChatGPT-${project.version}.jar"
|
archiveFileName = "MineChatGPT-${project.version}.jar"
|
||||||
relocate 'jodd', 'com.ddaodan.shaded.jodd'
|
relocate 'jodd', 'com.ddaodan.shaded.jodd'
|
||||||
relocate 'org.json', 'com.ddaodan.minechatgpt.libs.org.json'
|
relocate 'org.json', 'com.ddaodan.minechatgpt.libs.org.json'
|
||||||
|
relocate 'org.bstats', 'com.ddaodan.minechatgpt.libs.org.bstats'
|
||||||
}
|
}
|
||||||
|
|
||||||
def targetJavaVersion = 8
|
def targetJavaVersion = 8
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
# 更新日志
|
# 更新日志
|
||||||
|
## 2.4
|
||||||
|
- 添加自定义prompt
|
||||||
|
- 移除了没有任何效果的配置文件自动更新功能
|
||||||
|
## 2.3
|
||||||
|
- 修复 #3:[BUG]提问乱码/答非所问
|
||||||
|
## 2.2
|
||||||
|
- 修复插件无法加载
|
||||||
|
- 修复 #2:模型切换实际并不生效
|
||||||
## 2.1
|
## 2.1
|
||||||
- 接入bStats
|
- 接入bStats
|
||||||
- 调整配置文件,添加更新功能,现在更新插件应该不用再备份配置文件了
|
- 调整配置文件,添加更新功能,现在更新插件应该不用再备份配置文件了
|
||||||
|
|||||||
25
readme.md
25
readme.md
@@ -13,13 +13,24 @@
|
|||||||
- [ ] 自定义prompt
|
- [ ] 自定义prompt
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
- 下载插件,放在plugins文件夹中
|
1. 下载插件,放在plugins文件夹中
|
||||||
- 重启服务器
|
2. 重启服务器
|
||||||
> 为兼容更多版本,插件使用1.13版本进行构建,因此在较高版本加载插件时,控制台会出现以下错误信息,这属于正常现象。
|
> 为兼容更多版本,插件使用1.13版本进行构建,因此在较高版本加载插件时,控制台会出现以下错误信息,这属于正常现象。
|
||||||
> ```
|
> ```
|
||||||
> [Server thread/WARN]: Initializing Legacy Material Support. Unless you have legacy plugins and/or data this is a bug!
|
> [Server thread/WARN]: Initializing Legacy Material Support. Unless you have legacy plugins and/or data this is a bug!
|
||||||
> [Server thread/WARN]: Legacy plugin MineChatGPT v1.0 does not specify an api-version.
|
> [Server thread/WARN]: Legacy plugin MineChatGPT v1.0 does not specify an api-version.
|
||||||
> ```
|
> ```
|
||||||
|
3. 打开配置文件`config.yml`,修改以下两项设置:
|
||||||
|
```yaml
|
||||||
|
# API 相关设置
|
||||||
|
api:
|
||||||
|
# 你的 OpenAI API key,用于身份验证
|
||||||
|
# 获取 API key 的方法:访问 https://platform.openai.com/account/api-keys 并创建一个新的 API key
|
||||||
|
key: "sk-your_openai_api_key"
|
||||||
|
# OpenAI API 的基础 URL,用于构建请求
|
||||||
|
base_url: "https://api.openai.com/v1"
|
||||||
|
```
|
||||||
|
4. 输入`/chatgpt reload`重新加载配置文件
|
||||||
|
|
||||||
## 截图
|
## 截图
|
||||||
- 服务端截图(Spigot 1.20.1)
|
- 服务端截图(Spigot 1.20.1)
|
||||||
@@ -46,6 +57,7 @@ models:
|
|||||||
- "gpt-4-turbo"
|
- "gpt-4-turbo"
|
||||||
- "gpt-4-turbo-preview"
|
- "gpt-4-turbo-preview"
|
||||||
- "gpt-4o"
|
- "gpt-4o"
|
||||||
|
- "gpt-4o-mini"
|
||||||
# Google Gemini
|
# Google Gemini
|
||||||
# - "gemini-pro"
|
# - "gemini-pro"
|
||||||
# - "gemini-1.5-pro"
|
# - "gemini-1.5-pro"
|
||||||
@@ -55,6 +67,10 @@ models:
|
|||||||
# 以及更多...
|
# 以及更多...
|
||||||
# 默认使用的模型
|
# 默认使用的模型
|
||||||
default_model: "gpt-3.5-turbo"
|
default_model: "gpt-3.5-turbo"
|
||||||
|
conversation:
|
||||||
|
# 连续对话开关
|
||||||
|
context_enabled: false
|
||||||
|
max_history_size: 10
|
||||||
# 消息相关设置
|
# 消息相关设置
|
||||||
messages:
|
messages:
|
||||||
reload: "&a已重新加载配置文件!"
|
reload: "&a已重新加载配置文件!"
|
||||||
@@ -63,6 +79,7 @@ messages:
|
|||||||
help_reload: "&e/chatgpt reload - 重新加载配置文件"
|
help_reload: "&e/chatgpt reload - 重新加载配置文件"
|
||||||
help_model: "&e/chatgpt model <model_name> - 切换至其他模型"
|
help_model: "&e/chatgpt model <model_name> - 切换至其他模型"
|
||||||
help_modellist: "&e/chatgpt modellist - 可用的模型列表"
|
help_modellist: "&e/chatgpt modellist - 可用的模型列表"
|
||||||
|
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: "&bChatGPT: %s"
|
||||||
@@ -71,7 +88,7 @@ messages:
|
|||||||
available_models: "&e可用模型列表:"
|
available_models: "&e可用模型列表:"
|
||||||
no_permission: "&c你没有权限使用这个指令。需要的权限:%s"
|
no_permission: "&c你没有权限使用这个指令。需要的权限:%s"
|
||||||
# 不要动!!!!!
|
# 不要动!!!!!
|
||||||
version: 2.1
|
version: 2.3
|
||||||
```
|
```
|
||||||
|
|
||||||
## 指令与权限
|
## 指令与权限
|
||||||
@@ -88,11 +105,13 @@ version: 2.1
|
|||||||
? = 部分支持
|
? = 部分支持
|
||||||
× = 不支持
|
× = 不支持
|
||||||
只列出经过测试的版本
|
只列出经过测试的版本
|
||||||
|
|
||||||
|服务端|支持情况|
|
|服务端|支持情况|
|
||||||
|-|-|
|
|-|-|
|
||||||
|Mohist 1.20.1|✔|
|
|Mohist 1.20.1|✔|
|
||||||
|Spigot 1.20.1|✔|
|
|Spigot 1.20.1|✔|
|
||||||
|Spigot 1.12.2|✔|
|
|Spigot 1.12.2|✔|
|
||||||
|
|KCauldron 1.7.10|×|
|
||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
### `Failed to contact ChatGPT.` `无法联系ChatGPT。`
|
### `Failed to contact ChatGPT.` `无法联系ChatGPT。`
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ import org.json.JSONObject;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import jodd.http.HttpRequest;
|
import jodd.http.HttpRequest;
|
||||||
import jodd.http.HttpResponse;
|
import jodd.http.HttpResponse;
|
||||||
@@ -16,20 +20,33 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
private final Main plugin;
|
private final Main plugin;
|
||||||
private final ConfigManager configManager;
|
private final ConfigManager configManager;
|
||||||
private static final Logger logger = Logger.getLogger(CommandHandler.class.getName());
|
private static final Logger logger = Logger.getLogger(CommandHandler.class.getName());
|
||||||
|
private final Map<String, ConversationContext> userContexts;
|
||||||
|
private final Map<String, Boolean> userContextEnabled;
|
||||||
|
|
||||||
public CommandHandler(Main plugin, ConfigManager configManager) {
|
public CommandHandler(Main plugin, ConfigManager configManager) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.configManager = configManager;
|
this.configManager = configManager;
|
||||||
|
this.userContexts = new HashMap<>();
|
||||||
|
this.userContextEnabled = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||||
|
String userId = sender.getName();
|
||||||
|
|
||||||
|
if (!userContexts.containsKey(userId)) {
|
||||||
|
userContexts.put(userId, new ConversationContext(configManager.getMaxHistorySize()));
|
||||||
|
userContextEnabled.put(userId, configManager.isContextEnabled());
|
||||||
|
}
|
||||||
|
|
||||||
|
ConversationContext conversationContext = userContexts.get(userId);
|
||||||
|
boolean contextEnabled = userContextEnabled.get(userId);
|
||||||
|
|
||||||
if (command.getName().equalsIgnoreCase("chatgpt")) {
|
if (command.getName().equalsIgnoreCase("chatgpt")) {
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
sendHelpMessage(sender);
|
sendHelpMessage(sender);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
String subCommand = args[0];
|
String subCommand = args[0];
|
||||||
if (subCommand.equalsIgnoreCase("reload")) {
|
if (subCommand.equalsIgnoreCase("reload")) {
|
||||||
if (!sender.hasPermission("minechatgpt.reload")) {
|
if (!sender.hasPermission("minechatgpt.reload")) {
|
||||||
@@ -45,13 +62,14 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (args.length < 2) {
|
if (args.length < 2) {
|
||||||
sender.sendMessage(configManager.getHelpModelMessage());
|
String currentModel = configManager.getCurrentModel();
|
||||||
|
sender.sendMessage(configManager.getCurrentModelInfoMessage().replace("%s", currentModel));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
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);
|
||||||
sender.sendMessage(configManager.getModelSwitchMessage().replace("%s", model));
|
sender.sendMessage(configManager.getModelSwitchMessage().replace("%s", model));
|
||||||
} else {
|
} else {
|
||||||
sender.sendMessage(configManager.getInvalidModelMessage());
|
sender.sendMessage(configManager.getInvalidModelMessage());
|
||||||
@@ -68,55 +86,122 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
sender.sendMessage("- " + model);
|
sender.sendMessage("- " + model);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
} else if (args.length > 0 && args[0].equalsIgnoreCase("context")) {
|
||||||
|
contextEnabled = !contextEnabled;
|
||||||
|
userContextEnabled.put(userId, contextEnabled);
|
||||||
|
String status = contextEnabled ? configManager.getContextToggleEnabledMessage() : configManager.getContextToggleDisabledMessage();
|
||||||
|
sender.sendMessage(configManager.getContextToggleMessage().replace("%s", status));
|
||||||
|
return true;
|
||||||
|
} else if (subCommand.equalsIgnoreCase("clear")) {
|
||||||
|
if (!sender.hasPermission("minechatgpt.clear")) {
|
||||||
|
sender.sendMessage(configManager.getNoPermissionMessage().replace("%s", "minechatgpt.clear"));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
conversationContext.clearHistory();
|
||||||
|
sender.sendMessage(configManager.getClearMessage());
|
||||||
|
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"));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
String question = String.join(" ", args);
|
String question = String.join(" ", args);
|
||||||
// Logic to send question to ChatGPT
|
if (contextEnabled) {
|
||||||
|
conversationContext.addMessage(question);
|
||||||
|
}
|
||||||
sender.sendMessage(configManager.getQuestionMessage().replace("%s", question));
|
sender.sendMessage(configManager.getQuestionMessage().replace("%s", question));
|
||||||
askChatGPT(sender, question);
|
askChatGPT(sender, question, conversationContext, contextEnabled);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void askChatGPT(CommandSender sender, String question) {
|
private void askChatGPT(CommandSender sender, String question, ConversationContext conversationContext, boolean contextEnabled) {
|
||||||
|
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
|
||||||
|
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", question);
|
message.put("content", utf8Question);
|
||||||
messages.put(message);
|
messages.put(message);
|
||||||
|
if (contextEnabled) {
|
||||||
|
String history = conversationContext.getConversationHistory();
|
||||||
|
if (!history.isEmpty()) {
|
||||||
|
JSONObject historyMessage = new JSONObject();
|
||||||
|
historyMessage.put("role", "system");
|
||||||
|
historyMessage.put("content", history);
|
||||||
|
messages.put(historyMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
json.put("messages", messages);
|
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")
|
HttpRequest request = HttpRequest.post(configManager.getBaseUrl() + "/chat/completions")
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json; charset=UTF-8")
|
||||||
.header("Authorization", "Bearer " + configManager.getApiKey())
|
.header("Authorization", "Bearer " + configManager.getApiKey())
|
||||||
.body(json.toString());
|
.bodyText(json.toString());
|
||||||
|
|
||||||
HttpResponse response = request.send();
|
if (configManager.isDebugMode()) {
|
||||||
|
logger.info("Sending request to ChatGPT: " + request.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
//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) {
|
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) {
|
||||||
|
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) {
|
||||||
|
try {
|
||||||
|
// 尝试将输入字符串转换为 UTF-8 编码
|
||||||
|
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
|
||||||
|
return new String(bytes, StandardCharsets.UTF_8);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.severe("Failed to convert input to UTF-8: " + e.getMessage());
|
||||||
|
return input; // 如果转换失败,返回原始输入
|
||||||
|
}
|
||||||
|
}
|
||||||
private void sendHelpMessage(CommandSender sender) {
|
private void sendHelpMessage(CommandSender sender) {
|
||||||
sender.sendMessage(configManager.getHelpMessage());
|
sender.sendMessage(configManager.getHelpMessage());
|
||||||
sender.sendMessage(configManager.getHelpAskMessage());
|
sender.sendMessage(configManager.getHelpAskMessage());
|
||||||
sender.sendMessage(configManager.getHelpReloadMessage());
|
sender.sendMessage(configManager.getHelpReloadMessage());
|
||||||
sender.sendMessage(configManager.getHelpModelMessage());
|
sender.sendMessage(configManager.getHelpModelMessage());
|
||||||
sender.sendMessage(configManager.getHelpModelListMessage());
|
sender.sendMessage(configManager.getHelpModelListMessage());
|
||||||
|
sender.sendMessage(configManager.getHelpContextMessage());
|
||||||
|
sender.sendMessage(configManager.getHelpClearMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,88 +7,109 @@ import java.util.List;
|
|||||||
public class ConfigManager {
|
public class ConfigManager {
|
||||||
private final Main plugin;
|
private final Main plugin;
|
||||||
private FileConfiguration config;
|
private FileConfiguration config;
|
||||||
|
private String currentModel;
|
||||||
|
|
||||||
public ConfigManager(Main plugin) {
|
public ConfigManager(Main plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
reloadConfig();
|
reloadConfig();
|
||||||
}
|
}
|
||||||
|
public boolean isDebugMode() {
|
||||||
|
return config.getBoolean("debug", false);
|
||||||
|
}
|
||||||
public void reloadConfig() {
|
public void reloadConfig() {
|
||||||
plugin.reloadConfig();
|
plugin.reloadConfig();
|
||||||
config = plugin.getConfig();
|
config = plugin.getConfig();
|
||||||
|
currentModel = config.getString("default_model");
|
||||||
}
|
}
|
||||||
|
|
||||||
private String translateColorCodes(String message) {
|
private String translateColorCodes(String message) {
|
||||||
return ChatColor.translateAlternateColorCodes('&', message);
|
return ChatColor.translateAlternateColorCodes('&', message);
|
||||||
}
|
}
|
||||||
public String getConfigVersion() {
|
public String getCurrentModel() {
|
||||||
return config.getString("version", "1.0");
|
return currentModel;
|
||||||
|
}
|
||||||
|
public void setCurrentModel(String model) {
|
||||||
|
currentModel = model;
|
||||||
}
|
}
|
||||||
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() {
|
||||||
|
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() {
|
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() {
|
||||||
|
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."));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.ddaodan.MineChatGPT;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
public class ConversationContext {
|
||||||
|
private Queue<String> conversationHistory;
|
||||||
|
private int maxHistorySize;
|
||||||
|
|
||||||
|
public ConversationContext(int maxHistorySize) {
|
||||||
|
this.maxHistorySize = maxHistorySize;
|
||||||
|
this.conversationHistory = new LinkedList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMessage(String message) {
|
||||||
|
if (conversationHistory.size() >= maxHistorySize) {
|
||||||
|
conversationHistory.poll(); // Remove the oldest message
|
||||||
|
}
|
||||||
|
conversationHistory.offer(message); // Add the new message
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConversationHistory() {
|
||||||
|
return String.join("\n", conversationHistory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearHistory() {
|
||||||
|
conversationHistory.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,6 +18,9 @@ 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);
|
||||||
|
if (configManager.isDebugMode()) {
|
||||||
|
getLogger().info( "DEBUG MODE IS TRUE!!!!!");
|
||||||
|
}
|
||||||
// Initialize bStats
|
// Initialize bStats
|
||||||
int pluginId = 22635;
|
int pluginId = 22635;
|
||||||
new Metrics(this, pluginId);
|
new Metrics(this, pluginId);
|
||||||
@@ -28,31 +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)) {
|
|
||||||
// 备份旧配置文件
|
|
||||||
saveConfig();
|
|
||||||
saveResource("config.old.yml", true);
|
|
||||||
|
|
||||||
// 加载默认配置文件
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -23,6 +23,8 @@ public class MineChatGPTTabCompleter implements TabCompleter {
|
|||||||
completions.add("reload");
|
completions.add("reload");
|
||||||
completions.add("model");
|
completions.add("model");
|
||||||
completions.add("modellist");
|
completions.add("modellist");
|
||||||
|
completions.add("context");
|
||||||
|
completions.add("clear");
|
||||||
} else if (args.length == 2 && args[0].equalsIgnoreCase("model")) {
|
} else if (args.length == 2 && args[0].equalsIgnoreCase("model")) {
|
||||||
// 补全模型名称
|
// 补全模型名称
|
||||||
completions.addAll(configManager.getModels());
|
completions.addAll(configManager.getModels());
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ models:
|
|||||||
- "gpt-4-turbo"
|
- "gpt-4-turbo"
|
||||||
- "gpt-4-turbo-preview"
|
- "gpt-4-turbo-preview"
|
||||||
- "gpt-4o"
|
- "gpt-4o"
|
||||||
|
- "gpt-4o-mini"
|
||||||
# Google Gemini
|
# Google Gemini
|
||||||
# - "gemini-pro"
|
# - "gemini-pro"
|
||||||
# - "gemini-1.5-pro"
|
# - "gemini-1.5-pro"
|
||||||
@@ -23,14 +24,26 @@ 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:
|
||||||
|
# Continuous conversation switch
|
||||||
|
context_enabled: false
|
||||||
|
max_history_size: 10
|
||||||
|
prompt: "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!"
|
||||||
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_clear: "/chatgpt clear - Clear conversation history."
|
||||||
|
context_toggle: "Context is now %s."
|
||||||
|
context_toggle_enabled: "enabled"
|
||||||
|
context_toggle_disabled: "disabled"
|
||||||
|
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: "&bChatGPT: %s"
|
||||||
@@ -38,5 +51,7 @@ messages:
|
|||||||
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"
|
||||||
|
# If you don't know what this is, don't change it
|
||||||
|
debug: false
|
||||||
# DO NOT EDIT!!!!!
|
# DO NOT EDIT!!!!!
|
||||||
version: 2.1
|
version: 2.4
|
||||||
@@ -14,6 +14,7 @@ models:
|
|||||||
- "gpt-4-turbo"
|
- "gpt-4-turbo"
|
||||||
- "gpt-4-turbo-preview"
|
- "gpt-4-turbo-preview"
|
||||||
- "gpt-4o"
|
- "gpt-4o"
|
||||||
|
- "gpt-4o-mini"
|
||||||
# Google Gemini
|
# Google Gemini
|
||||||
# - "gemini-pro"
|
# - "gemini-pro"
|
||||||
# - "gemini-1.5-pro"
|
# - "gemini-1.5-pro"
|
||||||
@@ -23,14 +24,25 @@ models:
|
|||||||
# 以及更多...
|
# 以及更多...
|
||||||
# 默认使用的模型
|
# 默认使用的模型
|
||||||
default_model: "gpt-3.5-turbo"
|
default_model: "gpt-3.5-turbo"
|
||||||
|
conversation:
|
||||||
|
# 连续对话开关
|
||||||
|
context_enabled: false
|
||||||
|
max_history_size: 10
|
||||||
# 消息相关设置
|
# 消息相关设置
|
||||||
messages:
|
messages:
|
||||||
reload: "&a已重新加载配置文件!"
|
reload: "&a已重新加载配置文件!"
|
||||||
|
clear: "&a对话历史已清空!"
|
||||||
help: "&e===== MineChatGPT 帮助 ====="
|
help: "&e===== MineChatGPT 帮助 ====="
|
||||||
help_ask: "&e/chatgpt <text> - 向ChatGPT提问"
|
help_ask: "&e/chatgpt <text> - 向ChatGPT提问"
|
||||||
help_reload: "&e/chatgpt reload - 重新加载配置文件"
|
help_reload: "&e/chatgpt reload - 重新加载配置文件"
|
||||||
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_clear: "/chatgpt clear - 清空对话历史"
|
||||||
|
context_toggle: "&a连续对话模式已%s。"
|
||||||
|
context_toggle_enabled: "开启"
|
||||||
|
context_toggle_disabled: "关闭"
|
||||||
|
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: "&bChatGPT: %s"
|
||||||
@@ -38,5 +50,7 @@ messages:
|
|||||||
invalid_model: "&c模型无效。使用 /chatgpt modellist 查看可用模型。"
|
invalid_model: "&c模型无效。使用 /chatgpt modellist 查看可用模型。"
|
||||||
available_models: "&e可用模型列表:"
|
available_models: "&e可用模型列表:"
|
||||||
no_permission: "&c你没有权限使用这个指令。需要的权限:%s"
|
no_permission: "&c你没有权限使用这个指令。需要的权限:%s"
|
||||||
|
# 如果你不知道这是什么,请不要动
|
||||||
|
debug: false
|
||||||
# 不要动!!!!!
|
# 不要动!!!!!
|
||||||
version: 2.1
|
version: 2.3
|
||||||
@@ -21,3 +21,9 @@ permissions:
|
|||||||
minechatgpt.modellist:
|
minechatgpt.modellist:
|
||||||
description: Allows listing available models
|
description: Allows listing available models
|
||||||
default: op
|
default: op
|
||||||
|
minechatgpt.context:
|
||||||
|
description: Allows toggling context mode
|
||||||
|
default: true
|
||||||
|
chatgpt.clear:
|
||||||
|
description: Allows clearing conversation history
|
||||||
|
default: true
|
||||||
Reference in New Issue
Block a user