mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-11 03:58:14 +00:00
(improvement)(Chat) Split chatService into chatManageService and chatService (#826)
Co-authored-by: jolunoluo
This commit is contained in:
@@ -7,7 +7,7 @@ import com.tencent.supersonic.chat.api.pojo.request.PageQueryInfoReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.QueryResp;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.ShowCaseResp;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatDO;
|
||||
import com.tencent.supersonic.chat.server.service.ChatService;
|
||||
import com.tencent.supersonic.chat.server.service.ChatManageService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
@@ -23,9 +24,9 @@ import java.util.List;
|
||||
@RequestMapping({"/api/chat/manage", "/openapi/chat/manage"})
|
||||
public class ChatController {
|
||||
|
||||
private final ChatService chatService;
|
||||
private final ChatManageService chatService;
|
||||
|
||||
public ChatController(ChatService chatService) {
|
||||
public ChatController(ChatManageService chatService) {
|
||||
this.chatService = chatService;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.tencent.supersonic.chat.server.service;
|
||||
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatExecuteReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatParseReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.PageQueryInfoReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.QueryResp;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.ShowCaseResp;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatParseDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatQueryDO;
|
||||
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.QueryResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ChatManageService {
|
||||
Boolean addChat(User user, String chatName, Integer agentId);
|
||||
|
||||
List<ChatDO> getAll(String userName, Integer agentId);
|
||||
|
||||
boolean updateChatName(Long chatId, String chatName, String userName);
|
||||
|
||||
boolean updateFeedback(Integer id, Integer score, String feedback);
|
||||
|
||||
boolean updateChatIsTop(Long chatId, int isTop);
|
||||
|
||||
Boolean deleteChat(Long chatId, String userName);
|
||||
|
||||
PageInfo<QueryResp> queryInfo(PageQueryInfoReq pageQueryInfoReq, long chatId);
|
||||
|
||||
void createChatQuery(ChatParseReq chatParseReq, ParseResp parseResp);
|
||||
|
||||
QueryResp getChatQuery(Long queryId);
|
||||
|
||||
ShowCaseResp queryShowCase(PageQueryInfoReq pageQueryInfoReq, int agentId);
|
||||
|
||||
ChatQueryDO saveQueryResult(ChatExecuteReq chatExecuteReq, QueryResult queryResult);
|
||||
|
||||
int updateQuery(ChatQueryDO chatQueryDO);
|
||||
|
||||
List<ChatParseDO> batchAddParse(ChatParseReq chatParseReq, ParseResp parseResult);
|
||||
|
||||
SemanticParseInfo getParseInfo(Long questionId, int parseId);
|
||||
}
|
||||
@@ -1,16 +1,9 @@
|
||||
package com.tencent.supersonic.chat.server.service;
|
||||
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatExecuteReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatParseReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatQueryDataReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.PageQueryInfoReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.QueryResp;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.ShowCaseResp;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatParseDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatQueryDO;
|
||||
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
|
||||
import com.tencent.supersonic.headless.api.pojo.request.DimensionValueReq;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
|
||||
@@ -33,27 +26,4 @@ public interface ChatService {
|
||||
|
||||
Object queryDimensionValue(DimensionValueReq dimensionValueReq, User user) throws Exception;
|
||||
|
||||
Boolean addChat(User user, String chatName, Integer agentId);
|
||||
|
||||
List<ChatDO> getAll(String userName, Integer agentId);
|
||||
|
||||
boolean updateChatName(Long chatId, String chatName, String userName);
|
||||
|
||||
boolean updateFeedback(Integer id, Integer score, String feedback);
|
||||
|
||||
boolean updateChatIsTop(Long chatId, int isTop);
|
||||
|
||||
Boolean deleteChat(Long chatId, String userName);
|
||||
|
||||
PageInfo<QueryResp> queryInfo(PageQueryInfoReq pageQueryInfoCommend, long chatId);
|
||||
|
||||
QueryResp getChatQuery(Long queryId);
|
||||
|
||||
ShowCaseResp queryShowCase(PageQueryInfoReq pageQueryInfoCommend, int agentId);
|
||||
|
||||
List<ChatParseDO> batchAddParse(ChatParseReq chatParseReq, ParseResp parseResult);
|
||||
|
||||
int updateQuery(ChatQueryDO chatQueryDO);
|
||||
|
||||
void saveQueryResult(ChatExecuteReq chatExecuteReq, QueryResult queryResult);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
package com.tencent.supersonic.chat.server.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatExecuteReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatParseReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.PageQueryInfoReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.ShowCaseResp;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatParseDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatQueryDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.QueryDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.repository.ChatQueryRepository;
|
||||
import com.tencent.supersonic.chat.server.persistence.repository.ChatRepository;
|
||||
import com.tencent.supersonic.chat.server.service.ChatManageService;
|
||||
import com.tencent.supersonic.common.util.JsonUtil;
|
||||
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.QueryResp;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.QueryResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ChatManageServiceImpl implements ChatManageService {
|
||||
|
||||
@Autowired
|
||||
private ChatRepository chatRepository;
|
||||
@Autowired
|
||||
private ChatQueryRepository chatQueryRepository;
|
||||
|
||||
@Override
|
||||
public Boolean addChat(User user, String chatName, Integer agentId) {
|
||||
ChatDO chatDO = new ChatDO();
|
||||
chatDO.setChatName(chatName);
|
||||
chatDO.setCreator(user.getName());
|
||||
chatDO.setCreateTime(getCurrentTime());
|
||||
chatDO.setIsDelete(0);
|
||||
chatDO.setLastTime(getCurrentTime());
|
||||
chatDO.setLastQuestion("Hello, welcome to using supersonic");
|
||||
chatDO.setIsTop(0);
|
||||
chatDO.setAgentId(agentId);
|
||||
return chatRepository.createChat(chatDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChatDO> getAll(String userName, Integer agentId) {
|
||||
return chatRepository.getAll(userName, agentId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateChatName(Long chatId, String chatName, String userName) {
|
||||
return chatRepository.updateChatName(chatId, chatName, getCurrentTime(), userName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateFeedback(Integer id, Integer score, String feedback) {
|
||||
QueryDO intelligentQueryDO = new QueryDO();
|
||||
intelligentQueryDO.setId(id);
|
||||
intelligentQueryDO.setScore(score);
|
||||
intelligentQueryDO.setFeedback(feedback);
|
||||
return chatRepository.updateFeedback(intelligentQueryDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateChatIsTop(Long chatId, int isTop) {
|
||||
return chatRepository.updateConversionIsTop(chatId, isTop);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteChat(Long chatId, String userName) {
|
||||
return chatRepository.deleteChat(chatId, userName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<QueryResp> queryInfo(PageQueryInfoReq pageQueryInfoReq, long chatId) {
|
||||
PageInfo<QueryResp> queryRespPageInfo = chatQueryRepository.getChatQuery(pageQueryInfoReq, chatId);
|
||||
if (CollectionUtils.isEmpty(queryRespPageInfo.getList())) {
|
||||
return queryRespPageInfo;
|
||||
}
|
||||
fillParseInfo(queryRespPageInfo.getList());
|
||||
return queryRespPageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createChatQuery(ChatParseReq chatParseReq, ParseResp parseResp) {
|
||||
Long queryId = chatQueryRepository.createChatQuery(chatParseReq);
|
||||
parseResp.setQueryId(queryId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryResp getChatQuery(Long queryId) {
|
||||
return chatQueryRepository.getChatQuery(queryId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShowCaseResp queryShowCase(PageQueryInfoReq pageQueryInfoReq, int agentId) {
|
||||
ShowCaseResp showCaseResp = new ShowCaseResp();
|
||||
showCaseResp.setCurrent(pageQueryInfoReq.getCurrent());
|
||||
showCaseResp.setPageSize(pageQueryInfoReq.getPageSize());
|
||||
List<QueryResp> queryResps = chatQueryRepository.queryShowCase(pageQueryInfoReq, agentId);
|
||||
if (CollectionUtils.isEmpty(queryResps)) {
|
||||
return showCaseResp;
|
||||
}
|
||||
queryResps.removeIf(queryResp -> {
|
||||
if (queryResp.getQueryResult() == null) {
|
||||
return true;
|
||||
}
|
||||
if (queryResp.getQueryResult().getResponse() != null) {
|
||||
return false;
|
||||
}
|
||||
if (CollectionUtils.isEmpty(queryResp.getQueryResult().getQueryResults())) {
|
||||
return true;
|
||||
}
|
||||
Map<String, Object> data = queryResp.getQueryResult().getQueryResults().get(0);
|
||||
return CollectionUtils.isEmpty(data);
|
||||
});
|
||||
queryResps = new ArrayList<>(queryResps.stream()
|
||||
.collect(Collectors.toMap(QueryResp::getQueryText, Function.identity(),
|
||||
(existing, replacement) -> existing, LinkedHashMap::new)).values());
|
||||
fillParseInfo(queryResps);
|
||||
Map<Long, List<QueryResp>> showCaseMap = queryResps.stream()
|
||||
.collect(Collectors.groupingBy(QueryResp::getChatId));
|
||||
showCaseResp.setShowCaseMap(showCaseMap);
|
||||
return showCaseResp;
|
||||
}
|
||||
|
||||
private void fillParseInfo(List<QueryResp> queryResps) {
|
||||
List<Long> queryIds = queryResps.stream()
|
||||
.map(QueryResp::getQuestionId).collect(Collectors.toList());
|
||||
List<ChatParseDO> chatParseDOs = chatQueryRepository.getParseInfoList(queryIds);
|
||||
if (CollectionUtils.isEmpty(chatParseDOs)) {
|
||||
return;
|
||||
}
|
||||
Map<Long, List<ChatParseDO>> chatParseMap = chatParseDOs.stream()
|
||||
.collect(Collectors.groupingBy(ChatParseDO::getQuestionId));
|
||||
for (QueryResp queryResp : queryResps) {
|
||||
List<ChatParseDO> chatParseDOList = chatParseMap.get(queryResp.getQuestionId());
|
||||
if (CollectionUtils.isEmpty(chatParseDOList)) {
|
||||
continue;
|
||||
}
|
||||
List<SemanticParseInfo> parseInfos = chatParseDOList.stream().map(chatParseDO ->
|
||||
JsonUtil.toObject(chatParseDO.getParseInfo(), SemanticParseInfo.class))
|
||||
.sorted(Comparator.comparingDouble(SemanticParseInfo::getScore).reversed())
|
||||
.collect(Collectors.toList());
|
||||
queryResp.setParseInfos(parseInfos);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatQueryDO saveQueryResult(ChatExecuteReq chatExecuteReq, QueryResult queryResult) {
|
||||
ChatQueryDO chatQueryDO = chatQueryRepository.getChatQueryDO(chatExecuteReq.getQueryId());
|
||||
chatQueryDO.setQuestionId(chatExecuteReq.getQueryId());
|
||||
chatQueryDO.setQueryResult(JsonUtil.toString(queryResult));
|
||||
chatQueryDO.setQueryState(1);
|
||||
updateQuery(chatQueryDO);
|
||||
chatRepository.updateLastQuestion(chatExecuteReq.getChatId().longValue(),
|
||||
chatExecuteReq.getQueryText(), getCurrentTime());
|
||||
return chatQueryDO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateQuery(ChatQueryDO chatQueryDO) {
|
||||
return chatQueryRepository.updateChatQuery(chatQueryDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChatParseDO> batchAddParse(ChatParseReq chatParseReq, ParseResp parseResult) {
|
||||
List<SemanticParseInfo> candidateParses = parseResult.getSelectedParses();
|
||||
return chatQueryRepository.batchSaveParseInfo(chatParseReq, parseResult, candidateParses);
|
||||
}
|
||||
|
||||
private String getCurrentTime() {
|
||||
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return tempDate.format(new java.util.Date());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SemanticParseInfo getParseInfo(Long questionId, int parseId) {
|
||||
ChatParseDO chatParseDO = chatQueryRepository.getParseInfo(questionId, parseId);
|
||||
return JSONObject.parseObject(chatParseDO.getParseInfo(), SemanticParseInfo.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +1,32 @@
|
||||
package com.tencent.supersonic.chat.server.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatExecuteReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatParseReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.ChatQueryDataReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.PageQueryInfoReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.SimilarQueryReq;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.ShowCaseResp;
|
||||
import com.tencent.supersonic.chat.server.agent.Agent;
|
||||
import com.tencent.supersonic.chat.server.executor.ChatExecutor;
|
||||
import com.tencent.supersonic.chat.server.parser.ChatParser;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatParseDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatQueryDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.QueryDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.repository.ChatQueryRepository;
|
||||
import com.tencent.supersonic.chat.server.persistence.repository.ChatRepository;
|
||||
import com.tencent.supersonic.chat.server.pojo.ChatExecuteContext;
|
||||
import com.tencent.supersonic.chat.server.pojo.ChatParseContext;
|
||||
import com.tencent.supersonic.chat.server.processor.execute.ExecuteResultProcessor;
|
||||
import com.tencent.supersonic.chat.server.processor.parse.ParseResultProcessor;
|
||||
import com.tencent.supersonic.chat.server.service.AgentService;
|
||||
import com.tencent.supersonic.chat.server.service.ChatManageService;
|
||||
import com.tencent.supersonic.chat.server.service.ChatService;
|
||||
import com.tencent.supersonic.chat.server.util.ComponentFactory;
|
||||
import com.tencent.supersonic.chat.server.util.QueryReqConverter;
|
||||
import com.tencent.supersonic.chat.server.util.SimilarQueryManager;
|
||||
import com.tencent.supersonic.common.util.BeanMapper;
|
||||
import com.tencent.supersonic.common.util.ContextUtils;
|
||||
import com.tencent.supersonic.common.util.JsonUtil;
|
||||
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
|
||||
import com.tencent.supersonic.headless.api.pojo.request.DimensionValueReq;
|
||||
import com.tencent.supersonic.headless.api.pojo.request.QueryDataReq;
|
||||
import com.tencent.supersonic.headless.api.pojo.request.QueryReq;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.MapResp;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
|
||||
import com.tencent.supersonic.chat.api.pojo.response.QueryResp;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.QueryResult;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.SearchResult;
|
||||
import com.tencent.supersonic.headless.server.service.ChatQueryService;
|
||||
@@ -44,15 +34,7 @@ import com.tencent.supersonic.headless.server.service.SearchService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@@ -60,9 +42,7 @@ import java.util.stream.Collectors;
|
||||
public class ChatServiceImpl implements ChatService {
|
||||
|
||||
@Autowired
|
||||
private ChatRepository chatRepository;
|
||||
@Autowired
|
||||
private ChatQueryRepository chatQueryRepository;
|
||||
private ChatManageService chatManageService;
|
||||
@Autowired
|
||||
private ChatQueryService chatQueryService;
|
||||
@Autowired
|
||||
@@ -84,7 +64,7 @@ public class ChatServiceImpl implements ChatService {
|
||||
@Override
|
||||
public ParseResp performParsing(ChatParseReq chatParseReq) {
|
||||
ParseResp parseResp = new ParseResp(chatParseReq.getChatId(), chatParseReq.getQueryText());
|
||||
createChatQuery(chatParseReq, parseResp);
|
||||
chatManageService.createChatQuery(chatParseReq, parseResp);
|
||||
ChatParseContext chatParseContext = buildParseContext(chatParseReq);
|
||||
for (ChatParser chatParser : chatParsers) {
|
||||
chatParser.parse(chatParseContext, parseResp);
|
||||
@@ -92,7 +72,7 @@ public class ChatServiceImpl implements ChatService {
|
||||
for (ParseResultProcessor processor : parseResultProcessors) {
|
||||
processor.process(chatParseContext, parseResp);
|
||||
}
|
||||
batchAddParse(chatParseReq, parseResp);
|
||||
chatManageService.batchAddParse(chatParseReq, parseResp);
|
||||
return parseResp;
|
||||
}
|
||||
|
||||
@@ -128,7 +108,8 @@ public class ChatServiceImpl implements ChatService {
|
||||
private ChatExecuteContext buildExecuteContext(ChatExecuteReq chatExecuteReq) {
|
||||
ChatExecuteContext chatExecuteContext = new ChatExecuteContext();
|
||||
BeanMapper.mapper(chatExecuteReq, chatExecuteContext);
|
||||
SemanticParseInfo parseInfo = getParseInfo(chatExecuteReq.getQueryId(), chatExecuteReq.getParseId());
|
||||
SemanticParseInfo parseInfo = chatManageService.getParseInfo(
|
||||
chatExecuteReq.getQueryId(), chatExecuteReq.getParseId());
|
||||
chatExecuteContext.setParseInfo(parseInfo);
|
||||
return chatExecuteContext;
|
||||
}
|
||||
@@ -136,7 +117,8 @@ public class ChatServiceImpl implements ChatService {
|
||||
@Override
|
||||
public Object queryData(ChatQueryDataReq chatQueryDataReq, User user) throws Exception {
|
||||
Integer parseId = chatQueryDataReq.getParseId();
|
||||
SemanticParseInfo parseInfo = getParseInfo(chatQueryDataReq.getQueryId(), parseId);
|
||||
SemanticParseInfo parseInfo = chatManageService.getParseInfo(
|
||||
chatQueryDataReq.getQueryId(), parseId);
|
||||
QueryDataReq queryData = new QueryDataReq();
|
||||
BeanMapper.mapper(chatQueryDataReq, queryData);
|
||||
queryData.setParseInfo(parseInfo);
|
||||
@@ -153,160 +135,15 @@ public class ChatServiceImpl implements ChatService {
|
||||
return chatQueryService.queryDimensionValue(dimensionValueReq, user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean addChat(User user, String chatName, Integer agentId) {
|
||||
ChatDO chatDO = new ChatDO();
|
||||
chatDO.setChatName(chatName);
|
||||
chatDO.setCreator(user.getName());
|
||||
chatDO.setCreateTime(getCurrentTime());
|
||||
chatDO.setIsDelete(0);
|
||||
chatDO.setLastTime(getCurrentTime());
|
||||
chatDO.setLastQuestion("Hello, welcome to using supersonic");
|
||||
chatDO.setIsTop(0);
|
||||
chatDO.setAgentId(agentId);
|
||||
return chatRepository.createChat(chatDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChatDO> getAll(String userName, Integer agentId) {
|
||||
return chatRepository.getAll(userName, agentId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateChatName(Long chatId, String chatName, String userName) {
|
||||
return chatRepository.updateChatName(chatId, chatName, getCurrentTime(), userName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateFeedback(Integer id, Integer score, String feedback) {
|
||||
QueryDO intelligentQueryDO = new QueryDO();
|
||||
intelligentQueryDO.setId(id);
|
||||
intelligentQueryDO.setScore(score);
|
||||
intelligentQueryDO.setFeedback(feedback);
|
||||
return chatRepository.updateFeedback(intelligentQueryDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateChatIsTop(Long chatId, int isTop) {
|
||||
return chatRepository.updateConversionIsTop(chatId, isTop);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteChat(Long chatId, String userName) {
|
||||
return chatRepository.deleteChat(chatId, userName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<QueryResp> queryInfo(PageQueryInfoReq pageQueryInfoReq, long chatId) {
|
||||
PageInfo<QueryResp> queryRespPageInfo = chatQueryRepository.getChatQuery(pageQueryInfoReq, chatId);
|
||||
if (CollectionUtils.isEmpty(queryRespPageInfo.getList())) {
|
||||
return queryRespPageInfo;
|
||||
}
|
||||
fillParseInfo(queryRespPageInfo.getList());
|
||||
return queryRespPageInfo;
|
||||
}
|
||||
|
||||
public void createChatQuery(ChatParseReq chatParseReq, ParseResp parseResp) {
|
||||
Long queryId = chatQueryRepository.createChatQuery(chatParseReq);
|
||||
parseResp.setQueryId(queryId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryResp getChatQuery(Long queryId) {
|
||||
return chatQueryRepository.getChatQuery(queryId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShowCaseResp queryShowCase(PageQueryInfoReq pageQueryInfoReq, int agentId) {
|
||||
ShowCaseResp showCaseResp = new ShowCaseResp();
|
||||
showCaseResp.setCurrent(pageQueryInfoReq.getCurrent());
|
||||
showCaseResp.setPageSize(pageQueryInfoReq.getPageSize());
|
||||
List<QueryResp> queryResps = chatQueryRepository.queryShowCase(pageQueryInfoReq, agentId);
|
||||
if (CollectionUtils.isEmpty(queryResps)) {
|
||||
return showCaseResp;
|
||||
}
|
||||
queryResps.removeIf(queryResp -> {
|
||||
if (queryResp.getQueryResult() == null) {
|
||||
return true;
|
||||
}
|
||||
if (queryResp.getQueryResult().getResponse() != null) {
|
||||
return false;
|
||||
}
|
||||
if (CollectionUtils.isEmpty(queryResp.getQueryResult().getQueryResults())) {
|
||||
return true;
|
||||
}
|
||||
Map<String, Object> data = queryResp.getQueryResult().getQueryResults().get(0);
|
||||
return CollectionUtils.isEmpty(data);
|
||||
});
|
||||
queryResps = new ArrayList<>(queryResps.stream()
|
||||
.collect(Collectors.toMap(QueryResp::getQueryText, Function.identity(),
|
||||
(existing, replacement) -> existing, LinkedHashMap::new)).values());
|
||||
fillParseInfo(queryResps);
|
||||
Map<Long, List<QueryResp>> showCaseMap = queryResps.stream()
|
||||
.collect(Collectors.groupingBy(QueryResp::getChatId));
|
||||
showCaseResp.setShowCaseMap(showCaseMap);
|
||||
return showCaseResp;
|
||||
}
|
||||
|
||||
private void fillParseInfo(List<QueryResp> queryResps) {
|
||||
List<Long> queryIds = queryResps.stream()
|
||||
.map(QueryResp::getQuestionId).collect(Collectors.toList());
|
||||
List<ChatParseDO> chatParseDOs = chatQueryRepository.getParseInfoList(queryIds);
|
||||
if (CollectionUtils.isEmpty(chatParseDOs)) {
|
||||
return;
|
||||
}
|
||||
Map<Long, List<ChatParseDO>> chatParseMap = chatParseDOs.stream()
|
||||
.collect(Collectors.groupingBy(ChatParseDO::getQuestionId));
|
||||
for (QueryResp queryResp : queryResps) {
|
||||
List<ChatParseDO> chatParseDOList = chatParseMap.get(queryResp.getQuestionId());
|
||||
if (CollectionUtils.isEmpty(chatParseDOList)) {
|
||||
continue;
|
||||
}
|
||||
List<SemanticParseInfo> parseInfos = chatParseDOList.stream().map(chatParseDO ->
|
||||
JsonUtil.toObject(chatParseDO.getParseInfo(), SemanticParseInfo.class))
|
||||
.sorted(Comparator.comparingDouble(SemanticParseInfo::getScore).reversed())
|
||||
.collect(Collectors.toList());
|
||||
queryResp.setParseInfos(parseInfos);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveQueryResult(ChatExecuteReq chatExecuteReq, QueryResult queryResult) {
|
||||
//The history record only retains the query result of the first parse
|
||||
if (chatExecuteReq.getParseId() > 1) {
|
||||
return;
|
||||
}
|
||||
ChatQueryDO chatQueryDO = chatQueryRepository.getChatQueryDO(chatExecuteReq.getQueryId());
|
||||
chatQueryDO.setQuestionId(chatExecuteReq.getQueryId());
|
||||
chatQueryDO.setQueryResult(JsonUtil.toString(queryResult));
|
||||
chatQueryDO.setQueryState(1);
|
||||
updateQuery(chatQueryDO);
|
||||
chatRepository.updateLastQuestion(chatExecuteReq.getChatId().longValue(),
|
||||
chatExecuteReq.getQueryText(), getCurrentTime());
|
||||
ChatQueryDO chatQueryDO = chatManageService.saveQueryResult(chatExecuteReq, queryResult);
|
||||
SimilarQueryReq similarQueryReq = SimilarQueryReq.builder().queryId(chatExecuteReq.getQueryId())
|
||||
.queryText(chatQueryDO.getQueryText()).agentId(chatQueryDO.getAgentId()).build();
|
||||
similarQueryManager.saveSimilarQuery(similarQueryReq);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateQuery(ChatQueryDO chatQueryDO) {
|
||||
return chatQueryRepository.updateChatQuery(chatQueryDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChatParseDO> batchAddParse(ChatParseReq chatParseReq, ParseResp parseResult) {
|
||||
List<SemanticParseInfo> candidateParses = parseResult.getSelectedParses();
|
||||
return chatQueryRepository.batchSaveParseInfo(chatParseReq, parseResult, candidateParses);
|
||||
}
|
||||
|
||||
private String getCurrentTime() {
|
||||
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return tempDate.format(new java.util.Date());
|
||||
}
|
||||
|
||||
public SemanticParseInfo getParseInfo(Long questionId, int parseId) {
|
||||
ChatParseDO chatParseDO = chatQueryRepository.getParseInfo(questionId, parseId);
|
||||
return JSONObject.parseObject(chatParseDO.getParseInfo(), SemanticParseInfo.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,14 +15,13 @@ import com.tencent.supersonic.chat.server.plugin.PluginParseConfig;
|
||||
import com.tencent.supersonic.chat.server.plugin.build.ParamOption;
|
||||
import com.tencent.supersonic.chat.server.plugin.build.WebBase;
|
||||
import com.tencent.supersonic.chat.server.service.AgentService;
|
||||
import com.tencent.supersonic.chat.server.service.ChatManageService;
|
||||
import com.tencent.supersonic.chat.server.service.ChatService;
|
||||
import com.tencent.supersonic.chat.server.service.PluginService;
|
||||
import com.tencent.supersonic.common.pojo.SysParameter;
|
||||
import com.tencent.supersonic.common.service.SysParameterService;
|
||||
import com.tencent.supersonic.common.util.JsonUtil;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -31,6 +30,9 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@Order(3)
|
||||
@@ -40,6 +42,8 @@ public class ChatDemoLoader implements CommandLineRunner {
|
||||
@Autowired
|
||||
private ChatService chatService;
|
||||
@Autowired
|
||||
private ChatManageService chatManageService;
|
||||
@Autowired
|
||||
private PluginService pluginService;
|
||||
@Autowired
|
||||
private AgentService agentService;
|
||||
@@ -100,7 +104,7 @@ public class ChatDemoLoader implements CommandLineRunner {
|
||||
}
|
||||
|
||||
public void addSampleChats() throws Exception {
|
||||
chatService.addChat(user, "样例对话1", 1);
|
||||
chatManageService.addChat(user, "样例对话1", 1);
|
||||
|
||||
parseAndExecute(1, "超音数 访问次数");
|
||||
parseAndExecute(1, "按部门统计");
|
||||
@@ -108,7 +112,7 @@ public class ChatDemoLoader implements CommandLineRunner {
|
||||
}
|
||||
|
||||
public void addSampleChats2() throws Exception {
|
||||
chatService.addChat(user, "样例对话2", 1);
|
||||
chatManageService.addChat(user, "样例对话2", 1);
|
||||
|
||||
parseAndExecute(2, "alice 停留时长");
|
||||
parseAndExecute(2, "对比alice和lucy的访问次数");
|
||||
@@ -246,7 +250,7 @@ public class ChatDemoLoader implements CommandLineRunner {
|
||||
}
|
||||
|
||||
private void updateQueryScore(Integer queryId) {
|
||||
chatService.updateFeedback(queryId, 5, "");
|
||||
chatManageService.updateFeedback(queryId, 5, "");
|
||||
}
|
||||
|
||||
private boolean checkEnable() {
|
||||
|
||||
Reference in New Issue
Block a user