Переглянути джерело

400电话分析标签部分代码
客户详情ai标签工具类优化

lk 1 день тому
батько
коміт
8325b4b7a5

+ 114 - 2
fs-service/src/main/java/com/fs/company/service/impl/GeneralCustomerEntryServiceImpl.java

@@ -1,8 +1,16 @@
 package com.fs.company.service.impl;
 
+import cn.hutool.json.JSONUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fs.common.core.domain.R;
+import com.fs.common.core.domain.entity.SysDictData;
 import com.fs.common.utils.StringUtils;
+import com.fs.common.utils.spring.SpringUtils;
 import com.fs.company.domain.CompanyVoiceRobotic;
 import com.fs.company.mapper.CompanyVoiceRoboticMapper;
 import com.fs.company.param.EntryCustomerParam;
@@ -10,18 +18,29 @@ import com.fs.company.service.ICompanyVoiceRoboticService;
 import com.fs.company.service.IGeneralCustomerEntryService;
 import com.fs.company.util.CryptoUtil;
 import com.fs.company.util.PhoneNumberUtil;
+import com.fs.config.ai.AiHostProper;
 import com.fs.crm.domain.CrmCustomer;
+import com.fs.crm.domain.CrmCustomerPropertyTemplate;
+import com.fs.crm.dto.CrmCustomerAiAutoTagVo;
 import com.fs.crm.mapper.CrmCustomerMapper;
+import com.fs.crm.service.ICrmCustomerPropertyTemplateService;
+import com.fs.crm.utils.CrmCustomerAiTagUtil;
+import com.fs.crm.vo.CrmCustomerAiTagVo;
+import com.fs.fastgptApi.param.ChatParam;
+import com.fs.fastgptApi.service.ChatService;
+import com.fs.hisapi.util.MapUtil;
+import com.fs.system.mapper.SysDictDataMapper;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.logging.log4j.core.util.UuidUtil;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import java.time.LocalTime;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.Executor;
 import java.util.stream.Collectors;
@@ -45,6 +64,8 @@ public class GeneralCustomerEntryServiceImpl implements IGeneralCustomerEntrySer
     CompanyVoiceRoboticMapper companyVoiceRoboticMapper;
     @Autowired
     ICompanyVoiceRoboticService companyVoiceRoboticService;
