| | |
| | | @JsonBody(value = "botId", required = true) BigInteger botId, |
| | | @JsonBody(value = "sessionId", required = true) String sessionId, |
| | | @JsonBody(value = "isExternalMsg") int isExternalMsg, |
| | | @JsonBody(value = "files") List<String> files,//上传文件 |
| | | @JsonBody(value = "file") String file,//上传文件 |
| | | HttpServletResponse response) { |
| | | response.setContentType("text/event-stream"); |
| | |
| | | String systemPrompt = llmOptions != null ? (String) llmOptions.get("systemPrompt") : null; |
| | | |
| | | if (StringUtil.hasText(aiBot.getModelAPI())){ |
| | | if (aiBot.getBotTypeId() == 2) { |
| | | if (aiBot.getBotTypeId() == 2 || aiBot.getBotTypeId() == 3) { |
| | | |
| | | String apiUrl = aiBot.getModelAPI()+"/workflows/run"; // 替换为实际API URL |
| | | String apiKey = aiBot.getModelKEY(); // 替换为实际API Key |
| | | |
| | | DifyStreamClient client = new DifyStreamClient(apiUrl, apiKey, aiBotMessageService); |
| | | DifyStreamClient uploadClient = new DifyStreamClient(aiBot.getModelAPI()+"/files/upload", apiKey, aiBotMessageService); |
| | | String fileId = file; |
| | | // DifyStreamClient uploadClient = new DifyStreamClient(aiBot.getModelAPI()+"/files/upload", apiKey, aiBotMessageService); |
| | | |
| | | // 2. 构建文件参数对象 |
| | | Map<String, Object> fileParam = new HashMap<>(); |
| | | fileParam.put("transfer_method", "local_file"); |
| | | fileParam.put("upload_file_id", fileId); |
| | | // fileParam.put("type", fileJson.get("extension").getAsString()); // 例如 "excel"、"pdf" 等 |
| | | fileParam.put("type", "document"); // 例如 "excel"、"pdf" 等 |
| | | |
| | | // 3. 组装 inputs 参数 |
| | | |
| | | |
| | | Map<String, Object> inputs = new HashMap<>(); |
| | | inputs.put("w", fileParam); // 添加文件参数,variableName 如 "document" |
| | | if(aiBot.getBotTypeId() == 3){ |
| | | // 2. 构建文件参数对象 |
| | | |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.set("Authorization", apiKey); |
| | | |
| | | HttpEntity<Object> requestEntity = new HttpEntity<>(null, headers); |
| | | |
| | | ResponseEntity<String> parameters = restTemplate.exchange( |
| | | aiBot.getModelAPI()+"/parameters", |
| | | HttpMethod.GET, |
| | | requestEntity, // 请求体(GET 无请求体) |
| | | String.class // 响应结果类型 |
| | | ); |
| | | |
| | | org.json.JSONObject jsonObj = new org.json.JSONObject(parameters.getBody()); |
| | | // 先获取 user_input_form 数组 |
| | | org.json.JSONArray userInputFormArr = jsonObj.getJSONArray("user_input_form"); |
| | | String variable = null; |
| | | String type = ""; |
| | | // 遍历数组,找到 file - list 结构 |
| | | for (int i = 0; i < userInputFormArr.length(); i++) { |
| | | org.json.JSONObject formItem = userInputFormArr.getJSONObject(i); |
| | | if (formItem.has("file-list")) { |
| | | org.json.JSONObject fileListObj = formItem.getJSONObject("file-list"); |
| | | // 提取 variable 参数 |
| | | variable = fileListObj.getString("variable"); |
| | | // 提取 allowed_file_types 参数(数组形式) |
| | | org.json.JSONArray allowedFileTypesArr = fileListObj.getJSONArray("allowed_file_types"); |
| | | System.out.println("variable: " + variable); |
| | | System.out.println("allowed_file_types: " + allowedFileTypesArr.toString()); |
| | | type = allowedFileTypesArr.toString().replace("[\"", "").replace("\"]", ""); |
| | | break; // 这里假设只有一个符合的 file - list,找到就退出循环,可根据实际情况调整 |
| | | } |
| | | } |
| | | |
| | | List fileList = new ArrayList<>(); |
| | | Map<String, Object> fileParam = new HashMap<>(); |
| | | for (String fileId : files) { |
| | | fileParam.put("transfer_method", "local_file"); |
| | | fileParam.put("upload_file_id", fileId); |
| | | fileParam.put("type", type); // 例如 "excel"、"pdf" 等 |
| | | fileList.add(fileParam); |
| | | } |
| | | // 3. 组装 inputs 参数 |
| | | inputs.put(variable, fileList); // 添加文件参数,variableName 如 "document" |
| | | } |
| | | else { |
| | | |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.set("Authorization", apiKey); |
| | | |
| | | Map<String, Object> fileParam = new HashMap<>(); |
| | | fileParam.put("transfer_method", "local_file"); |
| | | fileParam.put("upload_file_id", file); |
| | | HttpEntity<Object> requestEntity = new HttpEntity<>(null, headers); |
| | | |
| | | ResponseEntity<String> parameters = restTemplate.exchange( |
| | | aiBot.getModelAPI()+"/parameters", |
| | | HttpMethod.GET, |
| | | requestEntity, // 请求体(GET 无请求体) |
| | | String.class // 响应结果类型 |
| | | ); |
| | | // System.out.println("========================\n"+parameters.getBody()+"\n====================="); |
| | | String variable = null; |
| | | String type = ""; |
| | | try { |
| | | // 解析 JSON 字符串 |
| | | org.json.JSONObject configJson = new org.json.JSONObject(parameters.getBody()); |
| | | |
| | | // 获取 user_input_form 数组 |
| | | org.json.JSONArray userInputFormArray = configJson.getJSONArray("user_input_form"); |
| | | |
| | | // 遍历 user_input_form 数组中的每个元素 |
| | | for (int i = 0; i < userInputFormArray.length(); i++) { |
| | | org.json.JSONObject formElement = userInputFormArray.getJSONObject(i); |
| | | |
| | | // 检查是否包含 "file" 对象 |
| | | if (formElement.has("file")) { |
| | | org.json.JSONObject fileObject = formElement.getJSONObject("file"); |
| | | |
| | | // 提取 variable |
| | | variable = fileObject.getString("variable"); |
| | | |
| | | // 提取 allowed_file_types 数组 |
| | | org.json.JSONArray allowedFileTypesArray = fileObject.getJSONArray("allowed_file_types"); |
| | | |
| | | // 打印结果 |
| | | System.out.println("Variable: " + variable); |
| | | System.out.print("Document Types: "); |
| | | |
| | | for (int j = 0; j < allowedFileTypesArray.length(); j++) { |
| | | type = type + allowedFileTypesArray.getString(j); |
| | | System.out.print(allowedFileTypesArray.getString(j) + " "); |
| | | } |
| | | |
| | | System.out.println(); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | fileParam.put("type", type); // 例如 "excel"、"pdf" 等 |
| | | // 3. 组装 inputs 参数 |
| | | inputs.put(variable, fileParam); // 添加文件参数,variableName 如 "document" |
| | | |
| | | } |
| | | |
| | | AiBotMessageMemory memory = new AiBotMessageMemory(botId, SaTokenUtil.getLoginAccount().getId(), |
| | | sessionId, isExternalMsg, aiBotMessageService, aiBotConversationMessageMapper, |
| | |
| | | } catch (Exception e) { |
| | | emitter.completeWithError(e); |
| | | } |
| | | System.out.println(emitter.toString()); |
| | | // System.out.println(emitter.toString()); |
| | | return emitter; |
| | | } |
| | | else{ |