| | |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.*; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| | | import tech.aiflowy.ai.config.DifyStreamClient; |
| | | import tech.aiflowy.ai.config.FileReference; |
| | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.math.BigInteger; |
| | | import java.util.*; |
| | | |
| | |
| | | @Autowired |
| | | private ObjectMapper objectMapper; |
| | | |
| | | // @PostMapping("chat") |
| | | // public SseEmitter chat2(@JsonBody(value = "prompt", required = true) String prompt, |
| | | // @JsonBody(value = "botId", required = true) BigInteger botId, |
| | | // @JsonBody(value = "sessionId", required = true) String sessionId, |
| | | // @JsonBody(value = "isExternalMsg") int isExternalMsg, |
| | | // @JsonBody("file") String file,//上传文件 |
| | | // HttpServletResponse response){ |
| | | // File file = |
| | | // } |
| | | |
| | | @PostMapping("chat") |
| | | public SseEmitter chat(@JsonBody(value = "prompt", required = true) String prompt, |
| | | @JsonBody(value = "botId", required = true) BigInteger botId, |
| | | @JsonBody(value = "sessionId", required = true) String sessionId, |
| | | @JsonBody(value = "isExternalMsg") int isExternalMsg, |
| | | @JsonBody(value = "file") String file, |
| | | @JsonBody(value = "file") String file,//上传文件 |
| | | HttpServletResponse response) { |
| | | response.setContentType("text/event-stream"); |
| | | AiBot aiBot = service.getById(botId); |
| | |
| | | |
| | | DifyStreamClient client = new DifyStreamClient(apiUrl, apiKey, aiBotMessageService); |
| | | DifyStreamClient uploadClient = new DifyStreamClient(aiBot.getModelAPI()+"/files/upload", apiKey, aiBotMessageService); |
| | | String test = uploadClient.fileUpload(SaTokenUtil.getLoginAccount().getId() + "", file); |
| | | System.out.println(test); |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | JsonObject fileJson = gson.fromJson(test, JsonObject.class); |
| | | String fileId = fileJson.get("id").getAsString(); |
| | | String fileId = file; |
| | | |
| | | // 2. 构建文件参数对象 |
| | | Map<String, Object> fileParam = new HashMap<>(); |
| | | fileParam.put("transfer_method", "local_file"); |
| | | fileParam.put("upload_file_id", fileId); |
| | | String[] split = file.split("\\."); |
| | | // fileParam.put("type", fileJson.get("extension").getAsString()); // 例如 "excel"、"pdf" 等 |
| | | fileParam.put("type", "document"); // 例如 "excel"、"pdf" 等 |
| | | |
| | |
| | | } |
| | | |
| | | @PostMapping("files/upload") |
| | | public Result filesUpload(@JsonBody(value = "botId", required = true) BigInteger botId, |
| | | String file, |
| | | public Result filesUpload(@RequestParam("botId") BigInteger botId, |
| | | @RequestParam("file") MultipartFile file, |
| | | HttpServletResponse response){ |
| | | try{ |
| | | String userId = SaTokenUtil.getLoginAccount().getId() + ""; |
| | |
| | | String apiUrl = aiBot.getModelAPI(); // 替换为实际API URL |
| | | String apiKey = aiBot.getModelKEY(); // 替换为实际API Key |
| | | DifyStreamClient client = new DifyStreamClient(apiUrl, apiKey, aiBotMessageService); |
| | | client.fileUpload(userId,file); |
| | | return Result.success("成功!"); |
| | | String s = client.fileUpload(userId, file); |
| | | return Result.success(s); |
| | | }catch (Exception e){ |
| | | return Result.fail(400, String.valueOf(e)); |
| | | } |