+    @Autowired
+    private SysDictDataMapper sysDictDataMapper;
 
     /**
      * 录入客户
@@ -85,6 +106,97 @@ public class GeneralCustomerEntryServiceImpl implements IGeneralCustomerEntrySer
         });
     }
 
+    private static final String TRADE_TYPE = "trade_type";
+    @Value("${crm.customer.ai.key:mygpt-tbQfq4ejR162mGJBCTTDUH9ecP1XCVuUfaOGTipnLjb1hP8x5prg}")
+    private String appKey;
+    private List<CrmCustomerAiTagVo> getAiTags(String chatRecord) throws JsonProcessingException {
+        Map<String, Object> requestParam = new HashMap<>();
+        List<CrmCustomerPropertyTemplate> crmCustomerPropertyTemplates = SpringUtils.getBean(ICrmCustomerPropertyTemplateService.class).getBaseMapper().selectList(new LambdaQueryWrapper<CrmCustomerPropertyTemplate>());
+        List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType(TRADE_TYPE);
+        ArrayList<Map<String, String>> tags = new ArrayList<>();//标签及提示词
+        crmCustomerPropertyTemplates.forEach(o -> {
+            Map<String, String> tag = MapUtil.convertToMap(new CrmCustomerAiAutoTagVo(String.valueOf(o.getId()), o.getName(), o.getAiHint()));
+            tags.add(tag);
+        });
+        StringBuilder history = new StringBuilder();
+        List<Map<String, Object>> communication;
+        ObjectMapper mapper = new ObjectMapper();
+        try {
+            communication =  mapper.readValue(chatRecord,
+                    new TypeReference<List<Map<String, Object>>>() {});
+        } catch (Exception e) {
+            throw new RuntimeException("数据格式错误", e);
+        }
+        //对话内容删掉第一条(提示词)
+        if (!communication.isEmpty())communication.remove(0);
+        history.append("{");
+        for (Map<String, Object> o :
+                communication) {
+            String role = (String) o.get("role");
+            String content = (String) o.get("content");
+            String roleTag = "user".equals(role) ? "user" : "ai";
+            history.append(String.format("\"%s\":\"%s\",", roleTag, content));
+        }
+        history.deleteCharAt(history.length() - 1).append("}");
+        HashMap<String, String> userInfo = new HashMap<String, String>();
+        HashMap<String, String> aiInfo = new HashMap<>();
+        aiInfo.put("name", "");
+        aiInfo.put("sex", "");
+        aiInfo.put("age", "");
+        aiInfo.put("city", "");
+        aiInfo.put("habits", "");
+        aiInfo.put("describe", "");
+        userInfo.put("name", "");
+        userInfo.put("sex", "");
+        userInfo.put("age", "");
+        userInfo.put("city", "");
+        userInfo.put("habits", "");
+        userInfo.put("describe", "");
+        requestParam.put("aiInfo", aiInfo);
+        requestParam.put("likeRatio", "");
+        requestParam.put("userInfo", userInfo);
+        requestParam.put("history", history);
+        requestParam.put("tags", tags);
+        requestParam.put("tradeName", sysDictData.stream().map(SysDictData::getDictLabel));
+        requestParam.put("tradeType", sysDictData.stream().map(SysDictData::getDictValue));
+        requestParam.put("tagInfos", Collections.emptyList());
+        requestParam.put("isRepository", "");
+        requestParam.put("userContent", "");
+        requestParam.put("aiContent", "");
+        requestParam.put("likeRatio", "");
+
+        String requestJson = mapper.writeValueAsString(requestParam);
+        ChatParam param = new ChatParam();
+        param.setChatId(UuidUtil.getTimeBasedUuid().toString());
+        param.setStream(false);
+        param.setDetail(true);
+        ChatParam.Message message = new ChatParam.Message();
+        List<ChatParam.Message> messageList = new ArrayList<ChatParam.Message>();
+        message.setContent(requestJson);
+        message.setRole("user");
+        messageList.add(message);
+        param.setMessages(messageList);
+        ChatService chatService = SpringUtils.getBean(ChatService.class);
+        AiHostProper aiHost = SpringUtils.getBean(AiHostProper.class);
+        R aiResponse = chatService.initiatingTakeChat(param, aiHost.getAiApi(), appKey);
+        if (aiResponse == null || !Integer.valueOf(200).equals(aiResponse.get("code"))) {
+            throw new RuntimeException("AI响应异常: " +
+                    (aiResponse != null ? aiResponse.get("msg") : "响应为空"));
+        }
+
+        List<Map<String, String>> tagInfos = CrmCustomerAiTagUtil.extractTagInfos(JSONUtil.toJsonStr(aiResponse));
+        if (CollectionUtils.isEmpty(tagInfos)) {
+            return Collections.emptyList();
+        }
+
+        List<CrmCustomerAiTagVo> collect = tagInfos.stream()
+                .map(tag -> {
+                    return new CrmCustomerAiTagVo()
+                            .setValue(tag.get("value")).setName(tag.get("name"));
+                })
+                .collect(Collectors.toList());
+        return collect;
+    }
     /**
      * 处理单条数据
      * @param data

+ 8 - 3
fs-service/src/main/java/com/fs/crm/utils/CrmCustomerAiTagUtil.java

@@ -146,12 +146,16 @@ public class CrmCustomerAiTagUtil {
         Map<String, Object> requestParam = new HashMap<>();
 
         // 获取各类数据
+        Map<String, Object> remove = communication.remove(0);
         String tradeName = getDictLabel(tradeType);
         Map<String, Object> tags = getTags(tradeType);
         Map<String, Object> history = getHistory(communication,customerId.toString());
         Map<String, Object> userInfo = getUserInfo(customerId);
-        Map<String, Object> aiInfo = getAiInfo(communication.remove(0));
-
+        Map<String, Object> aiInfo = getAiInfo(remove);
+        String likeRatio ="";
+        if (!userInfo.isEmpty()){
+            likeRatio = (String) userInfo.remove("likeRatio");
+        }
         // 合并数据
         Stream.of(tags, history, userInfo, aiInfo)
                 .filter(Objects::nonNull)
@@ -164,7 +168,7 @@ public class CrmCustomerAiTagUtil {
         requestParam.put("isRepository", "");
         requestParam.put("userContent", "");
         requestParam.put("aiContent", "");
-        requestParam.put("likeRatio", userInfo != null ? userInfo.remove("likeRatio") : null);
+        requestParam.put("likeRatio", likeRatio);
 
         return requestParam;
     }
@@ -361,6 +365,7 @@ public class CrmCustomerAiTagUtil {
         Map<String, Object> result = new HashMap<String, Object>();
         result.put("history", history);
         ArrayList<Map> maps = new ArrayList<>();
+        //存入crm_customer_info
         communication.forEach(o->{
             String role = (String) o.get("role");
             String content = (String) o.get("content");