ai聊天input修改

This commit is contained in:
2026-01-01 13:12:42 +08:00
parent a07daa715a
commit 4e373e6d2c
2 changed files with 41 additions and 24 deletions

View File

@@ -256,7 +256,7 @@ public class AgentChatServiceImpl implements AgentChatService {
String agentId = prepareData.getAgentId();
String chatId = prepareData.getChatId();
String query = prepareData.getQuery();
// 1. 校验智能体
ResultDomain<TbAgent> agentResult = agentService.selectAgentById(agentId);
if (!agentResult.getSuccess() || agentResult.getData() == null || !agentResult.getData().getIsOuter()) {
@@ -269,7 +269,7 @@ public class AgentChatServiceImpl implements AgentChatService {
chatFilter.setAgentId(agentId);
chatFilter.setUserId(prepareData.getUserId());
chatFilter.setUserType(prepareData.getUserType());
LoginDomain loginDomain = LoginUtil.getCurrentLogin();
String userId = loginDomain.getUser().getUserId();
if (userId == null) {
@@ -290,7 +290,30 @@ public class AgentChatServiceImpl implements AgentChatService {
// 4. 生成临时消息IDsessionId
String sessionId = IdUtil.getSnowflakeId();
// 5. 存储会话数据到Redis
// 5. 准备 inputs 参数
Map<String, Object> inputsMap = prepareData.getInputsMap();
if (inputsMap == null) {
inputsMap = new HashMap<>();
}
// 处理动态知识库
Boolean isGuest = "guest".equals(loginDomain.getUser().getStatus());
if (agent.getIsOuter() && NonUtils.isNotEmpty(prepareData.getService())) {
TbKnowledge filter = new TbKnowledge();
filter.setService(prepareData.getService());
filter.setCategory(isGuest ? "external" : "internal");
ResultDomain<TbKnowledge> knowledgeRD = knowledgeService.listKnowledges(filter);
List<String> datasets = new ArrayList<>();
if (knowledgeRD.getSuccess()) {
datasets = knowledgeRD.getDataList().stream()
.map(TbKnowledge::getDifyDatasetId)
.toList();
}
inputsMap.put("datasets", JSON.toJSONString(datasets));
inputsMap.put("dataset_apikey", difyConfig.getKnowledgeApiKey());
}
// 6. 存储会话数据到Redis
Map<String, Object> sessionData = new HashMap<>();
sessionData.put("agentId", agentId);
sessionData.put("chatId", chatId);
@@ -300,7 +323,8 @@ public class AgentChatServiceImpl implements AgentChatService {
sessionData.put("apiKey", agent.getApiKey());
sessionData.put("outer", agent.getIsOuter());
sessionData.put("service", prepareData.getService());
sessionData.put("isGuest", "guest".equals(loginDomain.getUser().getStatus()));
sessionData.put("isGuest", isGuest);
sessionData.put("inputsMap", inputsMap); // 存储处理好的 inputs
String cacheKey = CHAT_SESSION_PREFIX + sessionId;
redisService.set(cacheKey, sessionData, SESSION_TTL, TimeUnit.SECONDS);
@@ -334,9 +358,9 @@ public class AgentChatServiceImpl implements AgentChatService {
String query = (String) sessionData.get("query");
String userId = (String) sessionData.get("userId");
String apiKey = (String) sessionData.get("apiKey");
String service = (String) sessionData.get("service");
Boolean outer = (Boolean) sessionData.get("outer");
Boolean isGuest = (Boolean) sessionData.get("isGuest");
@SuppressWarnings("unchecked")
Map<String, Object> inputsMap = (Map<String, Object>) sessionData.get("inputsMap");
@SuppressWarnings("unchecked")
List<DifyFileInfo> filesData = (List<DifyFileInfo>) sessionData.get("filesData");
@@ -352,7 +376,7 @@ public class AgentChatServiceImpl implements AgentChatService {
userMessage.setChatId(chatId);
userMessage.setRole("user");
userMessage.setContent(query);
// 提取系统文件ID列表保存到消息中
if (filesData != null && !filesData.isEmpty()) {
List<String> sysFileIds = filesData.stream()
@@ -363,7 +387,7 @@ public class AgentChatServiceImpl implements AgentChatService {
userMessage.setFiles(sysFileIds);
}
}
chatMessageMapper.insertChatMessage(userMessage);
// 5. 构建Dify请求
@@ -371,23 +395,12 @@ public class AgentChatServiceImpl implements AgentChatService {
chatRequest.setQuery(query);
chatRequest.setUser(userId);
chatRequest.setResponseMode("streaming");
Map<String, Object> inputsMap = new HashMap<>();
chatRequest.setInputs(inputsMap); // Dify API 要求 inputs 必传
// 处理动态知识库的问题
if(outer && NonUtils.isNotEmpty(service)){
TbKnowledge filter = new TbKnowledge();
filter.setService(service);
filter.setCategory(isGuest?"external":"internal");
ResultDomain<TbKnowledge> knowledgeRD = knowledgeService.listKnowledges(filter);
List<String> datasets = new ArrayList<>();
if(knowledgeRD.getSuccess()){
datasets = knowledgeRD.getDataList().stream().map(TbKnowledge::getDifyDatasetId).toList();
}
inputsMap.put("datasets", JSON.toJSONString(datasets));
inputsMap.put("dataset_apikey", difyConfig.getKnowledgeApiKey());
// 使用从Redis获取的inputsMap如果为空则创建新的
if (inputsMap == null) {
inputsMap = new HashMap<>();
}
chatRequest.setInputs(inputsMap); // Dify API 要求 inputs 必传
if (filesData != null && !filesData.isEmpty()) {
chatRequest.setFiles(filesData);