Commit fc5cac7c authored by alex yao's avatar alex yao

style: 优化function call 逻辑

parent c8c7e4e6
......@@ -3,6 +3,7 @@ package cn.com.poc.agent_application.aggregate;
import cn.com.poc.agent_application.entity.BizAgentApplicationInfoEntity;
import cn.com.poc.agent_application.entity.CreateAgentTitleAndDescEntity;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.Message;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.Tool;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
......@@ -17,9 +18,9 @@ public interface AgentApplicationInfoService {
/**
* 应用预览
*/
String callAgentApplication(String largeModel, String[] unitIds, String agentSystem,
String callAgentApplication(String identifier, String largeModel, String[] unitIds, String agentSystem,
Integer[] knowledgeIds, Integer communicationTurn, Float topP,
List<Message> messages, Boolean isLongMemory, String[] variableStructure, boolean isFunction, String[] functionName, List<String> functionConfig, String useStatus, HttpServletResponse httpServletResponse) throws Exception;
List<Message> messages, List<Tool> tools, HttpServletResponse httpServletResponse) throws Exception;
/**
......
......@@ -40,10 +40,8 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoService {
......@@ -99,65 +97,22 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
@Override
public String callAgentApplication(String largeModel, String[] unitIds, String agentSystem, Integer[] kdIds, Integer communicationTurn, Float topP, List<Message> messages, Boolean isLongMemory, String[] variableStructure, boolean isFunction, String[] functionName, List<String> functionConfig, String useStatus, HttpServletResponse httpServletResponse) throws Exception {
public String callAgentApplication(String identifier, String largeModel, String[] unitIds, String agentSystem, Integer[] kdIds, Integer communicationTurn, Float topP, List<Message> messages, List<Tool> tools, HttpServletResponse httpServletResponse) throws Exception {
logger.info("--------- Call Agent Application large model:{},unitIds:{},agentSystem:{},knowledgeIds:{}" + " communicationTurn:{},topP:{},messages:{}--------------", largeModel, unitIds, agentSystem, kdIds, communicationTurn, topP, messages);
String model = modelConvert(largeModel);
String promptTemplate = buildDialogsPrompt(messages, agentSystem, kdIds);
Tool[] toolArray = tools.toArray(new Tool[0]);
Message[] messageArray = buildMessages(messages, communicationTurn, promptTemplate);
BufferedReader bufferedReader;
String promptTemplate = buildDialogsPrompt(messages, agentSystem, kdIds, toolArray, identifier);
Message[] messageArray = buildMessages(messages, communicationTurn, promptTemplate);
List<String> toolsConfig = null;
// 判断是否开启了function开关
if(isFunction){
// 判断传的是不是完整配置
if(functionConfig != null && !functionConfig.isEmpty()){
toolsConfig = functionConfig;
}else { //使用变量名的方法
toolsConfig = toolsConfig(functionName, variableStructure, isLongMemory);
}
}
// 解析toolConfig,转变成tools
if(toolsConfig != null && !toolsConfig.isEmpty()){
Tool[] tools = new Tool[toolsConfig.size()];
int i = 0;
for (; i < toolsConfig.size(); i++) {
Gson gson = new Gson();
Tool toolConfig = gson.fromJson(toolsConfig.get(i), Tool.class);
tools[i] = new Tool();
tools[i] = toolConfig;
}
bufferedReader = invokeLLM(model, messageArray, topP, tools, useStatus);
}else {
bufferedReader = invokeLLM(model, messageArray, topP, null, useStatus);
}
BufferedReader bufferedReader = invokeLLM(model, messageArray, topP, toolArray, identifier);
return textOutput(httpServletResponse, bufferedReader);
}
private List<String> toolsConfig(String[] functionName, String[] variableStructure, boolean isLongMemory){
List<String> resultConfig = new ArrayList<>();
for (int i = 0; i < functionName.length; i++) {
LargeModelFunctionEnum function = LargeModelFunctionEnum.valueOf(functionName[i]);
List<String> llmConfig = function.getFunction().getLLMConfig();
if(llmConfig != null && !llmConfig.isEmpty()){
resultConfig.add(llmConfig.get(0));
}
}
if(isLongMemory){
resultConfig.add(LargeModelFunctionEnum.valueOf("set_long_memory").getFunction().getLLMConfig().get(0));
resultConfig.add(LargeModelFunctionEnum.valueOf("search_memory_content").getFunction().getLLMConfig().get(0));
}
if (ArrayUtils.isNotEmpty(variableStructure)){
resultConfig.add(LargeModelFunctionEnum.valueOf("set_value_memory").getFunction().getVariableStructureLLMConfig(variableStructure).get(0));
resultConfig.add(LargeModelFunctionEnum.valueOf("search_memory_content_by_enum").getFunction().getVariableStructureLLMConfig(variableStructure).get(0));
}
return resultConfig;
}
@Override
public void createAgentSystem(String input, HttpServletResponse httpServletResponse) throws Exception {
......@@ -227,24 +182,6 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
return largeModelDemandResult.getMessage();
}
private static String buildPreambleConfigSystem(String configSystem, String agentTitle, String agentDesc, String agentSystem) {
//若 name desc prompt 都为空,传入默认参数 name 入参 智能问答机器人 desc 随时为你解答问题
if (StringUtils.isBlank(agentSystem) && StringUtils.isBlank(agentTitle) && StringUtils.isBlank(agentDesc)) {
return configSystem.replace("${agent_title}", "智能问答机器人").replace("${agent_desc}", " 随时为你解答问题");
}
//若 name desc 其中一项不为空则入参 不传入prompt
if (StringUtils.isNotBlank(agentTitle) || (StringUtils.isNotBlank(agentDesc))) {
return configSystem.replace("${agent_title}", StringUtils.isNotBlank(agentTitle) ? agentTitle : StringUtils.EMPTY)
.replace("${agent_desc}", StringUtils.isNotBlank(agentDesc) ? agentDesc : StringUtils.EMPTY);
}
// 若 name desc 都空 prompt 有内容,则把prompt 传入desc
if (StringUtils.isNotBlank(agentSystem)) {
return configSystem.replace("${agent_desc}", agentSystem).replace("${agent_title}", "智能问答机器人");
}
return configSystem;
}
@Override
public List<String> createFeaturedQuestions(String agentTitle, String agentDesc) {
BizAgentApplicationGcConfigEntity configEntity = bizAgentApplicationGcConfigService.getByConfigCode(AgentApplicationGCConfigConstants.AGENT_FEATURED_QUESTIONS);
......@@ -381,7 +318,44 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
return JsonUtils.deSerialize(res.substring(start, end + 1), CreateAgentTitleAndDescEntity.class);
}
private String buildDialogsPrompt(List<Message> messages, String agentSystem, Integer[] kdIds) {
/**
* 构建应用信息提示词
*
* @param configSystem
* @param agentTitle
* @param agentDesc
* @param agentSystem
* @return
*/
private String buildPreambleConfigSystem(String configSystem, String agentTitle, String agentDesc, String agentSystem) {
//若 name desc prompt 都为空,传入默认参数 name 入参 智能问答机器人 desc 随时为你解答问题
if (StringUtils.isBlank(agentSystem) && StringUtils.isBlank(agentTitle) && StringUtils.isBlank(agentDesc)) {
return configSystem.replace("${agent_title}", "智能问答机器人").replace("${agent_desc}", " 随时为你解答问题");
}
//若 name desc 其中一项不为空则入参 不传入prompt
if (StringUtils.isNotBlank(agentTitle) || (StringUtils.isNotBlank(agentDesc))) {
return configSystem.replace("${agent_title}", StringUtils.isNotBlank(agentTitle) ? agentTitle : StringUtils.EMPTY)
.replace("${agent_desc}", StringUtils.isNotBlank(agentDesc) ? agentDesc : StringUtils.EMPTY);
}
// 若 name desc 都空 prompt 有内容,则把prompt 传入desc
if (StringUtils.isNotBlank(agentSystem)) {
return configSystem.replace("${agent_desc}", agentSystem).replace("${agent_title}", "智能问答机器人");
}
return configSystem;
}
/**
* 构建对话提示词
*
* @param messages
* @param agentSystem
* @param kdIds
* @return
*/
private String buildDialogsPrompt(List<Message> messages, String agentSystem, Integer[] kdIds, Tool[] tools, String identifier) {
String promptTemplate = bizAgentApplicationGcConfigService.getByConfigCode(AgentApplicationGCConfigConstants.AGENT_BASE_SYSTEM).getConfigSystem();
promptTemplate = promptTemplate.replace("${agentSystem}", StringUtils.isNotBlank(agentSystem) ? agentSystem : StringUtils.EMPTY);
// 调用知识库
......@@ -397,6 +371,21 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
List<String> knowledgeResults = demandKnowledgeService.searchKnowledge(messages.get(messages.size() - 1).getContent().get(0).getText(), knowledgeIds, 3);
promptTemplate = promptTemplate.replace("${knowledgeResults}", knowledgeResults.toString());
}
// todo 获取记忆
if (ArrayUtils.isNotEmpty(tools)) {
for (Tool tool : tools) {
String name = tool.getFunction().getName();
if ("set_long_memory".equals(name)) {
String searchMemoryContent = LargeModelFunctionEnum.valueOf("search_memory_content").getFunction().doFunction(null, identifier);
promptTemplate = promptTemplate.replace("${longMemory}", searchMemoryContent);
}
if ("set_value_memory".equals(name)) {
// String searchMemoryContent = LargeModelFunctionEnum.valueOf("search_memory_content").getFunction().doFunction(null, identifier);
}
}
}
return promptTemplate;
}
......@@ -409,26 +398,30 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
* @return
* @throws Exception
*/
private BufferedReader invokeLLM(String largeModel, Message[] messageArray, Float topP, Tool[] tools, String useStatus) throws Exception {
private BufferedReader invokeLLM(String largeModel, Message[] messageArray, Float topP, Tool[] tools, String identifier) throws Exception {
LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setModel(largeModel);
largeModelResponse.setMessages(messageArray);
largeModelResponse.setTopP(topP);
largeModelResponse.setStream(true);
largeModelResponse.setUser("POE");
largeModelResponse.setTools(tools);
largeModelResponse.setTool_choice("auto");
if (ArrayUtils.isNotEmpty(tools)) {
largeModelResponse.setTools(tools);
largeModelResponse.setTool_choice("auto");
}
BufferedReader bufferedReader = llmService.chatChunk(largeModelResponse);
String res = "";
bufferedReader.mark(200);
String res = "";
boolean isFunctionCall = false;
String functionResult = null;
StringBuffer finishReason = new StringBuffer();
StringBuffer functionName = new StringBuffer();
StringBuffer functionArguments = new StringBuffer();
boolean isFunctionCall = false;
while ((res = bufferedReader.readLine()) != null) {
if (StringUtils.isBlank(res)) {
continue;
......@@ -464,7 +457,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
if (!functionName.toString().isEmpty() && !functionArguments.toString().isEmpty()) {
// 执行函数返回结果
LargeModelFunctionEnum functionEnum = LargeModelFunctionEnum.valueOf(functionName.toString());
functionResult = functionEnum.getFunction().doFunction(functionArguments.toString(), useStatus);
functionResult = functionEnum.getFunction().doFunction(functionArguments.toString(), identifier);
}
if (functionResult != null) {
......@@ -480,7 +473,16 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
private Message[] buildFunctionMessage(Message[] messageArray, String functionName, String functionArguments, String functionResult){
/**
* 构建function_call消息体
*
* @param messageArray
* @param functionName
* @param functionArguments
* @param functionResult
* @return
*/
private Message[] buildFunctionMessage(Message[] messageArray, String functionName, String functionArguments, String functionResult) {
Message assistantMessage = new Message();
assistantMessage.setRole("assistant");
assistantMessage.setContent(null);
......@@ -508,7 +510,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
functionMessage.setContent(content);
sendMessage[messageArray.length + 1] = functionMessage;
return sendMessage;
return sendMessage;
}
......@@ -556,8 +558,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
* @param promptTemplate
* @return
*/
private static Message[] buildMessages(List<Message> messages, Integer communicationTurn, String
promptTemplate) {
private static Message[] buildMessages(List<Message> messages, Integer communicationTurn, String promptTemplate) {
int messLength = messages.size() - 1;
int skip = communicationTurn * 2;
if (skip < messLength) {
......@@ -592,4 +593,5 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
return largeModelEntity.getModelName();
}
}
......@@ -4,9 +4,9 @@ import cn.com.poc.agent_application.domain.AgentApplicationBaseInfo;
import cn.com.poc.agent_application.domain.AgentApplicationCommConfig;
import cn.com.poc.agent_application.domain.AgentApplicationCommModelConfig;
import cn.com.poc.agent_application.domain.AgentApplicationKnowledgeConfig;
import cn.com.poc.agent_application.model.BizAgentApplicationInfoModel;
import cn.com.poc.agent_application.entity.BizAgentApplicationInfoEntity;
import cn.com.poc.agent_application.dto.AgentApplicationInfoDto;
import cn.com.poc.agent_application.entity.BizAgentApplicationInfoEntity;
import cn.com.poc.agent_application.model.BizAgentApplicationInfoModel;
import cn.com.poc.agent_application.query.AgentApplicationInfoQueryItem;
import cn.com.poc.agent_application.query.PublishAgentApplicationQueryItem;
import cn.com.poc.common.utils.JsonUtils;
......@@ -43,11 +43,7 @@ public class AgentApplicationInfoConvert {
entity.setPreamble(model.getPreamble());
entity.setPublishTime(model.getPublishTime());
entity.setIsLongMemory(model.getIsLongMemory());
entity.setIsFunction(model.getIsFunction());
if(StringUtils.isNotBlank(model.getFunctionName())) {
entity.setFunctionName(JsonUtils.deSerialize(model.getFunctionName(), String[].class));
}
if(StringUtils.isNotBlank(model.getVariableStructure())) {
if (StringUtils.isNotBlank(model.getVariableStructure())) {
entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
}
if (StringUtils.isNotBlank(model.getFeaturedQuestions())) {
......@@ -87,10 +83,6 @@ public class AgentApplicationInfoConvert {
model.setPreamble(entity.getPreamble());
model.setPublishTime(entity.getPublishTime());
model.setIsLongMemory(entity.getIsLongMemory());
model.setIsFunction(entity.getIsFunction());
if (ArrayUtils.isNotEmpty(entity.getFunctionName())) {
model.setFunctionName(JsonUtils.serialize(entity.getFunctionName()));
}
if (ArrayUtils.isNotEmpty(entity.getVariableStructure())) {
model.setVariableStructure(JsonUtils.serialize(entity.getVariableStructure()));
}
......@@ -137,8 +129,7 @@ public class AgentApplicationInfoConvert {
commConfig.setContinuousQuestionTurn(entity.getContinuousQuestionTurn());
commConfig.setVariableStructure(entity.getVariableStructure());
commConfig.setIsLongMemory(entity.getIsLongMemory());
commConfig.setFunctionName(entity.getFunctionName());
commConfig.setIsFunction(entity.getIsFunction());
AgentApplicationKnowledgeConfig knowledgeConfig = new AgentApplicationKnowledgeConfig();
knowledgeConfig.setKnowledgeIds(entity.getKnowledgeIds());
......@@ -184,10 +175,6 @@ public class AgentApplicationInfoConvert {
entity.setVariableStructure(dto.getCommConfig().getVariableStructure());
}
entity.setIsLongMemory(dto.getCommConfig().getIsLongMemory());
if (ObjectUtil.isNotEmpty(dto.getCommConfig().getFunctionName())) {
entity.setFunctionName(dto.getCommConfig().getFunctionName());
}
entity.setIsFunction(dto.getCommConfig().getIsFunction());
}
if (ObjectUtil.isNotEmpty(dto.getKnowledgeConfig())) {
......@@ -221,12 +208,8 @@ public class AgentApplicationInfoConvert {
entity.setPreamble(infoQueryItem.getPreamble());
entity.setPublishTime(infoQueryItem.getPublishTime());
entity.setIsLongMemory(infoQueryItem.getIsLongMemory());
entity.setIsFunction(infoQueryItem.getIsFunction());
if(StringUtils.isNotBlank(infoQueryItem.getFunctionName())) {
entity.setFunctionName(JsonUtils.deSerialize(infoQueryItem.getFunctionName(), String[].class));
}
if(StringUtils.isNotBlank(infoQueryItem.getVariableStructure())) {
if (StringUtils.isNotBlank(infoQueryItem.getVariableStructure())) {
entity.setVariableStructure(JsonUtils.deSerialize(infoQueryItem.getVariableStructure(), String[].class));
}
......
package cn.com.poc.agent_application.convert;
package cn.com.poc.agent_application.convert;
import cn.com.poc.agent_application.domain.AgentApplicationBaseInfo;
import cn.com.poc.agent_application.domain.AgentApplicationCommConfig;
import cn.com.poc.agent_application.domain.AgentApplicationCommModelConfig;
import cn.com.poc.agent_application.domain.AgentApplicationKnowledgeConfig;
import cn.com.poc.agent_application.model.BizAgentApplicationPublishModel;
import cn.com.poc.agent_application.entity.BizAgentApplicationPublishEntity;
import cn.com.poc.agent_application.dto.BizAgentApplicationPublishDto;
import cn.com.poc.agent_application.entity.BizAgentApplicationPublishEntity;
import cn.com.poc.agent_application.model.BizAgentApplicationPublishModel;
import cn.com.poc.common.utils.JsonUtils;
import cn.hutool.core.util.ObjectUtil;
import com.tencent.core.utils.JsonUtil;
......@@ -16,7 +16,7 @@ import org.apache.commons.lang3.StringUtils;
public class BizAgentApplicationPublishConvert {
public static BizAgentApplicationPublishEntity modelToEntity(BizAgentApplicationPublishModel model){
public static BizAgentApplicationPublishEntity modelToEntity(BizAgentApplicationPublishModel model) {
BizAgentApplicationPublishEntity entity = new BizAgentApplicationPublishEntity();
entity.setId(model.getId());
entity.setMemberId(model.getMemberId());
......@@ -27,12 +27,11 @@ public class BizAgentApplicationPublishConvert {
entity.setAgentSystem(model.getAgentSystem());
entity.setPreamble(model.getPreamble());
entity.setIsLongMemory(model.getIsLongMemory());
entity.setIsFunction(model.getIsFunction());
if(StringUtils.isNotBlank(model.getVariableStructure())) {
if (StringUtils.isNotBlank(model.getVariableStructure())) {
entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
}
if(StringUtils.isNotBlank(model.getVariableStructure())) {
if (StringUtils.isNotBlank(model.getVariableStructure())) {
entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
}
if (StringUtils.isNotBlank(model.getFeaturedQuestions())) {
......@@ -59,7 +58,7 @@ public class BizAgentApplicationPublishConvert {
return entity;
}
public static BizAgentApplicationPublishModel entityToModel(BizAgentApplicationPublishEntity entity){
public static BizAgentApplicationPublishModel entityToModel(BizAgentApplicationPublishEntity entity) {
BizAgentApplicationPublishModel model = new BizAgentApplicationPublishModel();
model.setId(entity.getId());
model.setMemberId(entity.getMemberId());
......@@ -70,10 +69,6 @@ public class BizAgentApplicationPublishConvert {
model.setAgentSystem(entity.getAgentSystem());
model.setPreamble(entity.getPreamble());
model.setIsLongMemory(entity.getIsLongMemory());
model.setIsFunction(entity.getIsFunction());
if (ArrayUtils.isNotEmpty(entity.getFunctionName())) {
model.setFunctionName(JsonUtils.serialize(entity.getFunctionName()));
}
if (ArrayUtils.isNotEmpty(entity.getVariableStructure())) {
model.setVariableStructure(JsonUtils.serialize(entity.getVariableStructure()));
}
......@@ -100,8 +95,8 @@ public class BizAgentApplicationPublishConvert {
model.setSysVersion(entity.getSysVersion());
return model;
}
public static BizAgentApplicationPublishDto entityToDto(BizAgentApplicationPublishEntity entity){
public static BizAgentApplicationPublishDto entityToDto(BizAgentApplicationPublishEntity entity) {
BizAgentApplicationPublishDto dto = new BizAgentApplicationPublishDto();
AgentApplicationBaseInfo baseInfo = new AgentApplicationBaseInfo();
baseInfo.setMemberId(entity.getMemberId());
......@@ -121,8 +116,6 @@ public class BizAgentApplicationPublishConvert {
commConfig.setContinuousQuestionTurn(entity.getContinuousQuestionTurn());
commConfig.setVariableStructure(entity.getVariableStructure());
commConfig.setIsLongMemory(entity.getIsLongMemory());
commConfig.setIsFunction(entity.getIsFunction());
commConfig.setFunctionName(entity.getFunctionName());
AgentApplicationKnowledgeConfig knowledgeConfig = new AgentApplicationKnowledgeConfig();
knowledgeConfig.setKnowledgeIds(entity.getKnowledgeIds());
......@@ -143,7 +136,7 @@ public class BizAgentApplicationPublishConvert {
return dto;
}
public static BizAgentApplicationPublishEntity dtoToEntity(BizAgentApplicationPublishDto dto){
public static BizAgentApplicationPublishEntity dtoToEntity(BizAgentApplicationPublishDto dto) {
BizAgentApplicationPublishEntity entity = new BizAgentApplicationPublishEntity();
if (ObjectUtil.isNotEmpty(dto.getBaseInfo())) {
entity.setAgentId(dto.getBaseInfo().getAgentId());
......@@ -165,10 +158,6 @@ public class BizAgentApplicationPublishConvert {
entity.setContinuousQuestionSystem(dto.getCommConfig().getContinuousQuestionSystem());
entity.setContinuousQuestionTurn(dto.getCommConfig().getContinuousQuestionTurn());
entity.setIsLongMemory(dto.getCommConfig().getIsLongMemory());
entity.setIsFunction(dto.getCommConfig().getIsFunction());
if (ObjectUtil.isNotEmpty(dto.getCommConfig().getFunctionName())) {
entity.setFunctionName(dto.getCommConfig().getFunctionName());
}
if (ObjectUtil.isNotEmpty(dto.getCommConfig().getVariableStructure())) {
entity.setVariableStructure(dto.getCommConfig().getVariableStructure());
}
......
......@@ -75,57 +75,32 @@ public class AgentApplicationCommConfig {
this.continuousQuestionTurn = continuousQuestionTurn;
}
/** variable_structure
*变量结构
/**
* variable_structure
* 变量结构
*/
private java.lang.String[] variableStructure;
public java.lang.String[] getVariableStructure(){
public java.lang.String[] getVariableStructure() {
return this.variableStructure;
}
public void setVariableStructure(java.lang.String[] variableStructure){
public void setVariableStructure(java.lang.String[] variableStructure) {
this.variableStructure = variableStructure;
}
/** is_long_memory
*是否开启长期记忆 1、Y 是 2、N 否
/**
* is_long_memory
* 是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isLongMemory;
public java.lang.String getIsLongMemory(){
public java.lang.String getIsLongMemory() {
return this.isLongMemory;
}
public void setIsLongMemory(java.lang.String isLongMemory){
public void setIsLongMemory(java.lang.String isLongMemory) {
this.isLongMemory = isLongMemory;
}
/** function_name
*方法名
*/
private java.lang.String[] functionName;
public java.lang.String[] getFunctionName(){
return this.functionName;
}
public void setFunctionName(java.lang.String[] functionName){
this.functionName = functionName;
}
/** is_function
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isFunction;
public java.lang.String getIsFunction(){
return this.isFunction;
}
public void setIsFunction(java.lang.String isFunction){
this.isFunction = isFunction;
}
}
......@@ -261,32 +261,6 @@ public class BizAgentApplicationInfoEntity {
this.isLongMemory = isLongMemory;
}
/** function_name
*方法名
*/
private java.lang.String[] functionName;
public java.lang.String[] getFunctionName(){
return this.functionName;
}
public void setFunctionName(java.lang.String[] functionName){
this.functionName = functionName;
}
/** is_function
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isFunction;
public java.lang.String getIsFunction(){
return this.isFunction;
}
public void setIsFunction(java.lang.String isFunction){
this.isFunction = isFunction;
}
/** is_deleted
*是否删除 1、Y 是 2、N 否
......
......@@ -260,32 +260,7 @@ public class BizAgentApplicationPublishEntity {
this.isLongMemory = isLongMemory;
}
/** function_name
*方法名
*/
private java.lang.String[] functionName;
public java.lang.String[] getFunctionName(){
return this.functionName;
}
public void setFunctionName(java.lang.String[] functionName){
this.functionName = functionName;
}
/** is_function
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isFunction;
public java.lang.String getIsFunction(){
return this.isFunction;
}
public void setIsFunction(java.lang.String isFunction){
this.isFunction = isFunction;
}
/** is_deleted
*是否删除 1、Y 是 2、N 否
......
......@@ -358,38 +358,6 @@ public class BizAgentApplicationInfoModel extends BaseModelClass implements Seri
this.isLongMemory = isLongMemory;
super.addValidField("isLongMemory");
}
/** function_name
* 方法名
*/
private java.lang.String functionName;
@Column(name = "function_name",length = 2147483647)
public java.lang.String getFunctionName(){
return this.functionName;
}
public void setFunctionName(java.lang.String functionName){
this.functionName = functionName;
super.addValidField("functionName");
}
/** is_function
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isFunction;
@Column(name = "is_function",length = 1)
public java.lang.String getIsFunction(){
return this.isFunction;
}
public void setIsFunction(java.lang.String isFunction){
this.isFunction = isFunction;
super.addValidField("isFunction");
}
/** is_deleted
*是否删除 1、Y 是 2、N 否
......
......@@ -327,40 +327,6 @@ public class BizAgentApplicationPublishModel extends BaseModelClass implements S
super.addValidField("isLongMemory");
}
/** function_name
* 方法名
*/
private java.lang.String functionName;
@Column(name = "function_name",length = 2147483647)
public java.lang.String getFunctionName(){
return this.functionName;
}
public void setFunctionName(java.lang.String functionName){
this.functionName = functionName;
super.addValidField("functionName");
}
/** is_function
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isFunction;
@Column(name = "is_function",length = 1)
public java.lang.String getIsFunction(){
return this.isFunction;
}
public void setIsFunction(java.lang.String isFunction){
this.isFunction = isFunction;
super.addValidField("isFunction");
}
/** is_deleted
*是否删除 1、Y 是 2、N 否
*/
......
package cn.com.poc.agent_application.rest.impl;
import cn.com.poc.agent_application.aggregate.AgentApplicationInfoService;
import cn.com.poc.agent_application.constant.AgentApplicationConstants;
import cn.com.poc.agent_application.convert.AgentApplicationInfoConvert;
import cn.com.poc.agent_application.dto.*;
import cn.com.poc.agent_application.entity.BizAgentApplicationInfoEntity;
......@@ -15,15 +14,13 @@ import cn.com.poc.agent_application.service.BizAgentApplicationPublishService;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.common.utils.ListUtils;
import cn.com.poc.knowledge.aggregate.KnowledgeService;
import cn.com.poc.knowledge.entity.BizKnowledgeInfoEntity;
import cn.com.poc.knowledge.service.BizKnowledgeInfoService;
import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.Tool;
import cn.com.poc.thirdparty.resource.demand.ai.function.LargeModelFunctionEnum;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.com.yict.framemax.data.model.PagingInfo;
import cn.hutool.core.collection.ListUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -32,7 +29,6 @@ import org.springframework.util.Assert;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.ListUI;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
......@@ -146,10 +142,27 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
//获取知识库配置
List<Integer> kdIds = knowledgeService.getKdIdsByKnowledgeInfoIds(infoEntity.getKnowledgeIds());
//配置对话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);
}
//调用应用服务
agentApplicationInfoService.callAgentApplication(infoEntity.getLargeModel(), infoEntity.getUnitIds()
, infoEntity.getAgentSystem(), kdIds.toArray(new Integer[0]), infoEntity.getCommunicationTurn(), infoEntity.getTopP()
, dto.getMessages(), infoEntity.getIsLongMemory().equals(CommonConstant.IsDeleted.Y), infoEntity.getVariableStructure(), infoEntity.getIsFunction().equals(CommonConstant.IsDeleted.Y), infoEntity.getFunctionName(), null , AgentApplicationConstants.USE_AGENT_STATUS.PREVIEW,httpServletResponse);
agentApplicationInfoService.callAgentApplication(agentId, infoEntity.getLargeModel(), infoEntity.getUnitIds()
, infoEntity.getAgentSystem(), kdIds.toArray(new Integer[0]), infoEntity.getCommunicationTurn(), infoEntity.getTopP()
, dto.getMessages(), tools, httpServletResponse);
} catch (Exception e) {
httpServletResponse.setContentType("text/event-stream");
PrintWriter writer = httpServletResponse.getWriter();
......
package cn.com.poc.expose.aggregate.impl;
import cn.com.poc.agent_application.aggregate.AgentApplicationInfoService;
import cn.com.poc.agent_application.constant.AgentApplicationConstants;
import cn.com.poc.agent_application.constant.AgentApplicationDialoguesRecordConstants;
import cn.com.poc.agent_application.constant.AgentApplicationGCConfigConstants;
import cn.com.poc.agent_application.entity.BizAgentApplicationDialoguesRecordEntity;
......@@ -14,16 +13,14 @@ import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.expose.aggregate.AgentApplicationService;
import cn.com.poc.expose.dto.AgentApplicationDto;
import cn.com.poc.expose.rest.AgentApplicationRest;
import cn.com.poc.knowledge.aggregate.KnowledgeService;
import cn.com.poc.knowledge.entity.BizKnowledgeInfoEntity;
import cn.com.poc.knowledge.service.BizKnowledgeInfoService;
import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.Message;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.MultiContent;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.Tool;
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.service.LLMService;
import cn.com.yict.framemax.core.exception.BusinessException;
import com.fasterxml.jackson.core.type.TypeReference;
......@@ -36,7 +33,6 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
......@@ -88,10 +84,26 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
List<Message> messages = new ArrayList<>();
buildMessages(dialogsId, agentId, userBaseEntity.getUserId(), messages, 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);
}
String output = agentApplicationInfoService.callAgentApplication(infoEntity.getLargeModel(), infoEntity.getUnitIds()
, infoEntity.getAgentSystem(), kdIdList.toArray(new Integer[0]), infoEntity.getCommunicationTurn(), infoEntity.getTopP()
, messages, infoEntity.getIsLongMemory().equals(CommonConstant.IsDeleted.Y), infoEntity.getVariableStructure(), infoEntity.getIsFunction().equals(CommonConstant.IsDeleted.Y), infoEntity.getFunctionName(), null, AgentApplicationConstants.USE_AGENT_STATUS.PUBLISH, httpServletResponse);
String output = agentApplicationInfoService.callAgentApplication(dialogsId, infoEntity.getLargeModel(),
infoEntity.getUnitIds(), infoEntity.getAgentSystem(), kdIdList.toArray(new Integer[0]), infoEntity.getCommunicationTurn(),
infoEntity.getTopP(), messages, tools, httpServletResponse);
//保存对话记录
......
......@@ -3,24 +3,18 @@ package cn.com.poc.thirdparty.resource.demand.ai.function;
import java.util.List;
public class AbstractLargeModelFunction {
public abstract class AbstractLargeModelFunction {
public String doFunction(String content, String key){
return null;
}
public abstract String doFunction(String content, String key);
/**
* 获取配置
*/
public List<String> getLLMConfig(){
return null;
}
public abstract List<String> getLLMConfig();
/**
* 获取有关变量的配置
*/
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
return null;
}
public abstract List<String> getVariableStructureLLMConfig(String[] variableStructure);
}
......@@ -35,6 +35,11 @@ public class SearchMemoryContentByNameFunction extends AbstractLargeModelFunctio
return result.toString();
}
@Override
public List<String> getLLMConfig() {
return null;
}
@Override
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
Map<String, Object> config = new HashMap<>();
......
......@@ -62,4 +62,9 @@ public class SearchMemoryContentFunction extends AbstractLargeModelFunction {
resultList.add(jsonString);
return resultList;
}
@Override
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
return null;
}
}
......@@ -23,25 +23,25 @@ public class SetLongMemoryFunction extends AbstractLargeModelFunction {
@Override
public String doFunction(String content, String key) {
// todo 执行保存长期记忆的操作
// 创建 JSONObject 对象
JSONObject jsonObject = new JSONObject(content);
// 提取 content
String contents = jsonObject.getStr("content");
String contentKey = key + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + "longMemory";
Map<Object, Object> hmget = redisService.hmget(contentKey);
Map<String, Object> result = new HashMap<>();
for (Map.Entry<Object, Object> entry : hmget.entrySet()) {
if (entry.getKey() instanceof String) {
// 将 Object 强制转换为 String
String tempKey = (String) entry.getKey();
result.put(tempKey, entry.getValue());
}
// 创建 JSONObject 对象
JSONObject jsonObject = new JSONObject(content);
// 提取 content
String contents = jsonObject.getStr("content");
String contentKey = key + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + "longMemory";
Map<Object, Object> hmget = redisService.hmget(key);
Map<String, Object> result = new HashMap<>();
for (Map.Entry<Object, Object> entry : hmget.entrySet()) {
if (entry.getKey() instanceof String) {
// 将 Object 强制转换为 String
String tempKey = (String) entry.getKey();
result.put(tempKey, entry.getValue());
}
List<String> list = new ArrayList<>();
list.add("timestamp:" + DateUtils.getCurrTime());
list.add("content:" + contents);
result.put(Integer.toString(hmget.size()), list);
redisService.hmset(contentKey, result);
}
List<String> list = new ArrayList<>();
list.add("timestamp:" + DateUtils.getCurrTime());
list.add("content:" + contents);
result.put(Integer.toString(hmget.size()), list);
redisService.hmset(contentKey, result);
return "SUCCESS";
}
......@@ -52,14 +52,14 @@ public class SetLongMemoryFunction extends AbstractLargeModelFunction {
Map<String, Object> content = new HashMap<>();
content.put("type", "string");
content.put("description","内容的详细说明");
content.put("description", "内容的详细说明");
Map<String, Object> properties = new HashMap<>();
properties.put("content", content);
Map<String, Object> parameters = new HashMap<>();
parameters.put("type", "object");
parameters.put("properties",properties);
parameters.put("properties", properties);
List<String> required = new ArrayList<>();
required.add("content");
......@@ -79,6 +79,11 @@ public class SetLongMemoryFunction extends AbstractLargeModelFunction {
List<String> resultList = new ArrayList<>();
resultList.add(jsonString);
return resultList;
return resultList;
}
@Override
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
return null;
}
}
......@@ -18,6 +18,7 @@ public class SetValueMemoryFunction extends AbstractLargeModelFunction {
private RedisService redisService;
@Override
//todo 保存【变量】方法重构
public String doFunction(String content, String key) {
// todo 执行保存变量的操作
// 创建 JSONObject 对象
......@@ -30,6 +31,11 @@ public class SetValueMemoryFunction extends AbstractLargeModelFunction {
return "SUCCESS";
}
@Override
public List<String> getLLMConfig() {
return null;
}
@Override
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
Map<String, Object> config = new HashMap<>();
......@@ -53,7 +59,7 @@ public class SetValueMemoryFunction extends AbstractLargeModelFunction {
properties.put("contentName", contentName);
properties.put("contentValue", contentValue);
parameters.put("properties", properties);
parameters.put("type","object");
parameters.put("type", "object");
function.put("name", "set_value_memory");
function.put("description", "用enum给定的内容名来保存用户想记录的内容值");
......@@ -68,6 +74,6 @@ public class SetValueMemoryFunction extends AbstractLargeModelFunction {
List<String> resultList = new ArrayList<>();
resultList.add(jsonString);
return resultList;
return resultList;
}
}
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