From 8e1e721cbfe486e622c40182fb4687a5a2ddd252 Mon Sep 17 00:00:00 2001 From: qfrjava <13402782+qfrjava@user.noreply.gitee.com> Date: 星期三, 19 三月 2025 17:39:41 +0800 Subject: [PATCH] feat(system): 添加大语言模型和问题替换相关功能 --- JAVA/SMTAIServer/src/main/resources/requestmap/system_manager.json | 87 ++++++++++++++ JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTSystemManagerControl.java | 211 ++++++++++++++++++++++++++++++++++ JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTKnowledgeControl.java | 42 +++--- 3 files changed, 316 insertions(+), 24 deletions(-) diff --git a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTKnowledgeControl.java b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTKnowledgeControl.java index e7f88a9..a9dc942 100644 --- a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTKnowledgeControl.java +++ b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTKnowledgeControl.java @@ -2,11 +2,7 @@ import java.io.BufferedReader; import java.io.StringReader; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @@ -423,23 +419,27 @@ public ModelAndView addKnowledgeFile(SMTAIServerRequest tranReq, @RequestParam(value = "file", required = false) MultipartFile file) throws Exception { - String fileName = file.getOriginalFilename(); - String groupId = tranReq.convParamToString("group_id", true); - String fileText = SMTStatic.readTextStream(file.getInputStream()); + String fileText = ""; + String fileName = file.getOriginalFilename(); + String groupId = tranReq.convParamToString("group_id", true); + fileText = SMTStatic.readTextStream(file.getInputStream()); - - String fileType; - List<String> listBlock = null; - if(fileName.endsWith(".md")) - { - fileType = "markdown"; - listBlock = splitMarkdownToVectorBlock(fileText); - } - else - { - return tranReq.returnJsonState(false, "鏂囦欢鏍煎紡涓嶆敮鎸�", null); - } - + String fileType = ""; + List<String> listBlock = null; + + // 鍏堝垽鏂枃浠跺悕鏄惁涓虹┖锛岄伩鍏嶇┖鎸囬拡寮傚父 + if (fileName != null && fileName.contains(".")) { + fileType = fileName.substring(fileName.lastIndexOf(".")).toLowerCase(); // 缁熶竴杞崲涓哄皬鍐欙紝閬垮厤澶у皬鍐欓棶棰� + } + + // 鐢� Set 瀛樺偍鏀寔鐨勬枃浠剁被鍨嬶紝鎻愰珮鍙鎬� + Set<String> supportedTypes = new HashSet<>(Arrays.asList(".md", ".pdf", ".docx", ".doc")); + if (supportedTypes.contains(fileType)) { + fileText = SMTAIServerApp.fileTranslTxt(file); + } else { + return tranReq.returnJsonState(false, "鏂囦欢鏍煎紡涓嶆敮鎸�", null); + } + listBlock = splitMarkdownToVectorBlock(fileText); List<String> listVector = new ArrayList<>(); SMTLLMConnect llm = SMTAIServerApp.getApp().allocLLMConnect(null); try diff --git a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTSystemManagerControl.java b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTSystemManagerControl.java index c229c5f..0b0f542 100644 --- a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTSystemManagerControl.java +++ b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTSystemManagerControl.java @@ -731,8 +731,142 @@ db.close(); } } - - + + public ModelAndView addLLMFactory(SMTAIServerRequest tranReq) throws Exception { + // 瑙f瀽璇锋眰鍙傛暟 + String factoryId = tranReq.convParamToString("factory_id", true); + String factoryTitle = tranReq.convParamToString("factory_title",true); + String factoryArgs = tranReq.convParamToString("factory_args",true); + SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); + try { + + String selectSql = "SELECT * FROM ai_llm_factory WHERE factory_id = ?"; + Object[] params1 = {factoryId}; + if (db.querySQL(selectSql, params1).getRowCount()>0) { + return tranReq.returnJsonState(false, "妯″瀷id宸插瓨鍦�", null); + } + // 鏋勫缓 SQL 璇彞 + String sql = "INSERT INTO ai_llm_factory (factory_id, factory_title, factory_args) VALUES (?, ?, ?)"; + Object[] params2 = {factoryId, factoryTitle, factoryArgs}; + // 鎵ц鎻掑叆鎿嶄綔 + db.executeSQL(sql, params2); + // 杩斿洖 JSON 缁撴灉 + SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); + return tranReq.returnJson(jsonWr); + } finally { + db.close(); + } + } + public ModelAndView addLLMConnect(SMTAIServerRequest tranReq) throws Exception { + // 瑙f瀽璇锋眰鍙傛暟 + String connectId = tranReq.convParamToString("connect_id", true); + String factoryId = tranReq.convParamToString("factory_id", true); + String connectTitle = tranReq.convParamToString("connect_title", true); + String className = tranReq.convParamToString("class_name", true); + String connectArgs = tranReq.convParamToString("connect_args", true); + + SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); + + try { + // 棣栧厛妫�鏌ユā鍨媔d鏄惁瀛樺湪 + String selectFactorySql = "SELECT * FROM ai_llm_factory WHERE factory_id = ?"; + Object[] factoryParams = {factoryId}; + if (db.querySQL(selectFactorySql, factoryParams).getRowCount() == 0) { + return tranReq.returnJsonState(false, "妯″瀷id涓嶅瓨鍦�", null); + } + + // 妫�鏌ヨ繛鎺d鏄惁宸插瓨鍦� + String selectConnectSql = "SELECT * FROM ai_llm_connect WHERE connect_id = ?"; + Object[] connectParams = {connectId}; + if (db.querySQL(selectConnectSql, connectParams).getRowCount() > 0) { + return tranReq.returnJsonState(false, "杩炴帴id宸插瓨鍦�", null); + } + + // 鏋勫缓 SQL 璇彞 + String insertSql = "INSERT INTO ai_llm_connect (connect_id, factory_id, connect_title, class_name, connect_args) VALUES (?, ?, ?, ?, ?)"; + Object[] insertParams = {connectId, factoryId, connectTitle, className, connectArgs}; + + // 鎵ц鎻掑叆鎿嶄綔 + db.executeSQL(insertSql, insertParams); + + // 杩斿洖 JSON 缁撴灉 + SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); + return tranReq.returnJson(jsonWr); + } finally { + db.close(); + } + } + public ModelAndView updateLLMFactory(SMTAIServerRequest tranReq) throws Exception { + // 瑙f瀽璇锋眰鍙傛暟 + String factoryId = tranReq.convParamToString("factory_id", true); + String factoryTitle = tranReq.convParamToString("factory_title", true); + String factoryArgs = tranReq.convParamToString("factory_args", true); + + SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); + + try { + // 妫�鏌ュ伐鍘� ID 鏄惁瀛樺湪 + String selectSql = "SELECT * FROM ai_llm_factory WHERE factory_id = ?"; + Object[] params = {factoryId}; + if (db.querySQL(selectSql, params).getRowCount() == 0) { + return tranReq.returnJsonState(false, "妯″瀷id涓嶅瓨鍦�", null); + } + + // 鏋勫缓 SQL 璇彞 + String updateSql = "UPDATE ai_llm_factory SET factory_title = ?, factory_args = ? WHERE factory_id = ?"; + Object[] updateParams = {factoryTitle, factoryArgs, factoryId}; + + // 鎵ц鏇存柊鎿嶄綔 + db.executeSQL(updateSql, updateParams); + + // 杩斿洖 JSON 缁撴灉 + SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); + return tranReq.returnJson(jsonWr); + } finally { + db.close(); + } + } + public ModelAndView updateLLMConnect(SMTAIServerRequest tranReq) throws Exception { + // 瑙f瀽璇锋眰鍙傛暟 + String connectId = tranReq.convParamToString("connect_id", true); + String factoryId = tranReq.convParamToString("factory_id", true); + String connectTitle = tranReq.convParamToString("connect_title", true); + String className = tranReq.convParamToString("class_name", true); + String connectArgs = tranReq.convParamToString("connect_args", true); + + SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); + + try { + // 妫�鏌ヨ繛鎺� ID 鏄惁瀛樺湪 + String selectConnectSql = "SELECT * FROM ai_llm_connect WHERE connect_id = ?"; + Object[] connectParams = {connectId}; + if (db.querySQL(selectConnectSql, connectParams).getRowCount() == 0) { + return tranReq.returnJsonState(false, "杩炴帴id涓嶅瓨鍦�", null); + } + + // 妫�鏌ユā鍨� ID 鏄惁瀛樺湪 + String selectFactorySql = "SELECT * FROM ai_llm_factory WHERE factory_id = ?"; + Object[] factoryParams = {factoryId}; + if (db.querySQL(selectFactorySql, factoryParams).getRowCount() == 0) { + return tranReq.returnJsonState(false, "妯″瀷id涓嶅瓨鍦�", null); + } + + // 鏋勫缓 SQL 璇彞 + String updateSql = "UPDATE ai_llm_connect SET factory_id = ?, connect_title = ?, class_name = ?, connect_args = ? WHERE connect_id = ?"; + Object[] updateParams = {factoryId, connectTitle, className, connectArgs, connectId}; + + // 鎵ц鏇存柊鎿嶄綔 + db.executeSQL(updateSql, updateParams); + + // 杩斿洖 JSON 缁撴灉 + SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); + return tranReq.returnJson(jsonWr); + } finally { + db.close(); + } + } + + public ModelAndView clearSystemCache(SMTAIServerRequest tranReq) throws Exception { SMTApp.getThis().clearEhCacheManager(); @@ -766,4 +900,77 @@ } return tranReq.returnJson(jsonWr); } + public ModelAndView addQuestionReplace(SMTAIServerRequest tranReq) throws Exception { + String loginUserId = tranReq.getLoginUserId(); + // 瑙f瀽璇锋眰鍙傛暟 + String replaceId = tranReq.convParamToString("replace_id", true); + String questionText = tranReq.convParamToString("question_text", true); + String replaceText = tranReq.convParamToString("replace_text", true); + int replaceOrder = tranReq.convParamToInteger("replace_order", true); + String replaceState = tranReq.convParamToString("replace_state", true); + String groupType = tranReq.convParamToString("group_type", true); + + SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); + + try { + // 妫�鏌ユ浛鎹D鏄惁宸插瓨鍦� + String selectSql = "SELECT * FROM ai_question_replace WHERE replace_id = ?"; + Object[] selectParams = {replaceId}; + if (db.querySQL(selectSql, selectParams).getRowCount() > 0) { + return tranReq.returnJsonState(false, "鏇挎崲ID宸插瓨鍦�", null); + } + + // 鏋勫缓 SQL 璇彞 + String insertSql = "INSERT INTO ai_question_replace (replace_id, question_text, replace_text, replace_order, replace_state, create_user, create_time, group_type) " + + "VALUES (?, ?, ?, ?, ?, ?, NOW(), ?)"; + Object[] insertParams = { + replaceId, questionText, replaceText, replaceOrder, replaceState, loginUserId, groupType + }; + + // 鎵ц鎻掑叆鎿嶄綔 + db.executeSQL(insertSql, insertParams); + + // 杩斿洖 JSON 缁撴灉 + SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); + return tranReq.returnJson(jsonWr); + } finally { + db.close(); + } + } + public ModelAndView updateQuestionReplace(SMTAIServerRequest tranReq) throws Exception { + // 瑙f瀽璇锋眰鍙傛暟 + String replaceId = tranReq.convParamToString("replace_id", true); + String questionText = tranReq.convParamToString("question_text", false); + String replaceText = tranReq.convParamToString("replace_text", false); + int replaceOrder = tranReq.convParamToInteger("replace_order", false); + String replaceState = tranReq.convParamToString("replace_state", false); + String groupType = tranReq.convParamToString("group_type", false); + + SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); + + try { + // 妫�鏌ユ浛鎹D鏄惁瀛樺湪 + String selectSql = "SELECT * FROM ai_question_replace WHERE replace_id = ?"; + Object[] selectParams = {replaceId}; + if (db.querySQL(selectSql, selectParams).getRowCount() == 0) { + return tranReq.returnJsonState(false, "鏇挎崲ID涓嶅瓨鍦�", null); + } + + // 鏋勫缓 SQL 璇彞 + String updateSql = "UPDATE ai_question_replace SET question_text = ?, replace_text = ?, replace_order = ?, replace_state = ?, group_type = ? " + + "WHERE replace_id = ?"; + Object[] updateParams = {questionText, replaceText, replaceOrder, replaceState, groupType, replaceId}; + + // 鎵ц鏇存柊鎿嶄綔 + db.executeSQL(updateSql, updateParams); + + // 杩斿洖 JSON 缁撴灉 + SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); + return tranReq.returnJson(jsonWr); + } finally { + db.close(); + } + } + + } diff --git a/JAVA/SMTAIServer/src/main/resources/requestmap/system_manager.json b/JAVA/SMTAIServer/src/main/resources/requestmap/system_manager.json index 951f679..d4f5ab1 100644 --- a/JAVA/SMTAIServer/src/main/resources/requestmap/system_manager.json +++ b/JAVA/SMTAIServer/src/main/resources/requestmap/system_manager.json @@ -198,7 +198,92 @@ } ] }, - + "admin/sample/add_LLM_factory":{"map":{"class":"#SMTSystemManagerControl", "method":"addLLMFactory"} , + "swaggers":[ + { "tags" : ["鏂板澶ц瑷�妯″瀷"], + "title" : "鏂板澶ц瑷�妯″瀷", + "parameters" : [ + {"name":"factory_id", "title":"妯″瀷id", "required":true}, + {"name":"factory_title", "title":"妯″瀷鍐呭", "required":true}, + {"name":"factory_args", "title":"妯″瀷閰嶇疆", "required":true} + ] + } + ] + }, + "admin/sample/add_LLM_connect":{"map":{"class":"#SMTSystemManagerControl", "method":"addLLMConnect"} , + "swaggers":[ + { "tags" : ["鏂板杩炴帴澶ц瑷�妯″瀷閰嶇疆"], + "title" : "鏂板杩炴帴澶ц瑷�妯″瀷閰嶇疆", + "parameters" : [ + {"name":"connect_id", "title":"杩炴帴id", "required":true}, + {"name":"factory_id", "title":"妯″瀷id", "required":true}, + {"name":"connect_title", "title":"杩炴帴鏍囬", "required":true}, + {"name":"class_name", "title":"绫诲悕", "required":true}, + {"name":"connect_args", "title":"杩炴帴閰嶇疆", "required":true} + ] + } + ] + }, + "admin/sample/update_LLMf_factory": {"map": { + "class": "#SMTSystemManagerControl", "method": "updateLLMFactory"} , + "swaggers": [ + {"tags": ["鏇存柊澶ц瑷�妯″瀷宸ュ巶閰嶇疆"], + "title": "鏇存柊澶ц瑷�妯″瀷宸ュ巶閰嶇疆", + "parameters": [ + {"name": "factory_id", "title": "妯″瀷id", "required": true}, + {"name": "factory_title", "title": "妯″瀷鏍囬", "required": false}, + {"name": "factory_args", "title": "妯″瀷閰嶇疆", "required": false} + ] + } + ] + }, + + "admin/sample/update_LLM_connect": {"map": {"class": "#SMTSystemManagerControl", "method": "updateLLMConnect"} , + "swaggers": [{ + "tags": ["鏇存柊杩炴帴澶ц瑷�妯″瀷閰嶇疆"], + "title": "鏇存柊杩炴帴澶ц瑷�妯″瀷閰嶇疆", + "parameters": [ + {"name": "connect_id", "title": "杩炴帴id", "required": true}, + {"name": "factory_id", "title": "妯″瀷id", "required": false}, + {"name": "connect_title", "title": "杩炴帴鏍囬", "required": false}, + {"name": "class_name", "title": "绫诲悕", "required": false}, + {"name": "connect_args", "title": "杩炴帴閰嶇疆", "required": false} + ] + } + ] + }, + "admin/sample/add_question_replace": {"map": {"class": "#SMTSystemManagerControl", "method": "addQuestionReplace"} ,"no_shrio": true, + "swaggers": [{ + "tags": ["鏂板闂缃崲鍙傛暟"], + "title": "鏂板闂缃崲鍙傛暟", + "parameters": [ + {"name": "replace_id", "title": "id", "required": true}, + {"name": "question_text", "title": "闂鏂囨湰", "required": true}, + {"name": "replace_text", "title": "鍒囨崲鏂囨湰", "required": true}, + {"name": "replace_order", "title": "鍒囨崲鍛戒护", "required": true}, + {"name": "replace_state", "title": "杩炴帴閰嶇疆", "required": true}, + {"name": "group_type", "title": "杩炴帴閰嶇疆", "required": true} + ] + } + ] + }, + "admin/sample/update_question_replace": {"map": {"class": "#SMTSystemManagerControl", "method": "updateQuestionReplace"},"no_shrio": true , + "swaggers": [{ + "tags": ["鏇存柊闂鏇挎崲閰嶇疆"], + "title": "鏇存柊闂鏇挎崲閰嶇疆", + "parameters": [ + {"name": "replace_id", "title": "id", "required": true}, + {"name": "question_text", "title": "闂鏂囨湰", "required": false}, + {"name": "replace_text", "title": "鍒囨崲鏂囨湰", "required": false}, + {"name": "replace_order", "title": "鍒囨崲鍛戒护", "required": false}, + {"name": "replace_state", "title": "杩炴帴閰嶇疆", "required": false}, + {"name": "group_type", "title": "杩炴帴閰嶇疆", "required": false} + ] + } + ] + }, + + "admin/system/clear_system_cache":{"map":{"class":"#SMTSystemManagerControl", "method":"clearSystemCache"}, "swaggers":[ { "tags" : ["绯荤粺绠$悊"], -- Gitblit v1.9.3