From 4bb4a6203db13a97f229de3942e3efb6ce255f4b Mon Sep 17 00:00:00 2001 From: 10710 <1071020418@qq.com> Date: Fri, 2 Feb 2024 17:42:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=20=20=20=20=201?= =?UTF-8?q?=E3=80=81=E5=9C=A8=E9=97=B2=E8=81=8A=E6=97=B6=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=94=B6=E9=9B=86=E5=88=B0=E7=9A=84=E7=83=AD?= =?UTF-8?q?=E7=82=B9=E8=BF=9B=E8=A1=8C=E9=97=AE=E7=AD=94=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nlt/api/nlt/feign/RemoteQAService.java | 3 ++ .../factory/RemoteQAFallbackFactory.java | 5 +++ .../nlt/nlt/template/FreeChatTemplate.java | 42 ++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/xueyi-api/xueyi-api-nlt/src/main/java/com/xueyi/nlt/api/nlt/feign/RemoteQAService.java b/xueyi-api/xueyi-api-nlt/src/main/java/com/xueyi/nlt/api/nlt/feign/RemoteQAService.java index 51628b39..342c6479 100644 --- a/xueyi-api/xueyi-api-nlt/src/main/java/com/xueyi/nlt/api/nlt/feign/RemoteQAService.java +++ b/xueyi-api/xueyi-api-nlt/src/main/java/com/xueyi/nlt/api/nlt/feign/RemoteQAService.java @@ -29,4 +29,7 @@ public interface RemoteQAService { @PostMapping ("/knowledge") R query(@RequestBody KnowledgeVo vo); + @PostMapping("/searchHotspot") + AjaxResult searchHotspot(@RequestBody KnowledgeVo vo); + } \ No newline at end of file diff --git a/xueyi-api/xueyi-api-nlt/src/main/java/com/xueyi/nlt/api/nlt/feign/factory/RemoteQAFallbackFactory.java b/xueyi-api/xueyi-api-nlt/src/main/java/com/xueyi/nlt/api/nlt/feign/factory/RemoteQAFallbackFactory.java index 9e589752..f325d5cb 100644 --- a/xueyi-api/xueyi-api-nlt/src/main/java/com/xueyi/nlt/api/nlt/feign/factory/RemoteQAFallbackFactory.java +++ b/xueyi-api/xueyi-api-nlt/src/main/java/com/xueyi/nlt/api/nlt/feign/factory/RemoteQAFallbackFactory.java @@ -31,6 +31,11 @@ public class RemoteQAFallbackFactory implements FallbackFactory public R query(KnowledgeVo vo) { return R.fail("知识库查询失败"); } + + @Override + public AjaxResult searchHotspot(KnowledgeVo vo) { + return AjaxResult.error("查询失败"); + } }; } } \ No newline at end of file diff --git a/xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/template/FreeChatTemplate.java b/xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/template/FreeChatTemplate.java index b92cdfb2..bd5178e1 100644 --- a/xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/template/FreeChatTemplate.java +++ b/xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/template/FreeChatTemplate.java @@ -3,6 +3,9 @@ package com.xueyi.nlt.nlt.template; import com.alibaba.druid.util.StringUtils; import com.alibaba.fastjson2.JSONObject; import com.xueyi.common.core.context.SecurityContextHolder; +import com.xueyi.common.core.web.result.AjaxResult; +import com.xueyi.nlt.api.nlt.domain.vo.KnowledgeVo; +import com.xueyi.nlt.api.nlt.feign.RemoteQAService; import com.xueyi.nlt.netty.client.WebSocketClientManager; import com.xueyi.nlt.nlt.context.TerminalSecurityContextHolder; import com.xueyi.nlt.nlt.domain.LlmContext; @@ -33,6 +36,9 @@ public class FreeChatTemplate implements BaseTemplate{ @Autowired private RedisTemplate redisTemplate; + @Autowired + private RemoteQAService remoteQAService; + private Map correctWordsMap = new HashMap<>(); // 中航信领导来访临时对策 @@ -57,12 +63,13 @@ public class FreeChatTemplate implements BaseTemplate{ List context = new ArrayList<>(); context.add("你是缔智元公司的前台,你叫小智,你是一位数字人。"); context.addAll(redisTemplate.opsForList().range(redisKey,size-6,size)); - + content = generatePrompts(content); context.add(content); //webSocketClient.sendMsg(context); LlmContext llmContext = LlmContext.parse(context,true); + llmContext.setDevId(devId); LlmParam param = new LlmParam(); LlmResponse response = sysLlmService.chat(llmContext,param); String result = response.getContent(); @@ -108,7 +115,7 @@ public class FreeChatTemplate implements BaseTemplate{ } //context.addAll(redisTemplate.opsForList().range(redisKey,size-2,size)); - content = "请用简短的话回答下面的问题:" + content; + content = generatePrompts(content); context.add(content); //使用stream去除context列表中所有字符串中的引号 context = context.stream().map(s -> s.replaceAll("\"", "")).collect(java.util.stream.Collectors.toList()); @@ -132,4 +139,35 @@ public class FreeChatTemplate implements BaseTemplate{ public JSONObject handle(String dev, String content, Long tenantId) { 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 hotspotList = (List) hotspotResult.get("data"); + for(String hotspot : hotspotList){ + result = result + hotspot + ","; + } + result = result.substring(0,result.length()-1); + }catch (Exception e){ + log.error("查询热点词失败",e); + return result; + } + return result; + } + + private String generatePrompts(String content){ + String msg = addHotspot(content); + if(msg.equals("")){ + return "请用简短的话回答下面的问题:" + content; + } + String result = "请根据下面的信息:'''/n" + msg + "/n'''请用简短的话回答下面的问题:" + content; + return result; + } }