Merge remote-tracking branch 'origin/main'

main
xueqingkun 11 months ago
commit a47c7bbca8

@ -3,10 +3,7 @@ package com.supervision.controller;
import cn.hutool.core.util.StrUtil;
import com.supervision.exception.BusinessException;
import com.supervision.service.AskService;
import com.supervision.vo.RoleSetResVO;
import com.supervision.vo.RoundTalkReqVO;
import com.supervision.vo.RoundTalkResVO;
import com.supervision.vo.RoleSetReqVO;
import com.supervision.vo.*;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -31,6 +28,12 @@ public class AskController {
return askService.roundTalk(roundTalkReqVO);
}
@ApiOperation("单轮对话")
@PostMapping("singleTalk")
public SingleTalkResVO singleTalk(@RequestBody SingleTalkReqVO singleTalkReqVO) {
return askService.singleTalk(singleTalkReqVO);
}
@ApiOperation("多轮对话中用户手动填写参数,可能直接返回结果")
@PostMapping("saveUserParam")
public RoundTalkResVO saveUserParam(@RequestBody RoleSetReqVO paramReqVO) {

@ -6,6 +6,7 @@ import com.supervision.service.KgPolicyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -33,4 +34,10 @@ public class PolicyController {
public IPage<KgPolicy> queryPolicyPage(String keyword, Integer level, Integer subject, Integer type, Integer order, Integer pageNum, Integer pageSize) {
return kgPolicyService.queryPolicyPage(keyword, level, subject, type, order, pageNum, pageSize);
}
@ApiOperation("阅读量+1(点击详情时调用)")
@GetMapping("incrementReadCount")
public void incrementReadCount(String id) {
kgPolicyService.incrementReadCount(id);
}
}

@ -41,4 +41,25 @@ public class AnswerQuestionHandler {
log.info("answerQuestion的答案是:{}", answer);
return answer;
}
/**
*
* @return
*/
public String answerSingleQuestion(String question, Map<String,List<String>> detailMap){
List<MessageDTO> messageList = new ArrayList<>();
messageList.add(new MessageDTO("system", "现在你是一个政务事项领域的问答大模型.\n" +
"现在有一个问题,根据这个问题我查到了政策内容\n" +
"请你根据政策内容组织成简短的内容来回答这个问题,请不要有文件内容之外的内容或加入你自己的理解"));
messageList.add(new MessageDTO("assistant", "好的"));
messageList.add(new MessageDTO("user", StrUtil.format("政务文件内容:[{}]", JSONUtil.toJsonStr(detailMap))));
messageList.add(new MessageDTO("assistant", "继续"));
messageList.add(new MessageDTO("user", StrUtil.format("问题:{}", question)));
messageList.add(new MessageDTO("assistant", "继续"));
messageList.add(new MessageDTO("user", StrUtil.format("现在你可以回答了,如果你从文件内容中,没有提取到回答,你就回复:我暂时还不会这个问题哦!")));
log.info("answerQuestion的prompt是:{}", JSONUtil.toJsonStr(messageList));
String answer = AiUtil.chatByMessage(messageList);
log.info("answerQuestion的答案是:{}", answer);
return answer;
}
}

