From 8d9902b30b3eea85c93156a84fed8c5ceee2f531 Mon Sep 17 00:00:00 2001
From: TangCheng <tangchengmail@163.com>
Date: 星期三, 19 三月 2025 18:12:12 +0800
Subject: [PATCH] 知识库去掉联想功能

---
 JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTJavaAIControl.java |   88 ++++++++++++++++++++++++++++++++-----------
 1 files changed, 65 insertions(+), 23 deletions(-)

diff --git a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTJavaAIControl.java b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTJavaAIControl.java
index 2458ca6..674e443 100644
--- a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTJavaAIControl.java
+++ b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTJavaAIControl.java
@@ -3,16 +3,12 @@
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.Map.Entry;
-import java.util.Set;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import com.google.gson.Gson;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.servlet.ModelAndView;
@@ -96,10 +92,14 @@
 					{
 						for(MultipartFile file : files)
 						{
-							String fileId = SMTStatic.newUUID();
+							String fileText="";
+							String fileSuffix = (String) SMTAIServerApp.getApp().getGlobalConfig("file_suffix", false);
 							String fileName = new File(file.getOriginalFilename()).getName();
 							byte[] fileData = readAttachFile(file);
-							String fileText = new String(fileData, "UTF-8");
+							if (fileSuffix.contains(fileName.substring(fileName.lastIndexOf(".") + 1))) {
+								fileText = SMTAIServerApp.fileTranslTxt(file);
+							}
+							String fileId = SMTStatic.newUUID();
 							db.executeSQL(
 								  " INSERT INTO chat_history_attach(attach_id, attach_type, attach_title, attach_bytes)"
 								+ " VALUES(?,?,?,?)", new Object[] {
@@ -127,7 +127,13 @@
 				tranReq.setAttachTables(jsonTables);
 				_mustRawMode = true;
 			}
-			
+			Json jsonMetrics = tranReq.convParamToJson("metrics", false);
+			if(jsonMetrics != null)
+			{
+				tranReq.setJsonAttachMetrics(jsonMetrics);
+				_mustRawMode = true;
+			}
+
 			_tranReq = tranReq;
 			_tranReq.setChunkedMode(true);
 		}
@@ -349,7 +355,7 @@
 	    	String sampleId = tranReq.convParamToString("sample_id", false);
 	    	String llmId = tranReq.convParamToString("llm_id", false);
 	    	String historyGroupId = tranReq.convParamToString("history_group_id", true);
-	    	String question = java.net.URLDecoder.decode(tranReq.convParamToString("question", true), "UTF-8");
+	    	String question = tranReq.convParamToString("question", true);
 	    	String processId = tranReq.convParamToString("process_id", false);
 	    	String sRawMode = tranReq.convParamToString("raw_mode", false);
 	    	String prevQuestion = tranReq.convParamToString("prev_question", false);
@@ -358,8 +364,6 @@
 	
 	    	if(SMTStatic.isNullOrEmpty(groupType))
 	    		groupType = "涓氬姟鍦烘櫙";
-	    	else
-	    		groupType = java.net.URLDecoder.decode(groupType, "UTF-8");
 	    	
 	    	tranReq.setCurQuestionPos(curPos);
 	    	tranReq.setCurGroupType(groupType);
@@ -1239,10 +1243,12 @@
     	String attachTableId = tranReq.convParamToString("id", true);
     	Json jsonFilters = tranReq.convParamToJson("filter", false);
     	Json jsonOrders = tranReq.convParamToJson("order", false);
+    	Integer limit = tranReq.convParamToInteger("limit", false);
     	SMTAIAttachTableDef attachTableDef = (SMTAIAttachTableDef) SMTAIServerApp.getApp().getAttachTableDef(attachTableId);
-    	
+    	if (limit==null)
+			limit=100;
     	SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
-    	attachTableDef.queryRecordsToJson(jsonFilters, jsonOrders, jsonWr);
+    	attachTableDef.queryRecordsToJson(jsonFilters, jsonOrders, jsonWr,limit);
     	
     	return tranReq.returnJson(jsonWr);
     	
@@ -1288,28 +1294,30 @@
     	Date startTime = tranReq.convParamToDate("start_time", true);
        	Date endTime = tranReq.convParamToDate("end_time", true);
     	Json jsonQuotaKeys = tranReq.convParamToJson("quota_keys", true);
-    	
+    	Integer limit = tranReq.convParamToInteger("limit", false);
+		if (limit==null)
+			limit=100;
     	SMTAIAttachMetricDef attachMetricDef = SMTAIServerApp.getApp().getAttachMetricDef(attachMetricId);
     	
     	SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
-    	attachMetricDef.queryValueToJson(startTime, endTime, jsonQuotaKeys, jsonWr);
+    	attachMetricDef.queryValueToJson(startTime, endTime, jsonQuotaKeys, jsonWr,limit);
     	
     	return tranReq.returnJson(jsonWr);
     }
-    
-    public ModelAndView callLLM(SMTAIServerRequest tranReq) throws Exception 
+
+    public ModelAndView callLLM(SMTAIServerRequest tranReq) throws Exception
     {
     	String llmId = tranReq.convParamToString("llm_id", false);
     	Json jsonSystems = tranReq.convParamToJson("system_json", false);
     	String question = tranReq.convParamToString("question", true);
     	boolean answerIsJson = tranReq.convParamToBoolean("answer_is_json", true);
-    	
-    	
+
+
     	SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
     	SMTLLMConnect conn = SMTAIServerApp.getApp().allocLLMConnect(llmId);
-    	
+
     	List<String> listSystem = new ArrayList<>();
-    	
+
     	if(jsonSystems != null)
     	{
 	    	for(Json jsonSystem : jsonSystems.asJsonList())
@@ -1317,7 +1325,7 @@
 	    		listSystem.add(jsonSystem.asString());
 	    	}
     	}
-    	
+
     	String answer = conn.callWithMessage(listSystem, question, tranReq);
     	if(answerIsJson)
     	{
@@ -1330,4 +1338,38 @@
 
 		return tranReq.returnJson(jsonWr);
     }
+	public ModelAndView downloadFileByFileId(SMTAIServerRequest tranReq, HttpServletResponse response) throws Exception {
+		String fileId = tranReq.convParamToString("file_id", true);  // 鑾峰彇璇锋眰涓殑file_id
+		SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
+
+		try {
+			// 鏌ヨ鎸囧畾file_id鐨勬枃浠惰褰�
+			DBRecords dbRecords = db.querySQL("SELECT * FROM ai_times.chat_history_attach WHERE attach_id=?", new Object[]{fileId});
+			if (dbRecords.getRecords().isEmpty()) {
+				throw new Exception("鏂囦欢涓嶅瓨鍦�");
+			}
+			// 鑾峰彇鏂囦欢淇℃伅
+			DBRecord fileRecord = dbRecords.getRecords().get(0);
+			String attachTitle = fileRecord.getString("attach_title");  // 鑾峰彇鏂囦欢鏍囬
+			String extension = attachTitle.substring(attachTitle.lastIndexOf(".") + 1);
+			// 鎵╁睍鍚嶅拰 MIME 绫诲瀷鏄犲皠
+
+			Object fileTypeMapping = SMTAIServerApp.getApp().getGlobalConfig("file_type_mapping", false);
+			Json json = Json.read(fileTypeMapping.toString());
+			Map<String, Object> mimeTypes = json.asMap();
+			String mimeType = (String) mimeTypes.getOrDefault(extension.toLowerCase(), "application/octet-stream");
+			// 鑾峰彇鏂囦欢鐨勫瓧鑺傛暟鎹紝鍋囪杩斿洖鐨勬槸瀛楄妭鏁扮粍
+			byte[] bytes = (byte[]) fileRecord.getValue("attach_bytes");  // 鑾峰彇瀛楄妭鏁版嵁
+			return tranReq.returnDownloadByteArray(bytes, attachTitle, mimeType);
+//			// 璁剧疆鍝嶅簲澶村拰鏂囦欢鍚�
+//			String fileName = attachTitle != null ? attachTitle : "downloaded_file";
+//			response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
+//			response.setContentType("application/octet-stream");
+//			response.getOutputStream().write(bytes);  // 鍐欏叆瀛楄妭鏁版嵁
+//			response.getOutputStream().flush();
+		} catch (Exception e) {
+			throw new Exception("Error processing file download: " + e.getMessage());
+		}
+	}
+
 }

--
Gitblit v1.9.3