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.

80 lines
4.6 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
import java.util.Map;
@Slf4j
@Component
public class AnswerQuestionHandler {
public String answerQuestion(String question, List<String> detailList, Map<String, String> talkRecord) {
List<MessageDTO> messageList = new ArrayList<>();
messageList.add(new MessageDTO("system", "现在你是一个政务事项领域的问答大模型.\n" +
"现在有一个问题,根据这个问题我查到了政策内容,并提供了用户问询过程中的对话记录,可供参考;\n" +
"请你严格根据政策内容回答这个问题,请不要有文件内容之外的内容"));
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", "继续"));
if (CollUtil.isNotEmpty(talkRecord)) {
List<String> record = new ArrayList<>();
for (Map.Entry<String, String> entry : talkRecord.entrySet()) {
record.add("询问用户:" + entry.getKey());
record.add("用户回答:" + entry.getValue());
}
messageList.add(new MessageDTO("user", "下面是用户回答过程中的对话记录,可供参考,并提供更精确的回答:{" + CollUtil.join(record, ";") + "}"));
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;
}
/**
* 回答单轮对话问题
*
* @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("现在你可以回答了,请用100字以内进行回答.如果你从文件内容中,没有提取到回答,你就回复:我暂时还不会这个问题哦!")));
log.info("answerQuestion的prompt是:{}", JSONUtil.toJsonStr(messageList));
String answer = AiUtil.chatByMessage(messageList);
log.info("answerQuestion的答案是:{}", answer);
return answer;
}
public String answerSingleQuestionNew(String question, Map<String, List<String>> detailMap) {
String template = "作为AI助手你的任务是帮助用户查找和理解特定政务的政策内容并给于用户答案。在这个场景中你将使用政策内容来回答用户问题{}。请根据政策内容\n" +
"\"\"\"{}\"\"\"\n" +
"并用不超过 100 个词的长度准确和详细的回答用户的问题。";
List<MessageDTO> messageList = new ArrayList<>();
messageList.add(new MessageDTO("user", StrUtil.format(template, question, JSONUtil.toJsonStr(detailMap))));
log.info("answerQuestion的prompt是:{}", JSONUtil.toJsonStr(messageList));
String answer = AiUtil.chatByMessage(messageList);
log.info("answerQuestion的答案是:{}", answer);
return answer;
}
}