智能客服-四合一开发

This commit is contained in:
zren25
2025-04-02 18:31:54 +08:00
parent b7a7a1000b
commit 1cef8c6b5d
14 changed files with 524 additions and 3 deletions

View File

@@ -5,4 +5,6 @@ public class Constant {
public static final String CHANNEL_DCC = "Channel_Dcc";
public static final String DISPLAY_STATUS = "FINISHED";
public static final String INTELLIGENT_CUSTOMER_4IN1 = "INTELLIGENT_CUSTOMER_4IN1";
}

View File

@@ -0,0 +1,32 @@
package com.volvo.ai.analytic.center.dto.req;
import lombok.Data;
/**
*
* @ClassName: FourRequestDTO
* @author: renzhen
* @Description: 四合一请求参数
* @date: 2025-04-02 10:36
*/
@Data
public class FourInOneRequestDTO {
// 语料唯一Id
private String workOrderId;
//【语料内容】不可为空"
private String workOrderContent;
// 消息来源
private String workOrderSource;
// 关联语料内容
private String relationCorpusContent;
private String aiAnalysisRequestId;
}

View File

@@ -0,0 +1,22 @@
package com.volvo.ai.analytic.center.dto.resp;
import lombok.Data;
/**
* @ClassName ComplainDTO
* @Description 四合一 投诉
* @Author renzhen
* @Date 2025-04-02 11:20
* @Version 1.0
**/
@Data
public class ComplainDTO {
private String orderType;
private String complainType;
private String complainTitle;
private String problemDescription;
private String contactTelephone;
private String contactName;
private String contactSex;
}

View File

@@ -0,0 +1,20 @@
package com.volvo.ai.analytic.center.dto.resp;
import lombok.Data;
/**
* @ClassName ComplainDTO
* @Description 四合一 咨询
* @Author renzhen
* @Date 2025-04-02 11:20
* @Version 1.0
**/
@Data
public class ConsultDTO {
private String orderType;
private String problemDescription;
private String contactTelephone;
private String contactName;
private String contactSex;
}

View File

@@ -0,0 +1,29 @@
package com.volvo.ai.analytic.center.dto.resp;
import lombok.Data;
/**
*
* @ClassName: FourRequestDTO
* @author: renzhen
* @Description: 四合一请求参数
* @date: 2025-04-02 10:36
*/
@Data
public class FourInOneResponseDTO {
// 语料唯一Id
private String workOrderId;
//投诉
private ComplainDTO complain;
// 咨询
private ConsultDTO consult;
private String aiAnalysisRequestId;
}

View File

@@ -0,0 +1,66 @@
package com.volvo.ai.analytic.center.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("tc_intelligent_customer")
public class TcIntelligentCustomer {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
// 咨询类【201】/投诉类【202】/投诉类型【203】
@TableField("intelligent_customer_type")
private String intelligentCustomerType;
@TableField("type_one")
private String typeOne;
@TableField("type_two")
private String typeTwo; // JSON 字符串
@TableField("type_three")
private String typeThree;
@TableField("is_deleted")
@TableLogic
private Integer isDeleted;
@TableField("versions")
@Version
private Integer versions;
/**
* 创建者
*/
@TableField("create_by")
private String createBy;
/**
* 创建时间
*/
@TableField("create_time")
private Date createTime;
/**
* 更新者
*/
@TableField("update_by")
private String updateBy;
/**
* 更新时间
*/
@TableField("update_time")
private Date updateTime;
}

View File

