DifyTest.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package cn.cslg.pas;
  2. import cn.cslg.pas.common.model.dify.ChatMessageDTO;
  3. import cn.cslg.pas.common.model.dify.ClaimExplainVO;
  4. import cn.cslg.pas.common.model.dify.DifyChatMessageDTO;
  5. import cn.cslg.pas.common.model.dify.GenerateInstructAnswerVO;
  6. import cn.cslg.pas.common.utils.ClaimUtils.ClaimSplitUtils;
  7. import cn.cslg.pas.common.utils.FileUtils;
  8. import cn.cslg.pas.common.utils.commonUtils.BusinessUtil;
  9. import cn.cslg.pas.common.vo.PatentRightParams;
  10. import cn.cslg.pas.common.vo.RePatentClaim;
  11. import cn.cslg.pas.domain.dify.GetInstructAnswerDTO;
  12. import cn.cslg.pas.service.common.DifyService;
  13. import cn.cslg.pas.service.dify.GenerateDiscoveryResultService;
  14. import cn.cslg.pas.service.dify.GenerateInstructionService;
  15. import com.alibaba.fastjson.JSON;
  16. import com.alibaba.fastjson.JSONObject;
  17. import jakarta.json.Json;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.apache.poi.ss.usermodel.Cell;
  20. import org.apache.poi.xssf.usermodel.XSSFCell;
  21. import org.apache.poi.xssf.usermodel.XSSFRow;
  22. import org.apache.poi.xssf.usermodel.XSSFSheet;
  23. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  24. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  25. import org.junit.jupiter.api.Test;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.boot.test.context.SpringBootTest;
  28. import java.io.File;
  29. import java.io.FileOutputStream;
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.regex.Matcher;
  35. import java.util.regex.Pattern;
  36. @SpringBootTest
  37. public class DifyTest {
  38. @Autowired
  39. private GenerateInstructionService generateInstructionService;
  40. @Autowired
  41. private DifyService difyService;
  42. @Autowired
  43. private GenerateDiscoveryResultService generateDiscoveryResultService;
  44. @Test
  45. public void checkFile() throws Exception {
  46. // String path = "E:\\temfile\\poi\\test1.docx";
  47. // File file=new File(path);
  48. //
  49. // XWPFDocument doc = poiService.checkPatentFile(path);
  50. // FileOutputStream out = new FileOutputStream("E:\\temfile\\poi\\output2.docx");
  51. // doc.write(out);
  52. // out.close();
  53. // doc.close();
  54. }
  55. @Test
  56. public void checkFile1() throws Exception {
  57. String a = "123";
  58. String b = "456";
  59. String c = "789";
  60. List<String> list2 =new ArrayList<>();
  61. list2.add(a);
  62. list2.add(b);
  63. list2.add(c);
  64. String tem= StringUtils.join(list2, "\\");
  65. String item = "a\\b\\c\\d";
  66. List<String> list = cn.cslg.pas.common.utils.StringUtils.changeStringToString(tem, "\\\\");
  67. System.out.println(list);
  68. }
  69. @Test
  70. public void test() throws Exception {
  71. String path = "E:\\20240716-工作记录文件\\功能文件\\小世系统\\20250421-生成说明书-lrj-v1\\testModel\\claim-7.xlsx";
  72. Integer StartCell = 12;
  73. List<Map<String, Object>> maps = new ArrayList<>();
  74. List<String> claims = new ArrayList<>();
  75. // 打开Excel工作簿文件
  76. XSSFWorkbook workbook = new XSSFWorkbook(path);
  77. // 获取第一个工作表
  78. XSSFSheet sheet = workbook.getSheetAt(0);
  79. // 遍历所有行
  80. for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  81. XSSFRow row = sheet.getRow(i);
  82. XSSFCell cell = row.getCell(0);
  83. if (cell != null) {
  84. String claimValue = cell.getStringCellValue();
  85. if (claimValue != null && !claimValue.trim().equals("")) {
  86. PatentRightParams params = new PatentRightParams();
  87. params.setCountry("CN");
  88. params.setContent(claimValue);
  89. params.setPatentNo("CN");
  90. List<RePatentClaim> rePatentClaims = ClaimSplitUtils.formatPatentRight(params);
  91. String conversationId = null;
  92. for (int t = 0; t < rePatentClaims.size(); t++) {
  93. Map<String, Object> map = new HashMap<>();
  94. RePatentClaim rePatentClaim = rePatentClaims.get(t);
  95. ClaimExplainVO claimExplainVO = null;
  96. String claimContent = rePatentClaim.getContent();
  97. GetInstructAnswerDTO getInstructAnswerDTO = new GetInstructAnswerDTO();
  98. getInstructAnswerDTO.setClaim(claimContent);
  99. getInstructAnswerDTO.setIndex(t + 1);
  100. getInstructAnswerDTO.setType("g&implementation");
  101. if (conversationId != null) {
  102. getInstructAnswerDTO.setConversationId(conversationId);
  103. }
  104. long start = System.currentTimeMillis();
  105. GenerateInstructAnswerVO generateInstructAnswerVO = generateInstructionService.getAnswerFromAI(getInstructAnswerDTO);
  106. long end = System.currentTimeMillis();
  107. generateInstructAnswerVO.getConversionId();
  108. String answer = generateInstructAnswerVO.getAnswer();
  109. if (generateInstructAnswerVO.getConversionId() != null && conversationId == null) {
  110. conversationId = generateInstructAnswerVO.getConversionId();
  111. }
  112. claimExplainVO = JSONObject.parseObject(answer, ClaimExplainVO.class);
  113. long overTime = (end - start) / 1000;
  114. map.put("claimExplainVO", claimExplainVO);
  115. map.put("claim", claimContent);
  116. map.put("overTime", overTime);
  117. maps.add(map);
  118. }
  119. }
  120. }
  121. }
  122. for (int i = 0; i < maps.size(); i++) {
  123. Map<String, Object> map = maps.get(i);
  124. ClaimExplainVO claimExplainVO = (ClaimExplainVO) map.get("claimExplainVO");
  125. String claim = (String) map.get("claim");
  126. long overTime = (long) map.get("overTime");
  127. XSSFRow row = sheet.getRow(i + 1);
  128. if (row == null) {
  129. row = sheet.createRow(i + 1);
  130. }
  131. XSSFCell cell1 = row.getCell(1);
  132. if (cell1 == null) {
  133. cell1 = row.createCell(1);
  134. }
  135. cell1.setCellValue(claim);
  136. XSSFCell cell2 = row.getCell(StartCell);
  137. if (cell2 == null) {
  138. cell2 = row.createCell(StartCell);
  139. }
  140. String jsons = JSONObject.toJSONString(claimExplainVO);
  141. cell2.setCellValue(jsons);
  142. XSSFCell cell3 = row.getCell(StartCell + 1);
  143. if (cell3 == null) {
  144. cell3 = row.createCell(StartCell + 1);
  145. }
  146. cell3.setCellValue(overTime);
  147. }
  148. // 关闭工作簿
  149. String outPath = "E:\\20240716-工作记录文件\\功能文件\\小世系统\\20250421-生成说明书-lrj-v1\\testModel\\claim-2.xlsx";
  150. FileOutputStream out = new FileOutputStream(outPath);
  151. workbook.write(out);
  152. out.flush();
  153. out.close();
  154. workbook.close();
  155. }
  156. public String chatMessage(String cl, String f1, String f2, String f3, String f4, String f5, String f6) throws Exception {
  157. String key = "app-yW12smqvBIGmocUDeeG1B8DF";
  158. Map<String, Object> map = new HashMap<>();
  159. map.put("cl", cl);
  160. map.put("f1", f1);
  161. map.put("f2", f2);
  162. map.put("f3", f3);
  163. map.put("f4", f4);
  164. map.put("f5", f5);
  165. map.put("f6", f6);
  166. DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
  167. difyChatMessageDTO.setUser("1");
  168. difyChatMessageDTO.setResponseMode("blocking");
  169. difyChatMessageDTO.setQuery("123");
  170. difyChatMessageDTO.setInputs(map);
  171. String re = difyService.chatMessage(difyChatMessageDTO, key);
  172. JSONObject jsonObject = JSONObject.parseObject(re);
  173. String answer = jsonObject.get("answer").toString();
  174. return answer;
  175. }
  176. @Test
  177. public void pingfen() throws Exception {
  178. String path = "E:\\20240716-工作记录文件\\功能文件\\小世系统\\20250421-生成说明书-lrj-v1\\testModel\\claim-8.xlsx";
  179. List<Map<String, Object>> maps = new ArrayList<>();
  180. List<String> claims = new ArrayList<>();
  181. // 打开Excel工作簿文件
  182. XSSFWorkbook workbook = new XSSFWorkbook(path);
  183. // 获取第一个工作表
  184. XSSFSheet sheet = workbook.getSheetAt(0);
  185. // 遍历所有行
  186. for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  187. XSSFRow row = sheet.getRow(i);
  188. XSSFCell cell1 = row.getCell(1);
  189. String v1 = cell1.getStringCellValue();
  190. XSSFCell cell2 = row.getCell(2);
  191. String v2 = cell2.getStringCellValue();
  192. XSSFCell cell3 = row.getCell(4);
  193. String v3 = cell3.getStringCellValue();
  194. XSSFCell cell4 = row.getCell(6);
  195. String v4 = cell4.getStringCellValue();
  196. XSSFCell cell5 = row.getCell(8);
  197. String v5 = cell5.getStringCellValue();
  198. XSSFCell cell6 = row.getCell(10);
  199. String v6 = cell6.getStringCellValue();
  200. XSSFCell cell7 = row.getCell(12);
  201. String v7 = cell7.getStringCellValue();
  202. String a = this.chatMessage(v1, v2, v3, v4, v5, v6, v7);
  203. XSSFCell xssfCell8 = row.createCell(14);
  204. xssfCell8.setCellValue(a);
  205. }
  206. String outPath = "E:\\20240716-工作记录文件\\功能文件\\小世系统\\20250421-生成说明书-lrj-v1\\testModel\\claim-2.xlsx";
  207. FileOutputStream out = new FileOutputStream(outPath);
  208. workbook.write(out);
  209. out.flush();
  210. out.close();
  211. workbook.close();
  212. }
  213. @Test
  214. public void TestWord() throws Exception {
  215. Integer start = 10;
  216. String path = "E:\\20240716-工作记录文件\\功能文件\\小世系统\\20250310-技术交底书自动生成-lrj-v1\\question3.xlsx";
  217. // 打开Excel工作簿文件
  218. XSSFWorkbook workbook = new XSSFWorkbook(path);
  219. // 获取第一个工作表
  220. XSSFSheet sheet = workbook.getSheetAt(0);
  221. // 遍历所有行
  222. for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  223. XSSFRow row = sheet.getRow(i);
  224. XSSFCell cell1 = row.getCell(0);
  225. String value = cell1.getStringCellValue();
  226. long a1 = System.currentTimeMillis();
  227. String a = this.chatMessageGetOpt(value);
  228. long b1 = System.currentTimeMillis();
  229. XSSFCell xssfCell8 = row.createCell(start);
  230. xssfCell8.setCellValue(a);
  231. XSSFCell xssfCell9 = row.createCell(start + 1);
  232. long overTime = (b1 - a1) / 1000;
  233. xssfCell9.setCellValue(overTime);
  234. }
  235. String outPath = "E:\\20240716-工作记录文件\\功能文件\\小世系统\\20250310-技术交底书自动生成-lrj-v1\\question4.xlsx";
  236. FileOutputStream out = new FileOutputStream(outPath);
  237. workbook.write(out);
  238. out.flush();
  239. out.close();
  240. workbook.close();
  241. }
  242. public String chatMessageGetOpt(String query) throws Exception {
  243. String key = "app-RJf21elYKkzosEbLQwjl7gqi";
  244. Map<String, Object> map = new HashMap<>();
  245. DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
  246. difyChatMessageDTO.setUser("1");
  247. difyChatMessageDTO.setResponseMode("blocking");
  248. difyChatMessageDTO.setQuery(query);
  249. difyChatMessageDTO.setInputs(map);
  250. String re = difyService.chatMessage(difyChatMessageDTO, key);
  251. JSONObject jsonObject = JSONObject.parseObject(re);
  252. String answer = jsonObject.get("answer").toString();
  253. return answer;
  254. }
  255. @Test
  256. public void test3() {
  257. String reMessage="{\"code\":203,\"data\":{\"app_file\":{\"appNo\":\"202210142766.8\",\"guid\":\"8f996c637fcf4ee49548486958193c22\"},\"modify_file\":{\"guid\":\"\"},\"contrast_file\":[{\"index\":1,\"publicNo\":\"CN112818020A\",\"guid\":\"10556774b810428f8593aa5554a0b854\"},{\"index\":2,\"publicNo\":\"CN109542591A\",\"guid\":\"5ef5eac1fa9847e18283edef6db624be\"}]}}";
  258. JSONObject dataObject = JSON.parseObject(reMessage);
  259. System.out.println(dataObject);
  260. }
  261. }