ArticleVideoAuditTest.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.tzld.longarticle.recommend.server;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.google.common.collect.Lists;
  5. import com.tzld.longarticle.recommend.server.common.enums.recommend.ArticleCategoryStatusEnum;
  6. import com.tzld.longarticle.recommend.server.mapper.longArticle.ArticleAuditMapper;
  7. import com.tzld.longarticle.recommend.server.model.dto.kimi.KimiResult;
  8. import com.tzld.longarticle.recommend.server.model.entity.aigc.ProducePlan;
  9. import com.tzld.longarticle.recommend.server.model.entity.aigc.ProducePlanExeRecord;
  10. import com.tzld.longarticle.recommend.server.model.entity.longArticle.ArticleCategory;
  11. import com.tzld.longarticle.recommend.server.model.entity.longArticle.LongArticleTitleAudit;
  12. import com.tzld.longarticle.recommend.server.remote.DeepSeekApiService;
  13. import com.tzld.longarticle.recommend.server.repository.aigc.ProducePlanExeRecordRepository;
  14. import com.tzld.longarticle.recommend.server.repository.aigc.ProducePlanRepository;
  15. import com.tzld.longarticle.recommend.server.repository.longArticle.ArticleCategoryRepository;
  16. import com.tzld.longarticle.recommend.server.repository.longArticle.LongArticleTitleAuditRepository;
  17. import com.tzld.longarticle.recommend.server.service.recommend.ArticleCategoryService;
  18. import com.tzld.longarticle.recommend.server.util.TitleSimilarCheckUtil;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.Row;
  22. import org.apache.poi.ss.usermodel.Sheet;
  23. import org.apache.poi.ss.usermodel.Workbook;
  24. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  25. import org.junit.jupiter.api.Test;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.beans.factory.annotation.Value;
  28. import org.springframework.boot.test.context.SpringBootTest;
  29. import org.springframework.util.StringUtils;
  30. import javax.annotation.Resource;
  31. import java.io.FileOutputStream;
  32. import java.io.IOException;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Objects;
  36. import java.util.stream.Collectors;
  37. @SpringBootTest(classes = Application.class)
  38. @Slf4j
  39. public class ArticleVideoAuditTest {
  40. @Resource
  41. private ArticleAuditMapper articleAuditMapper;
  42. @Resource
  43. private LongArticleTitleAuditRepository titleAuditRepository;
  44. @Resource
  45. private ProducePlanExeRecordRepository exeRecordRepository;
  46. @Resource
  47. private ProducePlanRepository producePlanRepository;
  48. @Resource
  49. private ArticleCategoryService articleCategoryService;
  50. @Autowired
  51. ArticleCategoryRepository articleCategoryRepository;
  52. @Autowired
  53. DeepSeekApiService deepSeekApiService;
  54. @Value("${kimiCategoryPrompt:}")
  55. private String kimiCategoryPrompt;
  56. @Test
  57. public void updateTitleAuditFlowPoolLevel() {
  58. List<LongArticleTitleAudit> list = titleAuditRepository.getByFlowPoolLevelIsNull();
  59. List<String> contentIds = list.stream().map(LongArticleTitleAudit::getContentId).collect(Collectors.toList());
  60. List<ProducePlanExeRecord> contentList = exeRecordRepository.findByPlanExeIdIn(contentIds);
  61. Map<String, String> contentPlanMap = contentList.stream().collect(Collectors.toMap(ProducePlanExeRecord::getPlanExeId, ProducePlanExeRecord::getPlanId));
  62. List<String> planIds = contentList.stream().map(ProducePlanExeRecord::getPlanId).collect(Collectors.toList());
  63. List<ProducePlan> planList = producePlanRepository.findByIdIn(planIds);
  64. Map<String, String> planMap = planList.stream().collect(Collectors.toMap(ProducePlan::getId, ProducePlan::getPlanTag));
  65. for (LongArticleTitleAudit titleAudit : list) {
  66. String contentId = titleAudit.getContentId();
  67. String planId = contentPlanMap.get(contentId);
  68. String tag = planMap.get(planId);
  69. if (!StringUtils.hasText(tag)) {
  70. continue;
  71. }
  72. titleAudit.setFlowPoolLevel(tag);
  73. titleAuditRepository.save(titleAudit);
  74. }
  75. articleAuditMapper.updateTitleAuditFlowPoolLevel();
  76. }
  77. @Test
  78. public void ArticleCategoryTest() {
  79. List<ArticleCategory> dealList = articleCategoryRepository.getByStatus(
  80. ArticleCategoryStatusEnum.SUCCESS.getCode());
  81. dealList = dealList.subList(0, 200);
  82. List<List<ArticleCategory>> partitionList = Lists.partition(dealList, 50);
  83. JSONArray result = new JSONArray();
  84. for (List<ArticleCategory> partition : partitionList) {
  85. List<String> partitionTitles = partition.stream().map(ArticleCategory::getTitle).distinct().collect(Collectors.toList());
  86. String prompt = buildKimiPrompt(partitionTitles);
  87. KimiResult kimiResult = deepSeekApiService.requestOfficialApi(prompt, null, null, true);
  88. long now = System.currentTimeMillis();
  89. JSONObject obj = null;
  90. if (kimiResult.isSuccess()) {
  91. try {
  92. obj = JSONObject.parseObject(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
  93. } catch (Exception e) {
  94. log.error(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
  95. }
  96. }
  97. for (ArticleCategory articleCategory : partition) {
  98. JSONObject res = new JSONObject();
  99. res.put("title", articleCategory.getTitle());
  100. res.put("category", articleCategory.getCategory());
  101. articleCategory.setKimiResult(kimiResult.getResponseStr());
  102. articleCategory.setUpdateTimestamp(now);
  103. if (kimiResult.isSuccess() && Objects.nonNull(obj) && obj.containsKey(articleCategory.getTitle())) {
  104. articleCategory.setCategory(obj.getString(articleCategory.getTitle()));
  105. articleCategory.setStatus(ArticleCategoryStatusEnum.SUCCESS.getCode());
  106. } else {
  107. if (Objects.nonNull(obj)) {
  108. for (Map.Entry<String, Object> entry : obj.entrySet()) {
  109. if (TitleSimilarCheckUtil.isSimilar(articleCategory.getTitle(), entry.getKey(),
  110. TitleSimilarCheckUtil.ARTICLE_PROMOTION_THRESHOLD)) {
  111. articleCategory.setCategory(obj.getString(entry.getKey()));
  112. articleCategory.setStatus(ArticleCategoryStatusEnum.SUCCESS.getCode());
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. if (articleCategory.getStatus().equals(ArticleCategoryStatusEnum.SUCCESS.getCode())) {
  119. res.put("newCategory", articleCategory.getCategory());
  120. }
  121. result.add(res);
  122. }
  123. }
  124. Workbook workbook = new XSSFWorkbook();
  125. Sheet sheet = workbook.createSheet("ExampleSheet");
  126. int rowNum = 0;
  127. // 创建标题行
  128. Row titleRow = sheet.createRow(rowNum);
  129. Cell titleCell = titleRow.createCell(0);
  130. titleCell.setCellValue("标题");
  131. titleCell = titleRow.createCell(1);
  132. titleCell.setCellValue("kimi品类");
  133. titleCell = titleRow.createCell(2);
  134. titleCell.setCellValue("deepseek品类");
  135. for (Object obj : result) {
  136. JSONObject jsonObject = (JSONObject) obj;
  137. String title = jsonObject.getString("title");
  138. String category = jsonObject.getString("category");
  139. String newCategory = jsonObject.getString("newCategory");
  140. rowNum++;
  141. Row row = sheet.createRow(rowNum);
  142. Cell cell = row.createCell(0);
  143. cell.setCellValue(title);
  144. cell = row.createCell(1);
  145. cell.setCellValue(category);
  146. cell = row.createCell(2);
  147. cell.setCellValue(newCategory);
  148. }
  149. try (FileOutputStream outputStream = new FileOutputStream("/Users/wangyunpeng/Downloads/category.xlsx")) {
  150. workbook.write(outputStream);
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. } finally {
  154. try {
  155. workbook.close();
  156. } catch (IOException e) {
  157. e.printStackTrace();
  158. }
  159. }
  160. }
  161. private String buildKimiPrompt(List<String> titleList) {
  162. StringBuilder prompt = new StringBuilder(kimiCategoryPrompt);
  163. prompt.append("\n");
  164. for (String title : titleList) {
  165. prompt.append(title).append("\n");
  166. }
  167. return prompt.toString();
  168. }
  169. }