@ -3,6 +3,7 @@ package com.supervision.handler.gpt;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.supervision.ai.AiUtil;
import com.supervision.ai.dto.MessageDTO;
@ -34,13 +35,41 @@ public class ConditionJudgeHandler {
messageList.add(new MessageDTO("assistant", "继续"));
messageList.add(new MessageDTO("user", StrUtil.format("候选答案列表:[{};未找到匹配答案]", StrUtil.join(";", candidateAnswerList))));
messageList.add(new MessageDTO("assistant", "继续"));
messageList.add(new MessageDTO("user", StrUtil.format("用户答案:[{}],现在请给我匹配的候选答案,其他什么都不要说.如果有多个候选答案,用;号分割", userAnswer)));
messageList.add(new MessageDTO("user", StrUtil.format("用户答案:[{}],现在请给我所有匹配的候选答案.如果有多个候选答案,用;号分割.现在你可以回答了.", userAnswer)));
log.info("conditionJudge判断候选答案:{}", JSONUtil.toJsonStr(messageList));
String judgeResult = AiUtil.chatByMessage(messageList);
log.info("conditionJudge判断结果是:{}", judgeResult);
return new HashSet<>(Arrays.asList(judgeResult.split(";")));
}
/**
* ,
* @param question
* @param candidateAnswerList
* @param userAnswer
* @return
*/
public Set<String> conditionJudgeAll(String question, Collection<String> candidateAnswerList, String userAnswer) {
List<MessageDTO> messageList = new ArrayList<>();
String template = "当我问用户:{},用户给我的回答是:[{}]\n" +
"基于用户的回答,请依次判断以下列表:[{}]中的哪些项满足用户的回答?\n" +
"请从给定列表中找出所有满足的项.以jsonArray格式输出.如果列表中项全部不满足,则返回:未找到";
String judgeAsk = StrUtil.format(template, question, userAnswer, CollUtil.join(candidateAnswerList, ";"));
log.info("conditionJudge问题是:{}", judgeAsk);
messageList.add(new MessageDTO("user", judgeAsk));
String judgeResult = AiUtil.chatByMessage(messageList);
log.info("conditionJudge判断结果是:{}", judgeResult);
if (judgeResult.startsWith("```json")) {
judgeResult = StrUtil.removePrefix(judgeResult, "```json");
judgeResult = StrUtil.removeSuffix(judgeResult, "```");
}
HashSet<String> result = new HashSet<>(JSONUtil.toList(judgeResult, String.class));
if (result.size() == 1 && result.contains("未找到")) {
return new HashSet<>();
}
return result;
}
/**
*
*

@ -1,9 +1,7 @@
package com.supervision.service;
import com.supervision.vo.RoleSetResVO;
import com.supervision.vo.RoundTalkReqVO;
import com.supervision.vo.RoundTalkResVO;
import com.supervision.vo.RoleSetReqVO;
import com.supervision.vo.*;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Set;
@ -11,6 +9,8 @@ public interface AskService {
RoundTalkResVO roundTalk(RoundTalkReqVO roundTalkReqVO);
SingleTalkResVO singleTalk(SingleTalkReqVO singleTalkReqVO);
RoundTalkResVO saveUserParam(RoleSetReqVO paramReqVO);
RoleSetResVO queryUserNeedParam(String sessionId);

@ -20,10 +20,7 @@ import com.supervision.handler.graph.FindItemNodeHandler;
import com.supervision.ngbatis.domain.tag.Condition;
import com.supervision.ngbatis.domain.tag.ItemLeaf;
import com.supervision.service.AskService;
import com.supervision.vo.RoleSetResVO;
import com.supervision.vo.RoundTalkReqVO;
import com.supervision.vo.RoundTalkResVO;
import com.supervision.vo.RoleSetReqVO;
import com.supervision.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
@ -196,26 +193,25 @@ public class AskServiceImpl implements AskService {
// 根据意图和节点,找到对应的结果
List<String> itemDetail = findItemDetailHandler.findItemDetail(matchItemLeaf.getVid(), intentEnum.getTagType(), intentEnum.getEdgeType());
if (CollUtil.isEmpty(itemDetail)) {
return RoundTalkResVO.builder().sessionId(sessionParamDTO.getSessionId()).replyQuestion("暂不支持该意图的问答").build();
return RoundTalkResVO.builder().sessionId(sessionParamDTO.getSessionId()).answerText("暂时还不会回答这个问题哦").build();
}
// 提交GPT,问问题的答案
String answer = answerQuestionHandler.answerQuestion(sessionParamDTO.getOriginalQuestion(), itemDetail, sessionParamDTO.getTalkRecord());
if (StrUtil.isBlank(answer)) {
return RoundTalkResVO.builder().sessionId(sessionParamDTO.getSessionId()).replyQuestion("暂时还不会回答这个问题哦").build();
return RoundTalkResVO.builder().sessionId(sessionParamDTO.getSessionId()).answerText("暂时还不会回答这个问题哦!").build();
}
// 清空问话实体
sessionParamDTO.setCurrentEntity(null);
redisTemplate.opsForValue().set(SESSION_PARAM + sessionParamDTO.getSessionId(), sessionParamDTO);
return RoundTalkResVO.builder().sessionId(sessionParamDTO.getSessionId()).replyQuestion(answer).build();
return RoundTalkResVO.builder().sessionId(sessionParamDTO.getSessionId()).answerText(answer).build();
}
private void filterPath(SessionParamDTO sessionParamDTO, String userTalk) {
// 遍历所有的path,找到回答
Set<String> possibleAnswerSet = findPossibleAnswerSet(sessionParamDTO);
Set<String> judgeResultSet = conditionJudgeHandler.newConditionJudge(sessionParamDTO.getCurrentEntity().getCurrentQuestion(),
Set<String> judgeResultSet = conditionJudgeHandler.conditionJudgeAll(sessionParamDTO.getCurrentEntity().getCurrentQuestion(),
possibleAnswerSet,
userTalk,
sessionParamDTO.getCurrentEntity().getCurrentEntityType());
userTalk);
// 筛选路径,如果某个路径的结果不在比较结果中,说明这个结果不对,排除这个路径
pathFilterByJudgeResult(sessionParamDTO.getCurrentEntity().getCurrentEntityType(), judgeResultSet, sessionParamDTO);
filterNotMatchNode(sessionParamDTO);
@ -317,6 +313,44 @@ public class AskServiceImpl implements AskService {
}
}
@Override
public SingleTalkResVO singleTalk(SingleTalkReqVO singleTalkReqVO) {
// 单轮对话,直接根据用户的问题去找意图
String intent = identifyIntentHandler.identifyIntent(singleTalkReqVO.getUserTalk());
// 提供用户问题中的业务
String extractValue = itemExtractHandler.itemExtractBusiness(singleTalkReqVO.getUserTalk());
// 根据提取的内容,开始在知识图谱中寻找节点(首先找叶子节点,如果叶子节点有数据,直接返回,如果叶子节点没数据,再去找分支节点)
List<ItemLeaf> allMatchLeafNode = findItemNodeHandler.findAllMatchLeafNode(Collections.singletonList(extractValue));
// 然后根据叶子节点,还有意图,去找到所有的对应数据
// 根据用户的意图找到对应的节点
IdentifyIntentEnum intentEnum = IdentifyIntentEnum.getEnumByIntent(intent);
if (ObjectUtil.isEmpty(intentEnum) || null == intentEnum) {
throw new BusinessException("暂不支持该意图的问答");
}
Map<String, List<String>> detailMap = new HashMap<>();
for (ItemLeaf itemLeaf : allMatchLeafNode) {
try {
// 根据意图和节点,找到对应的结果
List<String> itemDetailList = findItemDetailHandler.findItemDetail(itemLeaf.getVid(), intentEnum.getTagType(), intentEnum.getEdgeType());
if (CollUtil.isNotEmpty(itemDetailList)) {
detailMap.put(itemLeaf.getItemName(), itemDetailList);
}
} catch (BusinessException e) {
log.info("未找到对应节点,忽略");
}
}
if (CollUtil.isEmpty(detailMap)) {
return SingleTalkResVO.builder().answerText("暂时还不会回答这个问题哦!").build();
}
// 提交GPT,问问题的答案
String answer = answerQuestionHandler.answerSingleQuestion(singleTalkReqVO.getUserTalk(), detailMap);
if (StrUtil.isBlank(answer)) {
return SingleTalkResVO.builder().answerText("暂时还不会回答这个问题哦!").build();
}
return SingleTalkResVO.builder().answerText(answer).build();
}
@Override
public RoundTalkResVO saveUserParam(RoleSetReqVO paramReqVO) {
// 缓存到Redis中

@ -0,0 +1,10 @@
package com.supervision.vo;
import lombok.Data;
@Data
public class SingleTalkReqVO {
private String userTalk;
}

@ -0,0 +1,11 @@
package com.supervision.vo;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class SingleTalkResVO {
private String answerText;
}

@ -1,12 +1,15 @@
<mapper namespace="com.supervision.ngbatis.dao.CommonQueryDao">
<select id="findNextVertxByConditionEdge" resultType="java.lang.String">
GO FROM "${vid}" OVER condition_edge YIELD dst(edge);
GO FROM "${vid}" OVER condition_edge YIELD
dst(edge);
</select>
<select id="findNextConditionByConditionEdge" resultType="com.supervision.ngbatis.domain.tag.Condition">
GO FROM "${vid}" OVER condition_edge YIELD properties($$).entity_type AS entity_type, properties($$).condition
AS condition, id($$) AS id;
GO FROM "${vid}" OVER condition_edge YIELD
properties($$).entity_type AS entity_type,
properties($$).condition AS condition,
id($$) AS id;
</select>
<select id="findConditionVertexByLeafVertex" resultType="com.supervision.ngbatis.domain.tag.ProcessCondition">

@ -10,6 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.Set;
@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ -19,5 +20,28 @@ public class GlmTest {
@Autowired
private ConditionJudgeHandler conditionJudgeHandler;
@Test
public void test() {
ArrayList<String> strings = CollUtil.newArrayList("深圳户口", "广东省其他地区", "港澳台及外籍地区", "省外户口", "非深圳户口");
Set<String> strings1 = conditionJudgeHandler.newConditionJudge("你的户口所在地是哪里?", strings, "我是汕头人", "户籍所在地");
log.info("答案是: {}", CollUtil.join(strings1, ";"));
}
@Test
public void test1() {
ArrayList<String> strings = CollUtil.newArrayList("深圳户口", "广东省其他地区", "港澳台及外籍地区", "省外户口", "非深圳户口");
Set<String> strings1 = conditionJudgeHandler.conditionJudge("你的户口所在地是哪里?", strings, "我是汕头人");
log.info("答案是: {}", CollUtil.join(strings1, ";"));
}
@Test
public void test2() {
ArrayList<String> strings = CollUtil.newArrayList("大于60岁", "65岁以上", "年龄大于50周岁", "50岁以上");
Set<String> strings1 = conditionJudgeHandler.conditionJudgeAll("你的年龄多少岁?", strings, "我今天中午吃的酸菜鱼");
log.info("答案是: {}", CollUtil.join(strings1, ";"));
}
}

@ -13,4 +13,6 @@ public interface KgPolicyService extends IService<KgPolicy> {
IPage<KgPolicy> queryPolicyPage(String keyword, Integer level, Integer subject, Integer type, Integer order,Integer pageNum, Integer pageSize);
void incrementReadCount(String id);
}

@ -39,6 +39,11 @@ public class KgPolicyServiceImpl extends ServiceImpl<KgPolicyMapper, KgPolicy>
return this.baseMapper.queryPolicyPage(keyword, level, subject, type, page);
}
@Override
public void incrementReadCount(String id) {
this.lambdaUpdate().setSql("read_count = read_count + 1").eq(KgPolicy::getId, id).update();
}
}

Loading…
Cancel
Save