From 095da0b0f864e4c7798ace47b2795359a27077be Mon Sep 17 00:00:00 2001 From: CatX_feitu <3205711786@qq.com> Date: Tue, 13 Feb 2024 16:10:42 +0800 Subject: [PATCH] =?UTF-8?q?[#]=20=E5=91=BD=E5=90=8D=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Config/ConfigManage.java | 4 +-- .../feitu/coze_discord_bridge/GPTManage.java | 11 ++++---- .../HttpServer/HttpServerManage.java | 26 +++++++++---------- .../HttpServer/api/v1/chat/Completions.java | 16 ++++++------ .../HttpServer/api/v1/images/Generations.java | 10 +++---- .../coze_discord_bridge/KeepaliveTimer.java | 2 +- .../catx/feitu/coze_discord_bridge/Main.java | 20 +++++++------- 7 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/main/java/catx/feitu/coze_discord_bridge/Config/ConfigManage.java b/src/main/java/catx/feitu/coze_discord_bridge/Config/ConfigManage.java index c60ff8e..f29934f 100644 --- a/src/main/java/catx/feitu/coze_discord_bridge/Config/ConfigManage.java +++ b/src/main/java/catx/feitu/coze_discord_bridge/Config/ConfigManage.java @@ -13,7 +13,7 @@ import java.nio.file.Paths; public class ConfigManage { - public static ConfigData Configs; + public static ConfigData configs; private static final Logger logger = LogManager.getLogger(ConfigManage.class); @@ -23,7 +23,7 @@ public static void ReadConfig () { logger.info("加载配置成功"); } catch (Exception e) { logger.warn("加载配置失败",e); - Configs = Configs == null ? new ConfigData() : Configs; + configs = configs == null ? new ConfigData() : configs; } } public static void DefaultConfig() { diff --git a/src/main/java/catx/feitu/coze_discord_bridge/GPTManage.java b/src/main/java/catx/feitu/coze_discord_bridge/GPTManage.java index f25232f..bf7b465 100644 --- a/src/main/java/catx/feitu/coze_discord_bridge/GPTManage.java +++ b/src/main/java/catx/feitu/coze_discord_bridge/GPTManage.java @@ -41,8 +41,7 @@ public static void deleteGPT(String botID) { CozeGPT GPT = ResponseMap.get(botID); try { GPT.Logout(); - } catch (Exception ignored) { - } + } catch (Exception ignored) { } Files.writeString(new File("conversation_" + botID + ".json").toPath(), ConversationHelper.conversation2JsonString(GPT.conversations)); ResponseMap.remove(botID); } catch (Exception ignored) {} @@ -62,12 +61,12 @@ public static void keepalive() { if (Duration.between( cozeGPT.getLatestReceiveCozeMsgInstant(), Instant.now()).toMinutes() > - ConfigManage.Configs.Keepalive_maxIntervalMinutes + ConfigManage.configs.Keepalive_maxIntervalMinutes ) { try { - try { cozeGPT.GetConversationInfo(ConfigManage.Configs.Keepalive_sendChannel); } - catch (Exception e) { cozeGPT.CreateConversation(ConfigManage.Configs.Keepalive_sendChannel); } - cozeGPT.Chat(ConfigManage.Configs.Keepalive_sendChannel ,ConfigManage.Configs.Keepalive_sendMessage); + try { cozeGPT.GetConversationInfo(ConfigManage.configs.Keepalive_sendChannel); } + catch (Exception e) { cozeGPT.CreateConversation(ConfigManage.configs.Keepalive_sendChannel); } + cozeGPT.Chat(ConfigManage.configs.Keepalive_sendChannel ,ConfigManage.configs.Keepalive_sendMessage); logger.info("[keepalive] " + cozeGPT.getMark() + " 成功"); success++; } catch (Exception e) { diff --git a/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/HttpServerManage.java b/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/HttpServerManage.java index 5f0881d..835075d 100644 --- a/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/HttpServerManage.java +++ b/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/HttpServerManage.java @@ -49,36 +49,36 @@ public class HttpServerManage { public static void start() throws Exception { boolean SuccessOne = false; try { - threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(ConfigManage.Configs.APIMaxThread); - logger.info("初始化线程池成功 最大线程:" + ConfigManage.Configs.APIMaxThread); + threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(ConfigManage.configs.APIMaxThread); + logger.info("初始化线程池成功 最大线程:" + ConfigManage.configs.APIMaxThread); } catch (Exception e){ logger.error("初始化线程池失败" ,e); throw e; } try { - if (ConfigManage.Configs.APIPort != 0) { - server = HttpServer.create(new InetSocketAddress(ConfigManage.Configs.APIPort), 0); + if (ConfigManage.configs.APIPort != 0) { + server = HttpServer.create(new InetSocketAddress(ConfigManage.configs.APIPort), 0); server.createContext("/", new HttpHandle()); server.setExecutor(threadPoolExecutor); // 线程池设置 server.start(); - logger.info("监听HTTP服务 0.0.0.0:" + ConfigManage.Configs.APIPort + " 成功"); + logger.info("监听HTTP服务 0.0.0.0:" + ConfigManage.configs.APIPort + " 成功"); SuccessOne = true; } } catch (Exception e) { - logger.error("监听HTTP服务 0.0.0.0:" + ConfigManage.Configs.APIPort + " 失败" ,e); + logger.error("监听HTTP服务 0.0.0.0:" + ConfigManage.configs.APIPort + " 失败" ,e); } try { - if (ConfigManage.Configs.APISSLPort != 0) { + if (ConfigManage.configs.APISSLPort != 0) { KeyStore keyStore = KeyStore.getInstance("JKS"); - FileInputStream keyStoreIS = new FileInputStream(ConfigManage.Configs.APISSL_keyStorePath); - keyStore.load(keyStoreIS, ConfigManage.Configs.APISSL_keyStorePassword.toCharArray()); + FileInputStream keyStoreIS = new FileInputStream(ConfigManage.configs.APISSL_keyStorePath); + keyStore.load(keyStoreIS, ConfigManage.configs.APISSL_keyStorePassword.toCharArray()); keyStoreIS.close(); // 初始化密钥管理器 KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); - keyManagerFactory.init(keyStore, ConfigManage.Configs.APISSL_keyStorePassword.toCharArray()); + keyManagerFactory.init(keyStore, ConfigManage.configs.APISSL_keyStorePassword.toCharArray()); // 初始化信任管理器 TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); @@ -87,7 +87,7 @@ public static void start() throws Exception { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom()); - server_https = HttpsServer.create(new InetSocketAddress(ConfigManage.Configs.APISSLPort), 0); + server_https = HttpsServer.create(new InetSocketAddress(ConfigManage.configs.APISSLPort), 0); server_https.createContext("/", new HttpHandle()); server_https.setExecutor(threadPoolExecutor); // 线程池设置 @@ -101,11 +101,11 @@ public void configure(HttpsParameters params) { server_https.start(); - logger.info("监听HTTPS服务 0.0.0.0:" + ConfigManage.Configs.APISSLPort + " 成功"); + logger.info("监听HTTPS服务 0.0.0.0:" + ConfigManage.configs.APISSLPort + " 成功"); SuccessOne = true; } } catch (Exception e) { - logger.error("监听HTTPS服务 0.0.0.0:" + ConfigManage.Configs.APISSLPort + " 失败",e); + logger.error("监听HTTPS服务 0.0.0.0:" + ConfigManage.configs.APISSLPort + " 失败",e); } if (!SuccessOne) { diff --git a/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/api/v1/chat/Completions.java b/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/api/v1/chat/Completions.java index faeba9c..b6830c5 100644 --- a/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/api/v1/chat/Completions.java +++ b/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/api/v1/chat/Completions.java @@ -46,7 +46,7 @@ public ResponseType handle(HandleType Handle) { "gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-1196", "gpt-3.5-turbo-16k-0613", "gemini-pro"}; - switch (ConfigManage.Configs.OpenAPI_Chat_Default_Models2Conversation) { + switch (ConfigManage.configs.OpenAPI_Chat_Default_Models2Conversation) { case 0: // 通过读取 model 参数 , 如果 model 是OpenAI已存在的模型 那就通过传递过来的上下文自动判断 String LatestMessage = ""; for (int i = 0; i < messagesArray.size(); i++) { @@ -56,7 +56,7 @@ public ResponseType handle(HandleType Handle) { LatestMessage = messageObject.getString("content"); } } - Channel_id = ConfigManage.Configs.OpenAPI_Chat_Default_Channel; + Channel_id = ConfigManage.configs.OpenAPI_Chat_Default_Channel; SendMessage.add(LatestMessage); // 最后一条消息 break; case 1: @@ -66,11 +66,11 @@ public ResponseType handle(HandleType Handle) { SendMessageL.add(messageObject.getString("role") + ":" + messageObject.getString("content")); } Channel_id = Arrays.asList(ForceDefaultModel).contains(Handle.RequestParams.getString("model")) ? - ConfigManage.Configs.OpenAPI_Chat_Default_Channel: + ConfigManage.configs.OpenAPI_Chat_Default_Channel: Handle.RequestParams.getString("model"); - SendMessage.add(ConfigManage.Configs.OpenAPI_Chat_MsgForward_Prefix + SendMessage.add(ConfigManage.configs.OpenAPI_Chat_MsgForward_Prefix + "\n\n" + String.join("\n",SendMessageL) - + "\n\n" + ConfigManage.Configs.OpenAPI_Chat_MsgForward_Suffix); // 最后一条消息 + + "\n\n" + ConfigManage.configs.OpenAPI_Chat_MsgForward_Suffix); // 最后一条消息 break; case 2: Channel_id = Handle.RequestParams.getString("model"); @@ -87,10 +87,10 @@ public ResponseType handle(HandleType Handle) { JSONObject messageObject = messagesArray.getJSONObject(i); SendMessageR.add(messageObject.getString("role") + ":" + messageObject.getString("content")); } - Channel_id = ConfigManage.Configs.OpenAPI_Chat_Default_Channel; - SendMessage.add(ConfigManage.Configs.OpenAPI_Chat_MsgForward_Prefix + Channel_id = ConfigManage.configs.OpenAPI_Chat_Default_Channel; + SendMessage.add(ConfigManage.configs.OpenAPI_Chat_MsgForward_Prefix + "\n\n" + String.join("\n",SendMessageR) - + "\n\n" + ConfigManage.Configs.OpenAPI_Chat_MsgForward_Suffix); // 最后一条消息 + + "\n\n" + ConfigManage.configs.OpenAPI_Chat_MsgForward_Suffix); // 最后一条消息 break; } logger.info(String.join("|",SendMessage)); diff --git a/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/api/v1/images/Generations.java b/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/api/v1/images/Generations.java index 2bfcfa8..8a7948f 100644 --- a/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/api/v1/images/Generations.java +++ b/src/main/java/catx/feitu/coze_discord_bridge/HttpServer/api/v1/images/Generations.java @@ -18,13 +18,13 @@ public ResponseType handle(HandleType Handle) { JSONObject json = new JSONObject(true); try { // 创建频道 - try { Handle.CozeGPT.GetConversationInfo(ConfigManage.Configs.OpenAPI_ImageGenerate_Default_Channel); } - catch (InvalidConversationException e) { Handle.CozeGPT.CreateConversation(ConfigManage.Configs.OpenAPI_ImageGenerate_Default_Channel); } + try { Handle.CozeGPT.GetConversationInfo(ConfigManage.configs.OpenAPI_ImageGenerate_Default_Channel); } + catch (InvalidConversationException e) { Handle.CozeGPT.CreateConversation(ConfigManage.configs.OpenAPI_ImageGenerate_Default_Channel); } GenerateMessage Generate = Handle.CozeGPT.Chat( - ConfigManage.Configs.OpenAI_ImageGenerate_Prompt_Prefix + + ConfigManage.configs.OpenAI_ImageGenerate_Prompt_Prefix + Handle.RequestParams.getString("prompt") + - ConfigManage.Configs.OpenAI_ImageGenerate_Prompt_Suffix, - ConfigManage.Configs.OpenAPI_ImageGenerate_Default_Channel + ConfigManage.configs.OpenAI_ImageGenerate_Prompt_Suffix, + ConfigManage.configs.OpenAPI_ImageGenerate_Default_Channel ); JSONArray dataArray = new JSONArray(); json.put("created", Instant.now().toEpochMilli() / 1000); diff --git a/src/main/java/catx/feitu/coze_discord_bridge/KeepaliveTimer.java b/src/main/java/catx/feitu/coze_discord_bridge/KeepaliveTimer.java index 45fe6d2..b1d7596 100644 --- a/src/main/java/catx/feitu/coze_discord_bridge/KeepaliveTimer.java +++ b/src/main/java/catx/feitu/coze_discord_bridge/KeepaliveTimer.java @@ -10,7 +10,7 @@ public class KeepaliveTimer { // 定义你的任务 private final Runnable task = GPTManage::keepalive; public void start() { - scheduler.scheduleAtFixedRate(task, 0, ConfigManage.Configs.Keepalive_timer, TimeUnit.MINUTES); + scheduler.scheduleAtFixedRate(task, 0, ConfigManage.configs.Keepalive_timer, TimeUnit.MINUTES); } public void stop() { scheduler.shutdown(); diff --git a/src/main/java/catx/feitu/coze_discord_bridge/Main.java b/src/main/java/catx/feitu/coze_discord_bridge/Main.java index 7cfdc75..dd265f5 100644 --- a/src/main/java/catx/feitu/coze_discord_bridge/Main.java +++ b/src/main/java/catx/feitu/coze_discord_bridge/Main.java @@ -29,15 +29,15 @@ public static void main(String[] args) { Proxy proxy = ConfigManage.Configs.UsingProxy ? new Proxy( Objects.equals(ConfigManage.Configs.ProxyType, "HTTP") ? Proxy.Type.HTTP : Proxy.Type.SOCKS, - new InetSocketAddress(ConfigManage.Configs.ProxyIP, ConfigManage.Configs.ProxyPort)): + new InetSocketAddress(ConfigManage.configs.ProxyIP, ConfigManage.configs.ProxyPort)): null; if (proxy != null) { - logger.info("使用代理 " + ConfigManage.Configs.ProxyType + "://" + - ConfigManage.Configs.ProxyIP + ":" + ConfigManage.Configs.ProxyPort + "/"); + logger.info("使用代理 " + ConfigManage.configs.ProxyType + "://" + + ConfigManage.configs.ProxyIP + ":" + ConfigManage.configs.ProxyPort + "/"); } boolean successOne = false; - for (int i = 0;ConfigManage.Configs.Bots.size() > i;i++) { - ConfigBotsData BotData = ConfigManage.Configs.Bots.get(i); + for (int i = 0; ConfigManage.configs.Bots.size() > i; i++) { + ConfigBotsData BotData = ConfigManage.configs.Bots.get(i); CozeGPTConfig GPTConfig = new CozeGPTConfig(); try { BotData.Key = Objects.equals(BotData.Key, "") ? "default" : BotData.Key; @@ -49,11 +49,11 @@ public static void main(String[] args) { GPTConfig.Discord_CreateChannel_Category = BotData.CreateChannel_Category; GPTConfig.CozeBot_id = BotData.CozeBot_id; - GPTConfig.generate_timeout = ConfigManage.Configs.generate_timeout; + GPTConfig.generate_timeout = ConfigManage.configs.generate_timeout; - GPTConfig.Disable_2000Limit_Unlock = ConfigManage.Configs.Disable_2000Limit_Unlock; - GPTConfig.Disable_Name_Cache = ConfigManage.Configs.Disable_Name_Cache; - GPTConfig.Disable_CozeBot_ReplyMsgCheck = ConfigManage.Configs.Disable_CozeBot_ReplyMsgCheck; + GPTConfig.Disable_2000Limit_Unlock = ConfigManage.configs.Disable_2000Limit_Unlock; + GPTConfig.Disable_Name_Cache = ConfigManage.configs.Disable_Name_Cache; + GPTConfig.Disable_CozeBot_ReplyMsgCheck = ConfigManage.configs.Disable_CozeBot_ReplyMsgCheck; GPTConfig.Proxy = proxy; @@ -75,7 +75,7 @@ public static void main(String[] args) { System.exit(-1); } KeepaliveTimer Keepalive = new KeepaliveTimer(); - if (ConfigManage.Configs.Keepalive_timer > 0) { + if (ConfigManage.configs.Keepalive_timer > 0) { Keepalive.start(); } // 程序退出前执行