Commit 9a822a4e authored by alex yao's avatar alex yao

feat:知识库超纲回复

parent 9acbfe59
......@@ -4,6 +4,9 @@ import cn.com.poc.agent_application.domain.FunctionResult;
import cn.com.poc.agent_application.entity.AgentResultEntity;
import cn.com.poc.agent_application.entity.BizAgentApplicationInfoEntity;
import cn.com.poc.agent_application.entity.CreateAgentTitleAndDescEntity;
import cn.com.poc.agent_application.entity.KnowledgeSuperclassProblemConfig;
import cn.com.poc.agent_application.entity.call_agent.CallAgentApplicationKnowledgeConfig;
import cn.com.poc.agent_application.entity.call_agent.CallAgentApplicationModelConfig;
import cn.com.poc.thirdparty.resource.demand.ai.constants.KnowledgeSearchTypeEnum;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Message;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool;
......@@ -56,6 +59,7 @@ public interface AgentApplicationInfoService {
* @param topK 知识库参数topK
* @param knowledgeSearchType 知识库参数知识搜索类型
*/
@Deprecated
AgentResultEntity callAgentApplication(String agentId, String identifier, String largeModel, String agentSystem,
Integer[] knowledgeIds, Integer communicationTurn, Float topP, Float temperature,
List<Message> messages, List<Tool> tools, List<String> fileUrls, boolean stream, List<String> imageUrls,
......@@ -64,28 +68,30 @@ public interface AgentApplicationInfoService {
/**
* Agent应用对话
*
* @param agentId 应用ID
* @param identifier 对话唯一标识
* @param largeModel 模型
* @param agentSystem 应用角色指令
* @param knowledgeIds 知识库ID
* @param communicationTurn 对话轮数
* @param topP 模型参数topP
* @param temperature 模型参数temperature
* @param messages 对话消息
* @param tools 插件配置
* @param functionCallResult 插件回调结果
* @param fileUrls 文件URLs
* @param imageUrls 图片URLs
* @param stream 是否流式传输
* @param score 知识库参数score
* @param topK 知识库参数topK
* @param knowledgeSearchType 知识库参数知识搜索类型
* @param agentId 应用ID
* @param identifier 对话唯一标识
* @param largeModel 模型
* @param agentSystem 应用角色指令
* @param knowledgeIds 知识库ID
* @param communicationTurn 对话轮数
* @param topP 模型参数topP
* @param temperature 模型参数temperature
* @param messages 对话消息
* @param tools 插件配置
* @param functionCallResult 插件回调结果
* @param fileUrls 文件URLs
* @param imageUrls 图片URLs
* @param stream 是否流式传输
* @param score 知识库参数score
* @param topK 知识库参数topK
* @param knowledgeSearchType 知识库参数知识搜索类型
* @param superclassProblemConfig 知识库参数超级类问题配置
*/
AgentResultEntity callAgentApplication(String agentId, String identifier, String largeModel, String agentSystem,
Integer[] knowledgeIds, Integer communicationTurn, Float topP, Float temperature,
List<Message> messages, List<Tool> tools, FunctionCallResult functionCallResult, List<String> fileUrls, boolean stream, List<String> imageUrls,
Double score, Integer topK, KnowledgeSearchTypeEnum knowledgeSearchType, HttpServletResponse httpServletResponse) throws Exception;
Double score, Integer topK, KnowledgeSearchTypeEnum knowledgeSearchType, KnowledgeSuperclassProblemConfig superclassProblemConfig, HttpServletResponse httpServletResponse) throws Exception;
/**
* 应用下架
......
......@@ -4,9 +4,12 @@ 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.constant.AgentApplicationKnowledgeConstants;
import cn.com.poc.agent_application.domain.FunctionResult;
import cn.com.poc.agent_application.dto.KnowledgeContentResult;
import cn.com.poc.agent_application.entity.KnowledgeContentResult;
import cn.com.poc.agent_application.entity.*;
import cn.com.poc.agent_application.entity.call_agent.CallAgentApplicationKnowledgeConfig;
import cn.com.poc.agent_application.entity.call_agent.CallAgentApplicationModelConfig;
import cn.com.poc.agent_application.query.DialogsIdsQueryByAgentIdQueryItem;
import cn.com.poc.agent_application.service.*;
import cn.com.poc.agent_application.utils.AgentApplicationTools;
......@@ -188,7 +191,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
FunctionResult functionResult = functionCall(dialogueId, messages, toolArray, agentId, fileUrls, imageUrls);
List<KnowledgeContentResult> knowledgeResult = knowledge(kdIds, messages, topK, score, KnowledgeSearchTypeEnum.MIX);
List<KnowledgeContentResult> knowledgeResult = knowledge(kdIds, messages, topK, score, knowledgeSearchType);
String promptTemplate = buildDialogsPrompt(functionResult, agentSystem, toolArray, dialogueId, agentId, knowledgeResult);
......@@ -198,16 +201,35 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
@Override
public AgentResultEntity callAgentApplication(String agentId, String dialogueId, String largeModel, String agentSystem, Integer[] kdIds, Integer communicationTurn, Float topP, Float temperature, List<Message> messages, List<Tool> tools, FunctionCallResult functionCallResult, List<String> fileUrls, boolean stream, List<String> imageUrls,
Double score, Integer topK, KnowledgeSearchTypeEnum knowledgeSearchType, HttpServletResponse httpServletResponse) throws Exception {
public AgentResultEntity callAgentApplication(String agentId, String dialogueId, String largeModel, String agentSystem, Integer[] kdIds,
Integer communicationTurn, Float topP, Float temperature, List<Message> messages, List<Tool> tools, FunctionCallResult functionCallResult, List<String> fileUrls, boolean stream, List<String> imageUrls,
Double score, Integer topK, KnowledgeSearchTypeEnum knowledgeSearchType, KnowledgeSuperclassProblemConfig superclassProblemConfig, HttpServletResponse httpServletResponse) throws Exception {
String model = modelConvert(largeModel);
List<KnowledgeContentResult> knowledgeResult = knowledge(kdIds, messages, topK, score, knowledgeSearchType);
if (superclassProblem(superclassProblemConfig, kdIds, knowledgeResult)) {
String mess = superclassProblemConfig.getKnowledgeCustomResponse()[0];
logger.info("触发知识库超纲回答,mess{}", mess);
if (stream) {
PrintWriter writer = httpServletResponse.getWriter();
LargeModelDemandResult result = new LargeModelDemandResult();
result.setCode("0");
result.setMessage(mess);
writer.write(EVENT_STREAM_PREFIX + JsonUtils.serialize(result) + "\n\n");
writer.write(EVENT_STREAM_PREFIX + "[DONE]" + "\n\n");
writer.flush();
writer.close();
}
AgentResultEntity agentResultEntity = new AgentResultEntity();
agentResultEntity.setMessage(mess);
return agentResultEntity;
}
Tool[] toolArray = tools.toArray(new Tool[0]);
FunctionResult functionResult = functionCall(dialogueId, functionCallResult, agentId);
List<KnowledgeContentResult> knowledgeResult = knowledge(kdIds, messages, topK, score, KnowledgeSearchTypeEnum.MIX);
String promptTemplate = buildDialogsPrompt(functionResult, agentSystem, toolArray, dialogueId, agentId, knowledgeResult);
Message[] messageArray = buildMessages(messages, communicationTurn, promptTemplate);
......@@ -215,6 +237,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
return llmExecutorAndOutput(topP, stream, model, messageArray, functionResult, knowledgeResult, httpServletResponse);
}
@Override
public void createAgentSystem(String input, HttpServletResponse httpServletResponse) throws Exception {
BizAgentApplicationGcConfigEntity configEntity = bizAgentApplicationGcConfigService.getByConfigCode(AgentApplicationGCConfigConstants.AGENT_SYSTEM);
......@@ -549,6 +572,10 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
result.setKnowledgeContentResult(knowledgeResult);
writer.write(EVENT_STREAM_PREFIX + JsonUtils.serialize(result) + "\n\n");
writer.flush();
} else {
//todo 处理知识库超纲问题
}
if (ObjectUtil.isNotNull(functionResult) && StringUtils.isNotBlank(functionResult.getFunctionName())) {
LargeModelDemandResult result = new LargeModelDemandResult();
......@@ -1091,4 +1118,16 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
MemoryVariableWriter.addItem(identifier, map);
}
}
private boolean superclassProblem(KnowledgeSuperclassProblemConfig superclassProblemConfig, Integer[] kdIds, List<KnowledgeContentResult> knowledgeContentResult) {
if (ArrayUtils.isNotEmpty(kdIds) && CollectionUtils.isEmpty(knowledgeContentResult)) {
String knowledgeResponseType = superclassProblemConfig.getKnowledgeResponseType();
if (AgentApplicationKnowledgeConstants.SUPER_CLASS_PROBLEM_TYPE.CUSTOM.equals(knowledgeResponseType)
&& ArrayUtils.isNotEmpty(superclassProblemConfig.getKnowledgeCustomResponse())
&& StringUtils.isNotBlank(superclassProblemConfig.getKnowledgeCustomResponse()[0])) {
return true;
}
}
return false;
}
}
package cn.com.poc.agent_application.constant;
/**
* @author alex.yao
* @date 2025/3/3
*/
public interface AgentApplicationKnowledgeConstants {
interface SUPER_CLASS_PROBLEM_TYPE {
String DEFAULT = "default";
String CUSTOM = "custom";
}
}
......@@ -279,7 +279,7 @@ public class BizAgentApplicationInfoEntity {
/**
* knowledge_response_type
* 知识库回复类型
* 知识库超纲回复类型
* default-默认
* custom-自定义
*/
......@@ -296,7 +296,7 @@ public class BizAgentApplicationInfoEntity {
/**
* knowledge_custom_response
* 知识库自定义回复内容
* 知识库超纲自定义回复内容-当knowledge_response_type为custom时生效
*/
private String[] knowledgeCustomResponse;
......
package cn.com.poc.agent_application.entity;
/**
* @author alex.yao
* @date 2025/3/3
*/
public class KnowledgeSuperclassProblemConfig {
/**
* knowledge_response_type
* 知识库超纲回复类型
* default-默认
* custom-自定义
*/
private String knowledgeResponseType;
public String getKnowledgeResponseType() {
return knowledgeResponseType;
}
public void setKnowledgeResponseType(String knowledgeResponseType) {
this.knowledgeResponseType = knowledgeResponseType;
}
/**
* knowledge_custom_response
* 知识库超纲自定义回复内容-当knowledge_response_type为custom时生效
*/
private String[] knowledgeCustomResponse;
public String[] getKnowledgeCustomResponse() {
return knowledgeCustomResponse;
}
public void setKnowledgeCustomResponse(String[] knowledgeCustomResponse) {
this.knowledgeCustomResponse = knowledgeCustomResponse;
}
}
package cn.com.poc.agent_application.entity.call_agent;
import cn.com.poc.thirdparty.resource.demand.ai.constants.KnowledgeSearchTypeEnum;
/**
* @author alex.yao
* @date 2025/2/28
*/
public class CallAgentApplicationKnowledgeConfig {
/**
* 知识库ID数组
*/
private Integer[] knowledgeIds;
/**
* 知识库检索得分阈值
*/
private Double score;
/**
* 知识库检索数量上限
*/
private Integer topK;
/**
* 知识库检索类型
*/
private KnowledgeSearchTypeEnum searchTypeEnum;
/**
* knowledge_response_type
* 知识库超纲回复类型
* default-默认
* custom-自定义
*/
private String knowledgeResponseType;
/**
* knowledge_custom_response
* 知识库自定义回复内容
*/
private String[] knowledgeCustomResponse;
public Integer[] getKnowledgeIds() {
return knowledgeIds;
}
public void setKnowledgeIds(Integer[] knowledgeIds) {
this.knowledgeIds = knowledgeIds;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public Integer getTopK() {
return topK;
}
public void setTopK(Integer topK) {
this.topK = topK;
}
public KnowledgeSearchTypeEnum getSearchTypeEnum() {
return searchTypeEnum;
}
public void setSearchTypeEnum(KnowledgeSearchTypeEnum searchTypeEnum) {
this.searchTypeEnum = searchTypeEnum;
}
public String getKnowledgeResponseType() {
return knowledgeResponseType;
}
public void setKnowledgeResponseType(String knowledgeResponseType) {
this.knowledgeResponseType = knowledgeResponseType;
}
public String[] getKnowledgeCustomResponse() {
return knowledgeCustomResponse;
}
public void setKnowledgeCustomResponse(String[] knowledgeCustomResponse) {
this.knowledgeCustomResponse = knowledgeCustomResponse;
}
}
package cn.com.poc.agent_application.entity.call_agent;
/**
* @author alex.yao
* @date 2025/2/28
*/
public class CallAgentApplicationModelConfig {
private String largeModel;
private String agentSystem;
private Integer communicationTurn;
private Float topP;
private Float temperature;
private Boolean stream;
public String getLargeModel() {
return largeModel;
}
public void setLargeModel(String largeModel) {
this.largeModel = largeModel;
}
public String getAgentSystem() {
return agentSystem;
}
public void setAgentSystem(String agentSystem) {
this.agentSystem = agentSystem;
}
public Integer getCommunicationTurn() {
return communicationTurn;
}
public void setCommunicationTurn(Integer communicationTurn) {
this.communicationTurn = communicationTurn;
}
public Float getTopP() {
return topP;
}
public void setTopP(Float topP) {
this.topP = topP;
}
public Float getTemperature() {
return temperature;
}
public void setTemperature(Float temperature) {
this.temperature = temperature;
}
}
......@@ -249,6 +249,10 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
//获取知识库配置
List<Integer> kdIds = knowledgeService.getKdIdsByKnowledgeInfoIds(infoEntity.getKnowledgeIds());
//知识库超纲问题配置
KnowledgeSuperclassProblemConfig superclassProblemConfig = new KnowledgeSuperclassProblemConfig();
superclassProblemConfig.setKnowledgeResponseType(infoEntity.getKnowledgeResponseType());
superclassProblemConfig.setKnowledgeCustomResponse(infoEntity.getKnowledgeCustomResponse());
//配置对话function
List<Tool> tools = AgentApplicationTools.buildFunctionConfig(infoEntity.getVariableStructure(), infoEntity.getIsLongMemory(), dialogueId, agentId, infoEntity.getUnitIds(), infoEntity.getIsDocumentParsing());
......@@ -276,7 +280,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
agentSystem, kdIds.toArray(new Integer[0]), communicationTurn, topP,
temperature, dto.getMessages(), tools, checkPluginUseEntity.getFunctionCallResult(), dto.getFileUrls(), true, imageUrls,
infoEntity.getKnowledgeSimilarity(), infoEntity.getKnowledgeNResult(), KnowledgeSearchTypeEnum.valueOf(infoEntity.getKnowledgeSearchType()),
httpServletResponse);
superclassProblemConfig, httpServletResponse);
//数据采集
if (StringUtils.isBlank(dto.getChannel())) {
dto.setChannel(DataAnalyzeChannelEnum.preview.getChannel());
......
......@@ -138,6 +138,11 @@ public class AgentApplicationApiServiceImpl implements AgentApplicationApiServic
//获取知识库配置
List<Integer> kdIdList = knowledgeService.getKdIdsByKnowledgeInfoIds(infoEntity.getKnowledgeIds());
//知识库超纲问题配置
KnowledgeSuperclassProblemConfig superclassProblemConfig = new KnowledgeSuperclassProblemConfig();
superclassProblemConfig.setKnowledgeResponseType(infoEntity.getKnowledgeResponseType());
superclassProblemConfig.setKnowledgeCustomResponse(infoEntity.getKnowledgeCustomResponse());
// 构造对话参数
List<Message> messages = buildMessages(conversationId, agentId, profileEntity.getMemberId(), query);
......@@ -167,7 +172,7 @@ public class AgentApplicationApiServiceImpl implements AgentApplicationApiServic
infoEntity.getAgentSystem(), kdIdList.toArray(new Integer[0]), infoEntity.getCommunicationTurn(),
infoEntity.getTopP(), infoEntity.getTemperature(), messages, tools, checkPluginUseEntity.getFunctionCallResult(), fileUrls, stream, imageUrls,
infoEntity.getKnowledgeSimilarity(), infoEntity.getKnowledgeNResult(), KnowledgeSearchTypeEnum.valueOf(infoEntity.getKnowledgeSearchType()),
httpServletResponse);
superclassProblemConfig, httpServletResponse);
saveRecord(conversationId, query, agentId, profileEntity, inputTimestamp, infoEntity, agentResultEntity.getMessage());
} catch (Exception e) {
memberEquityService.rollbackPoint(reduceSn);
......
......@@ -133,6 +133,10 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
//获取知识库配置
List<Integer> kdIdList = knowledgeService.getKdIdsByKnowledgeInfoIds(infoEntity.getKnowledgeIds());
//知识库超纲问题配置
KnowledgeSuperclassProblemConfig knowledgeSuperclassProblemConfig = new KnowledgeSuperclassProblemConfig();
knowledgeSuperclassProblemConfig.setKnowledgeResponseType(infoEntity.getKnowledgeResponseType());
knowledgeSuperclassProblemConfig.setKnowledgeCustomResponse(infoEntity.getKnowledgeCustomResponse());
// 构造对话参数
List<Message> messages = buildMessages(dialogsId, agentId, userBaseEntity.getUserId(), input);
......@@ -185,7 +189,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
infoEntity.getAgentSystem(), kdIdList.toArray(new Integer[0]), infoEntity.getCommunicationTurn(),
infoEntity.getTopP(), infoEntity.getTemperature(), messages, tools, checkPluginUseEntity.getFunctionCallResult(), fileUrls, true, imageUrls,
infoEntity.getKnowledgeSimilarity(), infoEntity.getKnowledgeNResult(), KnowledgeSearchTypeEnum.valueOf(infoEntity.getKnowledgeSearchType()),
httpServletResponse);
knowledgeSuperclassProblemConfig, httpServletResponse);
//保存对话记录
outputRecord.setContent(agentResultEntity.getMessage());
......
......@@ -10,6 +10,6 @@ public enum KnowledgeSearchTypeEnum {
MIX,
SEMANTIC;
SEMANTICS;
}
package cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel;
import cn.com.poc.agent_application.dto.KnowledgeContentResult;
import cn.com.poc.agent_application.entity.KnowledgeContentResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Usage;
import cn.com.poc.thirdparty.resource.demand.dgTools.result.AbstractResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.ToolFunction;
......
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