From 1fd4a9689602d316925984a298363d34eb93a4ff Mon Sep 17 00:00:00 2001 From: slhaf Date: Sun, 22 Dec 2024 12:47:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/work/slhaf/chatai/App.java | 4 +- .../work/slhaf/chatai/chat/ChatClient.java | 44 ++++++++++--------- .../work/slhaf/chatai/utils/ChatUtil.java | 12 ++--- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/main/java/work/slhaf/chatai/App.java b/src/main/java/work/slhaf/chatai/App.java index a72b08e..8138b45 100644 --- a/src/main/java/work/slhaf/chatai/App.java +++ b/src/main/java/work/slhaf/chatai/App.java @@ -26,7 +26,7 @@ public final class App extends JavaPlugin { public static final App INSTANCE = new App(); private App() { - super(new JvmPluginDescriptionBuilder("com.plugin.chatAI-InGroup", "0.1.0") + super(new JvmPluginDescriptionBuilder("com.plugin.chatAI-InGroup", "2.1.0") .name("ChatAI-InGroup") .author("SLHAF") .build()); @@ -120,7 +120,7 @@ public final class App extends JavaPlugin { for (int i = 0; i < Config.ConfigLoader.getConfig().getModelConfigTemplates().size(); i++) { ModelConfigTemplate modelConfigTemplate = Config.ConfigLoader.getConfig().getModelConfigTemplates().get(i); helpMsg[0] += "\r\n\r\n"+"TemplateIndex: "+i+"\r\n" + - "name: "+modelConfigTemplate.getTemplate_name() + + "name: "+modelConfigTemplate.getTemplate_name() +"\r\n"+ "base_url: "+modelConfigTemplate.getBase_url()+"\r\n"; } event.getSubject().sendMessage(helpMsg[0]); diff --git a/src/main/java/work/slhaf/chatai/chat/ChatClient.java b/src/main/java/work/slhaf/chatai/chat/ChatClient.java index c0f1d98..4328715 100644 --- a/src/main/java/work/slhaf/chatai/chat/ChatClient.java +++ b/src/main/java/work/slhaf/chatai/chat/ChatClient.java @@ -6,6 +6,7 @@ import cn.hutool.json.JSONUtil; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; import work.slhaf.chatai.chat.constant.Constant; import work.slhaf.chatai.chat.pojo.ChatBody; import work.slhaf.chatai.chat.pojo.ChatResponse; @@ -15,6 +16,7 @@ import work.slhaf.chatai.chat.pojo.PrimaryChatResponse; import java.util.ArrayList; import java.util.List; +@Slf4j @Data @AllArgsConstructor @NoArgsConstructor @@ -24,6 +26,7 @@ public class ChatClient { private String url; private String apikey; private String model; + private String prompt = "12333"; private int top_p; private int temperature; @@ -31,45 +34,44 @@ public class ChatClient { private List messages = new ArrayList<>(); - public ChatClient(String clientId,String url,String apikey,String model){ - this(url, apikey,model); + public ChatClient(String clientId, String url, String apikey, String model) { + this(url, apikey, model); this.clientId = clientId; } - public ChatClient(String clientId,String url, String apikey,String model, int top_p, int temperature, int max_tokens) { - this(url, apikey,model, top_p, temperature, max_tokens); + public ChatClient(String clientId, String url, String apikey, String model, int top_p, int temperature, int max_tokens) { + this(url, apikey, model, top_p, temperature, max_tokens); this.clientId = clientId; } - public ChatClient(String url, String apikey,String model, int top_p, int temperature, int max_tokens) { - this(url, apikey,model); + public ChatClient(String url, String apikey, String model, int top_p, int temperature, int max_tokens) { + this(url, apikey, model); this.top_p = top_p; this.temperature = temperature; this.max_tokens = max_tokens; } - public ChatClient(String url,String apikey,String model){ + public ChatClient(String url, String apikey, String model) { this.url = url; this.apikey = apikey; this.model = model; } - public void setPromotion(String promotion){ - if (!promotion.equals("null")) { - Message message = Message.builder() - .role(Constant.Character.SYSTEM) - .content(promotion) - .build(); - messages.add(0, message); - } + public void setPromotion(String promotion) { + Message message = Message.builder() + .role(Constant.Character.SYSTEM) + .content(promotion) + .build(); + messages.add(0, message); } - public ChatResponse runChat(String content){ - HttpRequest request = HttpRequest.post(url+ "/work/slhaf/chatai/completions"); + public ChatResponse runChat(String content) { + HttpRequest request = HttpRequest.post(url + "/completions"); + log.info("URL: {}",request.getUrl()); request.header("Content-Type", "application/json"); request.header("Authorization", "Bearer " + apikey); - Message message = new Message(Constant.Character.USER,content); + Message message = new Message(Constant.Character.USER, content); messages.add(message); ChatBody body; if (top_p > 0) { @@ -80,7 +82,7 @@ public class ChatClient { .temperature(temperature) .max_tokens(max_tokens) .build(); - }else { + } else { body = ChatBody.builder() .model(model) .messages(messages) @@ -97,10 +99,10 @@ public class ChatClient { .usageBean(primaryChatResponse.getUsage()) .build(); messages.add(new Message(Constant.Character.SYSTEM, finalResponse.getMessage())); - }catch (Exception e){ + } catch (Exception e) { finalResponse = ChatResponse.builder() .type(Constant.Response.ERROR) - .message(response.getStatus()+": "+response.body()) + .message(response.getStatus() + ": " + response.body()) .build(); } response.close(); diff --git a/src/main/java/work/slhaf/chatai/utils/ChatUtil.java b/src/main/java/work/slhaf/chatai/utils/ChatUtil.java index 9f5d47c..5c60491 100644 --- a/src/main/java/work/slhaf/chatai/utils/ChatUtil.java +++ b/src/main/java/work/slhaf/chatai/utils/ChatUtil.java @@ -15,7 +15,7 @@ import java.util.Objects; public class ChatUtil { private static final HashMap chatClients = new HashMap<>(); - private static final HashMap latestTime = new HashMap<>(); + private static final HashMap latestTime = new HashMap<>(); private ChatUtil() { @@ -53,7 +53,7 @@ public class ChatUtil { } public static String chat(String id, String content, String command) { - String chatId = id + "-" + command; + String chatId = id + "-" + command + "-" + System.currentTimeMillis(); ChatResponse chatResponse = null; synchronized (chatClients) { if (chatClients.containsKey(chatId)) { @@ -77,7 +77,7 @@ public class ChatUtil { ChatClient chatClient = new ChatClient(chatId, url, apikey, customModel); chatClient.setPromotion(customPromotion); chatClients.put(chatId, chatClient); - log.info("final content: {}",content); + log.info("final content: {}", content); chatResponse = chatClient.runChat(content); } } @@ -92,16 +92,16 @@ public class ChatUtil { return chatResponse.getMessage(); } - public static String removeClient(String id,String command) { + public static String removeClient(String id, String command) { String chatId = id + "-" + command; synchronized (chatClients) { chatClients.remove(chatId); latestTime.remove(chatId); } - return "消息记录: "+chatId +"已清空"; + return "消息记录: " + chatId + "已清空"; } - public static String removeAllClients(){ + public static String removeAllClients() { synchronized (chatClients) { chatClients.clear(); latestTime.clear();