|
@@ -1,23 +1,27 @@
|
|
|
package cn.cslg.pas.service.common;
|
|
|
|
|
|
|
|
|
-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.model.dify.*;
|
|
|
|
|
|
+import cn.cslg.pas.common.model.dify.GenerateClaimDTO;
|
|
|
+import cn.cslg.pas.common.model.dify.confessionSession.AddConfessionSessionDTO;
|
|
|
import cn.cslg.pas.common.model.dify.confessionSession.UpdateConfessionSessionDTO;
|
|
|
import cn.cslg.pas.common.utils.CacheUtils;
|
|
|
import cn.cslg.pas.common.utils.DataUtils;
|
|
|
+import cn.cslg.pas.common.utils.DateUtils;
|
|
|
import cn.cslg.pas.common.utils.LoginUtils;
|
|
|
import cn.cslg.pas.domain.business.TechnicalCase;
|
|
|
import cn.cslg.pas.domain.dify.ConfessionSession;
|
|
|
import cn.cslg.pas.domain.report.AssoProjectConfession;
|
|
|
+import cn.cslg.pas.mapper.dify.ConfessionSessionMapper;
|
|
|
import cn.cslg.pas.service.business.TechnicalCaseService;
|
|
|
import cn.cslg.pas.service.dify.ConfessionSessionService;
|
|
|
import cn.cslg.pas.service.dify.DifySessionService;
|
|
|
import cn.cslg.pas.service.report.AssoProjectConfessionService;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.google.gson.Gson;
|
|
@@ -25,20 +29,21 @@ import com.google.gson.GsonBuilder;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.w3c.dom.DocumentType;
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
|
+import javax.swing.text.Document;
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
@@ -54,10 +59,14 @@ import java.util.concurrent.TimeUnit;
|
|
|
public class DifyService {
|
|
|
@Value("${DIFY.apiKey}")
|
|
|
private String apiKey;
|
|
|
+ @Value("${DIFY.OAApiKey}")
|
|
|
+ private String OAApiKey;
|
|
|
@Value("${DIFY.url}")
|
|
|
private String url;
|
|
|
@Value("${FileDownloadUrl}")
|
|
|
private String fileDownloadUrl;
|
|
|
+ @Value("${DIFY.cliamKey}")
|
|
|
+ private String cliamKey;
|
|
|
private final DifySessionService difySessionService;
|
|
|
private final TechnicalCaseService technicalCaseService;
|
|
|
private final AssoProjectConfessionService assoProjectConfessionService;
|
|
@@ -66,6 +75,8 @@ public class DifyService {
|
|
|
@Autowired
|
|
|
private LoginUtils loginUtils;
|
|
|
private final ConfessionSessionService confessionSessionService;
|
|
|
+ @Autowired
|
|
|
+ private ConfessionSessionMapper confessionSessionMapper;
|
|
|
|
|
|
/**
|
|
|
* 调用文件系统删除文件接口
|
|
@@ -136,7 +147,7 @@ public class DifyService {
|
|
|
inventionPoint = this.getInventPoint(projectId);
|
|
|
} else if (confessionSessionId != null) {
|
|
|
ConfessionSession confessionSession = confessionSessionService.getById(confessionSessionId);
|
|
|
- fileContent = fileDownloadUrl +confessionSession.getGuid();
|
|
|
+ fileContent = fileDownloadUrl + confessionSession.getGuid();
|
|
|
inventionPoint = confessionSession.getInventionPoint();
|
|
|
}
|
|
|
|
|
@@ -268,14 +279,13 @@ public class DifyService {
|
|
|
String conversationId = chatMessageDTO.getConversationId();
|
|
|
DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
- String fileContent="";
|
|
|
- if(confessionSessionId!=null){
|
|
|
- ConfessionSession confessionSession=confessionSessionService.getById(confessionSessionId);
|
|
|
- fileContent=fileDownloadUrl+confessionSession.getGuid();
|
|
|
- }
|
|
|
- else if(projectId!=null) {
|
|
|
- fileContent = this.getConfression(projectId);
|
|
|
- }
|
|
|
+ String fileContent = "";
|
|
|
+ if (confessionSessionId != null) {
|
|
|
+ ConfessionSession confessionSession = confessionSessionService.getById(confessionSessionId);
|
|
|
+ fileContent = fileDownloadUrl + confessionSession.getGuid();
|
|
|
+ } else if (projectId != null) {
|
|
|
+ fileContent = this.getConfression(projectId);
|
|
|
+ }
|
|
|
map.put("file_path", fileContent);
|
|
|
difyChatMessageDTO.setInputs(map);
|
|
|
difyChatMessageDTO.setConversationId(conversationId);
|
|
@@ -311,8 +321,156 @@ public class DifyService {
|
|
|
}
|
|
|
map1.put("conversation_id", conversationId);
|
|
|
|
|
|
- map1.put("projectId",projectId);
|
|
|
- map1.put("confessionSessionId",confessionSessionId);
|
|
|
+ map1.put("projectId", projectId);
|
|
|
+ map1.put("confessionSessionId", confessionSessionId);
|
|
|
return map1;
|
|
|
}
|
|
|
+
|
|
|
+ public List<Object> generateClaimExplain(GenerateClaimDTO generateClaimDTO) throws Exception {
|
|
|
+ String claim = generateClaimDTO.getClaim();
|
|
|
+ DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
|
|
|
+ String userId = loginUtils.getId().toString();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("claim", claim);
|
|
|
+ difyChatMessageDTO.setInputs(map);
|
|
|
+ difyChatMessageDTO.setResponseMode("blocking");
|
|
|
+ difyChatMessageDTO.setUser(userId);
|
|
|
+
|
|
|
+ String param = new Gson().toJson(difyChatMessageDTO);
|
|
|
+ RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
|
|
|
+ OkHttpClient okHttpClient = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(6000, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(6000, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(6000, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url + "workflows/run")
|
|
|
+ .post(requestBody)
|
|
|
+ .addHeader("Authorization", "Bearer " + cliamKey)
|
|
|
+ .build();
|
|
|
+ String res = Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ String dataStr = jsonObject.get("data").toString();
|
|
|
+ JSONObject dataObject = JSONObject.parseObject(dataStr);
|
|
|
+ String outPuts = dataObject.get("outputs").toString();
|
|
|
+ JSONObject jsonObject1 = JSONObject.parseObject(outPuts);
|
|
|
+ Object retsObj= jsonObject1.get("json");
|
|
|
+ String rets = retsObj.toString();
|
|
|
+ List<Object> reObject = JSONArray.parseArray(rets, Object.class);
|
|
|
+ Map<String, Object> addMap = new HashMap<>();
|
|
|
+ Map<String,Object> queryMap =new HashMap<>();
|
|
|
+ queryMap.put("claim",claim);
|
|
|
+ addMap.put("query", queryMap);
|
|
|
+ addMap.put("answer", retsObj);
|
|
|
+ String addStr = JSONObject.toJSONString(addMap);
|
|
|
+ AddConfessionSessionDTO addConfessionSessionDTO = new AddConfessionSessionDTO();
|
|
|
+ addConfessionSessionDTO.setConversationId(jsonObject.get("workflow_run_id").toString());
|
|
|
+ addConfessionSessionDTO.setContent(addStr);
|
|
|
+ String name = DateUtils.dateTimeToStr(new Date());
|
|
|
+ name = name + "-权利要求解释及有益效果";
|
|
|
+ addConfessionSessionDTO.setConversationName(name);
|
|
|
+ addConfessionSessionDTO.setType(1);
|
|
|
+ confessionSessionService.addSession(addConfessionSessionDTO);
|
|
|
+ System.out.println(res);
|
|
|
+ return reObject;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Flux<String> successGetOAHttp(Integer confessionSessionId) {
|
|
|
+ ConfessionSession confessionSession = confessionSessionService.getById(confessionSessionId);
|
|
|
+ String fileUrl = fileDownloadUrl + confessionSession.getGuid();
|
|
|
+ String userId = loginUtils.getId().toString();
|
|
|
+
|
|
|
+ OkHttpClient client = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("fileUrl", fileUrl);
|
|
|
+
|
|
|
+ OAMessageDTO oaMessageDTO = new OAMessageDTO();
|
|
|
+ oaMessageDTO.setInputs(map);
|
|
|
+ oaMessageDTO.setResponseMode("streaming");
|
|
|
+ oaMessageDTO.setUser(userId);
|
|
|
+ oaMessageDTO.setFiles(new ArrayList<>());
|
|
|
+
|
|
|
+ String param = new Gson().toJson(oaMessageDTO);
|
|
|
+ RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url + "workflows/run")
|
|
|
+ .addHeader("Authorization", "Bearer " + OAApiKey)
|
|
|
+ .addHeader(HttpHeaders.CONTENT_TYPE, "application/json")
|
|
|
+ .post(requestBody)
|
|
|
+ .build();
|
|
|
+ return Flux.create(emitter -> {
|
|
|
+ client.newCall(request).enqueue(new Callback() {
|
|
|
+ @Override
|
|
|
+ public void onFailure(Call call, IOException e) {
|
|
|
+ emitter.error(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResponse(Call call, Response response) throws IOException {
|
|
|
+
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ emitter.error(new IOException("Unexpected code: " + response));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try (Reader reader = response.body().charStream()) {
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(reader);
|
|
|
+ String line;
|
|
|
+ String prefixToRemove = "data: ";
|
|
|
+ String runId = "";
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ if (line.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (line.startsWith(prefixToRemove)) {
|
|
|
+ line = line.substring(prefixToRemove.length());
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = JSON.parseObject(line);
|
|
|
+ String workflowRunId = jsonObject.get("workflow_run_id").toString();
|
|
|
+ String event = jsonObject.get("event").toString();
|
|
|
+ if (StringUtils.isNotEmpty(workflowRunId)) {
|
|
|
+ if (StringUtils.isEmpty(runId)) {
|
|
|
+ runId = workflowRunId;
|
|
|
+ confessionSessionMapper.updateSingleField(confessionSessionId, "conversation_id", workflowRunId);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (event.equals("workflow_finished")) {
|
|
|
+ String data = jsonObject.get("data").toString();
|
|
|
+ String content = "";
|
|
|
+ if (StringUtils.isNotEmpty(data)) {
|
|
|
+ JSONObject dataObject = JSON.parseObject(data);
|
|
|
+ String outputs = dataObject.get("outputs").toString();
|
|
|
+ if (StringUtils.isNotEmpty(outputs)) {
|
|
|
+ JSONObject outputsObject = JSON.parseObject(outputs);
|
|
|
+ String output = outputsObject.get("output").toString();
|
|
|
+ content = DataUtils.unicodeDecode(output);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(content)) {
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("answer", content);
|
|
|
+ confessionSessionMapper.updateSingleField(confessionSessionId, "content", obj.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ emitter.next(line); // 将每行数据发送到 Flux
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ emitter.error(e);
|
|
|
+ } finally {
|
|
|
+ emitter.complete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|