zzf 2 долоо хоног өмнө
parent
commit
f92f6738c3

+ 2 - 0
build.gradle

@@ -103,6 +103,8 @@ dependencies {
 
 	implementation "io.github.fanyong920:jvppeteer:1.1.5"
 
+// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
+	implementation 'com.squareup.okhttp3:okhttp:4.9.3'
 
 
 

+ 0 - 1
src/main/java/com/rf/AIquantum/dialogue/rest/ChatHistoryController.java

@@ -11,7 +11,6 @@ import com.rf.AIquantum.dialogue.service.ChatHistoryService;
 import com.rf.AIquantum.user.dao.model.UserEntity;
 import com.rf.AIquantum.user.service.UserService;
 import com.rf.AIquantum.utils.Constant;
-import com.rf.AIquantum.utils.FileUtil;
 import com.rf.AIquantum.utils.JWTUtil;
 import com.rf.AIquantum.utils.Result;
 import io.swagger.annotations.Api;

+ 55 - 29
src/main/java/com/rf/AIquantum/dialogue/rest/DialogueController.java

@@ -17,6 +17,10 @@ import com.rf.AIquantum.user.service.UserService;
 import com.rf.AIquantum.utils.*;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import okhttp3.MediaType;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.http.HttpEntity;
@@ -29,15 +33,11 @@ import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
-import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -116,7 +116,7 @@ public class DialogueController extends BaseController {
         chatHistoryEntity.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
         this.chatHistoryService.save(chatHistoryEntity);
         //调用模型相关操作
-        /*chatHistoryEntities = this.chatHistoryService.findChatHistoryByDialogueIdAndStatus(dialogueId);
+        chatHistoryEntities = this.chatHistoryService.findChatHistoryByDialogueIdAndStatus(dialogueId);
         JSONArray messages = new JSONArray();
         for (ChatHistoryEntity chatHistory : chatHistoryEntities) {
             JSONArray contents = new JSONArray();
@@ -141,43 +141,69 @@ public class DialogueController extends BaseController {
         jsonChat.put("messages",messages);
         jsonChat.put("stream",true);
 
-        String url = Constant.INVOKE_IP_PROT + Constant.CHAT_PATH;
+       /* String url = Constant.INVOKE_IP_PROT + Constant.CHAT_PATH;
         String data = HttpClientChat(jsonChat,url);
         JSONObject jsonSystem = JSONObject.parseObject(data);
         if (jsonSystem == null || !jsonSystem.containsKey("response")) {
             return fail("", "模型服务内部错误");
         }
         content = jsonSystem.getString("response");*/
-        String sseContent = "";
-        content = "<think>";
+
         SseResultDataDto sseResultDataDto = new SseResultDataDto();
         sseResultDataDto.setDialogueId(dialogueId);
-        String i0 = " 我是深度思考的内容,我是深度思考的内容,深度思考的内容  # 占位回复\n\n当前服务尚未部署实际的语言模型。\n\n## 您的选择\n- **模型**: `default`\n- **上传的图片数量**: 0\n\n## 示例 Markdown 样式\n- **加粗**: `**加粗文本**`\n- *斜体*: `*斜体文本*`\n- ~~删除线~~: `~~删除线文本~~`\n- [超链接](https://www.bing.com): `[超链接](URL)`\n- 图片:\n\n![示例图片](https://vuejs.org/images/logo.png)\n\n## 列表示例\n1. 第一项\n2. 第二项\n   - 子项 1\n   - 子项 2\n\n> 引用示例:这是一个引用块。\n\n```python\n# 代码块示例\ndef hello():\n    print('Hello, World!')\n```\n\n- 行内公式: `$E = mc^2$` 显示为 $E = mc^2$\n- 块级公式:\n\n$$\n\\int_a^b f(x) \\ dx = F(b) - F(a)\n$$\n\n上述公式显示为:\n\n$$\n\\int_a^b f(x) \\ dx = F(b) - F(a)\n$$\n`";
-        String[] i1= i0.split("");
-        boolean think = true;
-        for (int i = 0; i < i1.length; i++) {
-            sseContent = i1[i];
-            if (think) {
-                if (i == 15) {
-                    content = content + "</think>";
-                    think = false;
-                    sseResultDataDto.setType("text");
-                }else {
-                    sseResultDataDto.setType("think");
-                }
-            }else {
-                sseResultDataDto.setType("text");
+        OkHttpClient client = new OkHttpClient();
+        StringBuilder stringBuilder = new StringBuilder();
+        // 创建请求体
+        okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(jsonChat.toJSONString(), MediaType.parse("application/json"));
+
+        // 创建请求
+        Request request = new Request.Builder().header("Content-Type", "application/json").header("Accept", "text/event-stream")
+                .url(Constant.INVOKE_IP_PROT + Constant.CHAT_PATH)
+                .post(requestBody)
+                .build();
+
+        // 发送请求并处理响应
+        try (Response response = client.newCall(request).execute()) {
+            if (!response.isSuccessful()) {
+                throw new IOException("Unexpected code " + response);
             }
-            sseResultDataDto.setContent(sseContent);
 
-            content = content + sseContent;
-            sseEmitterService.sendMessage(dialogueId, sseResultDataDto);
-            Thread.sleep(100);
+            // 获取响应流
+            okhttp3.ResponseBody responseBody = response.body();
+            String flag = "think";
+            if (responseBody != null) {
+                try (BufferedReader reader = new BufferedReader(responseBody.charStream())) {
+                    String line;
+                    while ((line = reader.readLine()) != null) {
+//                        System.out.println("line===="+line);
+                        sseResultDataDto.setType(flag);
+                        int index = line.indexOf("data:");
+                        if(index != -1){
+                            String responseContent = line.substring(index+5);
+                            if(responseContent.trim().length() != 0){
+                                JSONObject jsonObject = JSONObject.parseObject(responseContent.trim());
+                                responseContent = jsonObject.getString("content");
+                                sseResultDataDto.setContent(responseContent);
+//                                System.out.println("flag==="+flag+"responseContent=="+responseContent); // 处理每一行响应
+                                stringBuilder.append(responseContent);
+//                                System.out.println("dto==="+ sseResultDataDto);
+                                sseEmitterService.sendMessage(dialogueId, sseResultDataDto);
+                                if (responseContent.contains("</think>")) {
+                                    flag = "text";
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
         }
+
         chatHistoryEntity = new ChatHistoryEntity();
         chatHistoryEntity.setDialogueId(dialogueId);
         chatHistoryEntity.setRole("system");
-        chatHistoryEntity.setContent(content);
+        chatHistoryEntity.setContent(stringBuilder.toString());
         chatHistoryEntity.setStatus(1);
         chatHistoryEntity.setEndorse(1);
         chatHistoryEntity.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));