Commit 2bece1b0 authored by alex yao's avatar alex yao

style: 优化编码

parent 6a719e97
......@@ -87,59 +87,31 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
throw new BusinessException("未找到应用");
}
if (StringUtils.isBlank(dialogsId)) {
// 用于针对只有单Agent应用分享使用的场景,dialogsId为空
dialogsId = agentId + "_" + userBaseEntity.getUserId();
}
//获取知识库配置
List<Integer> kdIdList = knowledgeService.getKdIdsByKnowledgeInfoIds(infoEntity.getKnowledgeIds());
// 构造对话参数
List<Message> messages = new ArrayList<>();
buildMessages(dialogsId, agentId, userBaseEntity.getUserId(), messages, input);
List<Message> messages = buildMessages(dialogsId, agentId, userBaseEntity.getUserId(), input);
//配置对话function
List<Tool> tools = new ArrayList<>();
//开启对话变量
if (ArrayUtils.isNotEmpty(infoEntity.getVariableStructure())) {
String functionName = "set_value_memory";
String llmConfig = LargeModelFunctionEnum.valueOf(functionName).getFunction().getVariableStructureLLMConfig(infoEntity.getVariableStructure()).get(0);
Tool tool = JsonUtils.deSerialize(llmConfig, Tool.class);
tools.add(tool);
}
//开启长期记忆
if (CommonConstant.YOrN.Y.equals(infoEntity.getIsLongMemory())) {
String functionName = "set_long_memory";
String llmConfig = LargeModelFunctionEnum.valueOf(functionName).getFunction().getLLMConfig().get(0);
Tool tool = JsonUtils.deSerialize(llmConfig, Tool.class);
tools.add(tool);
}
List<Tool> tools = buildMemoryConfig(infoEntity);
//对话
String output = agentApplicationInfoService.callAgentApplication(dialogsId, infoEntity.getLargeModel(),
infoEntity.getUnitIds(), infoEntity.getAgentSystem(), kdIdList.toArray(new Integer[0]), infoEntity.getCommunicationTurn(),
infoEntity.getTopP(), infoEntity.getTemperature(), messages, tools, httpServletResponse);
//保存对话记录
// 例句回答时间戳
Long outputTimestamp = System.currentTimeMillis();
BizAgentApplicationDialoguesRecordEntity inputRecord = new BizAgentApplicationDialoguesRecordEntity();
inputRecord.setAgentId(infoEntity.getAgentId());
inputRecord.setMemberId(userBaseEntity.getUserId());
inputRecord.setContent(input);
inputRecord.setDialogsId(dialogsId);
inputRecord.setRole(AgentApplicationDialoguesRecordConstants.ROLE.USER);
inputRecord.setTimestamp(inputTimestamp);
BizAgentApplicationDialoguesRecordEntity outputRecord = new BizAgentApplicationDialoguesRecordEntity();
outputRecord.setRole(AgentApplicationDialoguesRecordConstants.ROLE.ASSISTANT);
outputRecord.setAgentId(infoEntity.getAgentId());
outputRecord.setDialogsId(dialogsId);
outputRecord.setMemberId(userBaseEntity.getUserId());
outputRecord.setContent(output);
outputRecord.setTimestamp(outputTimestamp);
bizAgentApplicationDialoguesRecordService.save(inputRecord);
bizAgentApplicationDialoguesRecordService.save(outputRecord);
saveDialoguesRecord(dialogsId, input, infoEntity, userBaseEntity, inputTimestamp, output);
}
@Override
public List<String> createContinueQuestions(String input) {
BizAgentApplicationGcConfigEntity configEntity = bizAgentApplicationGcConfigService.getByConfigCode(AgentApplicationGCConfigConstants.AGENT_CONTINUE_QUESTIONS);
......@@ -167,12 +139,10 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
}}.toArray(new Message[1]));
largeModelResponse.setTopP(configEntity.getTopP());
largeModelResponse.setUser("POC-CONTINUE-QUESTIONS");
LargeModelDemandResult largeModelDemandResult = llmService.chat(largeModelResponse);
if (largeModelDemandResult == null || !"0".equals(largeModelDemandResult.getCode())) {
logger.error("continue question error ,largeModelDemandResult:{} , largeModelResponse:{}", largeModelDemandResult != null ? largeModelDemandResult.toString() : StringUtils.EMPTY
, largeModelResponse);
// throw new BusinessException("追问失败");
return null;
}
String res = largeModelDemandResult.getMessage();
......@@ -212,6 +182,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
return result;
}
@Override
public void createRecommendQuestion() throws InterruptedException {
List<Object> questions = new CopyOnWriteArrayList<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
......@@ -238,13 +209,16 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
countDownLatch.countDown();
});
}
countDownLatch.await(2, TimeUnit.MINUTES);
if (CollectionUtils.isEmpty(questions)) {
throw new BusinessException("生成推荐问题失败");
}
redisService.del(AGENT_APPLICATION_RECOMMEND_QUESTIONS);
redisService.lSet(AGENT_APPLICATION_RECOMMEND_QUESTIONS, questions);
}
private void buildMessages(String dialogsId, String agentId, Long userId, List<Message> messages, String input) throws Exception {
private List<Message> buildMessages(String dialogsId, String agentId, Long userId, String input) throws Exception {
List<Message> messages = new ArrayList<>();
BizAgentApplicationDialoguesRecordEntity recordEntity = new BizAgentApplicationDialoguesRecordEntity();
recordEntity.setDialogsId(dialogsId);
recordEntity.setMemberId(userId);
......@@ -273,6 +247,49 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
message.setRole(AgentApplicationDialoguesRecordConstants.ROLE.USER);
messages.add(message);
logger.info("--------- Build Messages dialogsId:{},agentId:{},messages:{}--------------", dialogsId, agentId, messages);
return messages;
}
private void saveDialoguesRecord(String dialogsId, String input, BizAgentApplicationPublishEntity infoEntity, UserBaseEntity userBaseEntity, Long inputTimestamp, String output) throws Exception {
// 回答时间戳
Long outputTimestamp = System.currentTimeMillis();
BizAgentApplicationDialoguesRecordEntity inputRecord = new BizAgentApplicationDialoguesRecordEntity();
inputRecord.setAgentId(infoEntity.getAgentId());
inputRecord.setMemberId(userBaseEntity.getUserId());
inputRecord.setContent(input);
inputRecord.setDialogsId(dialogsId);
inputRecord.setRole(AgentApplicationDialoguesRecordConstants.ROLE.USER);
inputRecord.setTimestamp(inputTimestamp);
BizAgentApplicationDialoguesRecordEntity outputRecord = new BizAgentApplicationDialoguesRecordEntity();
outputRecord.setRole(AgentApplicationDialoguesRecordConstants.ROLE.ASSISTANT);
outputRecord.setAgentId(infoEntity.getAgentId());
outputRecord.setDialogsId(dialogsId);
outputRecord.setMemberId(userBaseEntity.getUserId());
outputRecord.setContent(output);
outputRecord.setTimestamp(outputTimestamp);
bizAgentApplicationDialoguesRecordService.save(inputRecord);
bizAgentApplicationDialoguesRecordService.save(outputRecord);
}
private static List<Tool> buildMemoryConfig(BizAgentApplicationPublishEntity infoEntity) {
List<Tool> tools = new ArrayList<>();
//开启对话变量
if (ArrayUtils.isNotEmpty(infoEntity.getVariableStructure())) {
String functionName = LargeModelFunctionEnum.set_value_memory.name();
String llmConfig = LargeModelFunctionEnum.valueOf(functionName).getFunction().getVariableStructureLLMConfig(infoEntity.getVariableStructure()).get(0);
Tool tool = JsonUtils.deSerialize(llmConfig, Tool.class);
tools.add(tool);
}
//开启长期记忆
if (CommonConstant.YOrN.Y.equals(infoEntity.getIsLongMemory())) {
String functionName = LargeModelFunctionEnum.set_long_memory.name();
String llmConfig = LargeModelFunctionEnum.valueOf(functionName).getFunction().getLLMConfig().get(0);
Tool tool = JsonUtils.deSerialize(llmConfig, Tool.class);
tools.add(tool);
}
return tools;
}
}
......@@ -91,7 +91,7 @@ public class KnowledgeServiceImpl implements KnowledgeService {
//获取文件字符数
long charCount = fileContent.length();
//文件字符数不能超过100w
if (charCount > 2 * 10000) {
if (charCount > 100 * 10000) {
throw new BusinessException("文件内容字符数不能超过2w,文件名: " + documentName);
}
......
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