Commit 06124b3c authored by alex yao's avatar alex yao

feat: 首页推荐问

parent a965da2a
package cn.com.poc.agent_application.scheduler;
import cn.com.poc.expose.aggregate.AgentApplicationService;
import cn.com.poc.knowledge.constant.KnowledgeConstant;
import cn.com.poc.knowledge.query.KnowledgeInfosQueryCondition;
import cn.com.poc.knowledge.query.KnowledgeInfosQueryItem;
import cn.com.poc.message.entity.KnowledgeTrainStatusMessage;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
@Component
public class RecommendQuestionScheduler {
@Resource
private AgentApplicationService agentApplicationService;
/**
* 每日凌晨
*
* @throws Exception
*/
@Scheduled(cron = "0 0 0 * * ?")
public void knowledgeInfoStatusUpdateScheduler() throws Exception {
agentApplicationService.createRecommendQuestion();
}
}
...@@ -18,4 +18,16 @@ public interface AgentApplicationService { ...@@ -18,4 +18,16 @@ public interface AgentApplicationService {
*/ */
List<String> createContinueQuestions(String input); List<String> createContinueQuestions(String input);
/**
* [首页] 获取推荐问
*/
List<String> getRecommendQuestions() throws InterruptedException;
/**
* [首页] 生成推荐问
*
* @throws InterruptedException
*/
void createRecommendQuestion() throws InterruptedException;
} }
...@@ -10,6 +10,7 @@ import cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordSe ...@@ -10,6 +10,7 @@ import cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordSe
import cn.com.poc.agent_application.service.BizAgentApplicationGcConfigService; import cn.com.poc.agent_application.service.BizAgentApplicationGcConfigService;
import cn.com.poc.agent_application.service.BizAgentApplicationPublishService; import cn.com.poc.agent_application.service.BizAgentApplicationPublishService;
import cn.com.poc.common.constant.CommonConstant; import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext; import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.JsonUtils; import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.expose.aggregate.AgentApplicationService; import cn.com.poc.expose.aggregate.AgentApplicationService;
...@@ -33,14 +34,21 @@ import org.springframework.stereotype.Service; ...@@ -33,14 +34,21 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@Service @Service
public class AgentApplicationServiceImpl implements AgentApplicationService { public class AgentApplicationServiceImpl implements AgentApplicationService {
final private Logger logger = LoggerFactory.getLogger(AgentApplicationService.class); final private Logger logger = LoggerFactory.getLogger(AgentApplicationService.class);
final private String AGENT_APPLICATION_RECOMMEND_QUESTIONS = "AGENT_APPLICATION_RECOMMEND_QUESTIONS";
@Resource @Resource
private KnowledgeService knowledgeService; private KnowledgeService knowledgeService;
...@@ -59,6 +67,9 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -59,6 +67,9 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
@Resource @Resource
private LLMService llmService; private LLMService llmService;
@Resource
private RedisService redisService;
@Override @Override
public void callAgentApplication(String agentId, String dialogsId, String input, HttpServletResponse httpServletResponse) throws Exception { public void callAgentApplication(String agentId, String dialogsId, String input, HttpServletResponse httpServletResponse) throws Exception {
...@@ -172,6 +183,70 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -172,6 +183,70 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
}.getType()); }.getType());
} }
@Override
public List<String> getRecommendQuestions() throws InterruptedException {
if (!redisService.hasKey(AGENT_APPLICATION_RECOMMEND_QUESTIONS)) {
synchronized (AgentApplicationInfoService.class) {
if (!redisService.hasKey(AGENT_APPLICATION_RECOMMEND_QUESTIONS)) {
createRecommendQuestion();
}
}
}
long size = redisService.lGetListSize(AGENT_APPLICATION_RECOMMEND_QUESTIONS);
if (size < 3) {
redisService.del(AGENT_APPLICATION_RECOMMEND_QUESTIONS);
return null;
}
Set<Long> indexSet = new HashSet<>();
SecureRandom secureRandom = new SecureRandom();
do {
indexSet.add((long) secureRandom.nextInt((int) size));
} while (indexSet.size() < 3);
List<String> result = new ArrayList<>();
for (Long index : indexSet) {
Object str = redisService.lGetIndex(AGENT_APPLICATION_RECOMMEND_QUESTIONS, index);
result.add(str.toString());
}
return result;
}
public void createRecommendQuestion() throws InterruptedException {
List<String> questions = new CopyOnWriteArrayList<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
CountDownLatch countDownLatch = new CountDownLatch(20);
for (int i = 0; i < 20; i++) {
threadPoolExecutor.execute(() -> {
Message message = new Message();
message.setRole(AgentApplicationDialoguesRecordConstants.ROLE.USER);
MultiContent multiContent = new MultiContent();
multiContent.setText("请给我生成一个推荐问题,不超过50字");
multiContent.setType("text");
ArrayList<MultiContent> multiContents = new ArrayList<>();
multiContents.add(multiContent);
message.setContent(multiContents);
Message[] messages = new Message[]{message};
LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setModel("ERNIE-3.5-8K");
largeModelResponse.setMessages(messages);
LargeModelDemandResult largeModelDemandResult = llmService.chat(largeModelResponse);
questions.add(largeModelDemandResult.getMessage());
countDownLatch.countDown();
});
}
countDownLatch.await(2, TimeUnit.MINUTES);
redisService.del(AGENT_APPLICATION_RECOMMEND_QUESTIONS);
for (String question : questions) {
redisService.lSet(AGENT_APPLICATION_RECOMMEND_QUESTIONS, question);
}
}
private void buildMessages(String dialogsId, String agentId, Long userId, List<Message> messages, String input) throws Exception { private void buildMessages(String dialogsId, String agentId, Long userId, List<Message> messages, String input) throws Exception {
BizAgentApplicationDialoguesRecordEntity recordEntity = new BizAgentApplicationDialoguesRecordEntity(); BizAgentApplicationDialoguesRecordEntity recordEntity = new BizAgentApplicationDialoguesRecordEntity();
recordEntity.setDialogsId(dialogsId); recordEntity.setDialogsId(dialogsId);
......
...@@ -20,6 +20,11 @@ import java.util.List; ...@@ -20,6 +20,11 @@ import java.util.List;
@Permission(value = Access.Safety) @Permission(value = Access.Safety)
public interface AgentApplicationRest extends BaseRest { public interface AgentApplicationRest extends BaseRest {
/**
* 【首页】 获取推荐问
*/
List<String> getRecommendQuestions() throws Exception;
/** /**
* 创建对话 * 创建对话
*/ */
......
...@@ -54,6 +54,11 @@ public class AgentApplicationRestImpl implements AgentApplicationRest { ...@@ -54,6 +54,11 @@ public class AgentApplicationRestImpl implements AgentApplicationRest {
@Resource @Resource
private BizAgentApplicationDialoguesRecordService bizAgentApplicationDialoguesRecordService; private BizAgentApplicationDialoguesRecordService bizAgentApplicationDialoguesRecordService;
@Override
public List<String> getRecommendQuestions() throws Exception {
return agentApplicationService.getRecommendQuestions();
}
@Override @Override
public String createDialogues() { public String createDialogues() {
return "DIA_" + UUIDTool.getUUID(); return "DIA_" + UUIDTool.getUUID();
......
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