ai聊天input修改
This commit is contained in:
@@ -290,7 +290,30 @@ public class AgentChatServiceImpl implements AgentChatService {
|
|||||||
// 4. 生成临时消息ID(sessionId)
|
// 4. 生成临时消息ID(sessionId)
|
||||||
String sessionId = IdUtil.getSnowflakeId();
|
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<>();
|
Map<String, Object> sessionData = new HashMap<>();
|
||||||
sessionData.put("agentId", agentId);
|
sessionData.put("agentId", agentId);
|
||||||
sessionData.put("chatId", chatId);
|
sessionData.put("chatId", chatId);
|
||||||
@@ -300,7 +323,8 @@ public class AgentChatServiceImpl implements AgentChatService {
|
|||||||
sessionData.put("apiKey", agent.getApiKey());
|
sessionData.put("apiKey", agent.getApiKey());
|
||||||
sessionData.put("outer", agent.getIsOuter());
|
sessionData.put("outer", agent.getIsOuter());
|
||||||
sessionData.put("service", prepareData.getService());
|
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;
|
String cacheKey = CHAT_SESSION_PREFIX + sessionId;
|
||||||
redisService.set(cacheKey, sessionData, SESSION_TTL, TimeUnit.SECONDS);
|
redisService.set(cacheKey, sessionData, SESSION_TTL, TimeUnit.SECONDS);
|
||||||
@@ -334,9 +358,9 @@ public class AgentChatServiceImpl implements AgentChatService {
|
|||||||
String query = (String) sessionData.get("query");
|
String query = (String) sessionData.get("query");
|
||||||
String userId = (String) sessionData.get("userId");
|
String userId = (String) sessionData.get("userId");
|
||||||
String apiKey = (String) sessionData.get("apiKey");
|
String apiKey = (String) sessionData.get("apiKey");
|
||||||
String service = (String) sessionData.get("service");
|
|
||||||
Boolean outer = (Boolean) sessionData.get("outer");
|
@SuppressWarnings("unchecked")
|
||||||
Boolean isGuest = (Boolean) sessionData.get("isGuest");
|
Map<String, Object> inputsMap = (Map<String, Object>) sessionData.get("inputsMap");
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
List<DifyFileInfo> filesData = (List<DifyFileInfo>) sessionData.get("filesData");
|
List<DifyFileInfo> filesData = (List<DifyFileInfo>) sessionData.get("filesData");
|
||||||
@@ -371,23 +395,12 @@ public class AgentChatServiceImpl implements AgentChatService {
|
|||||||
chatRequest.setQuery(query);
|
chatRequest.setQuery(query);
|
||||||
chatRequest.setUser(userId);
|
chatRequest.setUser(userId);
|
||||||
chatRequest.setResponseMode("streaming");
|
chatRequest.setResponseMode("streaming");
|
||||||
Map<String, Object> inputsMap = new HashMap<>();
|
|
||||||
|
// 使用从Redis获取的inputsMap,如果为空则创建新的
|
||||||
|
if (inputsMap == null) {
|
||||||
|
inputsMap = new HashMap<>();
|
||||||
|
}
|
||||||
chatRequest.setInputs(inputsMap); // Dify API 要求 inputs 必传
|
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());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (filesData != null && !filesData.isEmpty()) {
|
if (filesData != null && !filesData.isEmpty()) {
|
||||||
chatRequest.setFiles(filesData);
|
chatRequest.setFiles(filesData);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.xyzh.api.ai.dto;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -31,4 +32,7 @@ public class ChatPrepareData implements Serializable {
|
|||||||
|
|
||||||
@Schema(description = "服务名称")
|
@Schema(description = "服务名称")
|
||||||
private String service;
|
private String service;
|
||||||
|
|
||||||
|
@Schema(description = "智能体输入参数,不同智能体可能需要不同的输入参数")
|
||||||
|
private Map<String, Object> inputsMap;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user