| @@ -31,9 +31,9 @@ public interface RemoteIntentService { | |||
| @PostMapping("/intent/inner/generateContextTask") | |||
| public AjaxResult generateContextTask(); | |||
| @PostMapping("/intent/inner/syncHotspot") | |||
| @PostMapping("/intent/syncHotspot") | |||
| public AjaxResult syncHotspot(); | |||
| @PostMapping("/intent/inner/collectHotspot") | |||
| @PostMapping("/intent/collectHotspot") | |||
| public AjaxResult collectHotspot(@RequestBody List<HotspotVo> hotspotVo); | |||
| } | |||
| @@ -21,7 +21,7 @@ import org.springframework.web.bind.annotation.RequestParam; | |||
| * @Param tenant_id 租户id | |||
| * @author yrx | |||
| */ | |||
| @FeignClient(url = "${notification.qa.url}",name = "qa", fallbackFactory = RemoteQAFallbackFactory.class) | |||
| @FeignClient(url = "192.168.33.15:5010",name = "qa", fallbackFactory = RemoteQAFallbackFactory.class) | |||
| public interface RemoteQAService { | |||
| @GetMapping("/knowledge") | |||
| @@ -38,15 +38,13 @@ import com.xueyi.nlt.nlt.domain.LlmContext; | |||
| import com.xueyi.nlt.nlt.domain.LlmParam; | |||
| import com.xueyi.nlt.nlt.domain.LlmResponse; | |||
| import com.xueyi.nlt.nlt.domain.dto.DmIntentDto; | |||
| import com.xueyi.nlt.nlt.domain.dto.DmPromptDto; | |||
| import com.xueyi.nlt.nlt.domain.dto.DmSensitiveWordDto; | |||
| import com.xueyi.nlt.nlt.domain.po.DmRegularPo; | |||
| import com.xueyi.nlt.nlt.domain.query.DmIntentQuery; | |||
| import com.xueyi.nlt.nlt.domain.vo.*; | |||
| import com.xueyi.nlt.nlt.mapper.DmRegularMapper; | |||
| import com.xueyi.nlt.nlt.service.IDmHotspotService; | |||
| import com.xueyi.nlt.nlt.service.IDmIntentService; | |||
| import com.xueyi.nlt.nlt.service.ISensitiveService; | |||
| import com.xueyi.nlt.nlt.service.ISysLlmService; | |||
| import com.xueyi.nlt.nlt.service.*; | |||
| import com.xueyi.nlt.nlt.service.impl.DmSensitiveWordServiceImpl; | |||
| import com.xueyi.nlt.nlt.template.*; | |||
| import com.xueyi.system.api.digitalmans.domain.dto.DmManDeviceDto; | |||
| @@ -1122,25 +1120,6 @@ public class DmIntentController extends BaseController<DmIntentQuery, DmIntentDt | |||
| return AjaxResult.error(res); | |||
| } | |||
| @PostMapping("/api/testforwelcome") | |||
| public AjaxResult test(){ | |||
| LocalDateTime dateTime = LocalDateTime.now(); | |||
| String date = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); | |||
| JSONObject jo = generativeWelcomeTemplate.handle("1", ""); | |||
| JSONArray ja = jo.getJSONArray("data"); | |||
| Long timeStamp = System.currentTimeMillis(); | |||
| JSONObject timeJson = new JSONObject(); | |||
| timeJson.put("timestamp", timeStamp); | |||
| ja.add(timeJson); | |||
| log.info(ja.toJSONString()); | |||
| redisTemplate2.opsForHash().put("group:task" + ":generate", date, AjaxResult.success(ja).toJson().toJSONString()); | |||
| Map<Object, Object> devices = redisTemplate2.opsForHash().entries("group:dgman:device:"); | |||
| for(Object devId:devices.keySet()){ | |||
| mqttTemplate.sendToMqtt("/" + activeProfile + "/" + "digital_man" + "/" + devId.toString() + "/" + "notify" + "/" + "generate-welcome", true, AjaxResult.success(ja).toJson().toJSONString()); | |||
| } | |||
| return AjaxResult.success(); | |||
| } | |||
| @PostMapping("/inner/generateContextTask") | |||
| @ResponseBody | |||
| public AjaxResult generateContextTask() { | |||
| @@ -1166,38 +1145,43 @@ public class DmIntentController extends BaseController<DmIntentQuery, DmIntentDt | |||
| log.error("生成任务失败:{}",e.getMessage()); | |||
| return AjaxResult.error("生成任务失败"); | |||
| } | |||
| return success(); | |||
| return AjaxResult.success(); | |||
| } | |||
| @PostMapping("/inner/syncHotspot") | |||
| @PostMapping("/syncHotspot") | |||
| @ResponseBody | |||
| public AjaxResult syncHotspot(){ | |||
| return dmHotspotService.syncHotspot(); | |||
| } | |||
| @PostMapping("/inner/collectHotspot") | |||
| @ResponseBody | |||
| public AjaxResult collectHotspot(@RequestBody List<HotspotVo> hotspotVo){ | |||
| return dmHotspotService.collectHotspot(hotspotVo); | |||
| Integer result = dmHotspotService.syncHotspot(); | |||
| if(result.equals(0)){ | |||
| return AjaxResult.success("同步成功"); | |||
| }else { | |||
| return AjaxResult.error("同步失败"); | |||
| } | |||
| } | |||
| @PostMapping("/api/updateHotspot") | |||
| @PostMapping("/collectHotspot") | |||
| @ResponseBody | |||
| public AjaxResult updateHotspot(@RequestBody JSONObject hotspotData){ | |||
| if(hotspotData == null||hotspotData.isEmpty()||!hotspotData.containsKey("data")){ | |||
| return AjaxResult.error("上传热点新闻失败"); | |||
| public AjaxResult collectHotspot(){ | |||
| Integer result = dmHotspotService.collectHotspot(); | |||
| if(result.equals(0)){ | |||
| return AjaxResult.success("热点收集成功"); | |||
| }else { | |||
| return AjaxResult.error("热点收集失败"); | |||
| } | |||
| log.info(hotspotData.getString("data")); | |||
| List<HotspotVo> hotspotVoList = hotspotData.getList("data",HotspotVo.class); | |||
| return dmHotspotService.collectHotspot(hotspotVoList); | |||
| } | |||
| @PostMapping("/api/testForHotspot") | |||
| public AjaxResult testForHotspot(){ | |||
| return dmHotspotService.syncHotspot(); | |||
| @PostMapping("/api/test") | |||
| @ResponseBody | |||
| public String collectHotspotApi(@RequestBody String query){ | |||
| String result = dmHotspotService.generateHotspotPrompt(query); | |||
| JSONObject a = freeChatTemplate.handle("-1", query); | |||
| JSONObject b = freeChatTemplate.handle("-1", query, true); | |||
| JSONArray c = new JSONArray(); | |||
| c.add(a); | |||
| c.add(b); | |||
| return c.toJSONString(); | |||
| } | |||
| interface Auth { | |||
| /** 系统 - 意图管理 | |||
| 管理 - 列表 */ | |||
| @@ -31,7 +31,9 @@ public class DmPromptDto extends DmPromptPo { | |||
| prompt.append(this.getPrefix()); | |||
| if (params != null && paramList != null && paramList.size() == params.size()) { | |||
| for (int i = 0; i < paramList.size(); i++) { | |||
| prompt.append(formatStr(paramList.get(i).getPrefix()) + formatStr(params.get(i)) + formatStr(paramList.get(i).getSuffix())); | |||
| if(params.get(i) != null && !"".equals(params.get(i))){ | |||
| prompt.append(formatStr(paramList.get(i).getPrefix()) + formatStr(params.get(i)) + formatStr(paramList.get(i).getSuffix())); | |||
| } | |||
| } | |||
| } | |||
| prompt.append(this.getSuffix()); | |||
| @@ -36,6 +36,6 @@ public class DmPromptParamMerge extends BasisEntity { | |||
| protected String suffix; | |||
| /** 排序顺序 */ | |||
| protected Integer order; | |||
| protected Integer orders; | |||
| } | |||
| @@ -47,7 +47,7 @@ public class DmPromptManager extends BaseManagerImpl<DmPromptQuery, DmPromptDto, | |||
| // 转化成dto对象 | |||
| DmPromptDto dto = baseConverter.mapperDto(po); | |||
| // 根据order顺序获取DmPromptParamMerge对象列表 | |||
| dto.setParamList(dmPromptParamMergeMapper.selectList(Wrappers.<DmPromptParamMerge>query().lambda().eq(DmPromptParamMerge::getPromptId, dto.getId()).orderByAsc(DmPromptParamMerge::getOrder))); | |||
| dto.setParamList(dmPromptParamMergeMapper.selectList(Wrappers.<DmPromptParamMerge>query().lambda().eq(DmPromptParamMerge::getPromptId, dto.getId()).orderByAsc(DmPromptParamMerge::getOrders))); | |||
| return dto; | |||
| } else { | |||
| return null; | |||
| @@ -7,7 +7,15 @@ import java.util.List; | |||
| public interface IDmHotspotService { | |||
| public AjaxResult syncHotspot(); | |||
| //向数字人下发热点 | |||
| public Integer syncHotspot(); | |||
| public AjaxResult collectHotspot(List<HotspotVo> hotspotVo); | |||
| //生成热点 | |||
| public Integer collectHotspot(); | |||
| //查询热点 | |||
| public String retrievalHotspot(String content); | |||
| //生成相应的提示词 | |||
| public String generateHotspotPrompt(String content); | |||
| } | |||
| @@ -5,7 +5,11 @@ import com.alibaba.fastjson2.JSONObject; | |||
| import com.xueyi.common.core.web.result.AjaxResult; | |||
| import com.xueyi.common.mqtt.connection.MqttTemplate; | |||
| import com.xueyi.nlt.api.nlt.domain.vo.HotspotVo; | |||
| import com.xueyi.nlt.api.nlt.domain.vo.KnowledgeVo; | |||
| import com.xueyi.nlt.api.nlt.feign.RemoteQAService; | |||
| import com.xueyi.nlt.nlt.domain.dto.DmPromptDto; | |||
| import com.xueyi.nlt.nlt.service.IDmHotspotService; | |||
| import com.xueyi.nlt.nlt.service.IDmPromptService; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -16,6 +20,7 @@ import org.springframework.stereotype.Service; | |||
| import java.time.LocalDateTime; | |||
| import java.time.format.DateTimeFormatter; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| @@ -31,11 +36,17 @@ public class DmHotspotServiceImpl implements IDmHotspotService { | |||
| @Autowired | |||
| MqttTemplate mqttTemplate; | |||
| @Autowired | |||
| private RemoteQAService remoteQAService; | |||
| @Autowired | |||
| private IDmPromptService dmPromptService; | |||
| @Value("${spring.profiles.active}") | |||
| private String activeProfile; | |||
| @Override | |||
| public AjaxResult syncHotspot() { | |||
| public Integer syncHotspot() { | |||
| LocalDateTime dateTime = LocalDateTime.now(); | |||
| String date = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); | |||
| String result = (String) redisTemplate.opsForHash().get("group:task:hotspot", date); | |||
| @@ -46,16 +57,48 @@ public class DmHotspotServiceImpl implements IDmHotspotService { | |||
| for(Object devId:devices.keySet()){ | |||
| mqttTemplate.sendToMqtt("/" + activeProfile + "/" + "digital_man" + "/" + devId.toString() + "/" + "notify" + "/" + "generate-hotspot", true, AjaxResult.success(resultArray).toJson().toJSONString()); | |||
| } | |||
| return AjaxResult.success(); | |||
| return 0; | |||
| } | |||
| @Override | |||
| public AjaxResult collectHotspot(List<HotspotVo> hotspotVoList) { | |||
| LocalDateTime dateTime = LocalDateTime.now(); | |||
| String date = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); | |||
| Long timestamp = System.currentTimeMillis(); | |||
| hotspotVoList.stream().forEach(hotspotVo -> hotspotVo.setTimestamp(timestamp)); | |||
| redisTemplate.opsForHash().put("group:task:hotspot", date, JSONArray.from(hotspotVoList).toJSONString()); | |||
| return AjaxResult.success(); | |||
| public Integer collectHotspot() { | |||
| //Todo 通过智谱大模型收集热点,并保存到redis中。 | |||
| return 0; | |||
| } | |||
| @Override | |||
| public String retrievalHotspot(String content) { | |||
| KnowledgeVo vo = new KnowledgeVo(); | |||
| String result = ""; | |||
| vo.setQuestion(content); | |||
| AjaxResult hotspotResult = remoteQAService.searchHotspot(vo); | |||
| log.info("hotspotResult:{}",hotspotResult); | |||
| if(hotspotResult.get("data") == null){ | |||
| return ""; | |||
| } | |||
| try{ | |||
| List<HotspotVo> voList = hotspotResult.toJson().getList("data", HotspotVo.class); | |||
| StringBuilder prompt = new StringBuilder(); | |||
| for(HotspotVo hotspotVo:voList){ | |||
| prompt.append(hotspotVo.getContent()); | |||
| prompt.append("\n"); | |||
| } | |||
| result = prompt.toString(); | |||
| }catch (Exception e){ | |||
| log.error("查询热点词失败",e); | |||
| return ""; | |||
| } | |||
| log.info("result:{}",result); | |||
| return result; | |||
| } | |||
| @Override | |||
| public String generateHotspotPrompt(String content) { | |||
| String result = retrievalHotspot(content); | |||
| DmPromptDto dmPromptDto = dmPromptService.selectByName("hotspot-chat"); | |||
| List<String> promptList = new ArrayList<>(); | |||
| promptList.add(result); | |||
| promptList.add(content); | |||
| return dmPromptDto.getPrompt(promptList); | |||
| } | |||
| } | |||
| @@ -12,6 +12,7 @@ import com.xueyi.nlt.nlt.domain.LlmContext; | |||
| import com.xueyi.nlt.nlt.domain.LlmParam; | |||
| import com.xueyi.nlt.nlt.domain.LlmResponse; | |||
| import com.xueyi.nlt.nlt.domain.dto.DmPromptDto; | |||
| import com.xueyi.nlt.nlt.service.IDmHotspotService; | |||
| import com.xueyi.nlt.nlt.service.IDmPromptService; | |||
| import com.xueyi.nlt.nlt.service.ISysLlmService; | |||
| import com.yomahub.tlog.core.annotation.TLogAspect; | |||
| @@ -44,6 +45,9 @@ public class FreeChatTemplate implements BaseTemplate{ | |||
| @Autowired | |||
| private RemoteQAService remoteQAService; | |||
| @Autowired | |||
| private IDmHotspotService dmHotspotService; | |||
| private Map<String,String> correctWordsMap = new HashMap<>(); | |||
| // 中航信领导来访临时对策 | |||
| @@ -65,10 +69,13 @@ public class FreeChatTemplate implements BaseTemplate{ | |||
| redisTemplate.opsForList().leftPop(redisKey,2); | |||
| } | |||
| size = redisTemplate.opsForList().size(redisKey); | |||
| // 获取prompt对象 | |||
| DmPromptDto promptDto = dmPromptService.selectByName("free-chat"); | |||
| List<String> context = new ArrayList<>(); | |||
| context.add("你是缔智元公司的前台,你叫小智,你是一位数字人。"); | |||
| context.add(promptDto.getRole()); | |||
| context.addAll(redisTemplate.opsForList().range(redisKey,size-6,size)); | |||
| content = generatePrompts(content); | |||
| content = promptDto.getPrefix() + content + promptDto.getSuffix(); | |||
| context.add(content); | |||
| //webSocketClient.sendMsg(context); | |||
| @@ -110,7 +117,7 @@ public class FreeChatTemplate implements BaseTemplate{ | |||
| } | |||
| size = redisTemplate.opsForList().size(redisKey); | |||
| // 获取prompt对象 | |||
| DmPromptDto promptDto = dmPromptService.selectByName("free-chat"); | |||
| DmPromptDto promptDto = dmPromptService.selectByName("hotspot-chat"); | |||
| List<String> context = new ArrayList<>(); | |||
| context.add(promptDto.getRole()); | |||
| @@ -123,8 +130,7 @@ public class FreeChatTemplate implements BaseTemplate{ | |||
| } | |||
| //context.addAll(redisTemplate.opsForList().range(redisKey,size-2,size)); | |||
| content = promptDto.getPrefix() + content; | |||
| content = generatePrompts(content); | |||
| content = dmHotspotService.generateHotspotPrompt(content); | |||
| context.add(content); | |||
| //使用stream去除context列表中所有字符串中的引号 | |||
| context = context.stream().map(s -> s.replaceAll("\"", "")).collect(java.util.stream.Collectors.toList()); | |||
| @@ -149,38 +155,12 @@ public class FreeChatTemplate implements BaseTemplate{ | |||
| return null; | |||
| } | |||
| private String addHotspot(String content) { | |||
| KnowledgeVo vo = new KnowledgeVo(); | |||
| String result = ""; | |||
| vo.setQuestion(content); | |||
| AjaxResult hotspotResult = remoteQAService.searchHotspot(vo); | |||
| log.info("hotspotResult:{}",hotspotResult); | |||
| if(hotspotResult.get("data") == null){ | |||
| return result; | |||
| } | |||
| try{ | |||
| List<String> hotspotList = (List<String>) hotspotResult.get("data"); | |||
| for(String hotspot : hotspotList){ | |||
| result = result + hotspot + "\n"; | |||
| if(result.length() >= 1000){ | |||
| break; | |||
| } | |||
| } | |||
| //去除末尾的换行符 | |||
| }catch (Exception e){ | |||
| log.error("查询热点词失败",e); | |||
| return result; | |||
| } | |||
| return result; | |||
| } | |||
| private String generatePrompts(String content){ | |||
| String msg = addHotspot(content); | |||
| String msg = content; | |||
| if(msg.equals("")){ | |||
| return "请用简短的话回答下面的问题:" + content; | |||
| } | |||
| String result = "请根据下面的信息:'''\n" + msg + "\n'''请用简短的话回答下面的问题:" + content; | |||
| String result = "根据下面的信息:'''\n" + msg + "\n'''请用简短的话回答下面的问题:" + content; | |||
| return result; | |||
| } | |||
| } | |||
| @@ -12,7 +12,9 @@ import com.xueyi.nlt.nlt.constant.PromptsConstants; | |||
| import com.xueyi.nlt.nlt.domain.LlmContext; | |||
| import com.xueyi.nlt.nlt.domain.LlmParam; | |||
| import com.xueyi.nlt.nlt.domain.LlmResponse; | |||
| import com.xueyi.nlt.nlt.domain.dto.DmPromptDto; | |||
| import com.xueyi.nlt.nlt.domain.vo.WelcomeLabelVo; | |||
| import com.xueyi.nlt.nlt.service.IDmPromptService; | |||
| import com.xueyi.nlt.nlt.service.ISysLlmService; | |||
| import com.xueyi.system.api.holiday.domain.po.DmHolidayPo; | |||
| import com.xueyi.system.api.holiday.feign.FeignHolidayService; | |||
| @@ -53,7 +55,7 @@ public class GenerativeWelcomeTemplate implements BaseTemplate{ | |||
| private RemoteSolarService remoteSolarService; | |||
| @Autowired | |||
| private RedisTemplate<String,String> redisTemplate; | |||
| private IDmPromptService dmPromptService; | |||
| @Override | |||
| public JSONObject handle(String devId, String content) { | |||
| @@ -88,10 +90,10 @@ public class GenerativeWelcomeTemplate implements BaseTemplate{ | |||
| holiday = po.getName(); | |||
| } | |||
| List<WelcomeLabelVo> resultList = new ArrayList<>(); | |||
| String prefix = "假设你是公司前台,"; | |||
| String suffix = "请根据以上信息生成简短的话对他打招呼。生成十句打招呼内容,输出为一个JSON数组[{}],每个元素是一个JSON:{“content”:}。不要给出任何解释说明,不要带人名。"; | |||
| DmPromptDto promptDto = dmPromptService.selectByName("generative-welcome"); | |||
| //按照时间生成特殊招呼语 | |||
| for(String time:timeType.keySet()) { | |||
| String timeString = getTimeTypeFormat(timeType.getString(time)); | |||
| WelcomeLabelVo labelVo = new WelcomeLabelVo(); | |||
| labelVo.setLabelName(time); | |||
| List<WelcomeLabelVo> welcomeLabelVoList = new ArrayList<>(); | |||
| @@ -100,8 +102,14 @@ public class GenerativeWelcomeTemplate implements BaseTemplate{ | |||
| if(StringUtils.isNotBlank(holiday) || StringUtils.isNotBlank(term)){ | |||
| WelcomeLabelVo festivalWelcomeLabelVo = new WelcomeLabelVo(); | |||
| festivalWelcomeLabelVo.setLabelName("festival"); | |||
| log.info(prefix + getFestivalFormat(holiday) + getSolarTermFormat(term) + timeString + "看到员工时," + suffix); | |||
| LlmContext llmContext = new LlmContext(prefix + getFestivalFormat(holiday) + getSolarTermFormat(term) + timeString + "看到员工时," + suffix); | |||
| List<String> paramList = new ArrayList<>(); | |||
| paramList.add(holiday); | |||
| paramList.add(term); | |||
| paramList.add(timeType.getString(time)); | |||
| paramList.add("员工"); | |||
| String prompt = promptDto.getPrompt(paramList); | |||
| log.info(prompt); | |||
| LlmContext llmContext = new LlmContext(prompt); | |||
| llmContext.setDevId(devId); | |||
| LlmParam llmParam = new LlmParam(); | |||
| LlmResponse response = sysLlmService.chat(llmContext, llmParam); | |||
| @@ -118,9 +126,14 @@ public class GenerativeWelcomeTemplate implements BaseTemplate{ | |||
| for (String people : peopleType.keySet()) { | |||
| WelcomeLabelVo welcomeLabelVo = new WelcomeLabelVo(); | |||
| welcomeLabelVo.setLabelName(people); | |||
| String peopleString = getPeopleTypeFormat(peopleType.getString(people)); | |||
| log.info(prefix + timeString + peopleString + suffix); | |||
| LlmContext llmContext = new LlmContext(prefix + timeString + peopleString + suffix); | |||
| List<String> paramList = new ArrayList<>(); | |||
| paramList.add(""); | |||
| paramList.add(""); | |||
| paramList.add(timeType.getString(time)); | |||
| paramList.add(peopleType.getString(people)); | |||
| String prompt = promptDto.getPrompt(paramList); | |||
| log.info(prompt); | |||
| LlmContext llmContext = new LlmContext(prompt); | |||
| llmContext.setDevId(devId); | |||
| LlmParam llmParam = new LlmParam(); | |||
| LlmResponse response = sysLlmService.chat(llmContext, llmParam); | |||