Commit e91da2d1 authored by alex yao's avatar alex yao

style

parent 0d05fe25
......@@ -7,6 +7,7 @@ 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.AgentApplicationUtils;
import cn.com.poc.common.annotation.RedisLimit;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.service.RedisService;
......@@ -207,7 +208,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
Assert.notEmpty(dto.getMessages());
try {
String agentId = dto.getAgentId();
String dialogueId = agentId;
String dialogueId = dto.getAgentId();
BizAgentApplicationInfoEntity infoEntity = bizAgentApplicationInfoService.getByAgentId(agentId);
if (infoEntity == null) {
throw new I18nMessageException("exception/application.does.not.exist");
......@@ -216,36 +217,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
List<Integer> kdIds = knowledgeService.getKdIdsByKnowledgeInfoIds(infoEntity.getKnowledgeIds());
//配置对话function
List<Tool> tools = new ArrayList<>();
//开启对话变量
if (CollectionUtils.isNotEmpty(infoEntity.getVariableStructure())) {
String functionName = LargeModelFunctionEnum.memory_variable_writer.name();
String llmConfig = LargeModelFunctionEnum.valueOf(functionName).getFunction().getVariableStructureLLMConfig(infoEntity.getVariableStructure()).get(0);
Tool tool = JsonUtils.deSerialize(llmConfig, Tool.class);
tools.add(tool);
//初始化变量函数
Map<Object, Object> map = GetMemoryVariable.get(agentId + ":" + agentId);
List<Variable> variableStructure = infoEntity.getVariableStructure();
if (MapUtils.isEmpty(map)) {
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(), agentId + ":" + agentId);
}
}
}
//开启长期记忆
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);
}
List<Tool> tools = AgentApplicationUtils.buildMemoryConfig(infoEntity.getVariableStructure(), infoEntity.getIsLongMemory(), dialogueId, agentId);
//对话大模型配置
String model = StringUtils.isNotBlank(dto.getModelNickName()) ? dto.getModelNickName() : infoEntity.getLargeModel();
......@@ -257,8 +229,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
agentApplicationInfoService.callAgentApplication(agentId, dialogueId, model,
infoEntity.getUnitIds(), agentSystem, kdIds.toArray(new Integer[0]), infoEntity.getCommunicationTurn(), topP,
temperature, dto.getMessages(), tools, httpServletResponse);
} catch (
Exception e) {
} catch (Exception e) {
httpServletResponse.setContentType("text/event-stream");
PrintWriter writer = httpServletResponse.getWriter();
writer.write("data: {\"code\":-1,\"message\":\"" + e.getLocalizedMessage() + "\"} \n\n");
......
package cn.com.poc.agent_application.utils;
import cn.com.poc.agent_application.entity.Variable;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool;
import cn.com.poc.thirdparty.resource.demand.ai.function.LargeModelFunctionEnum;
import cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.GetMemoryVariable;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class AgentApplicationUtils {
/**
* 构造Agent应用 记忆函数配置
*
* @param variableStructures
* @param isLongMemory
* @param identifier
* @param agentId
* @return
*/
public static List<Tool> buildMemoryConfig(List<Variable> variableStructures, String isLongMemory, String identifier, String agentId) {
List<Tool> tools = new ArrayList<>();
//开启对话变量
if (CollectionUtils.isNotEmpty(variableStructures)) {
String functionName = LargeModelFunctionEnum.memory_variable_writer.name();
String llmConfig = LargeModelFunctionEnum.valueOf(functionName).getFunction().getVariableStructureLLMConfig(variableStructures).get(0);
Tool tool = JsonUtils.deSerialize(llmConfig, Tool.class);
tools.add(tool);
//初始化变量函数
Map<Object, Object> map = GetMemoryVariable.get(identifier + ":" + agentId);
if (MapUtils.isEmpty(map)) {
List<Variable> variableStructure = variableStructures;
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 + ":" + agentId);
}
}
}
//开启长期记忆
if (CommonConstant.YOrN.Y.equals(isLongMemory)) {
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;
}
}
......@@ -18,6 +18,14 @@ import java.util.stream.Collectors;
**/
public class SQLUtils {
/**
* 获取批量插入sql和参数
*
* @param modelClass
* @param models
* @return
* @throws Exception
*/
public static BatchInsertResult getInsertSql(Class<? extends BaseModelClass> modelClass, List<? extends BaseModelClass> models) throws Exception {
//获取表名
Table table = modelClass.getAnnotation(Table.class);
......@@ -39,7 +47,7 @@ public class SQLUtils {
tableFieldList.add(column.name());
modelGetMethodList.add(method.getName());
}
BatchInsertResult result = new BatchInsertResult();
//构造insert sql
String fieldStr = tableFieldList.stream().collect(Collectors.joining(","));
StringBuilder insertSQL = new StringBuilder();
......@@ -52,22 +60,23 @@ public class SQLUtils {
}
insertSQL.append(")");
result.setInsertSQL(insertSQL.toString());
//构造实体
List<Object[]> toInserteParams = new ArrayList<>(models.size());
for (BaseModelClass model : models) {
Object[] array = new Object[modelGetMethodList.size()];
int i = 0;
for (String getMethod : modelGetMethodList) {
Method method = modelClass.getMethod(getMethod);
array[i] = method.invoke(model);
i++;
if (models != null) {
List<Object[]> toInserteParams = new ArrayList<>(models.size());
for (BaseModelClass model : models) {
Object[] array = new Object[modelGetMethodList.size()];
int i = 0;
for (String getMethod : modelGetMethodList) {
Method method = modelClass.getMethod(getMethod);
array[i] = method.invoke(model);
i++;
}
toInserteParams.add(array);
}
toInserteParams.add(array);
result.setInsertParams(toInserteParams);
}
BatchInsertResult result = new BatchInsertResult();
result.setInsertSQL(insertSQL.toString());
result.setInsertParams(toInserteParams);
return result;
}
......
......@@ -13,6 +13,7 @@ import cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordSe
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.AgentApplicationUtils;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.constant.XLangConstant;
import cn.com.poc.common.service.RedisService;
......@@ -29,7 +30,6 @@ import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelDema
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.GetMemoryVariable;
import cn.com.poc.thirdparty.resource.demand.ai.function.value_memory.GetValueMemory;
import cn.com.poc.thirdparty.service.LLMService;
import cn.com.yict.framemax.core.i18n.I18nMessageException;
import cn.com.yict.framemax.data.model.PagingInfo;
......@@ -109,12 +109,11 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
//获取知识库配置
List<Integer> kdIdList = knowledgeService.getKdIdsByKnowledgeInfoIds(infoEntity.getKnowledgeIds());
// 构造对话参数
List<Message> messages = buildMessages(dialogsId, agentId, userBaseEntity.getUserId(), input);
//配置对话function
List<Tool> tools = buildMemoryConfig(infoEntity, dialogsId);
List<Tool> tools = AgentApplicationUtils.buildMemoryConfig(infoEntity.getVariableStructure(), infoEntity.getIsLongMemory(), dialogsId, agentId);
// 保存用户输入记录
Long inputTimestamp = System.currentTimeMillis();
......
......@@ -9,6 +9,12 @@ public class UserDialoguesDto implements Serializable {
*/
private java.lang.String dialogsId;
/**
* 应用ID
*/
private java.lang.String agentId;
/**
* 内容
*/
......@@ -30,4 +36,12 @@ public class UserDialoguesDto implements Serializable {
public void setContent(String content) {
this.content = content;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
}
......@@ -185,6 +185,7 @@ public class AgentApplicationRestImpl implements AgentApplicationRest {
result = memberDialoguesQueryItems.stream().map(item -> {
UserDialoguesDto userDialoguesDto = new UserDialoguesDto();
userDialoguesDto.setDialogsId(item.getDialogsId());
userDialoguesDto.setAgentId(item.getAgentId());
String content = item.getContent().length() > 20 ? item.getContent().substring(0, 20) : item.getContent();
userDialoguesDto.setContent(content);
return userDialoguesDto;
......
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