Commit da8e40f1bb9445da70ea6b4a9a8e954a5685ec74

Authored by qianbao
1 parent cf793673

添加向量库

src/main/java/com/xly/entity/ToolMeta.java
... ... @@ -51,5 +51,6 @@ public class ToolMeta {
51 51 private String sVectorjson;
52 52 private String sVectorfiledAll;
53 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 85 InsertResp insertResp = milvusClient.insert(insertReq);
86 86 //是否初始化。初始化不需要插入到数据库
87 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 93 Map<String, Object> searMap = dynamicExeDbService.getDoProMap(sProName, data);
90 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 164 if(ObjectUtil.isNotEmpty(sUserInput)){
165 165 //根据问题查询向量库
166 166 Map<String,Object> serMap = aiGlobalAgentQuestionSqlEmitterService.queryAiGlobalAgentQuestionSqlEmitter(searchText, "ai_global_agent_question_sql");
167   - if(ObjectUtil.isNotEmpty(serMap)){
  167 + if(ObjectUtil.isEmpty(serMap)){
168 168 aiGlobalAgentQuestionSqlEmitterService.addAiGlobalAgentQuestionSqlEmitter(searchText,one,sUserInput,sSqlContent,"MYSQL","ai_global_agent_question_sql",false);
169 169 }else{
170 170 sb.append(sUserInput).append("已经存在").append("\n");
... ...
src/main/java/com/xly/service/XlyErpService.java
... ... @@ -515,16 +515,16 @@ public class XlyErpService {
515 515 // 待条件全查 不带 10条
516 516 List<Map<String,Object>> data = milvusService.getDataToCollection(sInputTabelName, milvusFilter,userInput,pageSize,filedsShow, vectorValue,sceneName);
517 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 529 if( retrunMarkdownType(userInput) ){
530 530 resultExplain = buildMarkdownTableWithStream(data, title);
... ... @@ -610,7 +610,7 @@ public class XlyErpService {
610 610 if(attempt==1){
611 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 614 }catch (SqlValidateException e){
615 615 return "本场景没有识别到您的意图<br/> 如果切换场景,点[回首页],如果在本场景下,转换意图,点[清除记忆]";
616 616 }catch (Exception e){
... ... @@ -680,13 +680,13 @@ public class XlyErpService {
680 680 * @return java.lang.String
681 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 684 // 1. 构建自然语言转SQLAgent,
685   - List<Map<String, Object>> sqlResult;
  685 + List<Map<String, Object>> sqlResult = null;
686 686 String rawSql;
687 687 String tableStruct = session.getCurrentTool().getSStructureMemo();
688 688 String sError_mes;
689   - Boolean doAddSql = false;
  689 +// Boolean doAddSql = false;
690 690 List<ChatMessage> chatMessage = new ArrayList<>();
691 691 try{
692 692 //如果之前已查询直接返回
... ... @@ -711,7 +711,7 @@ public class XlyErpService {
711 711 // 2. 清理SQL多余符号 + 生产级强校验(核心安全保障,不可省略)
712 712 cleanSql = SqlValidateUtil.cleanSqlSymbol(rawSql);
713 713 SqlValidateUtil.validateMysqlSql(cleanSql);
714   - doAddSql = true;
  714 +// doAddSql = true;
715 715 }
716 716 // List<ChatMessage> chatMessage2 = operableChatMemoryProvider.getCurrentChatMessages(session.getUserId());
717 717 try{
... ... @@ -730,8 +730,14 @@ public class XlyErpService {
730 730 sError_mes = e.getMessage();
731 731 doAiSqlErrorHistoryThread(session, StrUtil.EMPTY, cleanSql, sError_mes,input);
732 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 742 if(ObjectUtil.isEmpty(sqlResult)){
737 743 session.setDbType("X");
... ... @@ -746,11 +752,7 @@ public class XlyErpService {
746 752 if(Integer.valueOf(iErroCount)>0){
747 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 757 String resultExplain = StrUtil.EMPTY;
756 758 // if(retrunMarkdownType(userInput) ){
... ... @@ -838,11 +840,12 @@ public class XlyErpService {
838 840 String sQuestion,
839 841 String sSqlContent,
840 842 String cachType,
841   - List<ChatMessage> chatMessage
  843 + List<ChatMessage> chatMessage,
  844 + Boolean bSucess
842 845  
843 846 ){
844 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 849 mts.service(at);
847 850 }
848 851  
... ... @@ -1065,10 +1068,10 @@ public class XlyErpService {
1065 1068 List<Map<String,Object>> data = getAiUserAgentQuestion(session.getUserName(),sSceneId, page,3);
1066 1069 Integer iPageCount = pageCount;
1067 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 1072 if(page==1){
1070 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 1076 if(iPageCount2>iPageCount){
1074 1077 iPageCount = iPageCount2;
... ... @@ -1079,16 +1082,19 @@ public class XlyErpService {
1079 1082 data.forEach(one->{
1080 1083 ToolMeta tm = new ToolMeta();
1081 1084 tm.setSMethodName(one.get("sUserInput").toString());
  1085 + tm.setSQue(true);
  1086 + showListAll.add(tm);
1082 1087 });
1083 1088 }
1084 1089 List<List<ToolMeta>> toolMetaAllPage = ListUtil.split(toolMetaAll,5);
1085 1090 if(page>=iPageCount2){
1086   - showListAll.addAll(toolMetaAllPage.get(page-iPageCount2));
  1091 + int pages = page % iPageCount2;
  1092 + showListAll.addAll(toolMetaAllPage.get(pages));
1087 1093 }else{
1088 1094 showListAll.addAll(toolMetaAllPage.get(page-1));
1089 1095 }
1090 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 1098 .append(one.getSMethodName()).append("\" onclick=\"reset(").append(one.getSMethodName()).append("\">")
1093 1099 .append("<span style=\"display:inline-block; width:5px; height:5px; background-color:#ccc; border-radius:50%; margin-right:5px;\"></span>")
1094 1100 .append(one.getSMethodName())
... ...
src/main/java/com/xly/thread/AiUserAgentQuestionThread.java
... ... @@ -22,15 +22,17 @@ public class AiUserAgentQuestionThread implements Runnable {
22 22 private String sSqlContent;
23 23 private String sQuestion;
24 24 private String cachType;
  25 + private Boolean bSucess;
25 26 List<ChatMessage> userMessage;
26 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 30 this.session = session;
30 31 this.sSqlContent = sSqlContent;
31 32 this.sQuestion = sQuestion;
32 33 this.userMessage = userMessage;
33 34 this.cachType = cachType;
  35 + this.bSucess = bSucess;
34 36 }
35 37  
36 38 @Override
... ... @@ -55,11 +57,11 @@ public class AiUserAgentQuestionThread implements Runnable {
55 57 }
56 58 String sKey = sSceneId+"_"+sMethodId +"_"+sQuestion;
57 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 62 aiGlobalAgentQuestionSqlEmitterService.addAiGlobalAgentQuestionSqlEmitter(sKey,data,sQuestion,sSqlContent,cachType,"ai_global_agent_question_sql",false);
61 63 }
62   - //调用数据库插入数据库
  64 + //调用数据库插入数据库
63 65 Map<String, Object> searMap = dynamicExeDbService.getDoProMap(sProName, data);
64 66 dynamicExeDbService.getCallPro(searMap, sProName);
65 67 }
... ...