舆情自动化

This commit is contained in:
lxu75
2025-03-06 16:56:34 +08:00
parent 866574a4fe
commit fd9cdb31ae
5 changed files with 106 additions and 9 deletions

View File

@@ -15,7 +15,7 @@ public class CommunityTargetDTO {
@Setter
@Getter
class ContentNode {
public static class ContentNode {
private String nodeType;
private String nodeContent;
}

View File

@@ -0,0 +1,13 @@
package com.volvo.ai.analytic.center.dto.req;
import lombok.Data;
@Data
public class DifyImageWorkFlow {
private String transfer_method = "remote_url";
private String type = "image";
private String url;
}

View File

@@ -0,0 +1,13 @@
package com.volvo.ai.analytic.center.dto.resp;
import lombok.Data;
@Data
public class DifyCommunityTargetResult {
private String targetId;
private String commentAnswer;
private String targetType;
}

View File

@@ -0,0 +1,20 @@
package com.volvo.ai.analytic.center.enums;
import lombok.Getter;
@Getter
public enum NodeTypeEnum {
IMAGE("image", "图片"),
TEXT("text", "文本")
;
private String code;
private String message;
NodeTypeEnum(String code, String message) {
this.code = code;
this.message = message;
}
}

View File

@@ -65,6 +65,9 @@ public class MqMessageRecordServiceImpl extends ServiceImpl<MqMessageRecordMappe
@Value("${dify.flowId}")
private String flowId;
@Value("${dify.imageFlowId}")
private String imageFlowId;
@Value("${rocketmq.producer.topic}")
private String topic;
@@ -78,7 +81,7 @@ public class MqMessageRecordServiceImpl extends ServiceImpl<MqMessageRecordMappe
@Transactional
public boolean processMessageByMQ(String message) {
log.info("communityProcessMessageByMQ message: {}", message);
DifyCommunityTargetDTO communityTargetDTO = JSON.parseObject(message, DifyCommunityTargetDTO.class);
CommunityTargetDTO communityTargetDTO = JSON.parseObject(message, CommunityTargetDTO.class);
if (communityTargetDTO == null) {
log.error("communityProcessMessageByMQ message is null");
return false;
@@ -95,22 +98,70 @@ public class MqMessageRecordServiceImpl extends ServiceImpl<MqMessageRecordMappe
.aiAnalysisRequestType(BusinessTypeEnum.COMMUNITYTARGET.getCode())
.build());
//构建请求dify对象
DiFyReq diFyReq = new DiFyReq();
diFyReq.setUser(user);
diFyReq.setFlowId(flowId);
diFyReq.setInputs(communityTargetDTO);
JSONObject difResult = new JSONObject();
try {
//调用dify服务
boolean hasImageNodeType = communityTargetDTO.getContent().stream()
.anyMatch(contentNode -> NodeTypeEnum.IMAGE.getCode().equals(contentNode.getNodeType()));
//如果存在图片节点,则先处理图片节点
StringBuilder sb = new StringBuilder();
if (hasImageNodeType) {
communityTargetDTO.getContent().forEach(contentNode -> {
if (contentNode.getNodeType().equals(NodeTypeEnum.IMAGE.getCode())) {
//构建图片分析请求对象
DifyImageWorkFlow diFyImageWorkFlow = new DifyImageWorkFlow();
diFyImageWorkFlow.setUrl(contentNode.getNodeContent());
DiFyReq diFyImageReq = new DiFyReq();
diFyImageReq.setUser(user);
diFyImageReq.setFlowId(imageFlowId);
diFyImageReq.setInputs(diFyImageWorkFlow);
//调用dify图片分析workflow
JSONObject difImageResult = (JSONObject) diFyService.getDiFyObject(diFyImageReq);
sb.append(difImageResult.get("text"));
}
});
}
String textContent = Optional.ofNullable(communityTargetDTO.getContent())
.orElse(Collections.emptyList()).stream()
.filter(contentNode -> NodeTypeEnum.TEXT.getCode().equals(contentNode.getNodeType()))
.map(CommunityTargetDTO.ContentNode::getNodeContent)
.collect(Collectors.joining(""));
//构建舆情分析请求对象
DifyCommunityTargetDTO difyCommunityTargetDTO = new DifyCommunityTargetDTO();
difyCommunityTargetDTO.setTargetId(communityTargetDTO.getTargetId());
difyCommunityTargetDTO.setTargetType(communityTargetDTO.getTargetType());
difyCommunityTargetDTO.setTargetContent(textContent + sb.toString());
DiFyReq diFyReq = new DiFyReq();
diFyReq.setUser(user);
diFyReq.setFlowId(flowId);
diFyReq.setInputs(difyCommunityTargetDTO);
//调用舆情文本分析dify工作流
difResult = (JSONObject) diFyService.getDiFyObject(diFyReq);
JSONObject difResultJSONObject = difResult.getJSONObject("text");
if (difResultJSONObject != null) {
DifyCommunityTargetResult difyCommunityTargetResult = new DifyCommunityTargetResult();
String targetId = difResultJSONObject.getString("targetId");
String targetType = difResultJSONObject.getString("targetType");
String commentAnswer = difResultJSONObject.getString("commentAnswer");
difyCommunityTargetResult.setTargetType(targetType);
difyCommunityTargetResult.setTargetId(targetId);
difyCommunityTargetResult.setCommentAnswer(commentAnswer);
//返回结果推送到社区的MQ
rocketMQTemplate.syncSend("", JSON.toJSONString(difyCommunityTargetResult));
log.info("舆情分析发送回调MQ完成: {}", JSON.toJSONString(difyCommunityTargetResult));
}
} catch (Exception e) {
log.error("请求DiFy异常:{}", e);
//保存错误日志
aiAnalysisErrorsMapper.insert(AiAnalysisErrors.builder()
.aiAnalysisRequestId(aiAnalysisRequestId)
.difyResponse(difResult.toJSONString())
.aiAnalysisErrorMessage(e.getMessage())
.aiAnalysisErrorMessage(e.getMessage())
.build());
}
return true;