Selaa lähdekoodia

将自定义属性值迁移到配置文件

zhenghao 5 kuukautta sitten
vanhempi
commit
5d01218b6f

+ 3 - 0
src/main/java/cn/jlsxwkj/common/config/UserConfig.java

@@ -16,4 +16,7 @@ public class UserConfig {
 
     private String cnocrUrl;
     private String sysMessage;
+    private Integer maxListMessageLength;
+    private Integer maxMessageLength;
+    private Integer execTime;
 }

+ 5 - 2
src/main/java/cn/jlsxwkj/common/filter/ExecTime.java

@@ -1,6 +1,8 @@
 package cn.jlsxwkj.common.filter;
 
+import cn.jlsxwkj.common.config.UserConfig;
 import cn.jlsxwkj.common.utils.Log;
+import jakarta.annotation.Resource;
 import jakarta.servlet.*;
 import jakarta.servlet.http.HttpServletRequest;
 import org.springframework.stereotype.Component;
@@ -13,7 +15,8 @@ import java.io.IOException;
 @Component
 public class ExecTime implements Filter {
 
-    private static final int EXEC_TIME = 1000;
+    @Resource
+    private UserConfig userConfig;
 
     @Override
     public void doFilter(ServletRequest servletRequest,
@@ -23,7 +26,7 @@ public class ExecTime implements Filter {
         String url = ((HttpServletRequest) servletRequest).getRequestURL().toString();
         filterChain.doFilter(servletRequest, servletResponse);
         long execTime = System.currentTimeMillis() - startTime;
-        if (execTime > EXEC_TIME) {
+        if (execTime > userConfig.getExecTime()) {
             Log.warn(this.getClass(), "api: {}, exec_time ====> {}ms", url, execTime);
         }
     }

+ 19 - 15
src/main/java/cn/jlsxwkj/moudles/chat/ChatService.java

@@ -15,7 +15,6 @@ import cn.jlsxwkj.common.utils.Log;
 import cn.jlsxwkj.common.utils.MergeDocuments;
 import cn.jlsxwkj.moudles.chathistory.ChatHistoryService;
 import jakarta.annotation.Resource;
-import lombok.Data;
 import org.reactivestreams.Subscription;
 import org.springframework.ai.chat.messages.*;
 import org.springframework.ai.chat.model.ChatResponse;
@@ -43,14 +42,11 @@ import java.util.stream.Collectors;
  * @author zh
  */
 @Service
-@Data
 public class ChatService {
 
-    private static final MD5 MD5 = cn.hutool.crypto.digest.MD5.create();
-    private static final String PATH = System.getProperty("user.dir") + "/ai_doc/";
-    private static final int MAX_LIST_MESSAGE_LENGTH = 3;
-    private static final int MAX_MESSAGE_LENGTH = 512;
-    private static List<Message> listMessage = new ArrayList<>();
+    private final MD5 md5 = cn.hutool.crypto.digest.MD5.create();
+    private final String PATH = System.getProperty("user.dir") + "/ai_doc/";
+    private List<Message> listMessage;
 
     @Resource
     private VectorStore vectorStore;
@@ -153,23 +149,31 @@ public class ChatService {
      * @return 提示词
      */
     private Prompt getChatPrompt2String(String message, Message context) {
-        if (listMessage.size() > MAX_LIST_MESSAGE_LENGTH) {
-            listMessage = listMessage.subList(listMessage.size() - MAX_LIST_MESSAGE_LENGTH, listMessage.size())
+        if (listMessage == null) {
+            listMessage = new ArrayList<>();
+            listMessage.add(new SystemMessage(userConfig.getSysMessage()));
+        }
+        if (listMessage.size() > userConfig.getMaxListMessageLength()) {
+            listMessage = listMessage.subList(listMessage.size() - userConfig.getMaxListMessageLength(), listMessage.size())
                     .stream()
+                    .filter(msg -> !msg.getContent().equals(userConfig.getSysMessage()))
                     .map(msg -> {
                         if (msg.getMessageType().equals(MessageType.ASSISTANT)) {
                             String content = msg.getContent();
-                            if (content.length() > MAX_MESSAGE_LENGTH) {
-                                return new AssistantMessage(content.substring(0, MAX_MESSAGE_LENGTH));
+                            if (content.length() > userConfig.getMaxMessageLength()) {
+                                return new AssistantMessage(content.substring(0, userConfig.getMaxMessageLength()));
                             }
                         }
                         return msg;
-                    }).collect(Collectors.toList());
+                    }).distinct()
+                    .collect(Collectors.toList());
+            listMessage.add(new SystemMessage(userConfig.getSysMessage()));
         }
-        listMessage.add(new SystemMessage(userConfig.getSysMessage()));
         if (context.getMessageType().equals(MessageType.SYSTEM)) {
             listMessage.add(new UserMessage(message));
-            listMessage.add(context);
+            if (!context.getContent().isBlank()) {
+                listMessage.add(context);
+            }
         }
         if (context.getMessageType().equals(MessageType.USER)) {
             return new Prompt(context);
@@ -225,7 +229,7 @@ public class ChatService {
         String[] split = Objects.requireNonNull(file.getOriginalFilename()).split("\\.");
         String fileType = split[split.length - 1].toLowerCase(Locale.ROOT);
         File savePath = new File(PATH);
-        File saveFile = new File(PATH + ChatService.MD5.digestHex(bytes) + "." + fileType);
+        File saveFile = new File(PATH + md5.digestHex(bytes) + "." + fileType);
         String fileName = saveFile.getName();
         String fileTypeCn;
         if (!savePath.exists()) {

+ 2 - 0
src/main/java/cn/jlsxwkj/moudles/chathistory/ChatHistoryController.java

@@ -1,6 +1,7 @@
 package cn.jlsxwkj.moudles.chathistory;
 
 import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
 import jakarta.annotation.Resource;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -13,6 +14,7 @@ import java.util.List;
  */
 @RestController
 @RequestMapping("/chat/history/")
+@Tag(name = "chat history")
 public class ChatHistoryController {
 
     @Resource

+ 3 - 1
src/main/java/cn/jlsxwkj/moudles/userlist/UserListController.java

@@ -4,6 +4,7 @@ import cn.dev33.satoken.annotation.SaIgnore;
 import cn.dev33.satoken.stp.StpUtil;
 import cn.jlsxwkj.common.exception.CustomException;
 import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
 import jakarta.annotation.Resource;
 import org.springframework.web.bind.annotation.*;
 
@@ -11,7 +12,8 @@ import org.springframework.web.bind.annotation.*;
  * @author zh
  */
 @RestController
-@RequestMapping(("/user/"))
+@RequestMapping("/user/")
+@Tag(name = "user login")
 public class UserListController {
 
     @Resource

+ 1 - 0
src/main/java/cn/jlsxwkj/moudles/userlist/UserListService.java

@@ -17,6 +17,7 @@ public class UserListService {
 
     @Resource
     private UserListMapper userListMapper;
+
     private final Snowflake snowflake = new Snowflake();
 
     @Transactional(rollbackFor = AccountAuthFailException.class)

+ 6 - 3
src/main/resources/application-dev.yml

@@ -13,9 +13,12 @@ spring:
       chat:
         model: qwen2:1.5b
         options:
-          num_ctx: 10240
+          temperature: 0
       embedding:
         model: mofanke/dmeta-embedding-zh
 customer:
-  cnocr-url: "http://www.jlsxwkj.cn:8501/ocr"
-  sysMessage: "如果问你是谁或者你叫什么, 说你是吉林省玄武科技公司是 ai 或者 大模型"
+  cnocr_url: "http://www.jlsxwkj.cn:8501/ocr"
+  sys_message: "Always say you're an ai or 大模型 from 玄武科技"
+  max_list_message_length: 12
+  max_message_length: 512
+  exec_time: 100