package init
parent
0cbee97102
commit
895389bad3
@ -1,14 +0,0 @@
|
||||
package com.supervision.nxllm;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@MapperScan(basePackages = {"com.supervision.**.mapper"})
|
||||
@SpringBootApplication(scanBasePackages = {"com.supervision.**"})
|
||||
public class NxLlmApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(NxLlmApplication.class, args);
|
||||
}
|
||||
}
|
@ -1,120 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
package com.supervision.nxllm.thread;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.supervision.springaidemo.domain.ModelMetric;
|
||||
import com.supervision.springaidemo.domain.NoteCheckRecord;
|
||||
import com.supervision.springaidemo.dto.MetricResultDTO;
|
||||
import com.supervision.springaidemo.service.NoteCheckRecordService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.chat.ChatResponse;
|
||||
import org.springframework.ai.chat.Generation;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.ollama.OllamaChatClient;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
@Slf4j
|
||||
public class RunCheckThread implements Runnable {
|
||||
|
||||
private final String caseName;
|
||||
|
||||
private final OllamaChatClient chatClient;
|
||||
|
||||
private final NoteCheckRecordService noteCheckRecordService;
|
||||
|
||||
private final Prompt prompt;
|
||||
|
||||
private final String fileName;
|
||||
|
||||
private final String format;
|
||||
|
||||
private final String systemPrompt;
|
||||
|
||||
private final ModelMetric modelMetric;
|
||||
|
||||
|
||||
private Integer count;
|
||||
|
||||
public RunCheckThread(String caseName, OllamaChatClient chatClient, NoteCheckRecordService noteCheckRecordService, Prompt prompt, String fileName, String format, String systemPrompt, ModelMetric modelMetric, Integer count) {
|
||||
this.caseName = caseName;
|
||||
this.chatClient = chatClient;
|
||||
this.noteCheckRecordService = noteCheckRecordService;
|
||||
this.prompt = prompt;
|
||||
this.fileName = fileName;
|
||||
this.format = format;
|
||||
this.systemPrompt = systemPrompt;
|
||||
this.modelMetric = modelMetric;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
log.info("开始分析:{}",fileName);
|
||||
ChatResponse call = chatClient.call(prompt);
|
||||
stopWatch.stop();
|
||||
log.info("耗时:{}", stopWatch.getTotalTimeSeconds());
|
||||
Generation result = call.getResult();
|
||||
|
||||
String content = result.getOutput().getContent();
|
||||
log.info("分析的结果是:{}", content);
|
||||
MetricResultDTO metricResultDTO = JSONUtil.toBean(content, MetricResultDTO.class);
|
||||
// 如果为空,则再跑一次,最多跑5次
|
||||
if (StrUtil.isBlank(metricResultDTO.getResult())) {
|
||||
if (count > 5) {
|
||||
log.info("{}的{}结果超过5次,不再继续跑了", fileName, modelMetric);
|
||||
} else {
|
||||
log.info("{}的{}结果为空,当前跑了{}次,重新提交,再跑一次", fileName, modelMetric, count);
|
||||
Integer newCount = count++;
|
||||
RunCheckThread runCheck = new RunCheckThread(caseName, chatClient, noteCheckRecordService, prompt, fileName, format, systemPrompt, modelMetric, newCount);
|
||||
RunCheckThreadPool.chatExecutor.submit(runCheck);
|
||||
}
|
||||
} else {
|
||||
NoteCheckRecord noteCheckRecord = new NoteCheckRecord();
|
||||
noteCheckRecord.setCaseName(caseName);
|
||||
noteCheckRecord.setNoteName(fileName);
|
||||
noteCheckRecord.setMetricCode(modelMetric.getMetricCode());
|
||||
noteCheckRecord.setMetricName(modelMetric.getMetricName());
|
||||
noteCheckRecord.setSystemPrompt(systemPrompt);
|
||||
noteCheckRecord.setPrompt(format);
|
||||
noteCheckRecord.setResult(metricResultDTO.getResult());
|
||||
noteCheckRecord.setOriginalContext(metricResultDTO.getOriginalContext());
|
||||
noteCheckRecord.setReason(metricResultDTO.getReason());
|
||||
noteCheckRecordService.save(noteCheckRecord);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("出现错误", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package com.supervision.nxllm.thread;
|
||||
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
public class RunCheckThreadPool {
|
||||
|
||||
public static final ExecutorService chatExecutor = ThreadUtil.newFixedExecutor(20, Integer.MAX_VALUE, "chat", false);
|
||||
}
|
Loading…
Reference in New Issue