提交知识库管理相关代码
parent
ffcb1aecb6
commit
cc89f48e5e
@ -0,0 +1,35 @@
|
||||
package com.supervision.controller;
|
||||
|
||||
import com.supervision.dto.MatchQuestionAnswerDTO;
|
||||
import com.supervision.service.MatchToolService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Api(tags = "相似度匹配工具")
|
||||
@RestController
|
||||
@RequestMapping("matchTool")
|
||||
@RequiredArgsConstructor
|
||||
public class MatchToolController {
|
||||
|
||||
private final MatchToolService matchToolService;
|
||||
|
||||
@ApiOperation("刷新匹配工具知识库")
|
||||
@GetMapping("refreshMatchToolLibrary")
|
||||
public void refreshMatchToolLibrary() {
|
||||
matchToolService.refreshMatchToolLibrary();
|
||||
}
|
||||
|
||||
@ApiOperation("进行问答")
|
||||
@GetMapping("execMatch")
|
||||
public List<MatchQuestionAnswerDTO> execMatch(String question) {
|
||||
return matchToolService.execMatch(question);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.supervision.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class MatchQuestionAnswerDTO {
|
||||
|
||||
private String matchQuestion;
|
||||
|
||||
private String matchQuestionCode;
|
||||
|
||||
private Double matchScore;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.supervision.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class MatchToolQuestionDTO {
|
||||
|
||||
private String questionId;
|
||||
|
||||
private List<String> questionList;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.supervision.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class QuestionReqDTO {
|
||||
|
||||
private String question;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.supervision.service;
|
||||
|
||||
import com.supervision.dto.MatchQuestionAnswerDTO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface MatchToolService {
|
||||
|
||||
void refreshMatchToolLibrary();
|
||||
|
||||
List<MatchQuestionAnswerDTO> execMatch(String question);
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package com.supervision.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.supervision.config.domain.GlobalResult;
|
||||
import com.supervision.domain.IrKnowledge;
|
||||
import com.supervision.domain.IrKnowledgeSimilar;
|
||||
import com.supervision.dto.MatchQuestionAnswerDTO;
|
||||
import com.supervision.dto.MatchToolQuestionDTO;
|
||||
import com.supervision.dto.QuestionReqDTO;
|
||||
import com.supervision.exception.BusinessException;
|
||||
import com.supervision.service.IrKnowledgeService;
|
||||
import com.supervision.service.IrKnowledgeSimilarService;
|
||||
import com.supervision.service.MatchToolService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MatchToolServiceImpl implements MatchToolService {
|
||||
|
||||
private final IrKnowledgeService irKnowledgeService;
|
||||
|
||||
private final IrKnowledgeSimilarService irKnowledgeSimilarService;
|
||||
|
||||
@Value("${matchTool.url}")
|
||||
private String matchToolUrl;
|
||||
|
||||
@Override
|
||||
public void refreshMatchToolLibrary() {
|
||||
// 获取所有的问题
|
||||
List<IrKnowledge> knowledgeList = irKnowledgeService.list();
|
||||
// 获取相似问题
|
||||
List<IrKnowledgeSimilar> similarList = irKnowledgeSimilarService.list();
|
||||
Map<String, List<IrKnowledgeSimilar>> similarKnowledgeMap = similarList.stream().collect(Collectors.groupingBy(IrKnowledgeSimilar::getKnowledgeId));
|
||||
Map<String, List<String>> questionLibrary = knowledgeList.stream().collect(Collectors.toMap(IrKnowledge::getId, e -> CollUtil.newArrayList(e.getStandardQuestion())));
|
||||
List<MatchToolQuestionDTO> matchToolQuestionDTOList = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, List<String>> entry : questionLibrary.entrySet()) {
|
||||
String questionId = entry.getKey();
|
||||
List<IrKnowledgeSimilar> similarKnowledgeList = similarKnowledgeMap.get(questionId);
|
||||
if (CollUtil.isNotEmpty(similarKnowledgeList)) {
|
||||
Set<String> similarQuestionSet = similarKnowledgeList.stream().map(IrKnowledgeSimilar::getSimilarQuestion).collect(Collectors.toSet());
|
||||
entry.getValue().addAll(similarQuestionSet);
|
||||
}
|
||||
MatchToolQuestionDTO matchToolQuestionDTO = new MatchToolQuestionDTO();
|
||||
matchToolQuestionDTO.setQuestionId(questionId);
|
||||
matchToolQuestionDTO.setQuestionList(entry.getValue());
|
||||
matchToolQuestionDTOList.add(matchToolQuestionDTO);
|
||||
}
|
||||
// 调用tool服务进行更新操作
|
||||
submitRefresh(matchToolQuestionDTOList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MatchQuestionAnswerDTO> execMatch(String question) {
|
||||
log.info("开始调用talkQaSimilarity,问题:{}", question);
|
||||
try {
|
||||
String post = HttpUtil.post(matchToolUrl + "/matchQuestion", JSONUtil.toJsonStr(new QuestionReqDTO(question)));
|
||||
log.info("相似度匹配答案:{}", post);
|
||||
TypeReference<GlobalResult<List<MatchQuestionAnswerDTO>>> globalResultTypeReference = new TypeReference<GlobalResult<List<MatchQuestionAnswerDTO>>>() {
|
||||
};
|
||||
GlobalResult<List<MatchQuestionAnswerDTO>> result = JSONUtil.toBean(post, globalResultTypeReference.getType(), true);
|
||||
if (result.getCode() != 200) {
|
||||
throw new BusinessException("匹配失败");
|
||||
}
|
||||
|
||||
return result.getData();
|
||||
} catch (Exception e) {
|
||||
log.error("调用talkQaSimilarity error ", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void submitRefresh(List<MatchToolQuestionDTO> matchToolQuestionDTOList) {
|
||||
log.info("开始调用matchTool服务进行更新操作");
|
||||
try {
|
||||
String post = HttpUtil.post(matchToolUrl + "/updateDatabase", JSONUtil.toJsonStr(matchToolQuestionDTOList));
|
||||
log.info(post);
|
||||
if (!JSONUtil.isTypeJSON(post)) {
|
||||
throw new BusinessException("更新失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("调用matchTool服务更新失败", e);
|
||||
throw new BusinessException("更新失败");
|
||||
}
|
||||
log.info("成功调用matchTool服务更新数据库");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,15 +1,26 @@
|
||||
package com.supervision;
|
||||
|
||||
import com.supervision.domain.IrKnowledge;
|
||||
import com.supervision.domain.IrKnowledgeSimilar;
|
||||
import com.supervision.service.IrKnowledgeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@MapperScan(basePackages = {"com.supervision.**.mapper"})
|
||||
@SpringBootTest
|
||||
@Slf4j
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@RequiredArgsConstructor
|
||||
class AskApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
@Autowired
|
||||
private IrKnowledgeService irKnowledgeService;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue