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.
fu-hsi-service/src/main/java/com/supervision/springaidemo/controller/ExampleChatController.java

258 lines
14 KiB
Java

11 months ago
package com.supervision.springaidemo.controller;
import cn.hutool.core.io.FileUtil;
import com.supervision.police.domain.ModelRecordType;
11 months ago
import com.supervision.police.domain.NoteRecordSplit;
11 months ago
import com.supervision.springaidemo.dto.QARecordNodeDTO;
import com.supervision.police.mapper.ModelRecordTypeMapper;
11 months ago
import com.supervision.police.mapper.NoteRecordSplitMapper;
11 months ago
import com.supervision.police.service.ModelRecordTypeService;
import com.supervision.springaidemo.service.ModelMetricService;
import com.supervision.police.service.RecordService;
import com.supervision.police.service.NoteCheckRecordService;
import com.supervision.springaidemo.util.RecordRegexUtil;
import com.supervision.springaidemo.util.WordReadUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.json.JSONObject;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.time.LocalDateTime;
11 months ago
import java.util.*;
import java.util.stream.Collectors;
/**
* ,
*/
@RestController
@Slf4j
@RequestMapping("exampleChat")
public class ExampleChatController {
private final OllamaChatClient chatClient;
@Autowired
private ModelMetricService modelMetricService;
@Autowired
private NoteCheckRecordService noteCheckRecordService;
@Autowired
private ModelRecordTypeMapper modelRecordTypeMapper;
@Autowired
11 months ago
private NoteRecordSplitMapper noteRecordSplitMapper;
11 months ago
@Autowired
public ExampleChatController(OllamaChatClient chatClient) {
this.chatClient = chatClient;
}
@Autowired
private RecordService recordService;
@Autowired
private ModelRecordTypeService modelRecordTypeService;
private static final String exampleTemplate = """
step by step,
step1:;
:
---
{context}
---
step2::
:
:{metricName}
true:{example};
false;
,,,empty
step3:,:
1.:true/false/empty
2.:,()true,!
3.:,,true/false,!
step4:json, JSONvalue,:
---
{"result":"结论", "originalContext":"笔录对应原话","reason":"原因"}
---
,!
""";
@GetMapping("exampleChat")
public void exampleChat() {
File file = FileUtil.file("E:\\jc\\宁夏\\Fw_裴金禄\\裴金禄第一次.docx");
String context = WordReadUtil.readWord(file.getPath());
List<QARecordNodeDTO> qaList = RecordRegexUtil.recordRegex(context, "裴金禄");
for (QARecordNodeDTO qa : qaList) {
11 months ago
NoteRecordSplit noteRecord = new NoteRecordSplit();
11 months ago
noteRecord.setNoteName(file.getName());
noteRecord.setPersonName("裴金禄");
noteRecord.setQuestion(qa.getQuestion());
noteRecord.setAnswer(qa.getAnswer());
noteRecord.setCreateTime(LocalDateTime.now());
11 months ago
// if (qa.getAnswer().length() > 500) {
// continue;
// }
//根据提示词分析类型 并保存
// String test = "你现在是一个笔录分析人员,请用四个字描述一下下述内容属于哪种类型的对话?";
String test = "你是一个善于归纳分析问讯笔录的大模型,请用四个字描述下述内容属于哪种类型的对话?对话内容为:";
String tip = "你的回答必须严格控制为四个字。";
String example = "示例1办案警官问:我们是中卫市公安局沙坡头区分局经侦大队的民警(出示工作证件),现依法对你进行讯问,你应当如实回答我们的提问,对与案件无关的问题,你有拒绝回答的权利。你听明白了吗? 张某答:听明白了。回复:{\"type\":\"权利义务\"}。" +
"示例2办案警官问你的个人情况张某答我叫裴金禄1984年01月10日出生藏族大学文化户籍所在地陕西省西安市雁塔区丈八四路6号7号楼1单元 2304室现住陕西省西安市雁塔区丈八四路6号7号楼1单元 2304室无业居民身份证号码622326198401100090联系电话15829319393。回复{\"type\":\"个人情况\"}。" +
"示例3办案警官问你的家庭情况张某答我父亲叫裴国智今年67岁退休电话13519353723; 母亲李金芬今年61岁无业我弟弟裴金生今年 34岁在中石油运输公司上班。回复{\"type\":\"家庭情况\"}。";
List<Message> messages = new ArrayList<>(List.of(new SystemMessage(test + qa.getQuestion() + qa.getAnswer() + tip + example)));
Prompt prompt = new Prompt(messages);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// log.info("开始分析:");
ChatResponse call = chatClient.call(prompt);
stopWatch.stop();
// log.info("耗时:{}", stopWatch.getTotalTimeSeconds());
Generation result = call.getResult();
String content = result.getOutput().getContent();
// log.info("问:{}, 答:{}", qa.getQuestion(), qa.getAnswer());
// log.info("分析的结果是:{}", content);
JSONObject jsonObject = new JSONObject(content);
String type = jsonObject.getString("type").trim();
System.out.println("问:"+qa.getQuestion()+"答:"+qa.getAnswer());
System.out.println("分析的结果是:"+ type);
//保存笔录
noteRecord.setRecordType(type);
recordService.save(noteRecord);
ModelRecordType exist = modelRecordTypeService.queryByName(type);
if (exist == null) {
ModelRecordType modelRecordType = new ModelRecordType();
modelRecordType.setRecordType(type);
modelRecordTypeService.save(modelRecordType);
}
}
// for (QARecordNodeDTO qaRecordNodeDTO : qaList) {
// // 只查入罪指标
// List<ModelMetric> list = modelMetricService.lambdaQuery().likeRight(ModelMetric::getMetricCode, "RZ").list();
// for (ModelMetric modelMetric : list) {
// String systemPrompt = """
// 你是一个善于分析办案笔录的模型,能够根据办案笔录的回答内容,结合给定的例子,实事求是的判断给定指标是否满足。注意,仅根据笔录进行分析,不要做笔录之外的推断。笔录内容可能比较长,可能分多次提交给你。Think step by step
// """;
// List<Message> messages = new ArrayList<>(List.of(new SystemMessage(systemPrompt)));
// Map<String, Object> param = new HashMap<>();
// param.put("context", qaRecordNodeDTO.toString());
// param.put("metricName", modelMetric.getMetricName());
// param.put("example", StrUtil.format(modelMetric.getExample(), MapUtil.of("action", "裴金禄")));
// String format = StrUtil.format(exampleTemplate, param);
// List<Message> userMessageList = new ArrayList<>();
// if (format.length() > 8000) {
// log.info("分段提交");
// for (String s : StrUtil.split(format, 6000)) {
// userMessageList.add(new UserMessage(s));
// userMessageList.add(new AssistantMessage("继续"));
// }
// userMessageList.remove(userMessageList.size() - 1);
// } else {
// userMessageList.add(new UserMessage(format));
// }
// messages.addAll(userMessageList);
//
// RunCheckThread runCheck = new RunCheckThread("裴金禄尝试正则来做", chatClient, noteCheckRecordService, new Prompt(messages), FileUtil.getName(file), format, systemPrompt, modelMetric, 0);
// RunCheckThreadPool.chatExecutor.submit(runCheck);
// }
// }
}
@GetMapping("test")
public List test(@Param("test") String test) {
List<Map> list = new ArrayList<>();
List<Message> messages = new ArrayList<>(List.of(new SystemMessage(test)));
Prompt prompt = new Prompt(messages);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
log.info("开始分析:");
ChatResponse call = chatClient.call(prompt);
stopWatch.stop();
log.info("耗时:{}", stopWatch.getTotalTimeSeconds());
Generation result = call.getResult();
String content = result.getOutput().getContent();
log.info("分析的结果是:{}", content);
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("result", content);
list.add(resultMap);
return list;
}
@GetMapping("queryRecordType")
11 months ago
public List<NoteRecordSplit> queryRecordType() {
11 months ago
List<ModelRecordType> types = modelRecordTypeMapper.selectList(null);
String allType = types.stream().map(ModelRecordType::getRecordType).collect(Collectors.joining("."));
11 months ago
List<NoteRecordSplit> list = noteRecordSplitMapper.selectList(null);
for (NoteRecordSplit record : list) {
11 months ago
record.setRecordType("");
String test = "你是一个善于总结问讯内容的大模型,请判断以下对话属于【" + allType + "】哪个分类?对话内容为:";
String tip = "你的回答必须在所给范围内。";
String example = "示例办案警官问你的家庭情况裴金禄回答我父亲叫裴国智今年67岁退休电话13519353723;母亲李金芬今年61岁无业我弟弟裴金生今年34岁在中石油运输公司上班。回复{\"type\":\"家庭情况\"}。";
List<Message> messages = new ArrayList<>(List.of(new SystemMessage(test + record.getQuestion() + record.getAnswer() + tip + example)));
Prompt prompt = new Prompt(messages);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// log.info("开始分析:");
ChatResponse call = chatClient.call(prompt);
stopWatch.stop();
// log.info("耗时:{}", stopWatch.getTotalTimeSeconds());
Generation result = call.getResult();
String content = result.getOutput().getContent();
// log.info("问:{}, 答:{}", qa.getQuestion(), qa.getAnswer());
// log.info("分析的结果是:{}", content);
System.out.println("问:"+record.getQuestion()+"答:"+record.getAnswer());
System.out.println("分析的结果是:"+ content);
record.setRecordType(content);
}
return list;
}
@GetMapping("test1")
public void test2(@Param("id") String id) {
11 months ago
NoteRecordSplit noteRecord = recordService.getById(id);
11 months ago
String question = noteRecord.getQuestion();
String answer = noteRecord.getAnswer();
String test = "请从以下对话中提取所有关于" + noteRecord.getRecordType() + "的所有三元组";
// String test = "请从以下对话中提取所有三元组,对话内容为:";
String example = "。示例办案警官问你的家庭情况裴金禄回答我父亲叫裴国智今年67岁电话13519353723;母亲李金芬今年61岁我弟弟裴金生今年34岁。" +
"回复:{\"result\":[{\"主体\":\"裴金禄\",\"关系\":\"父子\",\"客体\":\"裴国智\"},{\"主体\":\"裴金禄\",\"关系\":\"母子\",\"客体\":\"李金芬\"},{\"主体\":\"裴金禄\",\"关系\":\"兄弟\",\"客体\":\"裴金生\"},{\"主体\":\"裴国智\",\"关系\":\"年龄\",\"客体\":67},{\"主体\":\"李金芬\",\"关系\":\"年龄\",\"客体\":61},{\"主体\":\"裴金生\",\"关系\":\"年龄\",\"客体\":34}]}。";
String a = "你是一个问讯笔录分析师,请分析以下内容中所有规定的三元组信息并补充完整,规定的三元组信息如下:[{startNodeType:'Person',entity:'',endNodeType:'Person',property:'',value:'' },{startNodeType:'Person',entity:'',endNodeType:'Organ',property:'',value:''}],要求返回格式{result:[三元组信息]}。";
List<Message> messages = new ArrayList<>(List.of(new SystemMessage(a + question+answer)));
// List<Message> messages = new ArrayList<>(List.of(new SystemMessage(test + question+answer+example)));
Prompt prompt = new Prompt(messages);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
log.info("开始分析:");
ChatResponse call = chatClient.call(prompt);
stopWatch.stop();
log.info("耗时:{}", stopWatch.getTotalTimeSeconds());
Generation result = call.getResult();
String content = result.getOutput().getContent();
log.info("分析的结果是:{}", content);
}
}