|
|
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;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 条件判断handler
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
public class ConditionJudgeHandler {
|
|
|
|
|
|
/**
|
|
|
* 效果有些一般,但是也能勉强去用
|
|
|
*
|
|
|
* @param question
|
|
|
* @param candidateAnswerList
|
|
|
* @param userAnswer
|
|
|
* @return
|
|
|
*/
|
|
|
public Set<String> conditionJudge(String question, Collection<String> candidateAnswerList, String userAnswer) {
|
|
|
List<MessageDTO> messageList = new ArrayList<>();
|
|
|
messageList.add(new MessageDTO("system", "现在要做社会保障业务分类,我现在给你一个问题,给你候选答案列表,请你根据用户的实际回答,从候选答案列表中给我选择对应的候选答案.除了候选答案,什么其他的都不要说."));
|
|
|
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("候选答案列表:[{};未找到匹配答案]", StrUtil.join(";", candidateAnswerList))));
|
|
|
messageList.add(new MessageDTO("assistant", "继续"));
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 以循环的形式去判断条件是否满足
|
|
|
*
|
|
|
* @param question 问题
|
|
|
* @param candidateAnswerList 判断条件
|
|
|
* @param userAnswer 用户回答
|
|
|
*/
|
|
|
public Set<String> newConditionJudge(String question, Collection<String> candidateAnswerList, String userAnswer, String conditionType) {
|
|
|
Set<String> judgeResultSet = new HashSet<>();
|
|
|
String template = "当我问用户:{},用户给我的回答是:[{}],\n" +
|
|
|
"基于用户的回答,判断一下用户{}是否满足[{}]?满足就只回复true,反之只回复false";
|
|
|
for (String candidateAnswer : candidateAnswerList) {
|
|
|
String judgeResult = StrUtil.format(template, question, userAnswer, conditionType, candidateAnswer);
|
|
|
String answer = AiUtil.chat(judgeResult);
|
|
|
log.info("conditionJudge判断条件:\n{},\n结果是:{}", judgeResult, answer);
|
|
|
try {
|
|
|
if (BooleanUtil.toBoolean(answer)) {
|
|
|
judgeResultSet.add(candidateAnswer);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.info("{}非布尔类型,不统计在内", answer);
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
return judgeResultSet;
|
|
|
}
|
|
|
}
|