Commit fb52d923 authored by jennie chen's avatar jennie chen

1.重新定义function

2.优化部分代码细节
parent 5e486990
...@@ -19,7 +19,7 @@ public interface AgentApplicationInfoService { ...@@ -19,7 +19,7 @@ public interface AgentApplicationInfoService {
*/ */
String callAgentApplication(String largeModel, String[] unitIds, String agentSystem, String callAgentApplication(String largeModel, String[] unitIds, String agentSystem,
String[] knowledgeIds, Integer communicationTurn, Float topP, String[] knowledgeIds, Integer communicationTurn, Float topP,
List<Message> messages, Boolean isLongMemory, String[] variableStructure, String useStatus, HttpServletResponse httpServletResponse) throws Exception; List<Message> messages, Boolean isLongMemory, String[] variableStructure, boolean isFunction, String[] functionName, List<String> functionConfig, String useStatus, HttpServletResponse httpServletResponse) throws Exception;
/** /**
......
...@@ -26,6 +26,7 @@ import cn.com.poc.thirdparty.service.LLMService; ...@@ -26,6 +26,7 @@ import cn.com.poc.thirdparty.service.LLMService;
import cn.com.yict.framemax.core.exception.BusinessException; import cn.com.yict.framemax.core.exception.BusinessException;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.Gson;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -90,69 +91,63 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ ...@@ -90,69 +91,63 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
} }
@Override @Override
public String callAgentApplication(String largeModel, String[] unitIds, String agentSystem, String[] knowledgeIds, Integer communicationTurn, Float topP, List<Message> messages, Boolean isLongMemory, String[] variableStructure, String useStatus, HttpServletResponse httpServletResponse) throws Exception { public String callAgentApplication(String largeModel, String[] unitIds, String agentSystem, String[] 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 {
logger.info("--------- Call Agent Application large model:{},unitIds:{},agentSystem:{},knowledgeIds:{}" + " communicationTurn:{},topP:{},messages:{}--------------", largeModel, unitIds, agentSystem, knowledgeIds, communicationTurn, topP, messages); logger.info("--------- Call Agent Application large model:{},unitIds:{},agentSystem:{},knowledgeIds:{}" + " communicationTurn:{},topP:{},messages:{}--------------", largeModel, unitIds, agentSystem, knowledgeIds, communicationTurn, topP, messages);
String promptTemplate = buildDialogsPrompt(messages, agentSystem, knowledgeIds); String promptTemplate = buildDialogsPrompt(messages, agentSystem, knowledgeIds);
Message[] messageArray = buildMessages(messages, communicationTurn, promptTemplate); Message[] messageArray = buildMessages(messages, communicationTurn, promptTemplate);
BufferedReader bufferedReader = this.invokeLLM(largeModel, messageArray, topP, toolsConfig(variableStructure, isLongMemory), useStatus, isLongMemory); BufferedReader bufferedReader;
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(largeModel, messageArray, topP, tools, useStatus);
}else {
bufferedReader = invokeLLM(largeModel, messageArray, topP, null, useStatus);
}
textOutput(httpServletResponse, bufferedReader); textOutput(httpServletResponse, bufferedReader);
return null; return null;
} }
private Tool[] toolsConfig(String[] variableStructure, boolean isLongMemory){ private List<String> toolsConfig(String[] functionName, String[] variableStructure, boolean isLongMemory){
// 配置tools List<String> resultConfig = new ArrayList<>();
Tool[] tools = new Tool[AgentApplicationConstants.TOOLS.FUNCTION_NUMBER]; for (int i = 0; i < functionName.length; i++) {
tools[0] = new Tool(); LargeModelFunctionEnum function = LargeModelFunctionEnum.valueOf(functionName[i]);
tools[0].setType("function"); List<String> llmConfig = function.getFunction().getLLMConfig();
// 根据变量名保存记忆方法 if(llmConfig != null && !llmConfig.isEmpty()){
AbstractLargeModelFunction function = LargeModelFunctionEnum.valueOf("setValueMemory").getFunction(); resultConfig.add(llmConfig.get(0));
Map<String, Object> parameters = function.getParameters(); }
Map<String, Object> contentName = new HashMap<>(); }
contentName.put("type", "string"); if(isLongMemory){
contentName.put("description", "内容名"); resultConfig.add(LargeModelFunctionEnum.valueOf("set_long_memory").getFunction().getLLMConfig().get(0));
contentName.put("enum", variableStructure); // 设置变量 resultConfig.add(LargeModelFunctionEnum.valueOf("search_memory_content").getFunction().getLLMConfig().get(0));
Map<String, Object> contentValue = new HashMap<>();
contentValue.put("type", "string");
contentValue.put("description", "内容值");
Map<String, Object> properties = new HashMap<>();
properties.put("contentName", contentName);
properties.put("contentValue", contentValue);
parameters.put("properties", properties);
parameters.put("type","object");
function.setParameters(parameters);
tools[0].setFunction(function);
// 根据变量名查询记忆方法
AbstractLargeModelFunction searchFunction = LargeModelFunctionEnum.valueOf("search_memory_content_by_Enum").getFunction();
Map<String, Object> searchParameters = function.getParameters();
Map<String, Object> content = new HashMap<>();
content.put("type", "string");
content.put("description", "内容名");
content.put("enum", variableStructure); // 设置变量
Map<String, Object> searchProperties = new HashMap<>();
searchProperties.put("content", content);
searchParameters.put("properties", searchProperties);
searchParameters.put("type","object");
searchFunction.setParameters(searchParameters);
tools[1] = new Tool();
tools[1].setType("function");
tools[1].setFunction(searchFunction);
//todo 判断是否开启长期记忆的按钮,生成tools配置
if (isLongMemory) { //如果开启了
tools[2] = new Tool();
tools[2].setType("function");
tools[2].setFunction(LargeModelFunctionEnum.valueOf("searchMemoryContent").getFunction());
tools[3] = new Tool();
tools[3].setType("function");
tools[3].setFunction(LargeModelFunctionEnum.valueOf("setLongMemory").getFunction());
} }
return tools; 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 @Override
...@@ -397,7 +392,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ ...@@ -397,7 +392,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
* @return * @return
* @throws Exception * @throws Exception
*/ */
private BufferedReader invokeLLM(String largeModel, Message[] messageArray, Float topP, Tool[] tools, String useStatus, Boolean isLongMemory) throws Exception { private BufferedReader invokeLLM(String largeModel, Message[] messageArray, Float topP, Tool[] tools, String useStatus) throws Exception {
LargeModelResponse largeModelResponse = new LargeModelResponse(); LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setModel(largeModel); largeModelResponse.setModel(largeModel);
largeModelResponse.setMessages(messageArray); largeModelResponse.setMessages(messageArray);
...@@ -406,16 +401,10 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ ...@@ -406,16 +401,10 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
largeModelResponse.setUser("POE"); largeModelResponse.setUser("POE");
largeModelResponse.setTools(tools); largeModelResponse.setTools(tools);
largeModelResponse.setTool_choice("auto"); largeModelResponse.setTool_choice("auto");
BufferedReader bufferedReader = llmService.chatChunk(largeModelResponse); BufferedReader bufferedReader = llmService.chatChunk(largeModelResponse);
String res = ""; String res = "";
bufferedReader.mark(200); bufferedReader.mark(200);
LargeModelResponse modelResponse = new LargeModelResponse();
modelResponse.setModel(largeModel);
modelResponse.setTopP(topP);
modelResponse.setStream(true);
modelResponse.setUser("POE");
modelResponse.setTools(tools);
modelResponse.setTool_choice("auto");
String functionResult = null; String functionResult = null;
StringBuffer finishReason = new StringBuffer(); StringBuffer finishReason = new StringBuffer();
StringBuffer functionName = new StringBuffer(); StringBuffer functionName = new StringBuffer();
...@@ -457,41 +446,15 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ ...@@ -457,41 +446,15 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
// 如果是function_call,则处理function // 如果是function_call,则处理function
if (!functionName.toString().isEmpty() && !functionArguments.toString().isEmpty()) { if (!functionName.toString().isEmpty() && !functionArguments.toString().isEmpty()) {
// 执行函数返回结果 // 执行函数返回结果
ExecuteLargeModelFunctionEnum functionEnum = ExecuteLargeModelFunctionEnum.valueOf(functionName.toString()); LargeModelFunctionEnum functionEnum = LargeModelFunctionEnum.valueOf(functionName.toString());
AbstractExecuteLargeModelFunction executeFunction = functionEnum.getExecuteFunction(); functionResult = functionEnum.getFunction().doFunction(functionArguments.toString(), useStatus);
functionResult = executeFunction.doMemoryFunction(functionArguments.toString(), useStatus, isLongMemory);
} }
if (functionResult != null) { if (functionResult != null) {
// 处理function - 1,获取function_call 2. 调用对应function 3. 返回function_call并且调用LLM 4.获取bufferedReader 返回 // 处理function - 1,获取function_call 2. 调用对应function 3. 返回function_call并且调用LLM 4.获取bufferedReader 返回
Message assistantMessage = new Message(); Message[] sendMessage = buildFunctionMessage(messageArray, functionName.toString(), functionArguments.toString(), functionResult);
assistantMessage.setRole("assistant"); largeModelResponse.setMessages(sendMessage);
assistantMessage.setContent(null); return llmService.chatChunk(largeModelResponse);
List<FunctionCall> functionCalls = new ArrayList<>();
FunctionCall functionCall = new FunctionCall();
functionCall.setName(functionName.toString());
functionCall.setArguments(functionArguments.toString());
functionCalls.add(functionCall);
assistantMessage.setFunction_call(functionCalls);
Message[] sendMessage = new Message[messageArray.length + 2];
for (int i = 0; i < messageArray.length; i++) {
sendMessage[i] = messageArray[i];
}
sendMessage[messageArray.length] = assistantMessage;
Message functionMessage = new Message();
functionMessage.setRole("function");
functionMessage.setName(functionName.toString());
List<MultiContent> content = new ArrayList<>();
MultiContent multiContent = new MultiContent();
multiContent.setText(functionResult);
multiContent.setType("text");
content.add(multiContent);
functionMessage.setContent(content);
sendMessage[messageArray.length + 1] = functionMessage;
modelResponse.setMessages(sendMessage);
return llmService.chatChunk(modelResponse);
} }
} }
} }
...@@ -500,6 +463,38 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ ...@@ -500,6 +463,38 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
} }
private Message[] buildFunctionMessage(Message[] messageArray, String functionName, String functionArguments, String functionResult){
Message assistantMessage = new Message();
assistantMessage.setRole("assistant");
assistantMessage.setContent(null);
List<FunctionCall> functionCalls = new ArrayList<>();
FunctionCall functionCall = new FunctionCall();
functionCall.setName(functionName);
functionCall.setArguments(functionArguments);
functionCalls.add(functionCall);
assistantMessage.setFunction_call(functionCalls);
Message[] sendMessage = new Message[messageArray.length + 2];
for (int i = 0; i < messageArray.length; i++) {
sendMessage[i] = messageArray[i];
}
sendMessage[messageArray.length] = assistantMessage;
Message functionMessage = new Message();
functionMessage.setRole("function");
functionMessage.setName(functionName);
List<MultiContent> content = new ArrayList<>();
MultiContent multiContent = new MultiContent();
multiContent.setText(functionResult);
multiContent.setType("text");
content.add(multiContent);
functionMessage.setContent(content);
sendMessage[messageArray.length + 1] = functionMessage;
return sendMessage;
}
/** /**
* 文本输出结果 * 文本输出结果
* *
......
...@@ -30,6 +30,10 @@ public class AgentApplicationInfoConvert { ...@@ -30,6 +30,10 @@ public class AgentApplicationInfoConvert {
entity.setPreamble(model.getPreamble()); entity.setPreamble(model.getPreamble());
entity.setPublishTime(model.getPublishTime()); entity.setPublishTime(model.getPublishTime());
entity.setIsLongMemory(model.getIsLongMemory()); 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)); entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
} }
...@@ -70,6 +74,10 @@ public class AgentApplicationInfoConvert { ...@@ -70,6 +74,10 @@ public class AgentApplicationInfoConvert {
model.setPreamble(entity.getPreamble()); model.setPreamble(entity.getPreamble());
model.setPublishTime(entity.getPublishTime()); model.setPublishTime(entity.getPublishTime());
model.setIsLongMemory(entity.getIsLongMemory()); model.setIsLongMemory(entity.getIsLongMemory());
model.setIsFunction(entity.getIsFunction());
if (ArrayUtils.isNotEmpty(entity.getFunctionName())) {
model.setFunctionName(JsonUtils.serialize(entity.getFunctionName()));
}
if (ArrayUtils.isNotEmpty(entity.getVariableStructure())) { if (ArrayUtils.isNotEmpty(entity.getVariableStructure())) {
model.setVariableStructure(JsonUtils.serialize(entity.getVariableStructure())); model.setVariableStructure(JsonUtils.serialize(entity.getVariableStructure()));
} }
...@@ -98,7 +106,6 @@ public class AgentApplicationInfoConvert { ...@@ -98,7 +106,6 @@ public class AgentApplicationInfoConvert {
} }
public static AgentApplicationInfoDto entityToDto(BizAgentApplicationInfoEntity entity) { public static AgentApplicationInfoDto entityToDto(BizAgentApplicationInfoEntity entity) {
AgentApplicationBaseInfo baseInfo = new AgentApplicationBaseInfo(); AgentApplicationBaseInfo baseInfo = new AgentApplicationBaseInfo();
baseInfo.setMemberId(entity.getMemberId()); baseInfo.setMemberId(entity.getMemberId());
baseInfo.setAgentId(entity.getAgentId()); baseInfo.setAgentId(entity.getAgentId());
...@@ -117,6 +124,8 @@ public class AgentApplicationInfoConvert { ...@@ -117,6 +124,8 @@ public class AgentApplicationInfoConvert {
commConfig.setContinuousQuestionTurn(entity.getContinuousQuestionTurn()); commConfig.setContinuousQuestionTurn(entity.getContinuousQuestionTurn());
commConfig.setVariableStructure(entity.getVariableStructure()); commConfig.setVariableStructure(entity.getVariableStructure());
commConfig.setIsLongMemory(entity.getIsLongMemory()); commConfig.setIsLongMemory(entity.getIsLongMemory());
commConfig.setFunctionName(entity.getFunctionName());
commConfig.setIsFunction(entity.getIsFunction());
AgentApplicationKnowledgeConfig knowledgeConfig = new AgentApplicationKnowledgeConfig(); AgentApplicationKnowledgeConfig knowledgeConfig = new AgentApplicationKnowledgeConfig();
knowledgeConfig.setKnowledgeIds(entity.getKnowledgeIds()); knowledgeConfig.setKnowledgeIds(entity.getKnowledgeIds());
...@@ -162,6 +171,10 @@ public class AgentApplicationInfoConvert { ...@@ -162,6 +171,10 @@ public class AgentApplicationInfoConvert {
entity.setVariableStructure(dto.getCommConfig().getVariableStructure()); entity.setVariableStructure(dto.getCommConfig().getVariableStructure());
} }
entity.setIsLongMemory(dto.getCommConfig().getIsLongMemory()); 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())) { if (ObjectUtil.isNotEmpty(dto.getKnowledgeConfig())) {
...@@ -195,6 +208,11 @@ public class AgentApplicationInfoConvert { ...@@ -195,6 +208,11 @@ public class AgentApplicationInfoConvert {
entity.setPreamble(infoQueryItem.getPreamble()); entity.setPreamble(infoQueryItem.getPreamble());
entity.setPublishTime(infoQueryItem.getPublishTime()); entity.setPublishTime(infoQueryItem.getPublishTime());
entity.setIsLongMemory(infoQueryItem.getIsLongMemory()); 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)); entity.setVariableStructure(JsonUtils.deSerialize(infoQueryItem.getVariableStructure(), String[].class));
} }
......
...@@ -27,6 +27,11 @@ public class BizAgentApplicationPublishConvert { ...@@ -27,6 +27,11 @@ public class BizAgentApplicationPublishConvert {
entity.setAgentSystem(model.getAgentSystem()); entity.setAgentSystem(model.getAgentSystem());
entity.setPreamble(model.getPreamble()); entity.setPreamble(model.getPreamble());
entity.setIsLongMemory(model.getIsLongMemory()); entity.setIsLongMemory(model.getIsLongMemory());
entity.setIsFunction(model.getIsFunction());
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)); entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
} }
...@@ -65,6 +70,10 @@ public class BizAgentApplicationPublishConvert { ...@@ -65,6 +70,10 @@ public class BizAgentApplicationPublishConvert {
model.setAgentSystem(entity.getAgentSystem()); model.setAgentSystem(entity.getAgentSystem());
model.setPreamble(entity.getPreamble()); model.setPreamble(entity.getPreamble());
model.setIsLongMemory(entity.getIsLongMemory()); model.setIsLongMemory(entity.getIsLongMemory());
model.setIsFunction(entity.getIsFunction());
if (ArrayUtils.isNotEmpty(entity.getFunctionName())) {
model.setFunctionName(JsonUtils.serialize(entity.getFunctionName()));
}
if (ArrayUtils.isNotEmpty(entity.getVariableStructure())) { if (ArrayUtils.isNotEmpty(entity.getVariableStructure())) {
model.setVariableStructure(JsonUtils.serialize(entity.getVariableStructure())); model.setVariableStructure(JsonUtils.serialize(entity.getVariableStructure()));
} }
...@@ -112,6 +121,8 @@ public class BizAgentApplicationPublishConvert { ...@@ -112,6 +121,8 @@ public class BizAgentApplicationPublishConvert {
commConfig.setContinuousQuestionTurn(entity.getContinuousQuestionTurn()); commConfig.setContinuousQuestionTurn(entity.getContinuousQuestionTurn());
commConfig.setVariableStructure(entity.getVariableStructure()); commConfig.setVariableStructure(entity.getVariableStructure());
commConfig.setIsLongMemory(entity.getIsLongMemory()); commConfig.setIsLongMemory(entity.getIsLongMemory());
commConfig.setIsFunction(entity.getIsFunction());
commConfig.setFunctionName(entity.getFunctionName());
AgentApplicationKnowledgeConfig knowledgeConfig = new AgentApplicationKnowledgeConfig(); AgentApplicationKnowledgeConfig knowledgeConfig = new AgentApplicationKnowledgeConfig();
knowledgeConfig.setKnowledgeIds(entity.getKnowledgeIds()); knowledgeConfig.setKnowledgeIds(entity.getKnowledgeIds());
...@@ -154,6 +165,10 @@ public class BizAgentApplicationPublishConvert { ...@@ -154,6 +165,10 @@ public class BizAgentApplicationPublishConvert {
entity.setContinuousQuestionSystem(dto.getCommConfig().getContinuousQuestionSystem()); entity.setContinuousQuestionSystem(dto.getCommConfig().getContinuousQuestionSystem());
entity.setContinuousQuestionTurn(dto.getCommConfig().getContinuousQuestionTurn()); entity.setContinuousQuestionTurn(dto.getCommConfig().getContinuousQuestionTurn());
entity.setIsLongMemory(dto.getCommConfig().getIsLongMemory()); 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())) { if (ObjectUtil.isNotEmpty(dto.getCommConfig().getVariableStructure())) {
entity.setVariableStructure(dto.getCommConfig().getVariableStructure()); entity.setVariableStructure(dto.getCommConfig().getVariableStructure());
} }
......
...@@ -101,4 +101,31 @@ public class AgentApplicationCommConfig { ...@@ -101,4 +101,31 @@ public class AgentApplicationCommConfig {
this.isLongMemory = 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;
}
} }
package cn.com.poc.agent_application.entity; package cn.com.poc.agent_application.entity;
import javax.persistence.Column;
import java.util.Arrays; import java.util.Arrays;
public class BizAgentApplicationInfoEntity { public class BizAgentApplicationInfoEntity {
...@@ -260,6 +261,33 @@ public class BizAgentApplicationInfoEntity { ...@@ -260,6 +261,33 @@ public class BizAgentApplicationInfoEntity {
this.isLongMemory = 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;
}
/** is_deleted /** is_deleted
*是否删除 1、Y 是 2、N 否 *是否删除 1、Y 是 2、N 否
*/ */
......
...@@ -260,6 +260,33 @@ public class BizAgentApplicationPublishEntity { ...@@ -260,6 +260,33 @@ public class BizAgentApplicationPublishEntity {
this.isLongMemory = 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;
}
/** is_deleted /** is_deleted
*是否删除 1、Y 是 2、N 否 *是否删除 1、Y 是 2、N 否
*/ */
......
...@@ -359,6 +359,37 @@ public class BizAgentApplicationInfoModel extends BaseModelClass implements Seri ...@@ -359,6 +359,37 @@ public class BizAgentApplicationInfoModel extends BaseModelClass implements Seri
super.addValidField("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 /** is_deleted
*是否删除 1、Y 是 2、N 否 *是否删除 1、Y 是 2、N 否
......
...@@ -326,8 +326,41 @@ public class BizAgentApplicationPublishModel extends BaseModelClass implements S ...@@ -326,8 +326,41 @@ public class BizAgentApplicationPublishModel extends BaseModelClass implements S
this.isLongMemory = isLongMemory; this.isLongMemory = isLongMemory;
super.addValidField("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 /** is_deleted
*是否删除 1、Y 是 2、N 否 *是否删除 1、Y 是 2、N 否
*/ */
......
...@@ -328,6 +328,34 @@ public class AgentApplicationInfoQueryItem extends BaseItemClass implements Seri ...@@ -328,6 +328,34 @@ public class AgentApplicationInfoQueryItem extends BaseItemClass implements Seri
this.isLongMemory = isLongMemory; this.isLongMemory = 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;
}
/** 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;
}
/** /**
* is_deleted * is_deleted
......
...@@ -133,7 +133,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest { ...@@ -133,7 +133,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
} }
agentApplicationInfoService.callAgentApplication(infoEntity.getLargeModel(), infoEntity.getUnitIds() agentApplicationInfoService.callAgentApplication(infoEntity.getLargeModel(), infoEntity.getUnitIds()
, infoEntity.getAgentSystem(), infoEntity.getKnowledgeIds(), infoEntity.getCommunicationTurn(), infoEntity.getTopP() , infoEntity.getAgentSystem(), infoEntity.getKnowledgeIds(), infoEntity.getCommunicationTurn(), infoEntity.getTopP()
, dto.getMessages(), infoEntity.getIsLongMemory().equals(CommonConstant.IsDeleted.Y), infoEntity.getVariableStructure(), AgentApplicationConstants.USE_AGENT_STATUS.PREVIEW,httpServletResponse); , 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);
} catch (Exception e) { } catch (Exception e) {
httpServletResponse.setContentType("text/event-stream"); httpServletResponse.setContentType("text/event-stream");
PrintWriter writer = httpServletResponse.getWriter(); PrintWriter writer = httpServletResponse.getWriter();
......
...@@ -71,13 +71,20 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -71,13 +71,20 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
throw new BusinessException("未找到应用"); throw new BusinessException("未找到应用");
} }
//todo 这个agent 会开启那些 function
//todo 假设 1. 【变量】
//todo [变量] function_name , function_name
// 构造对话参数 // 构造对话参数
List<Message> messages = new ArrayList<>(); List<Message> messages = new ArrayList<>();
buildMessages(dialogsId, agentId, userBaseEntity.getUserId(), messages, input); buildMessages(dialogsId, agentId, userBaseEntity.getUserId(), messages, input);
// function_name -- List<>
String output = agentApplicationInfoService.callAgentApplication(infoEntity.getLargeModel(), infoEntity.getUnitIds() String output = agentApplicationInfoService.callAgentApplication(infoEntity.getLargeModel(), infoEntity.getUnitIds()
, infoEntity.getAgentSystem(), infoEntity.getKnowledgeIds(), infoEntity.getCommunicationTurn(), infoEntity.getTopP() , infoEntity.getAgentSystem(), infoEntity.getKnowledgeIds(), infoEntity.getCommunicationTurn(), infoEntity.getTopP()
, messages, infoEntity.getIsLongMemory().equals(CommonConstant.IsDeleted.Y), infoEntity.getVariableStructure(), AgentApplicationConstants.USE_AGENT_STATUS.PUBLISH, httpServletResponse); , messages, infoEntity.getIsLongMemory().equals(CommonConstant.IsDeleted.Y), infoEntity.getVariableStructure(), infoEntity.getIsFunction().equals(CommonConstant.IsDeleted.Y), infoEntity.getFunctionName(), null, AgentApplicationConstants.USE_AGENT_STATUS.PUBLISH, httpServletResponse);
//保存对话记录 //保存对话记录
......
package cn.com.poc.thirdparty.resource.demand.ai.common.domain; package cn.com.poc.thirdparty.resource.demand.ai.common.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Map; import java.util.Map;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Function { public class Function {
private String name; // 函数名 private String name; // 函数名
private String description; // 函数描述 private String description; // 函数描述
......
package cn.com.poc.thirdparty.resource.demand.ai.common.domain; package cn.com.poc.thirdparty.resource.demand.ai.common.domain;
import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractLargeModelFunction;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
...@@ -8,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; ...@@ -8,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class Tool { public class Tool {
private String type; private String type;
private AbstractLargeModelFunction function; private Function function;
public String getType() { public String getType() {
return type; return type;
...@@ -18,11 +17,11 @@ public class Tool { ...@@ -18,11 +17,11 @@ public class Tool {
this.type = type; this.type = type;
} }
public AbstractLargeModelFunction getFunction() { public Function getFunction() {
return function; return function;
} }
public void setFunction(AbstractLargeModelFunction function) { public void setFunction(Function function) {
this.function = function; this.function = function;
} }
......
package cn.com.poc.thirdparty.resource.demand.ai.function;
public interface AbstractExecuteLargeModelFunction {
String doMemoryFunction(String content, String useStatus, Boolean isLongMemory);
}
package cn.com.poc.thirdparty.resource.demand.ai.function; package cn.com.poc.thirdparty.resource.demand.ai.function;
import java.util.Map;
public class AbstractLargeModelFunction { import java.util.List;
private String name; // 函数名
private String description; // 函数描述
private Map<String, Object> parameters; // 函数请求参数
public String getName() {
return name;
}
public void setName(String name) { public class AbstractLargeModelFunction {
this.name = name;
}
public String getDescription() { public String doFunction(String content, String key){
return description; return null;
} }
public void setDescription(String description) { /**
this.description = description; * 获取配置
*/
public List<String> getLLMConfig(){
return null;
} }
public Map<String, Object> getParameters() { /**
return parameters; * 获取有关变量的配置
*/
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
return null;
} }
public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}
} }
package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.utils.SpringUtils;
public enum ExecuteLargeModelFunctionEnum {
set_long_memory(ExecuteSetLongMemoryFunction.class),
set_value_memory(ExecuteSetValueMemoryFunction.class),
search_memory_content(ExecuteSearchMemoryContentFunction.class),
search_memory_content_by_Enum(ExecuteSearchMemoryContentByEnumFunction.class);
private Class<? extends AbstractExecuteLargeModelFunction> executeFunction;
ExecuteLargeModelFunctionEnum(Class<? extends AbstractExecuteLargeModelFunction> executeFunction) {
this.executeFunction = executeFunction;
}
public AbstractExecuteLargeModelFunction getExecuteFunction() {
return SpringUtils.getBean(executeFunction);
}
public void setExecuteFunction(Class<AbstractExecuteLargeModelFunction> executeFunction) {
this.executeFunction = executeFunction;
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext;
import cn.hutool.json.JSONObject;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class ExecuteSearchMemoryContentByEnumFunction implements AbstractExecuteLargeModelFunction{
@Resource
private RedisService redisService;
@Override
public String doMemoryFunction(String content, String useStatus, Boolean isLongMemory) {
// 用enum给定的内容名来查询用户相关信息
// 创建 JSONObject 对象
JSONObject jsonObject = new JSONObject(content);
String contentName = jsonObject.getStr("content");
StringBuffer result = new StringBuffer();
// 先查询变量记忆
String key = useStatus + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + contentName;
result.append(redisService.get(key));
// 如果短期记忆没查到
if(result.toString().isEmpty()){
// 判断是否开启了长期记忆,若开启,则查询长期记忆
if (isLongMemory) {
String longMemoryKey = useStatus + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + "longMemory";
result.append(redisService.get(longMemoryKey));
}
}
return result.toString();
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class ExecuteSearchMemoryContentFunction implements AbstractExecuteLargeModelFunction{
@Resource
private RedisService redisService;
@Override
public String doMemoryFunction(String content, String useStatus, Boolean isLongMemory) {
// 查询用户相关信息(什么内容都可以)
String result = null;
// 判断是否开启了长期记忆,若开启,则查询长期记忆
if (isLongMemory) {
String longMemoryKey = useStatus + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + "longMemory";
result = redisService.hmget(longMemoryKey).toString();
}
return result;
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext;
import cn.hutool.json.JSONObject;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class ExecuteSetValueMemoryFunction implements AbstractExecuteLargeModelFunction{
@Resource
private RedisService redisService;
@Override
public String doMemoryFunction(String content, String useStatus, Boolean isLongMemory) {
// todo 执行保存变量的操作
// 创建 JSONObject 对象
JSONObject jsonObject = new JSONObject(content);
// 提取 contentName 和 contentValue
String contentName = jsonObject.getStr("contentName");
String contentValue = jsonObject.getStr("contentValue");
String key = useStatus + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + contentName;
redisService.set(key, contentValue);
return "SUCCESS";
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function; package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.utils.SpringUtils;
public enum LargeModelFunctionEnum { public enum LargeModelFunctionEnum {
setLongMemory(new SetLongMemoryLargeModelFunction()), set_long_memory(SetLongMemoryFunction.class),
setValueMemory(new SetValueMemoryLargeModelFunction()), set_value_memory(SetValueMemoryFunction.class),
searchMemoryContent(new SearchMemoryContentFunction()), search_memory_content(SearchMemoryContentFunction.class),
search_memory_content_by_Enum(new SearchMemoryContentByEnumFunction()); search_memory_content_by_enum(SearchMemoryContentByNameFunction.class);
private AbstractLargeModelFunction function; private Class<? extends AbstractLargeModelFunction> function;
LargeModelFunctionEnum(AbstractLargeModelFunction function){ LargeModelFunctionEnum(Class<? extends AbstractLargeModelFunction> function) {
this.function = function; this.function = function;
} }
public AbstractLargeModelFunction getFunction() { public AbstractLargeModelFunction getFunction() {
return function; return SpringUtils.getBean(function);
} }
public void setFunction(AbstractLargeModelFunction function) { public void setFunction(Class<AbstractLargeModelFunction> function) {
this.function = function; this.function = function;
} }
} }
package cn.com.poc.thirdparty.resource.demand.ai.function;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SearchMemoryContentByEnumFunction extends AbstractLargeModelFunction{
public SearchMemoryContentByEnumFunction() {
Map<String, Object> parameters = new HashMap<>();
parameters.put("type", "object");
List<String> required = new ArrayList<>();
required.add("content");
parameters.put("required", required);
super.setName("search_memory_content_by_Enum");
super.setDescription("用enum给定的内容名来查询用信息");
super.setParameters(parameters);
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext;
import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SearchMemoryContentByNameFunction extends AbstractLargeModelFunction {
@Resource
private RedisService redisService;
@Override
public String doFunction(String content, String key) {
// 用enum给定的内容名来查询用户相关信息
// 创建 JSONObject 对象
JSONObject jsonObject = new JSONObject(content);
String contentName = jsonObject.getStr("content");
StringBuffer result = new StringBuffer();
// 先查询变量记忆
String contentKey = key + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + contentName;
result.append(redisService.get(contentKey));
// 如果短期记忆没查到
return result.toString();
}
@Override
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
Map<String, Object> config = new HashMap<>();
Map<String, Object> function = new HashMap<>();
Map<String, Object> parameters = new HashMap<>();
parameters.put("type", "object");
List<String> required = new ArrayList<>();
required.add("content");
parameters.put("required", required);
// 根据变量名查询记忆方法
Map<String, Object> content = new HashMap<>();
content.put("type", "string");
content.put("description", "内容名");
content.put("enum", variableStructure); // 设置变量
Map<String, Object> searchProperties = new HashMap<>();
searchProperties.put("content", content);
parameters.put("properties", searchProperties);
parameters.put("type","object");
function.put("name", "search_memory_content_by_Enum");
function.put("description", "用enum给定的内容名来查询用户信息(什么内容都可以)");
function.put("parameters", parameters);
config.put("type", "function");
config.put("function", function);
// 将 Map 转换为 JSON 字符串
Gson gson = new Gson();
String jsonString = gson.toJson(config);
List<String> resultList = new ArrayList<>();
resultList.add(jsonString);
return resultList;
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function; package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class SearchMemoryContentFunction extends AbstractLargeModelFunction{ @Service
public SearchMemoryContentFunction() { public class SearchMemoryContentFunction extends AbstractLargeModelFunction {
@Resource
private RedisService redisService;
@Override
public String doFunction(String content, String key) {
// 查询用户相关信息(什么内容都可以)
String result;
String longMemoryKey = key + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + "longMemory";
result = redisService.hmget(longMemoryKey).toString();
return result;
}
@Override
public List<String> getLLMConfig() {
Map<String, Object> config = new HashMap<>();
Map<String, Object> function = new HashMap<>();
Map<String, Object> content = new HashMap<>(); Map<String, Object> content = new HashMap<>();
content.put("type", "string"); content.put("type", "string");
content.put("description","信息说明"); content.put("description","信息说明");
...@@ -22,8 +47,19 @@ public class SearchMemoryContentFunction extends AbstractLargeModelFunction{ ...@@ -22,8 +47,19 @@ public class SearchMemoryContentFunction extends AbstractLargeModelFunction{
required.add("content"); required.add("content");
parameters.put("required", required); parameters.put("required", required);
super.setName("search_memory_content"); function.put("name", "search_memory_content");
super.setDescription("获取用户相关信息"); function.put("description", "获取用户相关信息");
super.setParameters(parameters); function.put("parameters", parameters);
config.put("type", "function");
config.put("function", function);
// 将 Map 转换为 JSON 字符串
Gson gson = new Gson();
String jsonString = gson.toJson(config);
List<String> resultList = new ArrayList<>();
resultList.add(jsonString);
return resultList;
} }
} }
...@@ -4,6 +4,7 @@ import cn.com.poc.common.service.RedisService; ...@@ -4,6 +4,7 @@ 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.DateUtils; import cn.com.poc.common.utils.DateUtils;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -14,21 +15,20 @@ import java.util.Map; ...@@ -14,21 +15,20 @@ import java.util.Map;
@Service @Service
public class ExecuteSetLongMemoryFunction implements AbstractExecuteLargeModelFunction{ public class SetLongMemoryFunction extends AbstractLargeModelFunction {
@Resource @Resource
private RedisService redisService; private RedisService redisService;
@Override @Override
public String doMemoryFunction(String content, String useStatus, Boolean isLongMemory) { public String doFunction(String content, String key) {
// todo 执行保存长期记忆的操作 // todo 执行保存长期记忆的操作
if(isLongMemory){
// 创建 JSONObject 对象 // 创建 JSONObject 对象
JSONObject jsonObject = new JSONObject(content); JSONObject jsonObject = new JSONObject(content);
// 提取 content // 提取 content
String contents = jsonObject.getStr("content"); String contents = jsonObject.getStr("content");
String key = useStatus + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + "longMemory"; String contentKey = key + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + "longMemory";
Map<Object, Object> hmget = redisService.hmget(key); Map<Object, Object> hmget = redisService.hmget(contentKey);
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
for (Map.Entry<Object, Object> entry : hmget.entrySet()) { for (Map.Entry<Object, Object> entry : hmget.entrySet()) {
if (entry.getKey() instanceof String) { if (entry.getKey() instanceof String) {
...@@ -41,8 +41,44 @@ public class ExecuteSetLongMemoryFunction implements AbstractExecuteLargeModelFu ...@@ -41,8 +41,44 @@ public class ExecuteSetLongMemoryFunction implements AbstractExecuteLargeModelFu
list.add("timestamp:" + DateUtils.getCurrTime()); list.add("timestamp:" + DateUtils.getCurrTime());
list.add("content:" + contents); list.add("content:" + contents);
result.put(Integer.toString(hmget.size()), list); result.put(Integer.toString(hmget.size()), list);
redisService.hmset(key, result); redisService.hmset(contentKey, result);
}
return "SUCCESS"; return "SUCCESS";
} }
@Override
public List<String> getLLMConfig() {
Map<String, Object> config = new HashMap<>();
Map<String, Object> function = new HashMap<>();
Map<String, Object> content = new HashMap<>();
content.put("type", "string");
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);
List<String> required = new ArrayList<>();
required.add("content");
parameters.put("required", required);
function.put("name", "set_long_memory");
function.put("description", "用来保存用户想记录的内容(什么内容都可以)");
function.put("parameters", parameters);
config.put("type", "function");
config.put("function", function);
// 将 Map 转换为 JSON 字符串
Gson gson = new Gson();
String jsonString = gson.toJson(config);
List<String> resultList = new ArrayList<>();
resultList.add(jsonString);
return resultList;
}
} }
package cn.com.poc.thirdparty.resource.demand.ai.function;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SetLongMemoryLargeModelFunction extends AbstractLargeModelFunction {
public SetLongMemoryLargeModelFunction() {
Map<String, Object> content = new HashMap<>();
content.put("type", "string");
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);
List<String> required = new ArrayList<>();
required.add("content");
parameters.put("required", required);
super.setName("set_long_memory");
super.setDescription("用来保存用户想记录的内容(什么内容都可以)");
super.setParameters(parameters);
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext;
import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SetValueMemoryFunction extends AbstractLargeModelFunction {
@Resource
private RedisService redisService;
@Override
public String doFunction(String content, String key) {
// todo 执行保存变量的操作
// 创建 JSONObject 对象
JSONObject jsonObject = new JSONObject(content);
// 提取 contentName 和 contentValue
String contentName = jsonObject.getStr("contentName");
String contentValue = jsonObject.getStr("contentValue");
String contentKey = key + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + contentName;
redisService.set(contentKey, contentValue);
return "SUCCESS";
}
@Override
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
Map<String, Object> config = new HashMap<>();
Map<String, Object> function = new HashMap<>();
Map<String, Object> parameters = new HashMap<>();
parameters.put("type", "object");
List<String> required = new ArrayList<>();
required.add("contentName");
required.add("contentValue");
parameters.put("required", required);
Map<String, Object> properties = new HashMap<>();
Map<String, Object> contentName = new HashMap<>();
contentName.put("type", "string");
contentName.put("description", "内容名");
contentName.put("enum", variableStructure); // 设置变量
Map<String, Object> contentValue = new HashMap<>();
contentValue.put("type", "string");
contentValue.put("description", "内容值");
properties.put("contentName", contentName);
properties.put("contentValue", contentValue);
parameters.put("properties", properties);
parameters.put("type","object");
function.put("name", "set_value_memory");
function.put("description", "用enum给定的内容名来保存用户想记录的内容值");
function.put("parameters", parameters);
config.put("type", "function");
config.put("function", function);
// 将 Map 转换为 JSON 字符串
Gson gson = new Gson();
String jsonString = gson.toJson(config);
List<String> resultList = new ArrayList<>();
resultList.add(jsonString);
return resultList;
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SetValueMemoryLargeModelFunction extends AbstractLargeModelFunction {
public SetValueMemoryLargeModelFunction() {
Map<String, Object> parameters = new HashMap<>();
parameters.put("type", "object");
List<String> required = new ArrayList<>();
required.add("contentName");
required.add("contentValue");
parameters.put("required", required);
super.setName("set_value_memory");
super.setDescription("用enum给定的内容名来保存用户想记录的内容值");
super.setParameters(parameters);
}
}
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