瀏覽代碼

20250415 dify功能修改

lrj 5 月之前
父節點
當前提交
49a0df6f7d

+ 11 - 3
src/main/java/cn/cslg/pas/controller/outApi/DifyController.java

@@ -4,6 +4,8 @@ import cn.cslg.pas.common.core.base.Constants;
 import cn.cslg.pas.common.model.dify.ChatMessageDTO;
 import cn.cslg.pas.common.model.dify.DifyChatMessageDTO;
 import cn.cslg.pas.common.model.dify.DifyHistoryMessageDTO;
+import cn.cslg.pas.common.utils.CacheUtils;
+import cn.cslg.pas.common.utils.LoginUtils;
 import cn.cslg.pas.common.utils.Response;
 import cn.cslg.pas.domain.business.ProjectTask;
 import cn.cslg.pas.domain.business.ReportProject;
@@ -22,6 +24,7 @@ import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -44,6 +47,10 @@ import java.util.List;
 public class DifyController {
     private final DifyService difyService;
     private final DifySessionService difySessionService;
+    @Autowired
+    private CacheUtils cacheUtils;
+    @Autowired
+    private LoginUtils loginUtils;
 
     //
     @RequestMapping(value = "/chatMessage", method = RequestMethod.POST, produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@@ -56,7 +63,7 @@ public class DifyController {
     @RequestMapping(value = "/queryHistoryMessage", method = RequestMethod.POST)
     @Operation(summary = "查询历史会话")
     public Response queryHistoryMessage(@RequestBody DifyHistoryMessageDTO difyHistoryMessageDTO) throws IOException {
-      String jsons=  difyService.queryHistoryMessage(difyHistoryMessageDTO);
+        String jsons = difyService.queryHistoryMessage(difyHistoryMessageDTO);
         JSONObject jsonObject = JSONObject.parseObject(jsons);
         return Response.success(jsonObject);
     }
@@ -64,15 +71,16 @@ public class DifyController {
     @RequestMapping(value = "/querySessionId", method = RequestMethod.GET)
     @Operation(summary = "查询会话id")
     public Response querySessionId(Integer projectId) throws IOException {
-        String userId = "1";
+        String userId = loginUtils.getId().toString();
         return Response.success(difySessionService.getSessionId(projectId, userId));
     }
 
     @RequestMapping(value = "/stopMessage", method = RequestMethod.GET)
     @Operation(summary = "停止会话")
-    public Response stopMessage(String  taskId) throws IOException {
+    public Response stopMessage(String taskId) throws IOException {
         return Response.success(difyService.stopMessage(taskId));
     }
+
     @RequestMapping(value = "/generateInventionPoint", method = RequestMethod.POST)
     @Operation(summary = "生成发明点")
     public Response generateInventionPoint(@RequestBody ChatMessageDTO chatMessageDTO) throws IOException {

+ 14 - 33
src/main/java/cn/cslg/pas/service/common/DifyService.java

@@ -5,7 +5,9 @@ import cn.cslg.pas.common.model.dify.ChatMessageDTO;
 import cn.cslg.pas.common.model.dify.DifyChatMessageDTO;
 import cn.cslg.pas.common.model.dify.DifyHistoryMessageDTO;
 
+import cn.cslg.pas.common.utils.CacheUtils;
 import cn.cslg.pas.common.utils.DataUtils;
+import cn.cslg.pas.common.utils.LoginUtils;
 import cn.cslg.pas.domain.business.TechnicalCase;
 import cn.cslg.pas.domain.report.AssoProjectConfession;
 import cn.cslg.pas.service.business.TechnicalCaseService;
@@ -20,6 +22,7 @@ import com.google.gson.GsonBuilder;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import okhttp3.*;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpHeaders;
 import org.springframework.stereotype.Service;
@@ -56,6 +59,10 @@ public class DifyService {
     private final DifySessionService difySessionService;
     private final TechnicalCaseService technicalCaseService;
     private final AssoProjectConfessionService assoProjectConfessionService;
+    @Autowired
+    private CacheUtils cacheUtils;
+    @Autowired
+    private LoginUtils loginUtils;
 
     /**
      * 调用文件系统删除文件接口
@@ -81,7 +88,7 @@ public class DifyService {
      * 调用文件系统删除文件接口
      */
     public String queryHistoryMessage(DifyHistoryMessageDTO difyChatMessageDTO) throws IOException {
-        difyChatMessageDTO.setUser("1");
+        difyChatMessageDTO.setUser(loginUtils.getId().toString());
         OkHttpClient okHttpClient = new OkHttpClient.Builder()
                 .connectTimeout(600, TimeUnit.SECONDS)
                 .writeTimeout(600, TimeUnit.SECONDS)
@@ -99,37 +106,11 @@ public class DifyService {
         return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
     }
 
-    public Flux<String> chatMessageStream(DifyChatMessageDTO difyChatMessageDTO) {
-        Gson gson = new GsonBuilder()
-                .disableHtmlEscaping() // 禁用 HTML 转义
-                .setPrettyPrinting()
-                .create();
-
-        return WebClient.create().post().uri(url + "chat-messages").header("Authorization", "Bearer " + apiKey).header(HttpHeaders.CONTENT_TYPE, "application/json").bodyValue(gson.toJson(difyChatMessageDTO))
-                .retrieve().bodyToFlux(String.class) // 接收原始字节
-                .map(string -> {
-                    String chinese = "";
-                    try {
-                        ObjectMapper mapper = new ObjectMapper();
-                        JsonNode node = mapper.readTree(string);
-                        String prettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
-
-                        chinese = prettyJson;
-                    } catch (Exception e) {
-
-                    }
-                    return chinese;
-                })// 这里指定编码
-                .doOnNext(System.out::println).doFinally(data -> {
-                    System.out.println("数据接收完毕,可以在这里执行后续操作");
-                });
-    }
-
 
     public Flux<String> getOkhttp(ChatMessageDTO chatMessageDTO) {
         Integer projectId = chatMessageDTO.getProjectId();
         String conversationId = chatMessageDTO.getConversationId();
-        String userId = "1";
+        String userId = loginUtils.getId().toString();
         String query = chatMessageDTO.getQuery();
         if (conversationId == null) {
             conversationId = difySessionService.getSessionId(projectId, userId);
@@ -260,7 +241,7 @@ public class DifyService {
     public Map<String, Object> generateInventionPoint(ChatMessageDTO chatMessageDTO) throws IOException {
         Integer projectId = chatMessageDTO.getProjectId();
         String query = "生成发明点";
-        String userId = "1";
+        String userId = loginUtils.getId().toString();
         String conversationId = chatMessageDTO.getConversationId();
         DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
         Map<String, Object> map = new HashMap<>();
@@ -286,10 +267,10 @@ public class DifyService {
         JSONObject jsonObject = JSONObject.parseObject(res);
         String inventionPoint = jsonObject.get("answer").toString();
         inventionPoint = DataUtils.getMarkDownText(inventionPoint);
-        if (conversationId == null) {
-            conversationId = jsonObject.get("conversation_id").toString();
-            difySessionService.addDifySession(projectId, userId, conversationId);
-        }
+//        if (conversationId == null) {
+//            conversationId = jsonObject.get("conversation_id").toString();
+//            difySessionService.addDifySession(projectId, userId, conversationId);
+//        }
         Map<String, Object> map1 = new HashMap<>();
         map1.put("invention_point", inventionPoint);
         technicalCaseService.updateInventionPoint(projectId, inventionPoint);

+ 2 - 2
src/main/resources/application-dev.yml

@@ -68,7 +68,7 @@ PASUrl: http://localhost:8879
 FMSUrl: http://192.168.2.24:8803
 WDUrl: http://1.116.113.26:81
 PythonUrl: http://192.168.2.24:8001
-FileDownloadUrl: https://xsip.cn/api/fileManager/downloadFile?fileId=
+FileDownloadUrl: http://192.168.2.24:8803/fileManager/downloadFile?fileId=
 FileSource: 1
 ES:
   patentVector: patent_vector
@@ -87,4 +87,4 @@ management:
       show-details: always
 DIFY:
   apiKey: app-DDGJt4QUmzlc2aFQ5voOAXIj
-  url: https://ai.xsip.cn/v1
+  url: http://192.168.2.24/v1/