Commit fb52d923 authored by jennie chen's avatar jennie chen

1.重新定义function

2.优化部分代码细节
parent 5e486990
......@@ -19,7 +19,7 @@ public interface AgentApplicationInfoService {
*/
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;
List<Message> messages, Boolean isLongMemory, String[] variableStructure, boolean isFunction, String[] functionName, List<String> functionConfig, String useStatus, HttpServletResponse httpServletResponse) throws Exception;
/**
......
......@@ -30,6 +30,10 @@ 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())) {
entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
}
......@@ -70,6 +74,10 @@ 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()));
}
......@@ -98,7 +106,6 @@ public class AgentApplicationInfoConvert {
}
public static AgentApplicationInfoDto entityToDto(BizAgentApplicationInfoEntity entity) {
AgentApplicationBaseInfo baseInfo = new AgentApplicationBaseInfo();
baseInfo.setMemberId(entity.getMemberId());
baseInfo.setAgentId(entity.getAgentId());
......@@ -117,6 +124,8 @@ 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());
......@@ -162,6 +171,10 @@ 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())) {
......@@ -195,6 +208,11 @@ 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())) {
entity.setVariableStructure(JsonUtils.deSerialize(infoQueryItem.getVariableStructure(), String[].class));
}
......
......@@ -27,6 +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())) {
entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
}
if(StringUtils.isNotBlank(model.getVariableStructure())) {
entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
}
......@@ -65,6 +70,10 @@ 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()));
}
......@@ -112,6 +121,8 @@ 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());
......@@ -154,6 +165,10 @@ 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());
}
......
......@@ -101,4 +101,31 @@ public class AgentApplicationCommConfig {
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;
import javax.persistence.Column;
import java.util.Arrays;
public class BizAgentApplicationInfoEntity {
......@@ -260,6 +261,33 @@ 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,6 +260,33 @@ 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 否
*/
......
......@@ -359,6 +359,37 @@ public class BizAgentApplicationInfoModel extends BaseModelClass implements Seri
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 否
......
......@@ -328,6 +328,39 @@ public class BizAgentApplicationPublishModel extends BaseModelClass implements S
}
/** 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 否
*/
......
......@@ -328,6 +328,34 @@ public class AgentApplicationInfoQueryItem extends BaseItemClass implements Seri
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
......
......@@ -133,7 +133,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
}
agentApplicationInfoService.callAgentApplication(infoEntity.getLargeModel(), infoEntity.getUnitIds()
, 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) {
httpServletResponse.setContentType("text/event-stream");
PrintWriter writer = httpServletResponse.getWriter();
......
......@@ -71,13 +71,20 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
throw new BusinessException("未找到应用");
}
//todo 这个agent 会开启那些 function
//todo 假设 1. 【变量】
//todo [变量] function_name , function_name
// 构造对话参数
List<Message> messages = new ArrayList<>();
buildMessages(dialogsId, agentId, userBaseEntity.getUserId(), messages, input);
// function_name -- List<>
String output = agentApplicationInfoService.callAgentApplication(infoEntity.getLargeModel(), infoEntity.getUnitIds()
, 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;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Map;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Function {
private String name; // 函数名
private String description; // 函数描述
......
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.JsonInclude;
......@@ -8,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Tool {
private String type;
private AbstractLargeModelFunction function;
private Function function;
public String getType() {
return type;
......@@ -18,11 +17,11 @@ public class Tool {
this.type = type;
}
public AbstractLargeModelFunction getFunction() {
public Function getFunction() {
return function;
}
public void setFunction(AbstractLargeModelFunction function) {
public void setFunction(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;
import java.util.Map;
public class AbstractLargeModelFunction {
private String name; // 函数名
private String description; // 函数描述
private Map<String, Object> parameters; // 函数请求参数
public String getName() {
return name;
}
import java.util.List;
public void setName(String name) {
this.name = name;
}
public class AbstractLargeModelFunction {
public String getDescription() {
return description;
public String doFunction(String content, String key){
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;
import cn.com.poc.common.utils.SpringUtils;
public enum LargeModelFunctionEnum {
setLongMemory(new SetLongMemoryLargeModelFunction()),
setValueMemory(new SetValueMemoryLargeModelFunction()),
searchMemoryContent(new SearchMemoryContentFunction()),
search_memory_content_by_Enum(new SearchMemoryContentByEnumFunction());
set_long_memory(SetLongMemoryFunction.class),
set_value_memory(SetValueMemoryFunction.class),
search_memory_content(SearchMemoryContentFunction.class),
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;
}
public AbstractLargeModelFunction getFunction() {
return function;
return SpringUtils.getBean(function);
}
public void setFunction(AbstractLargeModelFunction function) {
public void setFunction(Class<AbstractLargeModelFunction> 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;
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.HashMap;
import java.util.List;
import java.util.Map;
public class SearchMemoryContentFunction extends AbstractLargeModelFunction{
public SearchMemoryContentFunction() {
@Service
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<>();
content.put("type", "string");
content.put("description","信息说明");
......@@ -22,8 +47,19 @@ public class SearchMemoryContentFunction extends AbstractLargeModelFunction{
required.add("content");
parameters.put("required", required);
super.setName("search_memory_content");
super.setDescription("获取用户相关信息");
super.setParameters(parameters);
function.put("name", "search_memory_content");
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;
}
}
......@@ -4,6 +4,7 @@ import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.DateUtils;
import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
......@@ -14,21 +15,20 @@ import java.util.Map;
@Service
public class ExecuteSetLongMemoryFunction implements AbstractExecuteLargeModelFunction{
public class SetLongMemoryFunction extends AbstractLargeModelFunction {
@Resource
private RedisService redisService;
@Override
public String doMemoryFunction(String content, String useStatus, Boolean isLongMemory) {
public String doFunction(String content, String key) {
// todo 执行保存长期记忆的操作
if(isLongMemory){
// 创建 JSONObject 对象
JSONObject jsonObject = new JSONObject(content);
// 提取 content
String contents = jsonObject.getStr("content");
String key = useStatus + ":" + BlContext.getCurrentUserNotException().getUserId().toString() + ":" + "longMemory";
Map<Object, Object> hmget = redisService.hmget(key);
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) {
......@@ -41,8 +41,44 @@ public class ExecuteSetLongMemoryFunction implements AbstractExecuteLargeModelFu
list.add("timestamp:" + DateUtils.getCurrTime());
list.add("content:" + contents);
result.put(Integer.toString(hmget.size()), list);
redisService.hmset(key, result);
}
redisService.hmset(contentKey, result);
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