Commit 4dc76bfc authored by alex yao's avatar alex yao

feat:【首页】推荐问 - 针对用户上次推荐不重复

parent 209f514f
...@@ -23,8 +23,11 @@ public interface AgentApplicationService { ...@@ -23,8 +23,11 @@ public interface AgentApplicationService {
/** /**
* [首页] 获取推荐问 * [首页] 获取推荐问
*
* @param xlang 语言 zh-cn 中文简体 en 英文 zh-tw 中文繁体
* @param memberId 用户ID
*/ */
List<String> getRecommendQuestions(String xlang) throws InterruptedException; List<String> getRecommendQuestions(String xlang, Long memberId) throws InterruptedException;
/** /**
* [首页] 生成推荐问 * [首页] 生成推荐问
......
...@@ -54,6 +54,11 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -54,6 +54,11 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
final private String AGENT_APPLICATION_RECOMMEND_QUESTIONS = "AGENT_APPLICATION_RECOMMEND_QUESTIONS:"; final private String AGENT_APPLICATION_RECOMMEND_QUESTIONS = "AGENT_APPLICATION_RECOMMEND_QUESTIONS:";
/**
* 用户获取推荐问题的上一次记录
*/
final private String MEMBER_RECOMMEND_QUESTIONS_LAST = AGENT_APPLICATION_RECOMMEND_QUESTIONS + "MEMBER_LAST:";
@Resource @Resource
private BizMemberAgentApplicationCollectService bizMemberAgentApplicationCollectService; private BizMemberAgentApplicationCollectService bizMemberAgentApplicationCollectService;
...@@ -130,7 +135,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -130,7 +135,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
outputRecord.setTimestamp(System.currentTimeMillis()); outputRecord.setTimestamp(System.currentTimeMillis());
//对话 //对话
String output = agentApplicationInfoService.callAgentApplication(dialogsId, infoEntity.getLargeModel(), String output = agentApplicationInfoService.callAgentApplication(dialogsId, infoEntity.getLargeModel(),
infoEntity.getUnitIds(), infoEntity.getAgentSystem(), kdIdList.toArray(new Integer[0]), infoEntity.getCommunicationTurn(), infoEntity.getUnitIds(), infoEntity.getAgentSystem(), kdIdList.toArray(new Integer[0]), infoEntity.getCommunicationTurn(),
infoEntity.getTopP(), infoEntity.getTemperature(), messages, tools, httpServletResponse); infoEntity.getTopP(), infoEntity.getTemperature(), messages, tools, httpServletResponse);
...@@ -184,10 +189,11 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -184,10 +189,11 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
} }
@Override @Override
public List<String> getRecommendQuestions(String xlang) { public List<String> getRecommendQuestions(String xlang, Long memberId) {
if (StringUtils.isBlank(xlang)) { if (StringUtils.isBlank(xlang)) {
xlang = "zh-cn"; xlang = "zh-cn";
} }
String redisKey = AGENT_APPLICATION_RECOMMEND_QUESTIONS + xlang; String redisKey = AGENT_APPLICATION_RECOMMEND_QUESTIONS + xlang;
if (!redisService.hasKey(redisKey)) { if (!redisService.hasKey(redisKey)) {
...@@ -204,17 +210,32 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -204,17 +210,32 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
return null; return null;
} }
Set<Long> indexSet = new HashSet<>(); // 获取用户上一次的推荐话题ID,并在本次不重复获取该ID
String lastRecommendQuestionRedisKey = MEMBER_RECOMMEND_QUESTIONS_LAST + memberId;
List<Object> lastRecomendIdSet = null;
if (redisService.hasKey(lastRecommendQuestionRedisKey)) {
lastRecomendIdSet = redisService.lGet(lastRecommendQuestionRedisKey, 0, -1);
}
Set<Long> indexSet = new HashSet<>(3);
SecureRandom secureRandom = new SecureRandom(); SecureRandom secureRandom = new SecureRandom();
do { do {
indexSet.add((long) secureRandom.nextInt((int) size)); long id = (long) secureRandom.nextInt((int) size);
if (lastRecomendIdSet != null && lastRecomendIdSet.stream().anyMatch(v -> String.valueOf(v).equals(String.valueOf(id)))) {
continue;
}
indexSet.add(id);
} while (indexSet.size() < 3); } while (indexSet.size() < 3);
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
List<Object> recordId = new ArrayList<>(3);
for (Long index : indexSet) { for (Long index : indexSet) {
recordId.add(index);
Object str = redisService.lGetIndex(redisKey, index); Object str = redisService.lGetIndex(redisKey, index);
result.add(str.toString()); result.add(str.toString());
} }
redisService.del(lastRecommendQuestionRedisKey);
redisService.lSet(lastRecommendQuestionRedisKey, recordId);
return result; return result;
} }
......
...@@ -61,7 +61,8 @@ public class AgentApplicationRestImpl implements AgentApplicationRest { ...@@ -61,7 +61,8 @@ public class AgentApplicationRestImpl implements AgentApplicationRest {
@Override @Override
public List<String> getRecommendQuestions(HttpServletRequest httpServletRequest) throws Exception { public List<String> getRecommendQuestions(HttpServletRequest httpServletRequest) throws Exception {
return agentApplicationService.getRecommendQuestions(httpServletRequest.getHeader("x-lang")); UserBaseEntity userBaseEntity = BlContext.getCurrentUserNotException();
return agentApplicationService.getRecommendQuestions(httpServletRequest.getHeader("x-lang"), userBaseEntity.getUserId());
} }
@Override @Override
......
...@@ -65,6 +65,7 @@ public class AgentApplicationInfoTest { ...@@ -65,6 +65,7 @@ public class AgentApplicationInfoTest {
public void test() { public void test() {
List<Object> list = Lists.newArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9", "1"); List<Object> list = Lists.newArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9", "1");
redisService.lSet("key",list); redisService.lSet("key",list);
// redisService.sSet("key",1);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment