|
|
@ -1,5 +1,6 @@
|
|
|
|
package com.supervision.police.service.impl;
|
|
|
|
package com.supervision.police.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import com.alibaba.druid.util.StringUtils;
|
|
|
|
import com.alibaba.druid.util.StringUtils;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
@ -17,6 +18,7 @@ import com.supervision.police.mapper.NoteRecordMapper;
|
|
|
|
import com.supervision.police.mapper.NotePromptMapper;
|
|
|
|
import com.supervision.police.mapper.NotePromptMapper;
|
|
|
|
import com.supervision.police.mapper.TripleInfoMapper;
|
|
|
|
import com.supervision.police.mapper.TripleInfoMapper;
|
|
|
|
import com.supervision.police.service.ModelRecordTypeService;
|
|
|
|
import com.supervision.police.service.ModelRecordTypeService;
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONObject;
|
|
|
@ -24,6 +26,7 @@ import org.springframework.ai.chat.ChatResponse;
|
|
|
|
import org.springframework.ai.chat.Generation;
|
|
|
|
import org.springframework.ai.chat.Generation;
|
|
|
|
import org.springframework.ai.chat.messages.Message;
|
|
|
|
import org.springframework.ai.chat.messages.Message;
|
|
|
|
import org.springframework.ai.chat.messages.SystemMessage;
|
|
|
|
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.chat.prompt.Prompt;
|
|
|
|
import org.springframework.ai.ollama.OllamaChatClient;
|
|
|
|
import org.springframework.ai.ollama.OllamaChatClient;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@ -37,31 +40,23 @@ import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMapper, ModelRecordType> implements ModelRecordTypeService {
|
|
|
|
public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMapper, ModelRecordType> implements ModelRecordTypeService {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private final ModelRecordTypeMapper modelRecordTypeMapper;
|
|
|
|
private ModelRecordTypeMapper modelRecordTypeMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private final NoteRecordMapper noteRecordMapper;
|
|
|
|
private NoteRecordMapper noteRecordMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private final NotePromptMapper notePromptMapper;
|
|
|
|
private NotePromptMapper notePromptMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final OllamaChatClient chatClient;
|
|
|
|
private final TripleInfoMapper tripleInfoMapper;
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
public ModelRecordTypeServiceImpl(OllamaChatClient chatClient) {
|
|
|
|
|
|
|
|
this.chatClient = chatClient;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private final Neo4jService neo4jService;
|
|
|
|
private TripleInfoMapper tripleInfoMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private final OllamaChatClient chatClient;
|
|
|
|
private Neo4jService neo4jService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public R<?> queryType(String name, Integer page, Integer size) {
|
|
|
|
public List<ModelRecordType> queryType(String name, Integer page, Integer size) {
|
|
|
|
// IPage<ModelRecordType> iPage = new Page<>(page, size);
|
|
|
|
// IPage<ModelRecordType> iPage = new Page<>(page, size);
|
|
|
|
// iPage = modelRecordTypeMapper.selectByName(iPage, name);
|
|
|
|
// iPage = modelRecordTypeMapper.selectByName(iPage, name);
|
|
|
|
// return R.ok(IPages.buildDataMap(iPage));
|
|
|
|
// return R.ok(IPages.buildDataMap(iPage));
|
|
|
@ -75,7 +70,7 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
List<NotePrompt> prompts = notePromptMapper.queryPrompt(modelRecordType.getId());
|
|
|
|
List<NotePrompt> prompts = notePromptMapper.queryPrompt(modelRecordType.getId());
|
|
|
|
modelRecordType.setPrompts(prompts);
|
|
|
|
modelRecordType.setPrompts(prompts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R.ok(list);
|
|
|
|
return list;
|
|
|
|
// return R.ok(IPages.buildDataMap(iPage));
|
|
|
|
// return R.ok(IPages.buildDataMap(iPage));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -128,8 +123,7 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
private List<TripleInfo> extractTripleInfo(String caseId, String name, String recordId) {
|
|
|
|
public R<?> getThreeInfo(String caseId, String name, String recordId) {
|
|
|
|
|
|
|
|
List<NoteRecord> records = noteRecordMapper.selectRecord(caseId, name, recordId);
|
|
|
|
List<NoteRecord> records = noteRecordMapper.selectRecord(caseId, name, recordId);
|
|
|
|
List<TripleInfo> tripleInfos = new ArrayList<>();
|
|
|
|
List<TripleInfo> tripleInfos = new ArrayList<>();
|
|
|
|
for (NoteRecord record : records) {
|
|
|
|
for (NoteRecord record : records) {
|
|
|
@ -139,17 +133,15 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// 分析三元组
|
|
|
|
|
|
|
|
List<Message> messages = new ArrayList<>(List.of(new SystemMessage(prompt.getPrompt() + record.getQuestion() + record.getAnswer())));
|
|
|
|
|
|
|
|
Prompt ask = new Prompt(messages);
|
|
|
|
|
|
|
|
StopWatch stopWatch = new StopWatch();
|
|
|
|
StopWatch stopWatch = new StopWatch();
|
|
|
|
|
|
|
|
// 分析三元组
|
|
|
|
|
|
|
|
Prompt ask = new Prompt(new UserMessage(prompt.getPrompt() + record.getQuestion() + record.getAnswer()));
|
|
|
|
stopWatch.start();
|
|
|
|
stopWatch.start();
|
|
|
|
log.info("开始分析:");
|
|
|
|
log.info("开始分析:");
|
|
|
|
ChatResponse call = chatClient.call(ask);
|
|
|
|
ChatResponse call = chatClient.call(ask);
|
|
|
|
stopWatch.stop();
|
|
|
|
stopWatch.stop();
|
|
|
|
log.info("耗时:{}", stopWatch.getTotalTimeSeconds());
|
|
|
|
log.info("耗时:{}", stopWatch.getTotalTimeSeconds());
|
|
|
|
Generation result = call.getResult();
|
|
|
|
String content = call.getResult().getOutput().getContent();
|
|
|
|
String content = result.getOutput().getContent();
|
|
|
|
|
|
|
|
log.info("分析的结果是:{}", content);
|
|
|
|
log.info("分析的结果是:{}", content);
|
|
|
|
JSONObject jsonObject = new JSONObject(content);
|
|
|
|
JSONObject jsonObject = new JSONObject(content);
|
|
|
|
JSONArray threeInfo = jsonObject.getJSONArray("result");
|
|
|
|
JSONArray threeInfo = jsonObject.getJSONArray("result");
|
|
|
@ -160,9 +152,8 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
String endNodeType = object.getString("endNodeType");
|
|
|
|
String endNodeType = object.getString("endNodeType");
|
|
|
|
String property = object.getString("property");
|
|
|
|
String property = object.getString("property");
|
|
|
|
String value = object.getString("value");
|
|
|
|
String value = object.getString("value");
|
|
|
|
//去空
|
|
|
|
// 去空,如果存在任何的空值,则忽略
|
|
|
|
if (StringUtils.isEmpty(startNodeType) || StringUtils.isEmpty(entity) || StringUtils.isEmpty(endNodeType)
|
|
|
|
if (StrUtil.hasEmpty(startNodeType, entity, endNodeType, property, value)) {
|
|
|
|
|| StringUtils.isEmpty(property) || StringUtils.isEmpty(value)) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TripleInfo tripleInfo = new TripleInfo(entity, property, value, record.getId(), LocalDateTime.now(), startNodeType, endNodeType);
|
|
|
|
TripleInfo tripleInfo = new TripleInfo(entity, property, value, record.getId(), LocalDateTime.now(), startNodeType, endNodeType);
|
|
|
@ -170,21 +161,28 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
tripleInfos.add(tripleInfo);
|
|
|
|
tripleInfos.add(tripleInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R.ok(tripleInfos);
|
|
|
|
return tripleInfos;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public List<TripleInfo> getThreeInfo(String caseId, String name, String recordId) {
|
|
|
|
|
|
|
|
// TODO 这里应该改成异步的形式,通过异步的形式来进行提取三元组信息,不能每次点击就跑一遍
|
|
|
|
|
|
|
|
return extractTripleInfo(caseId, name, recordId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public R<?> addNeo4j(List<String> ids) {
|
|
|
|
public String addNeo4j(List<String> ids) {
|
|
|
|
List<TripleInfo> tripleInfos = tripleInfoMapper.selectByIds(ids);
|
|
|
|
List<TripleInfo> tripleInfos = tripleInfoMapper.selectByIds(ids);
|
|
|
|
int i = 0;
|
|
|
|
int i = 0;
|
|
|
|
for (TripleInfo tripleInfo : tripleInfos) {
|
|
|
|
for (TripleInfo tripleInfo : tripleInfos) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
//开始节点
|
|
|
|
//开始节点
|
|
|
|
String start = tripleInfo.getStartNode();
|
|
|
|
String start = tripleInfo.getStartNode();
|
|
|
|
|
|
|
|
// 首先看是否已经存在了,如果已经存在了,就不添加了
|
|
|
|
CaseNode startNode = neo4jService.findOneByName(tripleInfo.getCaseId(), tripleInfo.getNoteRecordsId(), tripleInfo.getStartNodeType(), start, "1");
|
|
|
|
CaseNode startNode = neo4jService.findOneByName(tripleInfo.getCaseId(), tripleInfo.getNoteRecordsId(), tripleInfo.getStartNodeType(), start, "1");
|
|
|
|
if (startNode == null) {
|
|
|
|
if (startNode == null) {
|
|
|
|
startNode = new CaseNode(start, tripleInfo.getStartNodeType(), tripleInfo.getNoteRecordId(), tripleInfo.getNoteRecordsId(), tripleInfo.getCaseId(), "1");
|
|
|
|
startNode = new CaseNode(start, tripleInfo.getStartNodeType(), tripleInfo.getNoteRecordId(), tripleInfo.getNoteRecordsId(), tripleInfo.getCaseId(), "1");
|
|
|
@ -203,17 +201,18 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
Rel rel = new Rel(startNode.getId(), tripleInfo.getRelation(), endNode.getId());
|
|
|
|
Rel rel = new Rel(startNode.getId(), tripleInfo.getRelation(), endNode.getId());
|
|
|
|
Rel relation = neo4jService.findRelation(rel);
|
|
|
|
Rel relation = neo4jService.findRelation(rel);
|
|
|
|
if (relation == null) {
|
|
|
|
if (relation == null) {
|
|
|
|
R<?> r = neo4jService.saveRelation(rel);
|
|
|
|
neo4jService.saveRelation(rel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tripleInfo.setAddNeo4j("1");
|
|
|
|
tripleInfo.setAddNeo4j("1");
|
|
|
|
int j = tripleInfoMapper.updateById(tripleInfo);
|
|
|
|
int j = tripleInfoMapper.updateById(tripleInfo);
|
|
|
|
if (j > 0) {
|
|
|
|
if (j > 0) {
|
|
|
|
i++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO 重复添加的OK了,删除的呢?
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R.ok("成功插入" + i + "条信息");
|
|
|
|
return ("成功插入" + i + "条信息");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|