You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB
Java

package com.supervision.handler.gpt;
1 year ago
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
1 year ago
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
1 year ago
import com.supervision.ai.AiUtil;
import com.supervision.ai.dto.MessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.*;
1 year ago
import java.util.regex.Matcher;
import java.util.regex.Pattern;
1 year ago
/**
* 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" +
1 year ago
"基于用户的回答,请依次判断用户的回答满足以下列表:{}中的哪些项?\n" +
"以json数组的格式返回,格式示例[\"xxx\",\"xxx\"]。回答一定要来自于列表原文。不要胡乱编造。不需要推理过程。如果列表中项全部不满足,则返回:未找到";
1 year ago
1 year ago
String judgeAsk = StrUtil.format(template, question, userAnswer, JSONUtil.toJsonStr(candidateAnswerList));
log.info("conditionJudge问题是:{}", judgeAsk);
messageList.add(new MessageDTO("user", judgeAsk));
String judgeResult = AiUtil.chatByMessage(messageList);
log.info("conditionJudge判断结果是:{}", judgeResult);
1 year ago
String[] strings = StrUtil.subBetweenAll(judgeResult, "[", "]");
if (strings.length == 0) {
return new HashSet<>();
}
1 year ago
HashSet<String> result = new HashSet<>(JSONUtil.toList("[" + strings[0] + "]", String.class));
if (result.size() == 1 && result.contains("未找到")) {
return new HashSet<>();
}
return result;
}
1 year ago
}