Commit da8e40f1bb9445da70ea6b4a9a8e954a5685ec74

Authored by qianbao
1 parent cf793673

添加向量库

src/main/java/com/xly/entity/ToolMeta.java
@@ -51,5 +51,6 @@ public class ToolMeta { @@ -51,5 +51,6 @@ public class ToolMeta {
51 private String sVectorjson; 51 private String sVectorjson;
52 private String sVectorfiledAll; 52 private String sVectorfiledAll;
53 private String sVectorfiledShow; 53 private String sVectorfiledShow;
  54 + private Boolean sQue = false;
54 55
55 } 56 }
src/main/java/com/xly/milvus/service/impl/AiGlobalAgentQuestionSqlEmitterServiceImpl.java
@@ -85,7 +85,11 @@ public class AiGlobalAgentQuestionSqlEmitterServiceImpl implements AiGlobalAgent @@ -85,7 +85,11 @@ public class AiGlobalAgentQuestionSqlEmitterServiceImpl implements AiGlobalAgent
85 InsertResp insertResp = milvusClient.insert(insertReq); 85 InsertResp insertResp = milvusClient.insert(insertReq);
86 //是否初始化。初始化不需要插入到数据库 86 //是否初始化。初始化不需要插入到数据库
87 if(!isInit){ 87 if(!isInit){
88 - //调用数据库插入数据库 88 + //调用数据库插入数据库]
  89 + if(ObjectUtil.isEmpty(data.get("userInput"))){
  90 + Object userInput = ObjectUtil.isNotEmpty(data.get("sUserInput"))?data.get("sUserInput").toString():data.get("userInput");
  91 + data.put("userInput",userInput);
  92 + }
89 Map<String, Object> searMap = dynamicExeDbService.getDoProMap(sProName, data); 93 Map<String, Object> searMap = dynamicExeDbService.getDoProMap(sProName, data);
90 dynamicExeDbService.getCallPro(searMap, sProName); 94 dynamicExeDbService.getCallPro(searMap, sProName);
91 } 95 }
src/main/java/com/xly/milvus/service/impl/MilvusServiceImpl.java
@@ -164,7 +164,7 @@ public class MilvusServiceImpl implements MilvusService { @@ -164,7 +164,7 @@ public class MilvusServiceImpl implements MilvusService {
164 if(ObjectUtil.isNotEmpty(sUserInput)){ 164 if(ObjectUtil.isNotEmpty(sUserInput)){
165 //根据问题查询向量库 165 //根据问题查询向量库
166 Map<String,Object> serMap = aiGlobalAgentQuestionSqlEmitterService.queryAiGlobalAgentQuestionSqlEmitter(searchText, "ai_global_agent_question_sql"); 166 Map<String,Object> serMap = aiGlobalAgentQuestionSqlEmitterService.queryAiGlobalAgentQuestionSqlEmitter(searchText, "ai_global_agent_question_sql");
167 - if(ObjectUtil.isNotEmpty(serMap)){ 167 + if(ObjectUtil.isEmpty(serMap)){
168 aiGlobalAgentQuestionSqlEmitterService.addAiGlobalAgentQuestionSqlEmitter(searchText,one,sUserInput,sSqlContent,"MYSQL","ai_global_agent_question_sql",false); 168 aiGlobalAgentQuestionSqlEmitterService.addAiGlobalAgentQuestionSqlEmitter(searchText,one,sUserInput,sSqlContent,"MYSQL","ai_global_agent_question_sql",false);
169 }else{ 169 }else{
170 sb.append(sUserInput).append("已经存在").append("\n"); 170 sb.append(sUserInput).append("已经存在").append("\n");
src/main/java/com/xly/service/XlyErpService.java
@@ -515,16 +515,16 @@ public class XlyErpService { @@ -515,16 +515,16 @@ public class XlyErpService {
515 // 待条件全查 不带 10条 515 // 待条件全查 不带 10条
516 List<Map<String,Object>> data = milvusService.getDataToCollection(sInputTabelName, milvusFilter,userInput,pageSize,filedsShow, vectorValue,sceneName); 516 List<Map<String,Object>> data = milvusService.getDataToCollection(sInputTabelName, milvusFilter,userInput,pageSize,filedsShow, vectorValue,sceneName);
517 //存储到历史问题库(带where条件了就不存)并且没有记录过缓存 517 //存储到历史问题库(带where条件了就不存)并且没有记录过缓存
518 - if(!bCach && ObjectUtil.isEmpty(milvusFilter)){  
519 - //执行操作记录表  
520 - try{  
521 - List<ChatMessage> chatMessage = operableChatMemoryProvider.getCurrentChatMessages(session.getUserId());  
522 - //插入向量库  
523 - doAiUserAgentQuestion(session,userInput,milvusFilter,"MILVUS",chatMessage);  
524 - }catch (Exception e){  
525 - log.error("插入向量库异常",e);  
526 - }  
527 - } 518 +// if(!bCach && ObjectUtil.isEmpty(milvusFilter)){
  519 +// //执行操作记录表
  520 +// try{
  521 +// List<ChatMessage> chatMessage = operableChatMemoryProvider.getCurrentChatMessages(session.getUserId());
  522 +// //插入向量库
  523 +// doAiUserAgentQuestion(session,userInput,milvusFilter,"MILVUS",chatMessage);
  524 +// }catch (Exception e){
  525 +// log.error("插入向量库异常",e);
  526 +// }
  527 +// }
528 //采用表格形式显示明细、...详情、...记录、...列表、...清单 528 //采用表格形式显示明细、...详情、...记录、...列表、...清单
529 if( retrunMarkdownType(userInput) ){ 529 if( retrunMarkdownType(userInput) ){
530 resultExplain = buildMarkdownTableWithStream(data, title); 530 resultExplain = buildMarkdownTableWithStream(data, title);
@@ -610,7 +610,7 @@ public class XlyErpService { @@ -610,7 +610,7 @@ public class XlyErpService {
610 if(attempt==1){ 610 if(attempt==1){
611 addSessionUserQuestionList(session, userInput); 611 addSessionUserQuestionList(session, userInput);
612 } 612 }
613 - return getDynamicTableSqlExec(session, input, userId, userInput,errorSql,errorMessage,iErroCount,historySqlList, aiAgent, cleanSql); 613 + return getDynamicTableSqlExec(session, input, userId, userInput,errorSql,errorMessage,iErroCount,historySqlList, aiAgent, cleanSql,attempt);
614 }catch (SqlValidateException e){ 614 }catch (SqlValidateException e){
615 return "本场景没有识别到您的意图<br/> 如果切换场景,点[回首页],如果在本场景下,转换意图,点[清除记忆]"; 615 return "本场景没有识别到您的意图<br/> 如果切换场景,点[回首页],如果在本场景下,转换意图,点[清除记忆]";
616 }catch (Exception e){ 616 }catch (Exception e){
@@ -680,13 +680,13 @@ public class XlyErpService { @@ -680,13 +680,13 @@ public class XlyErpService {
680 * @return java.lang.String 680 * @return java.lang.String
681 * @Description 执行动态sSql 681 * @Description 执行动态sSql
682 **/ 682 **/
683 - private String getDynamicTableSqlExec(UserSceneSession session,String input,String userId,String userInput,String errorSql,String errorMessage,String iErroCount,String historySqlList,ErpAiAgent aiAgent,String cleanSql){ 683 + private String getDynamicTableSqlExec(UserSceneSession session,String input,String userId,String userInput,String errorSql,String errorMessage,String iErroCount,String historySqlList,ErpAiAgent aiAgent,String cleanSql,Integer attempt){
684 // 1. 构建自然语言转SQLAgent, 684 // 1. 构建自然语言转SQLAgent,
685 - List<Map<String, Object>> sqlResult; 685 + List<Map<String, Object>> sqlResult = null;
686 String rawSql; 686 String rawSql;
687 String tableStruct = session.getCurrentTool().getSStructureMemo(); 687 String tableStruct = session.getCurrentTool().getSStructureMemo();
688 String sError_mes; 688 String sError_mes;
689 - Boolean doAddSql = false; 689 +// Boolean doAddSql = false;
690 List<ChatMessage> chatMessage = new ArrayList<>(); 690 List<ChatMessage> chatMessage = new ArrayList<>();
691 try{ 691 try{
692 //如果之前已查询直接返回 692 //如果之前已查询直接返回
@@ -711,7 +711,7 @@ public class XlyErpService { @@ -711,7 +711,7 @@ public class XlyErpService {
711 // 2. 清理SQL多余符号 + 生产级强校验(核心安全保障,不可省略) 711 // 2. 清理SQL多余符号 + 生产级强校验(核心安全保障,不可省略)
712 cleanSql = SqlValidateUtil.cleanSqlSymbol(rawSql); 712 cleanSql = SqlValidateUtil.cleanSqlSymbol(rawSql);
713 SqlValidateUtil.validateMysqlSql(cleanSql); 713 SqlValidateUtil.validateMysqlSql(cleanSql);
714 - doAddSql = true; 714 +// doAddSql = true;
715 } 715 }
716 // List<ChatMessage> chatMessage2 = operableChatMemoryProvider.getCurrentChatMessages(session.getUserId()); 716 // List<ChatMessage> chatMessage2 = operableChatMemoryProvider.getCurrentChatMessages(session.getUserId());
717 try{ 717 try{
@@ -730,8 +730,14 @@ public class XlyErpService { @@ -730,8 +730,14 @@ public class XlyErpService {
730 sError_mes = e.getMessage(); 730 sError_mes = e.getMessage();
731 doAiSqlErrorHistoryThread(session, StrUtil.EMPTY, cleanSql, sError_mes,input); 731 doAiSqlErrorHistoryThread(session, StrUtil.EMPTY, cleanSql, sError_mes,input);
732 throw e; 732 throw e;
  733 + }finally {
  734 + //插入常用操作
  735 + Boolean bSucess = ObjectUtil.isNotEmpty(sqlResult);
  736 + if(attempt==1 || bSucess){
  737 + //执行操作记录表
  738 + doAiUserAgentQuestion(session,input,cleanSql,"MYSQL",chatMessage,bSucess);
  739 + }
733 } 740 }
734 -  
735 //如果查询不到数据走向量库 741 //如果查询不到数据走向量库
736 if(ObjectUtil.isEmpty(sqlResult)){ 742 if(ObjectUtil.isEmpty(sqlResult)){
737 session.setDbType("X"); 743 session.setDbType("X");
@@ -746,11 +752,7 @@ public class XlyErpService { @@ -746,11 +752,7 @@ public class XlyErpService {
746 if(Integer.valueOf(iErroCount)>0){ 752 if(Integer.valueOf(iErroCount)>0){
747 doAiSqlErrorHistoryThread(session, cleanSql, StrUtil.EMPTY, "执行正确去修改对应正确的SQl",input); 753 doAiSqlErrorHistoryThread(session, cleanSql, StrUtil.EMPTY, "执行正确去修改对应正确的SQl",input);
748 } 754 }
749 - //插入常用操作 不包含where 条件  
750 - if(doAddSql){  
751 - //执行操作记录表  
752 - doAiUserAgentQuestion(session,input,cleanSql,"MYSQL",chatMessage);  
753 - } 755 +
754 //采用表格形式显示明细、...详情、...记录、...列表、...清单 756 //采用表格形式显示明细、...详情、...记录、...列表、...清单
755 String resultExplain = StrUtil.EMPTY; 757 String resultExplain = StrUtil.EMPTY;
756 // if(retrunMarkdownType(userInput) ){ 758 // if(retrunMarkdownType(userInput) ){
@@ -838,11 +840,12 @@ public class XlyErpService { @@ -838,11 +840,12 @@ public class XlyErpService {
838 String sQuestion, 840 String sQuestion,
839 String sSqlContent, 841 String sSqlContent,
840 String cachType, 842 String cachType,
841 - List<ChatMessage> chatMessage 843 + List<ChatMessage> chatMessage,
  844 + Boolean bSucess
842 845
843 ){ 846 ){
844 MultiThreadPoolServer mts = MultiThreadPoolServer.getInstance(); 847 MultiThreadPoolServer mts = MultiThreadPoolServer.getInstance();
845 - AiUserAgentQuestionThread at = new AiUserAgentQuestionThread(session,sQuestion,sSqlContent,cachType,chatMessage); 848 + AiUserAgentQuestionThread at = new AiUserAgentQuestionThread(session,sQuestion,sSqlContent,cachType,chatMessage,bSucess);
846 mts.service(at); 849 mts.service(at);
847 } 850 }
848 851
@@ -1065,10 +1068,10 @@ public class XlyErpService { @@ -1065,10 +1068,10 @@ public class XlyErpService {
1065 List<Map<String,Object>> data = getAiUserAgentQuestion(session.getUserName(),sSceneId, page,3); 1068 List<Map<String,Object>> data = getAiUserAgentQuestion(session.getUserName(),sSceneId, page,3);
1066 Integer iPageCount = pageCount; 1069 Integer iPageCount = pageCount;
1067 Integer count = (ObjectUtil.isNotEmpty(toolMetaAll))?toolMetaAll.size():0; 1070 Integer count = (ObjectUtil.isNotEmpty(toolMetaAll))?toolMetaAll.size():0;
1068 - Integer iPageCount2 = (int) Math.ceil(count/5); 1071 + Integer iPageCount2 = (int) Math.ceil(Double.valueOf(count) / 5 );
1069 if(page==1){ 1072 if(page==1){
1070 iPageCount =getAiUserAgentQuestionCount(session.getUserName(),sSceneId); 1073 iPageCount =getAiUserAgentQuestionCount(session.getUserName(),sSceneId);
1071 - iPageCount = (int) Math.ceil(iPageCount/5); 1074 + iPageCount = (int) Math.ceil(Double.valueOf(iPageCount)/5);
1072 1075
1073 if(iPageCount2>iPageCount){ 1076 if(iPageCount2>iPageCount){
1074 iPageCount = iPageCount2; 1077 iPageCount = iPageCount2;
@@ -1079,16 +1082,19 @@ public class XlyErpService { @@ -1079,16 +1082,19 @@ public class XlyErpService {
1079 data.forEach(one->{ 1082 data.forEach(one->{
1080 ToolMeta tm = new ToolMeta(); 1083 ToolMeta tm = new ToolMeta();
1081 tm.setSMethodName(one.get("sUserInput").toString()); 1084 tm.setSMethodName(one.get("sUserInput").toString());
  1085 + tm.setSQue(true);
  1086 + showListAll.add(tm);
1082 }); 1087 });
1083 } 1088 }
1084 List<List<ToolMeta>> toolMetaAllPage = ListUtil.split(toolMetaAll,5); 1089 List<List<ToolMeta>> toolMetaAllPage = ListUtil.split(toolMetaAll,5);
1085 if(page>=iPageCount2){ 1090 if(page>=iPageCount2){
1086 - showListAll.addAll(toolMetaAllPage.get(page-iPageCount2)); 1091 + int pages = page % iPageCount2;
  1092 + showListAll.addAll(toolMetaAllPage.get(pages));
1087 }else{ 1093 }else{
1088 showListAll.addAll(toolMetaAllPage.get(page-1)); 1094 showListAll.addAll(toolMetaAllPage.get(page-1));
1089 } 1095 }
1090 showListAll.forEach(one->{ 1096 showListAll.forEach(one->{
1091 - sb.append("<div style=\"color: #4096ff; margin-top: 5px;display:flex;align-items:center;font-size:12px;\" data-action=\"reset\" data-text=\"") 1097 + sb.append("<div style=\"color:"+(one.getSQue()?"#002966":"#4096ff")+"; cursor: pointer;margin-top: 5px;display:flex;align-items:center;font-size:12px;\" data-action=\"reset\" data-text=\"")
1092 .append(one.getSMethodName()).append("\" onclick=\"reset(").append(one.getSMethodName()).append("\">") 1098 .append(one.getSMethodName()).append("\" onclick=\"reset(").append(one.getSMethodName()).append("\">")
1093 .append("<span style=\"display:inline-block; width:5px; height:5px; background-color:#ccc; border-radius:50%; margin-right:5px;\"></span>") 1099 .append("<span style=\"display:inline-block; width:5px; height:5px; background-color:#ccc; border-radius:50%; margin-right:5px;\"></span>")
1094 .append(one.getSMethodName()) 1100 .append(one.getSMethodName())
src/main/java/com/xly/thread/AiUserAgentQuestionThread.java
@@ -22,15 +22,17 @@ public class AiUserAgentQuestionThread implements Runnable { @@ -22,15 +22,17 @@ public class AiUserAgentQuestionThread implements Runnable {
22 private String sSqlContent; 22 private String sSqlContent;
23 private String sQuestion; 23 private String sQuestion;
24 private String cachType; 24 private String cachType;
  25 + private Boolean bSucess;
25 List<ChatMessage> userMessage; 26 List<ChatMessage> userMessage;
26 public final String sProName="Sp_Ai_AiUserAgentQuestionThread"; 27 public final String sProName="Sp_Ai_AiUserAgentQuestionThread";
27 28
28 - public AiUserAgentQuestionThread(UserSceneSession session,String sQuestion,String sSqlContent,String cachType, List<ChatMessage> userMessage ) { 29 + public AiUserAgentQuestionThread(UserSceneSession session,String sQuestion,String sSqlContent,String cachType, List<ChatMessage> userMessage,Boolean bSucess) {
29 this.session = session; 30 this.session = session;
30 this.sSqlContent = sSqlContent; 31 this.sSqlContent = sSqlContent;
31 this.sQuestion = sQuestion; 32 this.sQuestion = sQuestion;
32 this.userMessage = userMessage; 33 this.userMessage = userMessage;
33 this.cachType = cachType; 34 this.cachType = cachType;
  35 + this.bSucess = bSucess;
34 } 36 }
35 37
36 @Override 38 @Override
@@ -55,11 +57,11 @@ public class AiUserAgentQuestionThread implements Runnable { @@ -55,11 +57,11 @@ public class AiUserAgentQuestionThread implements Runnable {
55 } 57 }
56 String sKey = sSceneId+"_"+sMethodId +"_"+sQuestion; 58 String sKey = sSceneId+"_"+sMethodId +"_"+sQuestion;
57 // SqlValidateUtil.getsKey( sSceneId, sMethodId, SqlValidateUtil.getsQuestion(session.getSUserQuestionList())); 59 // SqlValidateUtil.getsKey( sSceneId, sMethodId, SqlValidateUtil.getsQuestion(session.getSUserQuestionList()));
58 - //存入向量库 不包含where 条件  
59 - if(!SqlWhereHelper.hasWhereButNoCompareOperators(sSqlContent)){ 60 + //存入向量库 存在SQL语句并且没有where 并且执行成功
  61 + if(!SqlWhereHelper.hasWhereButNoCompareOperators(sSqlContent) && ObjectUtil.isNotEmpty(sSqlContent) && bSucess){
60 aiGlobalAgentQuestionSqlEmitterService.addAiGlobalAgentQuestionSqlEmitter(sKey,data,sQuestion,sSqlContent,cachType,"ai_global_agent_question_sql",false); 62 aiGlobalAgentQuestionSqlEmitterService.addAiGlobalAgentQuestionSqlEmitter(sKey,data,sQuestion,sSqlContent,cachType,"ai_global_agent_question_sql",false);
61 } 63 }
62 - //调用数据库插入数据库 64 + //调用数据库插入数据库
63 Map<String, Object> searMap = dynamicExeDbService.getDoProMap(sProName, data); 65 Map<String, Object> searMap = dynamicExeDbService.getDoProMap(sProName, data);
64 dynamicExeDbService.getCallPro(searMap, sProName); 66 dynamicExeDbService.getCallPro(searMap, sProName);
65 } 67 }