增加部分日志输出

This commit is contained in:
2024-12-22 12:47:04 +08:00
parent ed1504f56d
commit 1fd4a96896
3 changed files with 31 additions and 29 deletions

View File

@@ -26,7 +26,7 @@ public final class App extends JavaPlugin {
public static final App INSTANCE = new App(); public static final App INSTANCE = new App();
private 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") .name("ChatAI-InGroup")
.author("SLHAF") .author("SLHAF")
.build()); .build());
@@ -120,7 +120,7 @@ public final class App extends JavaPlugin {
for (int i = 0; i < Config.ConfigLoader.getConfig().getModelConfigTemplates().size(); i++) { for (int i = 0; i < Config.ConfigLoader.getConfig().getModelConfigTemplates().size(); i++) {
ModelConfigTemplate modelConfigTemplate = Config.ConfigLoader.getConfig().getModelConfigTemplates().get(i); ModelConfigTemplate modelConfigTemplate = Config.ConfigLoader.getConfig().getModelConfigTemplates().get(i);
helpMsg[0] += "\r\n\r\n"+"TemplateIndex: "+i+"\r\n" + 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"; "base_url: "+modelConfigTemplate.getBase_url()+"\r\n";
} }
event.getSubject().sendMessage(helpMsg[0]); event.getSubject().sendMessage(helpMsg[0]);

View File

@@ -6,6 +6,7 @@ import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import work.slhaf.chatai.chat.constant.Constant; import work.slhaf.chatai.chat.constant.Constant;
import work.slhaf.chatai.chat.pojo.ChatBody; import work.slhaf.chatai.chat.pojo.ChatBody;
import work.slhaf.chatai.chat.pojo.ChatResponse; 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.ArrayList;
import java.util.List; import java.util.List;
@Slf4j
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@@ -24,6 +26,7 @@ public class ChatClient {
private String url; private String url;
private String apikey; private String apikey;
private String model; private String model;
private String prompt = "12333";
private int top_p; private int top_p;
private int temperature; private int temperature;
@@ -31,45 +34,44 @@ public class ChatClient {
private List<Message> messages = new ArrayList<>(); private List<Message> messages = new ArrayList<>();
public ChatClient(String clientId,String url,String apikey,String model){ public ChatClient(String clientId, String url, String apikey, String model) {
this(url, apikey,model); this(url, apikey, model);
this.clientId = clientId; this.clientId = clientId;
} }
public ChatClient(String clientId,String url, String apikey,String model, int top_p, int temperature, int 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(url, apikey, model, top_p, temperature, max_tokens);
this.clientId = clientId; this.clientId = clientId;
} }
public ChatClient(String url, String apikey,String model, int top_p, int temperature, int max_tokens) { public ChatClient(String url, String apikey, String model, int top_p, int temperature, int max_tokens) {
this(url, apikey,model); this(url, apikey, model);
this.top_p = top_p; this.top_p = top_p;
this.temperature = temperature; this.temperature = temperature;
this.max_tokens = max_tokens; 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.url = url;
this.apikey = apikey; this.apikey = apikey;
this.model = model; this.model = model;
} }
public void setPromotion(String promotion){ public void setPromotion(String promotion) {
if (!promotion.equals("null")) {
Message message = Message.builder() Message message = Message.builder()
.role(Constant.Character.SYSTEM) .role(Constant.Character.SYSTEM)
.content(promotion) .content(promotion)
.build(); .build();
messages.add(0, message); messages.add(0, message);
} }
}
public ChatResponse runChat(String content){ public ChatResponse runChat(String content) {
HttpRequest request = HttpRequest.post(url+ "/work/slhaf/chatai/completions"); HttpRequest request = HttpRequest.post(url + "/completions");
log.info("URL: {}",request.getUrl());
request.header("Content-Type", "application/json"); request.header("Content-Type", "application/json");
request.header("Authorization", "Bearer " + apikey); request.header("Authorization", "Bearer " + apikey);
Message message = new Message(Constant.Character.USER,content); Message message = new Message(Constant.Character.USER, content);
messages.add(message); messages.add(message);
ChatBody body; ChatBody body;
if (top_p > 0) { if (top_p > 0) {
@@ -80,7 +82,7 @@ public class ChatClient {
.temperature(temperature) .temperature(temperature)
.max_tokens(max_tokens) .max_tokens(max_tokens)
.build(); .build();
}else { } else {
body = ChatBody.builder() body = ChatBody.builder()
.model(model) .model(model)
.messages(messages) .messages(messages)
@@ -97,10 +99,10 @@ public class ChatClient {
.usageBean(primaryChatResponse.getUsage()) .usageBean(primaryChatResponse.getUsage())
.build(); .build();
messages.add(new Message(Constant.Character.SYSTEM, finalResponse.getMessage())); messages.add(new Message(Constant.Character.SYSTEM, finalResponse.getMessage()));
}catch (Exception e){ } catch (Exception e) {
finalResponse = ChatResponse.builder() finalResponse = ChatResponse.builder()
.type(Constant.Response.ERROR) .type(Constant.Response.ERROR)
.message(response.getStatus()+": "+response.body()) .message(response.getStatus() + ": " + response.body())
.build(); .build();
} }
response.close(); response.close();

View File

@@ -15,7 +15,7 @@ import java.util.Objects;
public class ChatUtil { public class ChatUtil {
private static final HashMap<String, ChatClient> chatClients = new HashMap<>(); private static final HashMap<String, ChatClient> chatClients = new HashMap<>();
private static final HashMap<String,Long> latestTime = new HashMap<>(); private static final HashMap<String, Long> latestTime = new HashMap<>();
private ChatUtil() { private ChatUtil() {
@@ -53,7 +53,7 @@ public class ChatUtil {
} }
public static String chat(String id, String content, String command) { public static String chat(String id, String content, String command) {
String chatId = id + "-" + command; String chatId = id + "-" + command + "-" + System.currentTimeMillis();
ChatResponse chatResponse = null; ChatResponse chatResponse = null;
synchronized (chatClients) { synchronized (chatClients) {
if (chatClients.containsKey(chatId)) { if (chatClients.containsKey(chatId)) {
@@ -77,7 +77,7 @@ public class ChatUtil {
ChatClient chatClient = new ChatClient(chatId, url, apikey, customModel); ChatClient chatClient = new ChatClient(chatId, url, apikey, customModel);
chatClient.setPromotion(customPromotion); chatClient.setPromotion(customPromotion);
chatClients.put(chatId, chatClient); chatClients.put(chatId, chatClient);
log.info("final content: {}",content); log.info("final content: {}", content);
chatResponse = chatClient.runChat(content); chatResponse = chatClient.runChat(content);
} }
} }
@@ -92,16 +92,16 @@ public class ChatUtil {
return chatResponse.getMessage(); return chatResponse.getMessage();
} }
public static String removeClient(String id,String command) { public static String removeClient(String id, String command) {
String chatId = id + "-" + command; String chatId = id + "-" + command;
synchronized (chatClients) { synchronized (chatClients) {
chatClients.remove(chatId); chatClients.remove(chatId);
latestTime.remove(chatId); latestTime.remove(chatId);
} }
return "消息记录: "+chatId +"已清空"; return "消息记录: " + chatId + "已清空";
} }
public static String removeAllClients(){ public static String removeAllClients() {
synchronized (chatClients) { synchronized (chatClients) {
chatClients.clear(); chatClients.clear();
latestTime.clear(); latestTime.clear();