Commit d9e13ce9 authored by alex yao's avatar alex yao

style

parent 51d16f4b
......@@ -12,12 +12,16 @@ public interface AgentApplicationInfoService {
/**
* 创建/更新应用
*
* @param entity 应用信息
*/
BizAgentApplicationInfoEntity saveOrUpdate(BizAgentApplicationInfoEntity entity) throws Exception;
/**
* 更新并发布应用
*
* @param entity 应用信息
*/
boolean updateAndPublish(BizAgentApplicationInfoEntity entity) throws Exception;
......@@ -29,7 +33,7 @@ public interface AgentApplicationInfoService {
boolean deletedAgentApplication(String agentId) throws Exception;
/**
* 应用对话
* Agent应用对话
*
* @param agentId 应用ID
* @param identifier 对话唯一标识
......@@ -50,7 +54,7 @@ public interface AgentApplicationInfoService {
/**
* 应用下架
*
* @param agentId
* @param agentId 应用ID
* @return
*/
boolean unPublish(String agentId) throws Exception;
......@@ -58,7 +62,7 @@ public interface AgentApplicationInfoService {
/**
* 角色指令AI生成
*
* @param input
* @param input 用户输入内容
* @param httpServletResponse
* @return
*/
......
......@@ -2,8 +2,14 @@ package cn.com.poc.agent_application.entity;
public class CreateAgentTitleAndDescEntity {
/**
* 应用名称
*/
private String agentTitle;
/**
* 应用描述
*/
private String agentDesc;
public String getAgentTitle() {
......
......@@ -7,18 +7,19 @@ import cn.com.poc.agent_application.entity.*;
import cn.com.poc.agent_application.query.AgentApplicationInfoQueryCondition;
import cn.com.poc.agent_application.rest.AgentApplicationInfoRest;
import cn.com.poc.agent_application.service.*;
import cn.com.poc.agent_application.utils.AgentApplicationToolsBuilder;
import cn.com.poc.agent_application.utils.AgentApplicationTools;
import cn.com.poc.common.annotation.RedisLimit;
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.knowledge.aggregate.KnowledgeService;
import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool;
import cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.SetLongMemoryConstants;
import cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.AgentLongMemoryEntity;
import cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.LongMemory;
import cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.MemoryVariableWriter;
import cn.com.yict.framemax.core.i18n.I18nMessageException;
import cn.com.yict.framemax.data.model.PagingInfo;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
......@@ -30,7 +31,10 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
......@@ -51,9 +55,6 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
@Resource
private KnowledgeService knowledgeService;
@Resource
private RedisService redisService;
@Resource
private BizMemberAgentApplicationCollectService bizMemberAgentApplicationCollectService;
......@@ -212,7 +213,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
List<Integer> kdIds = knowledgeService.getKdIdsByKnowledgeInfoIds(infoEntity.getKnowledgeIds());
//配置对话function
List<Tool> tools = AgentApplicationToolsBuilder.buildFunctionConfig(infoEntity.getVariableStructure(), infoEntity.getIsLongMemory(), dialogueId, agentId, infoEntity.getUnitIds(), infoEntity.getIsDocumentParsing());
List<Tool> tools = AgentApplicationTools.buildFunctionConfig(infoEntity.getVariableStructure(), infoEntity.getIsLongMemory(), dialogueId, agentId, infoEntity.getUnitIds(), infoEntity.getIsDocumentParsing());
//对话大模型配置
String model = StringUtils.isNotBlank(dto.getModelNickName()) ? dto.getModelNickName() : infoEntity.getLargeModel();
......@@ -340,35 +341,24 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
@Override
public List<AgentLongMemoryDto> getLongMemoryList(String agentId) {
Assert.notNull(agentId);
List<AgentLongMemoryDto> result = new ArrayList<>();
String key = agentId + ":" + agentId;
String contentKey = SetLongMemoryConstants.REDIS_PREFIX + key + ":" + BlContext.getCurrentUserNotException().getUserId().toString();
Map<Object, Object> map = redisService.hmget(contentKey);
Set<Object> keySet = map.keySet();
for (Object mapKey : keySet) {
AgentLongMemoryDto entity = new AgentLongMemoryDto();
entity.setContent(map.get(mapKey).toString());
entity.setTimestamp(mapKey.toString());
result.add(entity);
}
return result.stream().sorted(Comparator.comparing(AgentLongMemoryDto::getTimestamp).reversed()).collect(Collectors.toList());
List<AgentLongMemoryEntity> longMemoryEntities = LongMemory.get(AgentApplicationTools.identifier(agentId, agentId));
return longMemoryEntities.stream().map(value -> {
AgentLongMemoryDto agentLongMemoryDto = new AgentLongMemoryDto();
BeanUtil.copyProperties(value, agentLongMemoryDto);
return agentLongMemoryDto;
}).sorted(Comparator.comparing(AgentLongMemoryDto::getTimestamp).reversed()).collect(Collectors.toList());
}
@Override
public void deleteLongMemoryByKey(String agentId, String timestamp) {
Assert.notNull(agentId);
Assert.notNull(timestamp);
String key = agentId + ":" + agentId;
String contentKey = SetLongMemoryConstants.REDIS_PREFIX + key + ":" + BlContext.getCurrentUserNotException().getUserId().toString();
redisService.hdel(contentKey, timestamp);
LongMemory.delByKey(agentId, timestamp);
}
@Override
public void deleteLongMemory(String agentId) {
Assert.notNull(agentId);
String key = agentId + ":" + agentId;
String contentKey = SetLongMemoryConstants.REDIS_PREFIX + key + ":" + BlContext.getCurrentUserNotException().getUserId().toString();
redisService.del(contentKey);
LongMemory.clean(agentId);
}
}
\ No newline at end of file
......@@ -15,7 +15,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class AgentApplicationToolsBuilder {
public class AgentApplicationTools {
/**
......@@ -39,7 +39,7 @@ public class AgentApplicationToolsBuilder {
tools.add(tool);
//初始化变量函数
Map<Object, Object> map = MemoryVariableWriter.get(identifier + ":" + agentId);
Map<Object, Object> map = MemoryVariableWriter.get(identifier(identifier, agentId));
if (MapUtils.isEmpty(map)) {
List<Variable> variableStructure = variableStructures;
for (Variable variable : variableStructure) {
......@@ -48,7 +48,7 @@ public class AgentApplicationToolsBuilder {
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", key);
jsonObject.put("value", variableDefault);
LargeModelFunctionEnum.valueOf(functionName).getFunction().doFunction(jsonObject.toJSONString(), identifier + ":" + agentId);
LargeModelFunctionEnum.valueOf(functionName).getFunction().doFunction(jsonObject.toJSONString(), identifier(identifier, agentId));
}
}
}
......@@ -83,5 +83,15 @@ public class AgentApplicationToolsBuilder {
return tools;
}
/**
* 创建会话唯一标识符
*
* @param dialogueId 对话ID
* @param agentId 应用ID
* @return
*/
public static String identifier(String dialogueId, String agentId) {
return dialogueId + ":" + agentId;
}
}
......@@ -7,7 +7,7 @@ import java.lang.annotation.*;
/**
* 限流
*/
@Target(ElementType.METHOD)
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Component
@Documented
......@@ -77,7 +77,8 @@ public @interface RedisLimit {
*/
MONTH_OF_YEAR;
LimitTimeUnit(){}
LimitTimeUnit() {
}
}
}
......@@ -6,14 +6,13 @@ import cn.com.poc.agent_application.constant.AgentApplicationGCConfigConstants;
import cn.com.poc.agent_application.entity.BizAgentApplicationDialoguesRecordEntity;
import cn.com.poc.agent_application.entity.BizAgentApplicationGcConfigEntity;
import cn.com.poc.agent_application.entity.BizAgentApplicationPublishEntity;
import cn.com.poc.agent_application.entity.Variable;
import cn.com.poc.agent_application.query.MemberCollectQueryCondition;
import cn.com.poc.agent_application.query.MemberCollectQueryItem;
import cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordService;
import cn.com.poc.agent_application.service.BizAgentApplicationGcConfigService;
import cn.com.poc.agent_application.service.BizAgentApplicationPublishService;
import cn.com.poc.agent_application.service.BizMemberAgentApplicationCollectService;
import cn.com.poc.agent_application.utils.AgentApplicationToolsBuilder;
import cn.com.poc.agent_application.utils.AgentApplicationTools;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.constant.XLangConstant;
import cn.com.poc.common.service.RedisService;
......@@ -22,22 +21,18 @@ import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.expose.aggregate.AgentApplicationService;
import cn.com.poc.knowledge.aggregate.KnowledgeService;
import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import cn.com.poc.thirdparty.resource.demand.ai.constants.LLMRoleEnum;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Message;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.MultiContent;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool;
import cn.com.poc.thirdparty.resource.demand.ai.constants.LLMRoleEnum;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelDemandResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelResponse;
import cn.com.poc.thirdparty.resource.demand.ai.function.LargeModelFunctionEnum;
import cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.MemoryVariableWriter;
import cn.com.poc.thirdparty.service.LLMService;
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.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -46,7 +41,10 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.security.SecureRandom;
import java.util.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
......@@ -54,6 +52,9 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
final private Logger logger = LoggerFactory.getLogger(AgentApplicationService.class);
/**
* 推荐问Key
*/
final private String AGENT_APPLICATION_RECOMMEND_QUESTIONS = "AGENT_APPLICATION_RECOMMEND_QUESTIONS:";
/**
......@@ -103,7 +104,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
if (StringUtils.isBlank(dialogsId)) {
// 用于针对只有单Agent应用分享使用的场景,dialogsId为空
dialogsId = agentId + "_" + userBaseEntity.getUserId();
dialogsId = agentId + "-" + userBaseEntity.getUserId();
}
//获取知识库配置
......@@ -124,10 +125,10 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
bizAgentApplicationDialoguesRecordService.save(inputRecord);
//配置对话function
List<Tool> tools = AgentApplicationToolsBuilder.buildFunctionConfig(infoEntity.getVariableStructure(), infoEntity.getIsLongMemory(), dialogsId, agentId, infoEntity.getUnitIds(), infoEntity.getIsDocumentParsing());
List<Tool> tools = AgentApplicationTools.buildFunctionConfig(infoEntity.getVariableStructure(), infoEntity.getIsLongMemory(), dialogsId, agentId, infoEntity.getUnitIds(), infoEntity.getIsDocumentParsing());
// 记录输出时间戳
//记录输出时间戳
BizAgentApplicationDialoguesRecordEntity outputRecord = new BizAgentApplicationDialoguesRecordEntity();
outputRecord.setRole(AgentApplicationDialoguesRecordConstants.ROLE.ASSISTANT);
outputRecord.setAgentId(infoEntity.getAgentId());
......@@ -255,6 +256,17 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
createENQuestion();
}
@Override
public List<MemberCollectQueryItem> getCollectedApplications(Long memberId, PagingInfo pagingInfo) {
MemberCollectQueryCondition condition = new MemberCollectQueryCondition();
condition.setMemberId(memberId);
condition.setIsCollect(CommonConstant.YOrN.Y);
List<MemberCollectQueryItem> memberCollectQueryItems = bizMemberAgentApplicationCollectService.queryMemberCollect(condition, pagingInfo);
return memberCollectQueryItems;
}
private void createCNQuestion() {
Message message = new Message();
message.setRole(LLMRoleEnum.USER.getRole());
......@@ -308,15 +320,6 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
redisService.lSet(AGENT_APPLICATION_RECOMMEND_QUESTIONS + "en", questions);
}
@Override
public List<MemberCollectQueryItem> getCollectedApplications(Long memberId, PagingInfo pagingInfo) {
MemberCollectQueryCondition condition = new MemberCollectQueryCondition();
condition.setMemberId(memberId);
condition.setIsCollect(CommonConstant.YOrN.Y);
List<MemberCollectQueryItem> memberCollectQueryItems = bizMemberAgentApplicationCollectService.queryMemberCollect(condition, pagingInfo);
return memberCollectQueryItems;
}
private List<Message> buildMessages(String dialogsId, String agentId, Long userId, String input) throws Exception {
List<Message> messages = new ArrayList<>();
BizAgentApplicationDialoguesRecordEntity recordEntity = new BizAgentApplicationDialoguesRecordEntity();
......@@ -351,62 +354,4 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
logger.info("--------- Build Messages dialogsId:{},agentId:{},messages:{}--------------", dialogsId, agentId, messages);
return messages;
}
@Deprecated
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 List<Tool> buildMemoryConfig(BizAgentApplicationPublishEntity infoEntity, String identifier) {
List<Tool> tools = new ArrayList<>();
//开启对话变量
if (CollectionUtils.isNotEmpty(infoEntity.getVariableStructure())) {
String functionName = LargeModelFunctionEnum.memory_variable_writer.name();
String llmConfig = LargeModelFunctionEnum.valueOf(functionName).getFunction().getLLMConfig(infoEntity.getVariableStructure()).get(0);
Tool tool = JsonUtils.deSerialize(llmConfig, Tool.class);
tools.add(tool);
//初始化变量函数
Map<Object, Object> map = MemoryVariableWriter.get(identifier + ":" + infoEntity.getAgentId());
if (MapUtils.isEmpty(map)) {
List<Variable> variableStructure = infoEntity.getVariableStructure();
for (Variable variable : variableStructure) {
String key = variable.getKey();
String variableDefault = variable.getVariableDefault();
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", key);
jsonObject.put("value", variableDefault);
LargeModelFunctionEnum.valueOf(functionName).getFunction().doFunction(jsonObject.toJSONString(), identifier + ":" + infoEntity.getAgentId());
}
}
}
//开启长期记忆
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;
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function.long_memory;
import java.io.Serializable;
import java.util.Date;
public class LongMemoryEntity implements Serializable {
public class AgentLongMemoryEntity implements Serializable {
private String content;
......
package cn.com.poc.thirdparty.resource.demand.ai.function.long_memory;
import cn.com.poc.agent_application.utils.AgentApplicationTools;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.SpringUtils;
......@@ -11,16 +12,16 @@ import java.util.Set;
public class LongMemory {
public static List<LongMemoryEntity> get(String key) {
public static List<AgentLongMemoryEntity> get(String identifier) {
RedisService redisService = SpringUtils.getBean(RedisService.class);
List<LongMemoryEntity> result = new ArrayList<>();
List<AgentLongMemoryEntity> result = new ArrayList<>();
// 查询用户相关信息(什么内容都可以)
String contentKey = SetLongMemoryConstants.REDIS_PREFIX + key + ":" + BlContext.getCurrentUserNotException().getUserId().toString();
String contentKey = SetLongMemoryConstants.REDIS_PREFIX + identifier + ":" + BlContext.getCurrentUserNotException().getUserId().toString();
Map<Object, Object> map = redisService.hmget(contentKey);
Set<Object> keySet = map.keySet();
for (Object mapKey : keySet) {
LongMemoryEntity entity = new LongMemoryEntity();
AgentLongMemoryEntity entity = new AgentLongMemoryEntity();
entity.setContent(map.get(mapKey).toString());
entity.setTimestamp(mapKey.toString());
result.add(entity);
......@@ -28,4 +29,28 @@ public class LongMemory {
return result;
}
/**
* 清除用户在在应用的长期记忆内容
*
* @param agentId
*/
public static void clean(String agentId) {
RedisService redisService = SpringUtils.getBean(RedisService.class);
String key = AgentApplicationTools.identifier(agentId, agentId);
String contentKey = SetLongMemoryConstants.REDIS_PREFIX + key + ":" + BlContext.getCurrentUserNotException().getUserId().toString();
redisService.del(contentKey);
}
/**
* 根据时间戳 删除用户在应用的长期记忆 某个时间保存的内容
*
* @param agentId 应用id
* @param item 时间戳
*/
public static void delByKey(String agentId, String item) {
RedisService redisService = SpringUtils.getBean(RedisService.class);
String key = AgentApplicationTools.identifier(agentId, agentId);
String contentKey = SetLongMemoryConstants.REDIS_PREFIX + key + ":" + BlContext.getCurrentUserNotException().getUserId().toString();
redisService.hdel(contentKey, item);
}
}
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