@@ -6,8 +6,11 @@ import lombok.Getter;
public enum BusinessTypeEnum {
COMMUNITYTARGET("CommunityTarget", "社区舆情分析"),
SMART_ASSISTANT("SMART_ASSISTANT", "智能助手")
;
SMART_ASSISTANT("SMART_ASSISTANT", "智能助手-企微"),
SMART_ASSISTANT_DCC("SMART_ASSISTANT_DCC", "智能助手-DCC"),
INTELLIGENT_CUSTOMER("INTELLIGENT_CUSTOMER_4IN1", "智能客服-4合一")
;
private String code;
private String message;

View File

@@ -0,0 +1,19 @@
package com.volvo.ai.analytic.center.enums;
import lombok.Getter;
@Getter
public enum IntelligentCustomerTypeEnum {
CONSULT("201", "咨询类"),
COMPLAIN("202", "投诉类"),
CONSULT_TYPE("203", "投诉类型")
;
private String code;
private String message;
IntelligentCustomerTypeEnum(String code, String message) {
this.code = code;
this.message = message;
}
}

View File

@@ -0,0 +1,15 @@
package com.volvo.ai.analytic.center.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.volvo.ai.analytic.center.entity.AiAnalysisErrors;
import com.volvo.ai.analytic.center.entity.TcIntelligentCustomer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface TcIntelligentCustomerMapper extends BaseMapper<TcIntelligentCustomer> {
}

View File

@@ -0,0 +1,86 @@
package com.volvo.ai.analytic.center.mq;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.volvo.ai.analytic.center.dto.req.FourInOneRequestDTO;
import com.volvo.ai.analytic.center.service.AiAnalysisRequestLogsService;
import com.volvo.ai.analytic.center.service.IntelligentCustomerService;
import com.volvo.ai.analytic.center.service.TmTelephoneCorpusService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName CorpusProcessKafkaConsumer
* @Description 智能客服 语料解析 四合一
* @Author renzhen
* @Date 2025-03-04 10:18
* @Version 1.0
**/
@Slf4j
@Component
@RefreshScope
@RestController
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.intelligentCustomer.group}",
topic = "${rocketmq.consumer.intelligentCustomer.topic}",
instanceName = "intelligentCustomerMq",
consumeThreadNumber = 5,
enableMsgTrace = true)
public class IntelligentCustomerMqConsumer implements RocketMQListener<MessageExt> {
@Autowired
private IntelligentCustomerService intelligentCustomerService;
@Autowired
private AiAnalysisRequestLogsService aiAnalysisRequestLogsService;
@Value("${dify.corpus.checkDccRepeat}")
private String checkDccRepeat;
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void onMessage(MessageExt messageExt) {
long startTime = System.currentTimeMillis();
try {
log.info("intelligentCustomerMqConsumer 当前线程: {}, 线程ID: {}", Thread.currentThread().getName(), Thread.currentThread().getId());
String message = new String(messageExt.getBody());
log.info("IntelligentCustomerMqConsumer message: " + message);
FourInOneRequestDTO fourRequestDTO = objectMapper.readValue(message, FourInOneRequestDTO.class);
intelligentCustomerService.fourInOneMqService(fourRequestDTO);
log.info("FourInOneRequestDTO处理完成耗时{}", System.currentTimeMillis() - startTime);
} catch (JsonProcessingException e) {
log.info(" dcc mq 处理失败:{}", e.getMessage());
}
}
@PostMapping("ntelligentCustomerMqConsumer")
public void onMessage(@RequestBody String message) {
long startTime = System.currentTimeMillis();
try {
log.info("intelligentCustomerMqConsumer 当前线程: {}, 线程ID: {}", Thread.currentThread().getName(), Thread.currentThread().getId());
// String message = new String(messageExt.getBody());
log.info("IntelligentCustomerMqConsumer message: " + message);
FourInOneRequestDTO fourRequestDTO = objectMapper.readValue(message, FourInOneRequestDTO.class);
intelligentCustomerService.fourInOneMqService(fourRequestDTO);
log.info("FourInOneRequestDTO处理完成耗时{}", System.currentTimeMillis() - startTime);
} catch (JsonProcessingException e) {
log.info(" dcc mq 处理失败:{}", e.getMessage());
}
}
}

View File

@@ -0,0 +1,20 @@
package com.volvo.ai.analytic.center.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.volvo.ai.analytic.center.dto.corpus.AicorpusTelephoneDTO;
import com.volvo.ai.analytic.center.dto.req.FourInOneRequestDTO;
import com.volvo.ai.analytic.center.entity.TmTelephoneCorpus;
/**
* @description 电话语料表-同步表
* @author BEJSON
* @date 2025-03-04
*/
public interface IntelligentCustomerService{
void fourInOneMqService(FourInOneRequestDTO fourInOneRequestDTO);
void sendMq( String message);
}

View File

@@ -119,7 +119,7 @@ public class TmOdsVdqwMessagearchivingServiceImpl extends ServiceImpl<TmOdsVdqwM
// 获取消息列表
int optimalThreadPoolSize = Runtime.getRuntime().availableProcessors() + 1;
log.info("获取的线程数:{}",optimalThreadPoolSize);
log.info("企微处理总数据量:{},总页数:{},获取的线程数:{}",totalPages,totalPages,optimalThreadPoolSize);
// 创建线程池
ExecutorService executor = Executors.newFixedThreadPool(optimalThreadPoolSize); // 根据需求调整线程池大小

View File

