Commit 6749f753 authored by alex yao's avatar alex yao

Merge branch 'task/temp_function_call' into 'release'

Task/temp function call

See merge request !3
parents a3f0e6c8 fc5cac7c
......@@ -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,
Integer[] kdIds, Integer communicationTurn, Float topP,
List<Message> messages, HttpServletResponse httpServletResponse) throws Exception;
String callAgentApplication(String identifier, String largeModel, String[] unitIds, String agentSystem,
Integer[] knowledgeIds, Integer communicationTurn, Float topP,
List<Message> messages, List<Tool> tools, HttpServletResponse httpServletResponse) throws Exception;
/**
......
......@@ -16,16 +16,17 @@ import cn.com.poc.knowledge.service.BizKnowledgeDocumentService;
import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import cn.com.poc.thirdparty.resource.demand.ai.aggregate.AICreateImageService;
import cn.com.poc.thirdparty.resource.demand.ai.aggregate.DemandKnowledgeService;
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.*;
import cn.com.poc.thirdparty.resource.demand.ai.entity.generations.BaiduAISailsText2ImageRequest;
import cn.com.poc.thirdparty.resource.demand.ai.entity.generations.BaiduAISailsText2ImageResult;
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.*;
import cn.com.poc.thirdparty.service.LLMService;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.Gson;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -39,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 {
......@@ -98,18 +97,18 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
@Override
public String callAgentApplication(String largeModel, String[] unitIds, String agentSystem, Integer[] kdIds, Integer communicationTurn, Float topP, List<Message> messages, HttpServletResponse httpServletResponse) throws Exception {
logger.info("--------- Call Agent Application large model:{},unitIds:{},agentSystem:{},kdIds:{}" + " communicationTurn:{},topP:{},messages:{}--------------", largeModel, unitIds, agentSystem, kdIds, communicationTurn, topP, messages);
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);
String promptTemplate = buildDialogsPrompt(messages, agentSystem, kdIds, toolArray, identifier);
//todo buildFunction 配置
Message[] messageArray = buildMessages(messages, communicationTurn, promptTemplate);
BufferedReader bufferedReader = invokeLLM(model, messageArray, topP);
BufferedReader bufferedReader = invokeLLM(model, messageArray, topP, toolArray, identifier);
return textOutput(httpServletResponse, bufferedReader);
}
......@@ -183,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);
......@@ -337,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);
// 调用知识库
......@@ -353,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;
}
......@@ -365,42 +398,122 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
* @return
* @throws Exception
*/
private BufferedReader invokeLLM(String largeModel, Message[] messageArray, Float topP) 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);
//todo 添加tools参数
largeModelResponse.setUser("POE");
if (ArrayUtils.isNotEmpty(tools)) {
largeModelResponse.setTools(tools);
largeModelResponse.setTool_choice("auto");
}
BufferedReader bufferedReader = llmService.chatChunk(largeModelResponse);
bufferedReader.mark(200);
String res = "";
bufferedReader.mark(0);
boolean isFunctionCall = false;
String functionResult = null;
StringBuffer finishReason = new StringBuffer();
StringBuffer functionName = new StringBuffer();
StringBuffer functionArguments = new StringBuffer();
while ((res = bufferedReader.readLine()) != null) {
if (StringUtils.isBlank(res)) {
continue;
}
//todo 添加function 参数 LargeModelDemandResult 和中台接口一致
// 添加function 参数 LargeModelDemandResult 和中台接口一致
LargeModelDemandResult result = JsonUtils.deSerialize(res.replaceFirst(EVENT_STREAM_PREFIX, StringUtils.EMPTY), LargeModelDemandResult.class);
if (!"0".equals(result.getCode())) {
logger.error("LLM Error,code:{}", result.getCode());
BusinessException ex = new BusinessException("调用失败");
throw ex;
}
// todo 判断是否为function_call
// todo 如果是function_call,则处理function
if (!(result.getFunction() != null && (StringUtils.isNotBlank(result.getFunction().getName()) || StringUtils.isNotBlank(result.getFunction().getArguments()))) && !isFunctionCall) {
bufferedReader.reset();
break;
} else {
isFunctionCall = true;
if (result.getFunction().getName() != null && !result.getFunction().getName().isEmpty()) {
functionName.append(result.getFunction().getName());
}
if (result.getFunction().getArguments() != null && !result.getFunction().getArguments().isEmpty()) {
functionArguments.append(result.getFunction().getArguments());
}
}
// todo 处理function - 1. 获取function_call 2. 调用对应function 3. 返回function_call并且调用LLM 4. 获取bufferedReader 返回
if (result.getFinish_reason() != null) {
finishReason.append(result.getFinish_reason());
}
// 走到了最后一个流,判断是否为function_call
// 如果是function_call,则处理function
if (finishReason.toString().equals("tool_calls")) {
// 如果是function_call,则处理function
if (!functionName.toString().isEmpty() && !functionArguments.toString().isEmpty()) {
// 执行函数返回结果
LargeModelFunctionEnum functionEnum = LargeModelFunctionEnum.valueOf(functionName.toString());
functionResult = functionEnum.getFunction().doFunction(functionArguments.toString(), identifier);
}
// todo 若不为function_call, 则直接返回bufferedReader
bufferedReader.reset();
break;
if (functionResult != null) {
// 处理function - 1,获取function_call 2. 调用对应function 3. 返回function_call并且调用LLM 4.获取bufferedReader 返回
Message[] sendMessage = buildFunctionMessage(messageArray, functionName.toString(), functionArguments.toString(), functionResult);
largeModelResponse.setMessages(sendMessage);
return llmService.chatChunk(largeModelResponse);
}
}
}
// 若不为function_call,则直接返回bufferedReader
return bufferedReader;
}
/**
* 构建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);
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;
}
/**
* 文本输出结果
*
......@@ -445,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) {
......@@ -481,4 +593,5 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
return largeModelEntity.getModelName();
}
}
......@@ -2,6 +2,16 @@ package cn.com.poc.agent_application.constant;
public interface AgentApplicationConstants {
interface USE_AGENT_STATUS {
String PREVIEW = "preview";
String PUBLISH = "publish";
}
interface TOOLS {
int FUNCTION_NUMBER = 4;
}
interface AGENT_PUBLISH_STATUS {
String DRAFT = "draft"; // 草稿
......
......@@ -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;
......@@ -42,6 +42,7 @@ public class AgentApplicationInfoConvert {
entity.setAgentPublishStatus(model.getAgentPublishStatus());
entity.setPreamble(model.getPreamble());
entity.setPublishTime(model.getPublishTime());
entity.setIsLongMemory(model.getIsLongMemory());
if (StringUtils.isNotBlank(model.getVariableStructure())) {
entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
}
......@@ -81,6 +82,7 @@ public class AgentApplicationInfoConvert {
model.setAgentPublishStatus(entity.getAgentPublishStatus());
model.setPreamble(entity.getPreamble());
model.setPublishTime(entity.getPublishTime());
model.setIsLongMemory(entity.getIsLongMemory());
if (ArrayUtils.isNotEmpty(entity.getVariableStructure())) {
model.setVariableStructure(JsonUtils.serialize(entity.getVariableStructure()));
}
......@@ -109,7 +111,6 @@ public class AgentApplicationInfoConvert {
}
public static AgentApplicationInfoDto entityToDto(BizAgentApplicationInfoEntity entity) {
AgentApplicationBaseInfo baseInfo = new AgentApplicationBaseInfo();
baseInfo.setMemberId(entity.getMemberId());
baseInfo.setAgentId(entity.getAgentId());
......@@ -127,6 +128,8 @@ public class AgentApplicationInfoConvert {
commConfig.setContinuousQuestionSystem(entity.getContinuousQuestionSystem());
commConfig.setContinuousQuestionTurn(entity.getContinuousQuestionTurn());
commConfig.setVariableStructure(entity.getVariableStructure());
commConfig.setIsLongMemory(entity.getIsLongMemory());
AgentApplicationKnowledgeConfig knowledgeConfig = new AgentApplicationKnowledgeConfig();
knowledgeConfig.setKnowledgeIds(entity.getKnowledgeIds());
......@@ -171,6 +174,7 @@ public class AgentApplicationInfoConvert {
if (ObjectUtil.isNotEmpty(dto.getCommConfig().getVariableStructure())) {
entity.setVariableStructure(dto.getCommConfig().getVariableStructure());
}
entity.setIsLongMemory(dto.getCommConfig().getIsLongMemory());
}
if (ObjectUtil.isNotEmpty(dto.getKnowledgeConfig())) {
......@@ -203,6 +207,8 @@ public class AgentApplicationInfoConvert {
entity.setAgentPublishStatus(infoQueryItem.getAgentPublishStatus());
entity.setPreamble(infoQueryItem.getPreamble());
entity.setPublishTime(infoQueryItem.getPublishTime());
entity.setIsLongMemory(infoQueryItem.getIsLongMemory());
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());
......@@ -26,7 +26,12 @@ public class BizAgentApplicationPublishConvert {
entity.setAgentDesc(model.getAgentDesc());
entity.setAgentSystem(model.getAgentSystem());
entity.setPreamble(model.getPreamble());
if(StringUtils.isNotBlank(model.getVariableStructure())) {
entity.setIsLongMemory(model.getIsLongMemory());
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));
}
if (StringUtils.isNotBlank(model.getFeaturedQuestions())) {
......@@ -53,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());
......@@ -63,6 +68,7 @@ public class BizAgentApplicationPublishConvert {
model.setAgentDesc(entity.getAgentDesc());
model.setAgentSystem(entity.getAgentSystem());
model.setPreamble(entity.getPreamble());
model.setIsLongMemory(entity.getIsLongMemory());
if (ArrayUtils.isNotEmpty(entity.getVariableStructure())) {
model.setVariableStructure(JsonUtils.serialize(entity.getVariableStructure()));
}
......@@ -89,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());
......@@ -109,6 +115,7 @@ public class BizAgentApplicationPublishConvert {
commConfig.setContinuousQuestionSystem(entity.getContinuousQuestionSystem());
commConfig.setContinuousQuestionTurn(entity.getContinuousQuestionTurn());
commConfig.setVariableStructure(entity.getVariableStructure());
commConfig.setIsLongMemory(entity.getIsLongMemory());
AgentApplicationKnowledgeConfig knowledgeConfig = new AgentApplicationKnowledgeConfig();
knowledgeConfig.setKnowledgeIds(entity.getKnowledgeIds());
......@@ -129,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());
......@@ -150,6 +157,7 @@ public class BizAgentApplicationPublishConvert {
entity.setContinuousQuestionStatus(dto.getCommConfig().getContinuousQuestionStatus());
entity.setContinuousQuestionSystem(dto.getCommConfig().getContinuousQuestionSystem());
entity.setContinuousQuestionTurn(dto.getCommConfig().getContinuousQuestionTurn());
entity.setIsLongMemory(dto.getCommConfig().getIsLongMemory());
if (ObjectUtil.isNotEmpty(dto.getCommConfig().getVariableStructure())) {
entity.setVariableStructure(dto.getCommConfig().getVariableStructure());
}
......
......@@ -75,17 +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 否
*/
private java.lang.String isLongMemory;
public java.lang.String getIsLongMemory() {
return this.isLongMemory;
}
public void setIsLongMemory(java.lang.String isLongMemory) {
this.isLongMemory = isLongMemory;
}
}
......@@ -232,6 +232,20 @@ public class BizAgentApplicationInfoDto {
public void setUnitIds(java.lang.String unitIds){
this.unitIds = unitIds;
}
/** is_long_memory
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isLongMemory;
public java.lang.String getIsLongMemory(){
return this.isLongMemory;
}
public void setIsLongMemory(java.lang.String isLongMemory){
this.isLongMemory = isLongMemory;
}
/** is_deleted
*是否删除 1、Y 是 2、N 否
*/
......
package cn.com.poc.agent_application.entity;
import javax.persistence.Column;
import java.util.Arrays;
public class BizAgentApplicationInfoEntity {
......@@ -247,6 +248,20 @@ public class BizAgentApplicationInfoEntity {
this.variableStructure = variableStructure;
}
/** is_long_memory
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isLongMemory;
public java.lang.String getIsLongMemory(){
return this.isLongMemory;
}
public void setIsLongMemory(java.lang.String isLongMemory){
this.isLongMemory = isLongMemory;
}
/** is_deleted
*是否删除 1、Y 是 2、N 否
*/
......
......@@ -246,6 +246,22 @@ public class BizAgentApplicationPublishEntity {
public void setUnitIds(java.lang.String[] unitIds){
this.unitIds = unitIds;
}
/** is_long_memory
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isLongMemory;
public java.lang.String getIsLongMemory(){
return this.isLongMemory;
}
public void setIsLongMemory(java.lang.String isLongMemory){
this.isLongMemory = isLongMemory;
}
/** is_deleted
*是否删除 1、Y 是 2、N 否
*/
......
......@@ -344,6 +344,20 @@ public class BizAgentApplicationInfoModel extends BaseModelClass implements Seri
super.addValidField("variableStructure");
}
/** is_long_memory
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isLongMemory;
@Column(name = "is_long_memory",length = 1)
public java.lang.String getIsLongMemory(){
return this.isLongMemory;
}
public void setIsLongMemory(java.lang.String isLongMemory){
this.isLongMemory = isLongMemory;
super.addValidField("isLongMemory");
}
/** is_deleted
*是否删除 1、Y 是 2、N 否
......
......@@ -311,8 +311,22 @@ public class BizAgentApplicationPublishModel extends BaseModelClass implements S
this.unitIds = unitIds;
super.addValidField("unitIds");
}
/** is_long_memory
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isLongMemory;
@Column(name = "is_long_memory",length = 1)
public java.lang.String getIsLongMemory(){
return this.isLongMemory;
}
public void setIsLongMemory(java.lang.String isLongMemory){
this.isLongMemory = isLongMemory;
super.addValidField("isLongMemory");
}
/** is_deleted
*是否删除 1、Y 是 2、N 否
*/
......
......@@ -19,6 +19,7 @@ select distinct
top_p,
unit_ids,
variable_structure,
is_long_memory,
is_deleted,
CREATOR,
CREATED_TIME,
......
......@@ -314,6 +314,49 @@ public class AgentApplicationInfoQueryItem extends BaseItemClass implements Seri
this.variableStructure = variableStructure;
}
/** is_long_memory
*是否开启长期记忆 1、Y 是 2、N 否
*/
private java.lang.String isLongMemory;
@Column(name = "is_long_memory",length = 1)
public java.lang.String getIsLongMemory(){
return this.isLongMemory;
}
public void setIsLongMemory(java.lang.String 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
......
......@@ -11,17 +11,16 @@ import cn.com.poc.agent_application.rest.AgentApplicationInfoRest;
import cn.com.poc.agent_application.service.BizAgentApplicationInfoService;
import cn.com.poc.agent_application.service.BizAgentApplicationLargeModelListService;
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;
......@@ -30,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;
......@@ -144,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()
agentApplicationInfoService.callAgentApplication(agentId, infoEntity.getLargeModel(), infoEntity.getUnitIds()
, infoEntity.getAgentSystem(), kdIds.toArray(new Integer[0]), infoEntity.getCommunicationTurn(), infoEntity.getTopP()
, dto.getMessages(), httpServletResponse);
, 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;
import cn.com.poc.agent_application.entity.BizAgentApplicationGcConfigEntity;
import cn.com.poc.agent_application.entity.BizAgentApplicationInfoEntity;
import cn.com.poc.agent_application.entity.BizAgentApplicationPublishEntity;
import cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordService;
import cn.com.poc.agent_application.service.BizAgentApplicationGcConfigService;
import cn.com.poc.agent_application.service.BizAgentApplicationPublishService;
import cn.com.poc.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, httpServletResponse);
String output = agentApplicationInfoService.callAgentApplication(dialogsId, infoEntity.getLargeModel(),
infoEntity.getUnitIds(), infoEntity.getAgentSystem(), kdIdList.toArray(new Integer[0]), infoEntity.getCommunicationTurn(),
infoEntity.getTopP(), messages, tools, httpServletResponse);
//保存对话记录
......
......@@ -24,7 +24,7 @@ public interface AgentApplicationRest extends BaseRest {
/**
* 创建对话
*/
String createDialogues(@RequestParam String agentId) ;
String createDialogues(@RequestParam String agentId) throws Exception;
/**
* 调用 已发布Agent应用
......
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; // 函数描述
private Map<String, Object> parameters; // 函数请求参数
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Map<String, Object> getParameters() {
return parameters;
}
public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}
@Override
public String toString() {
return "Function{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
", parameters=" + parameters +
'}';
}
}
package cn.com.poc.thirdparty.resource.demand.ai.common.domain;
public class FunctionCall {
private String name;
private String arguments;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getArguments() {
return arguments;
}
public void setArguments(String arguments) {
this.arguments = arguments;
}
}
......@@ -16,6 +16,10 @@ public class Message {
private String role;
private List<FunctionCall> function_call;
private String name;
public List<MultiContent> getContent() {
return content;
}
......@@ -32,11 +36,29 @@ public class Message {
this.role = role;
}
public List<FunctionCall> getFunction_call() {
return function_call;
}
public void setFunction_call(List<FunctionCall> function_call) {
this.function_call = function_call;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Message{" +
"content=" + content +
", role='" + role + '\'' +
", function_call=" + function_call +
", name='" + name + '\'' +
'}';
}
}
package cn.com.poc.thirdparty.resource.demand.ai.common.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Tool {
private String type;
private Function function;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Function getFunction() {
return function;
}
public void setFunction(Function function) {
this.function = function;
}
@Override
public String toString() {
return "Tool{" +
"type='" + type + '\'' +
", function=" + function +
'}';
}
}
package cn.com.poc.thirdparty.resource.demand.ai.common.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ToolChoice {
// 属性: type, 必填
private String type;
private ToolFunction toolFunction;
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public ToolFunction getToolFunction() {
return toolFunction;
}
public void setToolFunction(ToolFunction toolFunction) {
this.toolFunction = toolFunction;
}
}
package cn.com.poc.thirdparty.resource.demand.ai.common.domain;
public class ToolFunction {
private String name;
private String arguments;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getArguments() {
return arguments;
}
public void setArguments(String arguments) {
this.arguments = arguments;
}
}
package cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel;
import cn.com.poc.support.dgTools.result.AbstractResult;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.ToolFunction;
import java.io.Serializable;
......@@ -10,6 +11,10 @@ public class LargeModelDemandResult extends AbstractResult implements Serializab
private String message;
private ToolFunction function;
private String finish_reason;
public String getCode() {
return code;
}
......@@ -26,11 +31,29 @@ public class LargeModelDemandResult extends AbstractResult implements Serializab
this.message = message;
}
public ToolFunction getFunction() {
return function;
}
public void setFunction(ToolFunction function) {
this.function = function;
}
public String getFinish_reason() {
return finish_reason;
}
public void setFinish_reason(String finish_reason) {
this.finish_reason = finish_reason;
}
@Override
public String toString() {
return "LargeModelDemandResult{" +
"code='" + code + '\'' +
", message='" + message + '\'' +
", function=" + function +
", finish_reason='" + finish_reason + '\'' +
'}';
}
}
package cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.Message;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.Tool;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Map;
/**
......@@ -22,6 +22,8 @@ public class LargeModelResponse implements Serializable {
private Message[] messages;
private Tool[] tools;
private Float temperature;
@JsonFilter("top_p")
......@@ -46,6 +48,8 @@ public class LargeModelResponse implements Serializable {
private String user;
private String tool_choice;
public String getModel() {
return model;
}
......@@ -142,21 +146,19 @@ public class LargeModelResponse implements Serializable {
this.user = user;
}
@Override
public String toString() {
return "LargeModelResponse{" +
"model='" + model + '\'' +
", messages=" + Arrays.toString(messages) +
", temperature=" + temperature +
", topP=" + topP +
", n=" + n +
", stream=" + stream +
", stop=" + Arrays.toString(stop) +
", maxTokens=" + maxTokens +
", presencePenalty=" + presencePenalty +
", frequencyPenalty=" + frequencyPenalty +
", logit_bias=" + logit_bias +
", user='" + user + '\'' +
'}';
public String getTool_choice() {
return tool_choice;
}
public void setTool_choice(String tool_choice) {
this.tool_choice = tool_choice;
}
public Tool[] getTools() {
return tools;
}
public void setTools(Tool[] tools) {
this.tools = tools;
}
}
package cn.com.poc.thirdparty.resource.demand.ai.function;
import java.util.List;
public abstract class AbstractLargeModelFunction {
public abstract String doFunction(String content, String key);
/**
* 获取配置
*/
public abstract List<String> getLLMConfig();
/**
* 获取有关变量的配置
*/
public abstract List<String> getVariableStructureLLMConfig(String[] variableStructure);
}
package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.utils.SpringUtils;
public enum LargeModelFunctionEnum {
set_long_memory(SetLongMemoryFunction.class),
set_value_memory(SetValueMemoryFunction.class),
search_memory_content(SearchMemoryContentFunction.class),
search_memory_content_by_enum(SearchMemoryContentByNameFunction.class);
private Class<? extends AbstractLargeModelFunction> function;
LargeModelFunctionEnum(Class<? extends AbstractLargeModelFunction> function) {
this.function = function;
}
public AbstractLargeModelFunction getFunction() {
return SpringUtils.getBean(function);
}
public void setFunction(Class<AbstractLargeModelFunction> function) {
this.function = 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 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> getLLMConfig() {
return null;
}
@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;
@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","信息说明");
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", "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;
}
@Override
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
return null;
}
}
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.com.poc.common.utils.DateUtils;
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 SetLongMemoryFunction extends AbstractLargeModelFunction {
@Resource
private RedisService redisService;
@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(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);
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;
}
@Override
public List<String> getVariableStructureLLMConfig(String[] variableStructure) {
return null;
}
}
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
//todo 保存【变量】方法重构
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> getLLMConfig() {
return null;
}
@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;
}
}
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