KBQA代码提交

main
liu 1 year ago
parent df31d7fb2e
commit e2bcf288e5

@ -6,23 +6,45 @@ import java.util.ArrayList;
import java.util.List;
public enum IdentifyIntentEnum {
("业务的受理条件", CollUtil.newArrayList("")),
("业务的办理流程", CollUtil.newArrayList("")),
("业务的材料清单", CollUtil.newArrayList("")),
("业务的设定依据", CollUtil.newArrayList("")),
("业务的实施依据", CollUtil.newArrayList("")),
("业务的办理途径", CollUtil.newArrayList("")),
("业务的办理窗口", CollUtil.newArrayList(""));
("业务的受理条件", "process_condition", "process_condition_edge", CollUtil.newArrayList("")),
("业务的办理流程", "handler_process", "handler_process_edge", CollUtil.newArrayList("")),
("业务的材料清单", "checklist", "checklist_edge", CollUtil.newArrayList("")),
("业务的受理范围", "accept_scope", "accept_scope_edge", CollUtil.newArrayList("")),
("业务的办理途径", "handler_channel", "handler_channel_edge", CollUtil.newArrayList("")),
("业务的办理窗口", "handler_place", "handler_place_edge", CollUtil.newArrayList(""));
private String intent;
private final String intent;
private List<String> explainList;
private final String tagType;
IdentifyIntentEnum(String intent, List<String> explainList) {
private final String edgeType;
private final List<String> explainList;
IdentifyIntentEnum(String intent, String tagType, String edgeType, List<String> explainList) {
this.intent = intent;
this.tagType = tagType;
this.edgeType = edgeType;
this.explainList = explainList;
}
public static IdentifyIntentEnum getEnumByIntent(String intent) {
for (IdentifyIntentEnum value : IdentifyIntentEnum.values()) {
if (value.getIntent().equals(intent)) {
return value;
}
}
return null;
}
public String getEdgeType() {
return edgeType;
}
public String getTagType() {
return tagType;
}
public String getIntent() {
return intent;
}

@ -0,0 +1,32 @@
package com.supervision.handler.gpt;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.supervision.ai.AiUtil;
import com.supervision.ai.dto.MessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component
public class AnswerQuestionHandler {
public String answerQuestion(String question, List<String> detailList) {
List<MessageDTO> messageList = new ArrayList<>();
messageList.add(new MessageDTO("system", "你是一个政务事项领域的大模型,我现在给一些政务文件的内容,再给你一个问题,请根据文件内容,将这个问题进行解答.除了解答的内容,什么其他的都不要说."));
messageList.add(new MessageDTO("assistant", "好的"));
messageList.add(new MessageDTO("user", StrUtil.format("政务文件内容:[{}]", CollUtil.join(detailList, ";"))));
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;
}
}

@ -0,0 +1,26 @@
package com.supervision.handler.graph;
import cn.hutool.core.collection.CollUtil;
import com.supervision.exception.BusinessException;
import com.supervision.ngbatis.dao.CommonQueryDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
@Slf4j
@Component
public class FindItemDetailHandler {
@Resource
private CommonQueryDao commonQueryDao;
public List<String> findItemDetail(String itemVid, String itemType, String edgeType) {
List<String> itemDetail = commonQueryDao.findItemDetail(itemVid, edgeType, itemType);
if (CollUtil.isEmpty(itemDetail)){
throw new BusinessException("未找到事项信息");
}
return itemDetail;
}
}

@ -73,4 +73,14 @@ public interface CommonQueryDao {
* @return
*/
List<ItemLeaf> findItemLeafByBranchId(@Param("branchVId") String branchVId);
/**
* ,
* @param leafVid ID
* @param edgeType
* @param tagType
* @return
*/
List<String> findItemDetail(@Param("leafVid") String leafVid, @Param("edgeType") String edgeType, @Param("tagType") String tagType);
}

@ -8,11 +8,14 @@ import com.supervision.dto.roundAsk.EntityQuestionDTO;
import com.supervision.dto.roundAsk.ItemNodeDTO;
import com.supervision.dto.roundAsk.SessionParamDTO;
import com.supervision.enums.EntityQuestionEnum;
import com.supervision.enums.IdentifyIntentEnum;
import com.supervision.exception.BusinessException;
import com.supervision.handler.gpt.AnswerQuestionHandler;
import com.supervision.handler.gpt.ConditionJudgeHandler;
import com.supervision.handler.gpt.IdentifyIntentHandler;
import com.supervision.handler.gpt.ItemExtractHandler;
import com.supervision.handler.graph.FindConditionPathHandler;
import com.supervision.handler.graph.FindItemDetailHandler;
import com.supervision.handler.graph.FindItemNodeHandler;
import com.supervision.ngbatis.domain.tag.Condition;
import com.supervision.ngbatis.domain.tag.ItemLeaf;
@ -46,6 +49,10 @@ public class AskServiceImpl implements AskService {
private final ConditionJudgeHandler conditionJudgeHandler;
private final FindItemDetailHandler findItemDetailHandler;
private final AnswerQuestionHandler answerQuestionHandler;
private static final String SESSION_PARAM = "KBQA:ASK:SESSION_PARAM:";
@ -180,10 +187,19 @@ public class AskServiceImpl implements AskService {
}
}
// 走到这里,说明就只有一个节点了,那么就可以进行下一步了
log.info("走到这里,说明找到了匹配的节点");
return RoundTalkResVO.builder().sessionId(sessionId).replyQuestion("找到了匹配的节点").build();
log.info("走到这里,说明找到了匹配的节点,开始根据用户的意图生成");
String intent = sessionParamDTO.getIntent();
ItemLeaf matchItemLeaf = sessionParamDTO.getMatchItemLeaf();
// 根据用户的意图找到对应的节点
IdentifyIntentEnum intentEnum = IdentifyIntentEnum.getEnumByIntent(intent);
if (ObjectUtil.isEmpty(intentEnum) || null == intentEnum) {
throw new BusinessException("暂不支持该意图的问答");
}
// 根据意图和节点,找到对应的结果
List<String> itemDetail = findItemDetailHandler.findItemDetail(matchItemLeaf.getVid(), intentEnum.getTagType(), intentEnum.getEdgeType());
// 提交GPT,问问题的答案
String answer = answerQuestionHandler.answerQuestion(sessionParamDTO.getOriginalQuestion(), itemDetail);
return RoundTalkResVO.builder().sessionId(sessionId).replyQuestion(answer).build();
}
private Map<String, Integer> countCondition(SessionParamDTO sessionParamDTO) {

@ -32,4 +32,8 @@
<select id="findItemLeafByBranchId" resultType="com.supervision.ngbatis.domain.tag.ItemLeaf">
MATCH (b:item_branch)-[r:dependence_edge*]->(v:item_leaf) WHERE id(b) == "${branchVId}" RETURN DISTINCT v;
</select>"
<select id="findItemDetail" resultType="java.lang.String">
MATCH (b:item_leaf)-[r:${edgeType}]->(v:${nodeType}}) WHERE id(b) == "${leafVid}" RETURN v.detail;
</select>
</mapper>
Loading…
Cancel
Save