|
|
|
@ -17,6 +17,7 @@ import com.supervision.police.mapper.NoteRecordMapper;
|
|
|
|
|
import com.supervision.police.mapper.NotePromptMapper;
|
|
|
|
|
import com.supervision.police.mapper.TripleInfoMapper;
|
|
|
|
|
import com.supervision.police.service.ModelRecordTypeService;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
@ -24,6 +25,7 @@ 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.messages.UserMessage;
|
|
|
|
|
import org.springframework.ai.chat.prompt.Prompt;
|
|
|
|
|
import org.springframework.ai.ollama.OllamaChatClient;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
@ -36,31 +38,23 @@ import java.util.List;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMapper, ModelRecordType> implements ModelRecordTypeService {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ModelRecordTypeMapper modelRecordTypeMapper;
|
|
|
|
|
private final ModelRecordTypeMapper modelRecordTypeMapper;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private NoteRecordMapper noteRecordMapper;
|
|
|
|
|
private final NoteRecordMapper noteRecordMapper;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private NotePromptMapper notePromptMapper;
|
|
|
|
|
private final NotePromptMapper notePromptMapper;
|
|
|
|
|
|
|
|
|
|
private final OllamaChatClient chatClient;
|
|
|
|
|
@Autowired
|
|
|
|
|
public ModelRecordTypeServiceImpl(OllamaChatClient chatClient) {
|
|
|
|
|
this.chatClient = chatClient;
|
|
|
|
|
}
|
|
|
|
|
private final TripleInfoMapper tripleInfoMapper;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private TripleInfoMapper tripleInfoMapper;
|
|
|
|
|
private final Neo4jService neo4jService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private Neo4jService neo4jService;
|
|
|
|
|
private final OllamaChatClient chatClient;
|
|
|
|
|
|
|
|
|
|
@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 = modelRecordTypeMapper.selectByName(iPage, name);
|
|
|
|
|
// return R.ok(IPages.buildDataMap(iPage));
|
|
|
|
@ -74,7 +68,7 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
|
List<NotePrompt> prompts = notePromptMapper.queryPrompt(modelRecordType.getId());
|
|
|
|
|
modelRecordType.setPrompts(prompts);
|
|
|
|
|
}
|
|
|
|
|
return R.ok(list);
|
|
|
|
|
return list;
|
|
|
|
|
// return R.ok(IPages.buildDataMap(iPage));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -127,8 +121,7 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public R<?> getThreeInfo(String caseId, String name, String recordId) {
|
|
|
|
|
private List<TripleInfo> extractThreeInfo(String caseId, String name, String recordId) {
|
|
|
|
|
List<NoteRecord> records = noteRecordMapper.selectRecord(caseId, name, recordId);
|
|
|
|
|
List<TripleInfo> tripleInfos = new ArrayList<>();
|
|
|
|
|
for (NoteRecord record : records) {
|
|
|
|
@ -138,17 +131,15 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
// 分析三元组
|
|
|
|
|
Prompt ask = new Prompt(new UserMessage(prompt.getPrompt() + record.getQuestion() + record.getAnswer()));
|
|
|
|
|
stopWatch.start();
|
|
|
|
|
log.info("开始分析:");
|
|
|
|
|
ChatResponse call = chatClient.call(ask);
|
|
|
|
|
stopWatch.stop();
|
|
|
|
|
log.info("耗时:{}", stopWatch.getTotalTimeSeconds());
|
|
|
|
|
Generation result = call.getResult();
|
|
|
|
|
String content = result.getOutput().getContent();
|
|
|
|
|
String content = call.getResult().getOutput().getContent();
|
|
|
|
|
log.info("分析的结果是:{}", content);
|
|
|
|
|
JSONObject jsonObject = new JSONObject(content);
|
|
|
|
|
JSONArray threeInfo = jsonObject.getJSONArray("result");
|
|
|
|
@ -169,15 +160,21 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
|
tripleInfos.add(tripleInfo);
|
|
|
|
|
}
|
|
|
|
|
} 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 extractThreeInfo(caseId, name, recordId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public R<?> addNeo4j(List<String> ids) {
|
|
|
|
|
public String addNeo4j(List<String> ids) {
|
|
|
|
|
List<TripleInfo> tripleInfos = tripleInfoMapper.selectByIds(ids);
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (TripleInfo tripleInfo : tripleInfos) {
|
|
|
|
@ -210,9 +207,9 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return R.ok("成功插入" + i + "条信息");
|
|
|
|
|
return ("成功插入" + i + "条信息");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|