feat(system): 添加大语言模型和问题替换相关功能
- 新增大语言模型工厂配置功能
- 新增连接大语言模型配置功能
- 实现大语言模型工厂配置更新
- 实现连接大语言模型配置更新
- 添加问题替换参数配置功能
- 实现问题替换参数配置更新
-优化文件上传逻辑,支持多种文件类型
| | |
| | |
|
| | | 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;
|
| | |
| | |
|
| | | 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
|
| | |
| | | db.close();
|
| | | }
|
| | | }
|
| | | |
| | | |
| | |
|
| | | public ModelAndView addLLMFactory(SMTAIServerRequest tranReq) throws Exception {
|
| | | // 解析请求参数
|
| | | 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 {
|
| | | // 解析请求参数
|
| | | 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 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);
|
| | | }
|
| | |
|
| | | // 检查连接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);
|
| | | }
|
| | |
|
| | | // 构建 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 {
|
| | | // 解析请求参数
|
| | | 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 {
|
| | | // 解析请求参数
|
| | | 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();
|
| | |
| | | }
|
| | | return tranReq.returnJson(jsonWr);
|
| | | }
|
| | | public ModelAndView addQuestionReplace(SMTAIServerRequest tranReq) throws Exception {
|
| | | String loginUserId = tranReq.getLoginUserId();
|
| | | // 解析请求参数
|
| | | 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 {
|
| | | // 检查替换ID是否已存在
|
| | | 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 {
|
| | | // 解析请求参数
|
| | | 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 {
|
| | | // 检查替换ID是否存在
|
| | | 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();
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | | }
|
| | |
| | | }
|
| | | ]
|
| | | },
|
| | | |
| | | "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" : ["系统管理"],
|