@@ -0,0 +1,203 @@
package com.volvo.ai.analytic.center.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.volvo.ai.analytic.center.constant.Constant;
import com.volvo.ai.analytic.center.dto.req.DiFyReq;
import com.volvo.ai.analytic.center.dto.req.FourInOneRequestDTO;
import com.volvo.ai.analytic.center.dto.req.RunMaskingRuleInput;
import com.volvo.ai.analytic.center.dto.resp.ComplainDTO;
import com.volvo.ai.analytic.center.dto.resp.ConsultDTO;
import com.volvo.ai.analytic.center.dto.resp.FourInOneResponseDTO;
import com.volvo.ai.analytic.center.entity.AiAnalysisErrors;
import com.volvo.ai.analytic.center.entity.AiAnalysisRequestLogs;
import com.volvo.ai.analytic.center.entity.DataMaskingRule;
import com.volvo.ai.analytic.center.entity.TcIntelligentCustomer;
import com.volvo.ai.analytic.center.enums.BusinessTypeEnum;
import com.volvo.ai.analytic.center.enums.IntelligentCustomerTypeEnum;
import com.volvo.ai.analytic.center.feign.RemoteCarModelClient;
import com.volvo.ai.analytic.center.mapper.AiAnalysisErrorsMapper;
import com.volvo.ai.analytic.center.mapper.TcIntelligentCustomerMapper;
import com.volvo.ai.analytic.center.service.*;
import com.volvo.ai.analytic.center.utils.ConstantStr;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @description 智能助手
* @author rz
* @date 2025-03-04
*/
@RefreshScope
@Slf4j
@Service
public class intelligentCustomerServiceImpl implements IntelligentCustomerService {
@Autowired
private DiFyService diFyService;
@Resource
private RocketMQTemplate rocketMqTemplate;
@Value("${rocketmq.producer.corpus.topic}")
private String topic;
@Value("${dify.intelligentCustomer.fourInOneToken}")
private String fourInOneToken;
@Autowired
private RemoteCarModelClient remoteCarModelClient;
@Autowired
private DataMaskingRuleService dataMaskingRuleService;
@Autowired
private AiAnalysisRequestLogsService aiAnalysisRequestLogsService;
@Autowired
private AiAnalysisErrorsService aiAnalysisErrorsService;
@Autowired
private AiAnalysisErrorsMapper aiAnalysisErrorsMapper;
@Autowired
private TcIntelligentCustomerMapper tcIntelligentCustomerMapper;
private static Map<String, List<String>> tcIntelligentCustomer = Collections.EMPTY_MAP;
@Override
public void fourInOneMqService(FourInOneRequestDTO fourInOneRequestDTO) {
if(null != fourInOneRequestDTO){
List<DataMaskingRule> maskingRuleItems = dataMaskingRuleService.getDataMaskingRuleListByApplicationChannel(Constant.INTELLIGENT_CUSTOMER_4IN1);
RunMaskingRuleInput runMaskingRuleInput = new RunMaskingRuleInput();
runMaskingRuleInput.setDataMaskingRules(maskingRuleItems);
Map<String, Object> inputMap = new HashMap();
DiFyReq diFyImageReq = new DiFyReq();
diFyImageReq.setUser(ConstantStr.INTELLIGENT_CUSTOMER_4IN1);
diFyImageReq.setFlowId(fourInOneToken);
inputMap.put("yuliao", fourInOneRequestDTO.getWorkOrderContent());
inputMap.put("laiyuan",fourInOneRequestDTO.getWorkOrderSource());
diFyImageReq.setInputs(inputMap);
// 获取配置
JSONObject execDifyFlow = diFyService.executeDifyFlow(diFyImageReq, BusinessTypeEnum.INTELLIGENT_CUSTOMER.getCode(), JSONObject.toJSONString(fourInOneRequestDTO), fourInOneRequestDTO.getAiAnalysisRequestId());
log.info("runDify execDifyFlow {}",execDifyFlow);
if(null != execDifyFlow && execDifyFlow.get("status").equals("succeeded")){
JSONObject textJsonObject = execDifyFlow.getJSONObject("outputs").getJSONObject("text");
JSONObject consult = textJsonObject.getJSONObject("answer"); // 咨询
ConsultDTO consultDTO = new ConsultDTO();
consultDTO.setOrderType(fourInOneMqServiceFilter(IntelligentCustomerTypeEnum.CONSULT.getCode(),consult.getString("工单类别")));
consultDTO.setProblemDescription(consult.getString("问题描述"));
consultDTO.setContactSex(consult.getString("工单联系人性别"));
consultDTO.setContactName(consult.getString("工单联系人姓名"));
consultDTO.setContactTelephone(consult.getString("工单联系电话"));
JSONObject complainObject = execDifyFlow.getJSONObject("outputs").getJSONObject("text1");
JSONObject complain = complainObject.getJSONObject("complain"); // 投诉
ComplainDTO complainDTO = new ComplainDTO();
complainDTO.setComplainType(fourInOneMqServiceFilter(IntelligentCustomerTypeEnum.CONSULT_TYPE.getCode(),complain.getString("投诉类型")));
complainDTO.setOrderType(fourInOneMqServiceFilter(IntelligentCustomerTypeEnum.COMPLAIN.getCode(),complain.getString("工单类别")));
complainDTO.setComplainTitle(complain.getString("投诉主题"));
complainDTO.setProblemDescription(complain.getString("问题描述"));
complainDTO.setContactSex(consultDTO.getContactSex());
complainDTO.setContactName(consultDTO.getContactName());
complainDTO.setContactTelephone(consultDTO.getContactTelephone());
FourInOneResponseDTO fourInOneResponseDTO = new FourInOneResponseDTO();
String aiAnalysisRequestId = execDifyFlow.getString("aiAnalysisRequestId");
fourInOneResponseDTO.setAiAnalysisRequestId(aiAnalysisRequestId);
fourInOneResponseDTO.setComplain(complainDTO);
fourInOneResponseDTO.setConsult(consultDTO);
fourInOneResponseDTO.setWorkOrderId(fourInOneRequestDTO.getWorkOrderId());
// 发送MQ
String message = JSONObject.toJSONString(fourInOneResponseDTO);
log.info("send mq {}",message);
sendMq(message);
try {
if(StringUtils.isNotEmpty(fourInOneRequestDTO.getAiAnalysisRequestId())){
AiAnalysisErrors aiAnalysisErrors = new AiAnalysisErrors();
aiAnalysisErrors.setAiAnalysisRequestId(aiAnalysisRequestId);
aiAnalysisErrors.setAiAnalysisErrorHandlingStatus("1");
aiAnalysisErrorsService.updateAiAnalysisErrors(aiAnalysisErrors);
}
aiAnalysisRequestLogsService.saveAiAnalysisRequestLogs(AiAnalysisRequestLogs.builder().aiAnalysisRequestId(execDifyFlow.getString("aiAnalysisRequestId")).businessResponse(message).build());
} catch (Exception e) {
log.info(" 电话语料处理保存报告异常processItem{} ", e);
}
}
}
}
@Override
public void sendMq(String message) {
rocketMqTemplate.asyncSend(topic, MessageBuilder.withPayload(message).build(),
new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
log.info("发送MQ成功 消息体:{}", message);
}
@Override
public void onException(Throwable e) {
log.error("送MQ异常 消息体:{}, 异常:", message, e);
}
}, 10000);
}
// 咨询类【201】/投诉类【202】/投诉类型【203】 过滤
private String fourInOneMqServiceFilter(String customerType,String customerValue){
if(tcIntelligentCustomer.isEmpty()){
queryTcIntelligentCustomer();
}
List<String> typeValues = tcIntelligentCustomer.get(customerType);
if(!typeValues.contains(customerValue)){
return "";
}
return customerValue;
}
public Map<String, List<String>> queryTcIntelligentCustomer() {
LambdaQueryWrapper<TcIntelligentCustomer> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TcIntelligentCustomer::getIsDeleted, "0");
List<TcIntelligentCustomer> tcIntelligentCustomerList =tcIntelligentCustomerMapper.selectList(queryWrapper);
tcIntelligentCustomer = tcIntelligentCustomerList.stream()
.collect(Collectors.groupingBy(
TcIntelligentCustomer::getIntelligentCustomerType, // 按照 IntelligentCustomerType 分组
Collectors.mapping(
customer -> {
if (customer.getIntelligentCustomerType().equals(IntelligentCustomerTypeEnum.CONSULT_TYPE.getCode())) {
return customer.getTypeOne(); // 如果是咨询类型,返回 TypeOne
} else {
return customer.getTypeOne().concat("#").concat(customer.getTypeTwo()).concat("#").concat(customer.getTypeThree()); // 其他类型,拼接 TypeOne、TypeTwo、TypeThree
}
},
Collectors.toList()
)
));
return tcIntelligentCustomer;
}
}

View File

@@ -13,4 +13,8 @@ public class ConstantStr {
public static final String corpus_user = "corpush_user";
public static final String CARMODELLIST_CACHEKEY = "ai_carmodellist";
public static final String INTELLIGENT_CUSTOMER_4IN1 = "INTELLIGENT_CUSTOMER_4IN1";
}