浏览代码

1.会话长度13000

zzf 2 周之前
父节点
当前提交
71ed77d5ef

+ 1 - 1
src/main/java/com/rf/AIquantum/base/model/BaseEntity.java

@@ -25,7 +25,7 @@ import java.io.Serializable;
 public class BaseEntity implements Serializable {
     @Getter
     @Id
-    @GenericGenerator(name = "jpa-uuid", strategy = "uuid")
+    @GenericGenerator(name = "jpa-uuid", strategy = "org.hibernate.id.UUIDGenerator")
     @GeneratedValue(generator = "jpa-uuid")
     @Column(name = "id", columnDefinition = "varchar(36) COMMENT '数据ID'")
     @NotNull(groups = Update.class)

+ 28 - 2
src/main/java/com/rf/AIquantum/dialogue/rest/ChatHistoryController.java

@@ -26,6 +26,7 @@ import org.springframework.web.bind.annotation.*;
 import javax.servlet.http.HttpServletResponse;
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
@@ -74,25 +75,47 @@ public class ChatHistoryController extends BaseController {
         //调用模型相关操作
         List<ChatHistoryEntity> chatHistoryEntities = this.chatHistoryService.findChatHistoryByDialogueIdAndStatus(dialogueId);
         JSONArray messages = new JSONArray();
-        for (ChatHistoryEntity chatHistory : chatHistoryEntities) {
+        int length = 0;
+        for (int i=chatHistoryEntities.size() -1 ;i>=0;i--) {
+            ChatHistoryEntity chatHistory = chatHistoryEntities.get(i);
             JSONArray contents = new JSONArray();
             JSONObject jsonText = new JSONObject();
             jsonText.put("type", "text");
-            jsonText.put("text", chatHistory.getContent());
+            String text = chatHistory.getContent();
+            if(text.contains("</think>")){
+                text = text.substring(text.indexOf("</think>")+8);
+            }
+            if(length + text.length()>Constant.MESSAGE_MAX_LENGTH){
+                break;
+            }
+            length += text.length();
             if (chatHistory.getImage() != null && !chatHistory.getImage().equals("")) {
+                if(length+chatHistory.getImage().length()>Constant.MESSAGE_MAX_LENGTH){
+                    break;
+                }
                 JSONObject jsonImage = new JSONObject();
                 jsonImage.put("type", "image_url");
                 JSONObject jsonUrl = new JSONObject();
                 jsonUrl.put("url", chatHistory.getImage());
+                length += chatHistory.getImage().length();
                 jsonImage.put("image_url", jsonUrl);
                 contents.add(jsonImage);
             }
+            jsonText.put("text", text);
             contents.add(jsonText);
             JSONObject jsonRole = new JSONObject();
             jsonRole.put("role", chatHistory.getRole());
             jsonRole.put("content", contents);
             messages.add(jsonRole);
         }
+        System.out.println("Message length: " + length);
+        //反转顺序
+        int size = messages.size();
+        for (int i = 0; i< size; i++){
+            Object temp = messages.get(i);
+            messages.set(i,messages.get(size-1-i));
+            messages.set(size-1-i,temp);
+        }
         JSONObject jsonChat = new JSONObject();
         jsonChat.put("messages", messages);
         jsonChat.put("stream",true);
@@ -134,6 +157,9 @@ public class ChatHistoryController extends BaseController {
                         int bytesRead =  bufferedSource.read(buffer);
                         line = new String(buffer, 0, bytesRead);
                         if( sseEmitterService.getEmitter(dialogueId) == null){
+                            if(flag.equals("think")){
+                                stringBuilder.append("</").append(flag).append(">");
+                            }
                             break;
                         }
                         sseResultDataDto.setType(flag);

+ 35 - 7
src/main/java/com/rf/AIquantum/dialogue/rest/DialogueController.java

@@ -100,13 +100,15 @@ public class DialogueController extends BaseController {
         }
         List<ChatHistoryEntity> chatHistoryEntities = chatHistoryService.findChatHistoryByDialogueIdAndStatus(dialogueId);
         if (chatHistoryEntities.size() < 1) {
-            if (content.length() > 50) {
-                content = content.substring(0, 50);
-            }
+
             //新建对话
             DialogueEntity dialogueEntity = new DialogueEntity();
             dialogueEntity.setId(dialogueId);
-            dialogueEntity.setDialogueName(content);
+            if (content.length() > 50) {
+                dialogueEntity.setDialogueName( content.substring(0, 50));
+            }else {
+                dialogueEntity.setDialogueName(content);
+            }
             dialogueEntity.setPhone(phone);
             dialogueEntity.setStatus(1);
             dialogueEntity.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
@@ -126,25 +128,47 @@ public class DialogueController extends BaseController {
         //调用模型相关操作
         chatHistoryEntities = this.chatHistoryService.findChatHistoryByDialogueIdAndStatus(dialogueId);
         JSONArray messages = new JSONArray();
-        for (ChatHistoryEntity chatHistory : chatHistoryEntities) {
+        int length = 0;
+        for (int i=chatHistoryEntities.size() -1 ;i>=0;i--) {
+            ChatHistoryEntity chatHistory = chatHistoryEntities.get(i);
             JSONArray contents = new JSONArray();
             JSONObject jsonText = new JSONObject();
             jsonText.put("type", "text");
-            jsonText.put("text", chatHistory.getContent());
-            if (chatHistory.getImage() != null && !chatHistory.getImage().equals("")) {
+            String text = chatHistory.getContent();
+            if(text.contains("</think>")){
+                text = text.substring(text.indexOf("</think>")+8);
+            }
+            if(length + text.length()>Constant.MESSAGE_MAX_LENGTH){
+                break;
+            }
+            length += text.length();
+            if (StringUtils.isNotEmpty(chatHistory.getImage())) {
+                if(length+chatHistory.getImage().length()>Constant.MESSAGE_MAX_LENGTH){
+                    break;
+                }
                 JSONObject jsonImage = new JSONObject();
                 jsonImage.put("type", "image_url");
                 JSONObject jsonUrl = new JSONObject();
                 jsonUrl.put("url", chatHistory.getImage());
+                length += chatHistory.getImage().length();
                 jsonImage.put("image_url", jsonUrl);
                 contents.add(jsonImage);
             }
+            jsonText.put("text", text);
             contents.add(jsonText);
             JSONObject jsonRole = new JSONObject();
             jsonRole.put("role", chatHistory.getRole());
             jsonRole.put("content", contents);
             messages.add(jsonRole);
         }
+        System.out.println("Message length ="+length);
+        //反转顺序
+        int size = messages.size();
+        for (int i = 0; i< size; i++){
+            Object temp = messages.get(i);
+            messages.set(i,messages.get(size-1-i));
+            messages.set(size-1-i,temp);
+        }
         JSONObject jsonChat = new JSONObject();
         jsonChat.put("messages", messages);
         jsonChat.put("stream", true);
@@ -186,6 +210,10 @@ public class DialogueController extends BaseController {
                         int bytesRead =  bufferedSource.read(buffer);
                         line = new String(buffer, 0, bytesRead);
                         if( sseEmitterService.getEmitter(dialogueId) == null){
+//                            sseEmitterService.sendMessage(dialogueId, stringBuilder.toString());
+                            if(flag.equals("think")){
+                                stringBuilder.append("</").append(flag).append(">");
+                            }
                             break;
                         }
                         sseResultDataDto.setType(flag);

+ 3 - 0
src/main/java/com/rf/AIquantum/utils/Constant.java

@@ -31,6 +31,9 @@ public class Constant {
     //聊天图片下载链接
     public static final String CHAT_IMAGE_URL = "http://49.232.26.44:8062/user/show?filePath=";
 
+    /**大模型上下文长度**/
+    public static final int MESSAGE_MAX_LENGTH = 13000;
+