|
@@ -0,0 +1,171 @@
|
|
|
+package cn.cslg.pas.factorys.difyFactory;
|
|
|
+
|
|
|
+import cn.cslg.pas.common.model.dify.DifyChatMessageDTO;
|
|
|
+import cn.cslg.pas.common.model.dify.generatePatentResult.PatentFileVO;
|
|
|
+import cn.cslg.pas.common.model.dify.generatePatentResult.PatentResultFactoryDTO;
|
|
|
+import cn.cslg.pas.common.model.enumVos.FluxMessageEventEnum;
|
|
|
+import cn.cslg.pas.common.model.enumVos.PatentResultEnum;
|
|
|
+import cn.cslg.pas.common.utils.ClaimUtils.ClaimSplitUtils;
|
|
|
+import cn.cslg.pas.common.vo.PatentRightParams;
|
|
|
+import cn.cslg.pas.common.vo.RePatentClaim;
|
|
|
+import cn.cslg.pas.domain.dify.ConfessionSession;
|
|
|
+import cn.cslg.pas.factorys.difyFactory.GeneratePatentResultService;
|
|
|
+import cn.cslg.pas.factorys.difyFactory.PatentResultFactory;
|
|
|
+import cn.cslg.pas.service.common.DifyService;
|
|
|
+import cn.cslg.pas.service.dify.AssoConfessionConversationService;
|
|
|
+import cn.cslg.pas.service.dify.ConfessionSessionService;
|
|
|
+import cn.cslg.pas.service.dify.FluxMessageService;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import okhttp3.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import reactor.core.publisher.FluxSink;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.Reader;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class GenerateImplementationMethodImp implements GeneratePatentResultService {
|
|
|
+ @Autowired
|
|
|
+ private DifyService difyService;
|
|
|
+ @Autowired
|
|
|
+ private ConfessionSessionService confessionSessionService;
|
|
|
+ @Autowired
|
|
|
+ private PatentResultFactory patentResultFactory;
|
|
|
+ @Autowired
|
|
|
+ private AssoConfessionConversationService assoConfessionConversationService;
|
|
|
+ @Autowired
|
|
|
+ private FluxMessageService fluxMessageService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void generatePatentResult(PatentResultFactoryDTO patentResultFactoryDTO) {
|
|
|
+ OkHttpClient client = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+ String conversationId = patentResultFactoryDTO.getConversationId();
|
|
|
+ String userId = patentResultFactoryDTO.getUserId();
|
|
|
+ String apiKey = patentResultFactoryDTO.getApiKey();
|
|
|
+ String targetMessageSplit = patentResultFactoryDTO.getTargetMessageSplit();
|
|
|
+ Integer confessionSessionId = patentResultFactoryDTO.getConfessionSessionId();
|
|
|
+ FluxSink emitter = patentResultFactoryDTO.getFluxSink();
|
|
|
+ Integer type = patentResultFactoryDTO.getType();
|
|
|
+
|
|
|
+ ConfessionSession confessionSession = confessionSessionService.getById(confessionSessionId);
|
|
|
+ String discoveryResult = confessionSession.getContent();
|
|
|
+ String result = confessionSession.getResultContent();
|
|
|
+ List<Integer> types = patentResultFactoryDTO.getTypes();
|
|
|
+ DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
|
|
|
+ difyChatMessageDTO.setConversationId(conversationId);
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("discovery_result", discoveryResult);
|
|
|
+ map.put("patent_field", "implementationMethod");
|
|
|
+ if (result != null) {
|
|
|
+ PatentFileVO patentFileVO = JSONObject.parseObject(result, PatentFileVO.class);
|
|
|
+ String claims = patentFileVO.getClaim();
|
|
|
+ PatentRightParams params = new PatentRightParams();
|
|
|
+ params.setCountry("CN");
|
|
|
+ params.setContent(claims);
|
|
|
+ params.setPatentNo("CN");
|
|
|
+ List<RePatentClaim> rePatentClaims = ClaimSplitUtils.formatPatentRight(params);
|
|
|
+ List<String> claimList=rePatentClaims.stream().map(RePatentClaim::getContent).collect(Collectors.toList());
|
|
|
+ String claimJson = com.alibaba.fastjson2.JSON.toJSONString(claimList);
|
|
|
+ map.put("claims", claimJson);
|
|
|
+ }
|
|
|
+ difyChatMessageDTO.setInputs(map);
|
|
|
+ difyChatMessageDTO.setConversationId(conversationId);
|
|
|
+ difyChatMessageDTO.setResponseMode("streaming");
|
|
|
+ difyChatMessageDTO.setUser(userId);
|
|
|
+ difyChatMessageDTO.setQuery("生成具体实施方式");
|
|
|
+ difyChatMessageDTO.setFiles(new ArrayList<>());
|
|
|
+ Request request = difyService.getGeneratePatentResultResultRequest(confessionSessionId, difyChatMessageDTO, apiKey);
|
|
|
+
|
|
|
+
|
|
|
+ StringBuilder aiContentBuilder = new StringBuilder();
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ String filed=PatentResultEnum.implementationMethod.toString();
|
|
|
+ Boolean ifSendMessage = false;
|
|
|
+ String localConversationId = conversationId;
|
|
|
+ String startMessage = fluxMessageService.getFluxMessage(filed, FluxMessageEventEnum.start.toString(), "", confessionSessionId, type - 1);
|
|
|
+ emitter.next(startMessage);
|
|
|
+ try (Reader reader = response.body().charStream()) {
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(reader);
|
|
|
+ String line;
|
|
|
+ String prefixToRemove = "data: ";
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ if (line.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (line.startsWith(prefixToRemove)) {
|
|
|
+ line = line.substring(prefixToRemove.length());
|
|
|
+ JSONObject jsonObject = JSON.parseObject(line);
|
|
|
+ if (localConversationId == null) {
|
|
|
+ localConversationId = jsonObject.get("conversation_id").toString();
|
|
|
+ assoConfessionConversationService.addConversationId(confessionSessionId, localConversationId, 50);
|
|
|
+ difyChatMessageDTO.setConversationId(localConversationId);
|
|
|
+ }
|
|
|
+ String event = jsonObject.get("event").toString();
|
|
|
+
|
|
|
+ if (event.equals("message")) {
|
|
|
+ String answer = jsonObject.get("answer").toString();
|
|
|
+ aiContentBuilder.append(answer);
|
|
|
+ String message = fluxMessageService.getFluxMessage(filed, FluxMessageEventEnum.message.toString(), answer, confessionSessionId, type - 1);
|
|
|
+ emitter.next(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ emitter.error(e);
|
|
|
+ emitter.complete();
|
|
|
+ } finally {
|
|
|
+ }
|
|
|
+ //保存结果
|
|
|
+ PatentFileVO patentFileVO = new PatentFileVO();
|
|
|
+ if (result != null) {
|
|
|
+ patentFileVO = JSONObject.parseObject(result, PatentFileVO.class);
|
|
|
+ }
|
|
|
+ String regex = "<think>.*?</think>";
|
|
|
+ String content=aiContentBuilder.toString();
|
|
|
+ content = content.replaceAll(regex, "");
|
|
|
+ patentFileVO.setImplementationMethod(content);
|
|
|
+ confessionSessionService.updatePatentResult(confessionSession, type, patentFileVO);
|
|
|
+ String message = fluxMessageService.getFluxMessage(filed, FluxMessageEventEnum.end.toString(), "", confessionSessionId, type);
|
|
|
+ emitter.next(message);
|
|
|
+ //继续往下进行任务
|
|
|
+ if (types != null && types.size() > 0) {
|
|
|
+ Integer type = types.remove(0);
|
|
|
+ patentResultFactoryDTO.setTypes(types);
|
|
|
+ GeneratePatentResultService generatePatentResultService = patentResultFactory.getClass(type);
|
|
|
+ generatePatentResultService.generatePatentResult(patentResultFactoryDTO);
|
|
|
+ } else {
|
|
|
+ String allEndMessage = fluxMessageService.getFluxMessage("", FluxMessageEventEnum.all_end.toString(), "", confessionSessionId, type);
|
|
|
+
|
|
|
+ emitter.next(allEndMessage);
|
|
|
+ emitter.complete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+}
|