demo
parent
6c03205fde
commit
54cff96d45
@ -0,0 +1,120 @@
|
||||
package com.supervision.nxllm.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.supervision.springaidemo.domain.ModelMetric;
|
||||
import com.supervision.springaidemo.dto.QARecordNodeDTO;
|
||||
import com.supervision.springaidemo.service.ModelMetricService;
|
||||
import com.supervision.springaidemo.service.NoteCheckRecordService;
|
||||
import com.supervision.springaidemo.thread.RunCheckThread;
|
||||
import com.supervision.springaidemo.thread.RunCheckThreadPool;
|
||||
import com.supervision.springaidemo.util.RecordRegexUtil;
|
||||
import com.supervision.springaidemo.util.WordReadUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.chat.messages.AssistantMessage;
|
||||
import org.springframework.ai.chat.messages.Message;
|
||||
import org.springframework.ai.chat.messages.SystemMessage;
|
||||
import org.springframework.ai.chat.messages.UserMessage;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.ollama.OllamaChatClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 目前这是效果最好的方案,暂时先用这个
|
||||
*/
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequestMapping("exampleChat")
|
||||
public class ExampleChatController {
|
||||
|
||||
private final OllamaChatClient chatClient;
|
||||
|
||||
@Autowired
|
||||
private ModelMetricService modelMetricService;
|
||||
|
||||
@Autowired
|
||||
private NoteCheckRecordService noteCheckRecordService;
|
||||
|
||||
@Autowired
|
||||
public ExampleChatController(OllamaChatClient chatClient) {
|
||||
this.chatClient = chatClient;
|
||||
}
|
||||
|
||||
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格式回复, JSON的value内容我给你的提示,在实际输出的时候不需要带上:
|
||||
---
|
||||
{"result":"结论", "originalContext":"笔录对应原话","reason":"原因"}
|
||||
---
|
||||
好了,现在可以回复了!
|
||||
""";
|
||||
|
||||
@GetMapping("exampleChat")
|
||||
public void exampleChat() {
|
||||
File file = FileUtil.file("/Users/flevance/Desktop/宁夏审讯大模型/裴金禄/行为人和受害人/裴金禄第一次.docx");
|
||||
String context = WordReadUtil.readWord(file.getPath());
|
||||
List<QARecordNodeDTO> qaList = RecordRegexUtil.recordRegex(context, "裴金禄");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue