Commit 5e8e4b56 authored by jennie chen's avatar jennie chen

变量记忆,长期记忆功能

parent 86b395ce
......@@ -19,7 +19,7 @@ public interface AgentApplicationInfoService {
*/
String callAgentApplication(String largeModel, String[] unitIds, String agentSystem,
String[] knowledgeIds, Integer communicationTurn, Float topP,
List<Message> messages, HttpServletResponse httpServletResponse) throws Exception;
List<Message> messages, Boolean isLongMemory, String[] variableStructure, String useStatus, HttpServletResponse httpServletResponse) throws Exception;
/**
......
......@@ -16,12 +16,12 @@ import cn.com.poc.common.utils.JsonUtils;
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;
......@@ -32,17 +32,13 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
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.*;
import java.util.stream.Collectors;
@Component
......@@ -72,6 +68,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
@Resource
private LLMService llmService;
@Override
public boolean updateAndPublish(BizAgentApplicationInfoEntity bizAgentApplicationInfoEntity) throws Exception {
UserBaseEntity userBaseEntity = BlContext.getCurrentUserNotException();
......@@ -93,17 +90,69 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
@Override
public String callAgentApplication(String largeModel, String[] unitIds, String agentSystem, String[] knowledgeIds, Integer communicationTurn, Float topP, List<Message> messages, 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, 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);
//todo 调用组件
String promptTemplate = buildDialogsPrompt(messages, agentSystem, knowledgeIds);
Message[] messageArray = buildMessages(messages, communicationTurn, promptTemplate);
BufferedReader bufferedReader = invokeLLM(largeModel, messageArray, topP);
BufferedReader bufferedReader = this.invokeLLM(largeModel, messageArray, topP, toolsConfig(variableStructure, isLongMemory), useStatus, isLongMemory);
textOutput(httpServletResponse, bufferedReader);
return null;
}
return textOutput(httpServletResponse, bufferedReader);
private Tool[] toolsConfig(String[] variableStructure, boolean isLongMemory){
// 配置tools
Tool[] tools = new Tool[AgentApplicationConstants.TOOLS.FUNCTION_NUMBER];
tools[0] = new Tool();
tools[0].setType("function");
// 根据变量名保存记忆方法
AbstractLargeModelFunction function = LargeModelFunctionEnum.valueOf("setValueMemory").getFunction();
Map<String, Object> parameters = function.getParameters();
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", "内容值");
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;
}
@Override
......@@ -348,18 +397,107 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
* @return
* @throws Exception
*/
private BufferedReader invokeLLM(String largeModel, Message[] messageArray, Float topP) throws Exception {
// todo 配置tools
private BufferedReader invokeLLM(String largeModel, Message[] messageArray, Float topP, Tool[] tools, String useStatus, Boolean isLongMemory) throws Exception {
LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setModel(largeModel);
largeModelResponse.setMessages(messageArray);
largeModelResponse.setTopP(topP);
largeModelResponse.setStream(true);
largeModelResponse.setUser("POE");
return llmService.chatChunk(largeModelResponse);
largeModelResponse.setTools(tools);
largeModelResponse.setTool_choice("auto");
BufferedReader bufferedReader = llmService.chatChunk(largeModelResponse);
String res = "";
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;
StringBuffer finishReason = new StringBuffer();
StringBuffer functionName = new StringBuffer();
StringBuffer functionArguments = new StringBuffer();
boolean isFunctionCall = false;
while ((res = bufferedReader.readLine()) != null) {
if (StringUtils.isBlank(res)) {
continue;
}
// 添加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;
}
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());
}
}
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()) {
// 执行函数返回结果
ExecuteLargeModelFunctionEnum functionEnum = ExecuteLargeModelFunctionEnum.valueOf(functionName.toString());
AbstractExecuteLargeModelFunction executeFunction = functionEnum.getExecuteFunction();
functionResult = executeFunction.doMemoryFunction(functionArguments.toString(), useStatus, isLongMemory);
}
if (functionResult != null) {
// 处理function - 1,获取function_call 2. 调用对应function 3. 返回function_call并且调用LLM 4.获取bufferedReader 返回
Message assistantMessage = new Message();
assistantMessage.setRole("assistant");
assistantMessage.setContent(null);
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);
}
}
}
// 若不为function_call,则直接返回bufferedReader
return bufferedReader;
//todo
}
/**
......
......@@ -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"; // 草稿
......
......@@ -29,6 +29,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));
}
......@@ -68,6 +69,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()));
}
......@@ -114,6 +116,7 @@ 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());
......@@ -158,6 +161,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())) {
......@@ -190,6 +194,7 @@ 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));
}
......
......@@ -26,6 +26,7 @@ public class BizAgentApplicationPublishConvert {
entity.setAgentDesc(model.getAgentDesc());
entity.setAgentSystem(model.getAgentSystem());
entity.setPreamble(model.getPreamble());
entity.setIsLongMemory(model.getIsLongMemory());
if(StringUtils.isNotBlank(model.getVariableStructure())) {
entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), String[].class));
}
......@@ -63,6 +64,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()));
}
......@@ -109,6 +111,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());
......@@ -150,6 +153,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());
}
......
......@@ -88,4 +88,17 @@ public class AgentApplicationCommConfig {
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 否
*/
......
......@@ -247,6 +247,19 @@ 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,20 @@ 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,21 @@ 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 否
......
......@@ -312,6 +312,21 @@ public class BizAgentApplicationPublishModel extends BaseModelClass implements S
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,21 @@ 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;
}
/**
* is_deleted
* is_deleted
......
package cn.com.poc.agent_application.rest.impl;
import cn.com.poc.agent_application.aggregate.AgentApplicationInfoService;
import cn.com.poc.agent_application.constant.AgentApplicationConstants;
import cn.com.poc.agent_application.convert.AgentApplicationInfoConvert;
import cn.com.poc.agent_application.convert.BizAgentApplicationLargeModelListConvert;
import cn.com.poc.agent_application.dto.*;
import cn.com.poc.agent_application.entity.BizAgentApplicationInfoEntity;
import cn.com.poc.agent_application.entity.BizAgentApplicationLargeModelListEntity;
import cn.com.poc.agent_application.entity.BizAgentApplicationPublishEntity;
import cn.com.poc.agent_application.entity.CreateAgentTitleAndDescEntity;
import cn.com.poc.agent_application.query.AgentApplicationInfoQueryCondition;
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.support.security.oauth.entity.UserBaseEntity;
import cn.com.yict.framemax.core.exception.BusinessException;
......@@ -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(), httpServletResponse);
, dto.getMessages(), infoEntity.getIsLongMemory().equals(CommonConstant.IsDeleted.Y), infoEntity.getVariableStructure(), AgentApplicationConstants.USE_AGENT_STATUS.PREVIEW,httpServletResponse);
} catch (Exception e) {
httpServletResponse.setContentType("text/event-stream");
PrintWriter writer = httpServletResponse.getWriter();
......
......@@ -6,16 +6,14 @@ import cn.com.poc.agent_application.constant.AgentApplicationDialoguesRecordCons
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.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;
......@@ -77,10 +75,9 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
List<Message> messages = new ArrayList<>();
buildMessages(dialogsId, agentId, userBaseEntity.getUserId(), messages, input);
String output = agentApplicationInfoService.callAgentApplication(infoEntity.getLargeModel(), infoEntity.getUnitIds()
, infoEntity.getAgentSystem(), infoEntity.getKnowledgeIds(), infoEntity.getCommunicationTurn(), infoEntity.getTopP()
, messages, httpServletResponse);
, messages, infoEntity.getIsLongMemory().equals(CommonConstant.IsDeleted.Y), infoEntity.getVariableStructure(), AgentApplicationConstants.USE_AGENT_STATUS.PUBLISH, httpServletResponse);
//保存对话记录
......
package cn.com.poc.thirdparty.resource.demand.ai.common.domain;
import java.util.Map;
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 cn.com.poc.thirdparty.resource.demand.ai.function.AbstractLargeModelFunction;
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 AbstractLargeModelFunction function;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public AbstractLargeModelFunction getFunction() {
return function;
}
public void setFunction(AbstractLargeModelFunction 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;
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;
}
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;
}
}
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.com.poc.common.utils.DateUtils;
import cn.hutool.json.JSONObject;
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 ExecuteSetLongMemoryFunction implements AbstractExecuteLargeModelFunction{
@Resource
private RedisService redisService;
@Override
public String doMemoryFunction(String content, String useStatus, Boolean isLongMemory) {
// 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);
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(key, result);
}
return "SUCCESS";
}
}
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;
public enum LargeModelFunctionEnum {
setLongMemory(new SetLongMemoryLargeModelFunction()),
setValueMemory(new SetValueMemoryLargeModelFunction()),
searchMemoryContent(new SearchMemoryContentFunction()),
search_memory_content_by_Enum(new SearchMemoryContentByEnumFunction());
private AbstractLargeModelFunction function;
LargeModelFunctionEnum(AbstractLargeModelFunction function){
this.function = function;
}
public AbstractLargeModelFunction getFunction() {
return function;
}
public void setFunction(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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SearchMemoryContentFunction extends AbstractLargeModelFunction{
public SearchMemoryContentFunction() {
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("search_memory_content");
super.setDescription("获取用户相关信息");
super.setParameters(parameters);
}
}
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 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