DifyTest.java 12 KB

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