|
@@ -1,7 +1,8 @@
|
|
|
package com.tzld.longarticle.recommend.server.service.recommend.score.strategy;
|
|
|
|
|
|
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
|
|
|
-import com.tzld.longarticle.recommend.server.common.enums.recommend.ArticleTypeEnum;
|
|
|
+import com.tzld.longarticle.recommend.server.model.dto.kimi.KimiResult;
|
|
|
+import com.tzld.longarticle.recommend.server.remote.KimiApiService;
|
|
|
import com.tzld.longarticle.recommend.server.remote.NLPRemoteService;
|
|
|
import com.tzld.longarticle.recommend.server.repository.crawler.ArticleRepository;
|
|
|
import com.tzld.longarticle.recommend.server.service.recommend.score.Score;
|
|
@@ -9,13 +10,18 @@ import com.tzld.longarticle.recommend.server.service.recommend.score.ScoreParam;
|
|
|
import com.tzld.longarticle.recommend.server.service.recommend.score.ScoreStrategy;
|
|
|
import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.RandomUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author dyp
|
|
@@ -28,9 +34,18 @@ public class SimilarityStrategy implements ScoreStrategy {
|
|
|
private NLPRemoteService nlpRemoteService;
|
|
|
@Autowired
|
|
|
private ArticleRepository articleRepository;
|
|
|
+ @Autowired
|
|
|
+ private KimiApiService kimiApiService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
@ApolloJsonValue("${account.score.sim.replace:{}}")
|
|
|
private Map<String, String> accountSimScoreReplaceMap;
|
|
|
+ @Value("${kimiSimilarityTypePrompt:}")
|
|
|
+ private String kimiSimilarityTypePrompt;
|
|
|
+ @ApolloJsonValue("${similarityTypeAccountMap:}")
|
|
|
+ private Map<String, List<String>> similarityTypeAccountMap;
|
|
|
|
|
|
@Override
|
|
|
public List<Score> score(ScoreParam param) {
|
|
@@ -40,27 +55,40 @@ public class SimilarityStrategy implements ScoreStrategy {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
String ghId = param.getGhId();
|
|
|
- if (accountSimScoreReplaceMap.containsKey(ghId)) {
|
|
|
- int historyCount = articleRepository.countByGhIdAndTypeAndItemIndex(ghId,
|
|
|
- ArticleTypeEnum.QUNFA.getVal(), 1);
|
|
|
- if (historyCount < 10) {
|
|
|
+ if (param.getReplaceSimilarityAccount()) {
|
|
|
+ if (accountSimScoreReplaceMap.containsKey(ghId)) {
|
|
|
ghId = accountSimScoreReplaceMap.get(ghId);
|
|
|
+ } else {
|
|
|
+ String redisKey = "AccountSimType_" + param.getAccountName();
|
|
|
+ String type = redisTemplate.opsForValue().get(redisKey);
|
|
|
+ if (!StringUtils.hasText(type)) {
|
|
|
+ String prompt = kimiSimilarityTypePrompt.replace("accountName", param.getAccountName());
|
|
|
+ // 调用kimi判断账号类型
|
|
|
+ KimiResult kimiResult = kimiApiService.requestOfficialApi(prompt, null, null);
|
|
|
+ if (kimiResult.isSuccess()) {
|
|
|
+ try {
|
|
|
+ type = kimiResult.getResponse().getChoices().get(0).getMessage().getContent();
|
|
|
+ log.info("SimilarityStrategy kimiResultType:{}", type);
|
|
|
+ redisTemplate.opsForValue().set(redisKey, type, 2L, TimeUnit.HOURS);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 根据类型随机再类型池中选取账号
|
|
|
+ List<String> ghIds = similarityTypeAccountMap.get(type);
|
|
|
+ if (CollectionUtils.isNotEmpty(ghIds)) {
|
|
|
+ ghId = ghIds.get(RandomUtils.nextInt(0, ghIds.size()));
|
|
|
+ }
|
|
|
+ log.info("SimilarityStrategy 排序账号:{} 替换账号:{}", param.getGhId(), ghId);
|
|
|
}
|
|
|
}
|
|
|
Map<String, Double> scoreMap = nlpRemoteService.score(ghId, param.getAccountName(), param.getContents());
|
|
|
|
|
|
-// double min = scoreMap.values().stream()
|
|
|
-// .min(Double::compareTo)
|
|
|
-// .orElse(0.0);
|
|
|
-// double max = scoreMap.values().stream()
|
|
|
-// .max(Double::compareTo)
|
|
|
-// .orElse(0.0);
|
|
|
-
|
|
|
List<Score> scores = CommonCollectionUtils.toList(param.getContents(), c -> {
|
|
|
Score score = new Score();
|
|
|
score.setContentId(c.getId());
|
|
|
double val = scoreMap.get(c.getId()) == null ? 0.0 : scoreMap.get(c.getId());
|
|
|
- // score.setScore(NormalizationUtils.minMax(val, min, max));
|
|
|
score.setScore(val);
|
|
|
score.setStrategy(this);
|
|
|
return score;
|