Commit 5ff4c61d authored by alex yao's avatar alex yao

feat:[首页]推荐问多语言

parent 028a598b
......@@ -24,14 +24,14 @@ public interface AgentApplicationService {
/**
* [首页] 获取推荐问
*/
List<String> getRecommendQuestions() throws InterruptedException;
List<String> getRecommendQuestions(String xlang) throws InterruptedException;
/**
* [首页] 生成推荐问
*
* @throws InterruptedException
*/
void createRecommendQuestion() throws InterruptedException;
void createRecommendQuestion();
/**
* 【首页】 获取用户已收藏的应用列表
......
......@@ -33,9 +33,9 @@ import cn.com.yict.framemax.core.i18n.I18nMessageException;
import cn.com.yict.framemax.data.model.PagingInfo;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.github.houbb.opencc4j.util.ZhConverterUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -45,13 +45,14 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.security.SecureRandom;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class AgentApplicationServiceImpl implements AgentApplicationService {
final private Logger logger = LoggerFactory.getLogger(AgentApplicationService.class);
final private String AGENT_APPLICATION_RECOMMEND_QUESTIONS = "AGENT_APPLICATION_RECOMMEND_QUESTIONS";
final private String AGENT_APPLICATION_RECOMMEND_QUESTIONS = "AGENT_APPLICATION_RECOMMEND_QUESTIONS:";
@Resource
......@@ -183,18 +184,23 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
}
@Override
public List<String> getRecommendQuestions() throws InterruptedException {
if (!redisService.hasKey(AGENT_APPLICATION_RECOMMEND_QUESTIONS)) {
public List<String> getRecommendQuestions(String xlang) {
if (StringUtils.isBlank(xlang)) {
xlang = "zh-cn";
}
String redisKey = AGENT_APPLICATION_RECOMMEND_QUESTIONS + xlang;
if (!redisService.hasKey(redisKey)) {
synchronized (AgentApplicationInfoService.class) {
if (!redisService.hasKey(AGENT_APPLICATION_RECOMMEND_QUESTIONS)) {
if (!redisService.hasKey(redisKey)) {
createRecommendQuestion();
}
}
}
long size = redisService.lGetListSize(AGENT_APPLICATION_RECOMMEND_QUESTIONS);
long size = redisService.lGetListSize(redisKey);
if (size < 3) {
redisService.del(AGENT_APPLICATION_RECOMMEND_QUESTIONS);
redisService.del(redisKey);
return null;
}
......@@ -206,7 +212,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
List<String> result = new ArrayList<>();
for (Long index : indexSet) {
Object str = redisService.lGetIndex(AGENT_APPLICATION_RECOMMEND_QUESTIONS, index);
Object str = redisService.lGetIndex(redisKey, index);
result.add(str.toString());
}
return result;
......@@ -214,7 +220,12 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
@Override
public void createRecommendQuestion() throws InterruptedException {
public void createRecommendQuestion() {
createCNQuestion();
createENQuestion();
}
private void createCNQuestion() {
Message message = new Message();
message.setRole(LLMRoleEnum.USER.getRole());
message.setContent("请你充当一个话题生成器,结合百度热榜数据,用json格式生成15条开放和引导式的推荐话题给我,我需要用于向AI提问问题,参考格式[\"话题内容\",\"话题N内容\"],要求1.避免涉及敏感或争议性过强的话题,以确保问题的中立性和客观性。2.只要求生成话题,不需要旁白");
......@@ -234,8 +245,37 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
List<Object> questions = JsonUtils.deSerialize(res.substring(start, end + 1), new TypeReference<List<Object>>() {
}.getType());
redisService.del(AGENT_APPLICATION_RECOMMEND_QUESTIONS);
redisService.lSet(AGENT_APPLICATION_RECOMMEND_QUESTIONS, questions);
redisService.del(AGENT_APPLICATION_RECOMMEND_QUESTIONS + "zh-cn");
redisService.lSet(AGENT_APPLICATION_RECOMMEND_QUESTIONS + "zh-cn", questions);
List<Object> traditionalQuestion = questions.stream().map(q -> ZhConverterUtil.convertToTraditional(q.toString())).collect(Collectors.toList());
redisService.del(AGENT_APPLICATION_RECOMMEND_QUESTIONS + "zh-tw");
redisService.lSet(AGENT_APPLICATION_RECOMMEND_QUESTIONS + "zh-tw", traditionalQuestion);
}
private void createENQuestion() {
Message message = new Message();
message.setRole(LLMRoleEnum.USER.getRole());
message.setContent("\n" +
"Please act as a topic generator, combined with Baidu hot list data, generate 15 open and guided recommended topics for me in json format, I need to ask AI questions, refer to the format [\" Topic content \",\" Topic N content \"], requirement 1.2. Only topic generation is required, no narration is required,user english");
Message[] messages = new Message[]{message};
LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setModel("ERNIE-4.0-8K");
largeModelResponse.setMessages(messages);
LargeModelDemandResult largeModelDemandResult = llmService.chat(largeModelResponse);
if (largeModelResponse == null || !"0".equals(largeModelDemandResult.getCode())) {
throw new I18nMessageException("exception/failed.to.generate.recommendation.question");
}
String res = largeModelDemandResult.getMessage();
int start = res.lastIndexOf("[");
int end = res.lastIndexOf("]");
List<Object> questions = JsonUtils.deSerialize(res.substring(start, end + 1), new TypeReference<List<Object>>() {
}.getType());
redisService.del(AGENT_APPLICATION_RECOMMEND_QUESTIONS + "en");
redisService.lSet(AGENT_APPLICATION_RECOMMEND_QUESTIONS + "en", questions);
}
@Override
......
......@@ -11,6 +11,7 @@ import cn.com.yict.framemax.web.permission.Permission;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
......@@ -20,7 +21,7 @@ public interface AgentApplicationRest extends BaseRest {
/**
* 【首页】 获取推荐问
*/
List<String> getRecommendQuestions() throws Exception;
List<String> getRecommendQuestions(HttpServletRequest httpServletRequest) throws Exception;
/**
* 【首页】获取用户已收藏应用
......
......@@ -26,6 +26,7 @@ import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
......@@ -50,8 +51,8 @@ public class AgentApplicationRestImpl implements AgentApplicationRest {
private BizAgentApplicationDialoguesRecordService bizAgentApplicationDialoguesRecordService;
@Override
public List<String> getRecommendQuestions() throws Exception {
return agentApplicationService.getRecommendQuestions();
public List<String> getRecommendQuestions(HttpServletRequest httpServletRequest) throws Exception {
return agentApplicationService.getRecommendQuestions(httpServletRequest.getHeader("x-lang"));
}
@Override
......
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