|
|
|
|
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> conditionJudgeAll(String question, Collection<String> candidateAnswerList, String userAnswer) {
|
|
|
|
|
List<MessageDTO> messageList = new ArrayList<>();
|
|
|
|
|
String template = "当我问用户:{},用户给我的回答是:[{}],\n" +
|
|
|
|
|
"基于用户的回答,请依次判断以下列表:[{}]中的哪些项满足用户的回答?\n" +
|
|
|
|
|
"请从给定列表中找出所有满足的项.以jsonArray格式输出且只返回jsonArray.jsonArray的格式示例为:```json[\"满足的条件1\",\"满足的条件2\"]```.如果列表中项全部不满足,则返回:未找到";
|
|
|
|
|
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.contains("```json")) {
|
|
|
|
|
judgeResult = StrUtil.subBetween(judgeResult, "```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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|