Browse Source

图片解析ai

zrd 4 months ago
parent
commit
9cc204d2a6

+ 176 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/aipromptmanagement/AppAiController.java

@@ -0,0 +1,176 @@
+package cn.iocoder.yudao.module.infra.controller.admin.aipromptmanagement;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.iocoder.yudao.framework.common.pojo.CommonResult;
+import cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo.WorkflowDifyFilesReqVO;
+import cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo.WorkflowDifyReqVO;
+import cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo.WorkflowRunFileReqVO;
+import cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo.WorkflowRunSaveReqVO;
+import cn.iocoder.yudao.module.infra.service.aipromptmanagement.WorkflowRunService;
+import cn.iocoder.yudao.module.infra.service.aipromptmanagement.utils.DifyFilesUtils;
+import cn.iocoder.yudao.module.infra.service.aipromptmanagement.utils.FileResp;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import jakarta.annotation.Resource;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
+
+@Tag(name = "管理平台 - dify 工作流")
+@RestController
+@RequestMapping("/infra/ai-dify")
+@Validated
+public class AppAiController {
+    @Resource
+    private WorkflowRunService workflowRunService;
+    
+    public static File convert(MultipartFile multipartFile) throws IOException {
+        // 获取文件的原始文件名
+        String originalFilename = multipartFile.getOriginalFilename();
+        // 使用 Hutool 的 FileUtil 工具类创建临时文件
+        File tempFile = FileUtil.createTempFile(new File(originalFilename));
+        // 将 MultipartFile 中的内容复制到临时文件中
+        multipartFile.transferTo(tempFile);
+        return tempFile;
+    }
+    
+    @PostMapping("/getDifyResul")
+    @Operation(summary = "获取工作流结果")
+    @PreAuthorize("@ss.hasPermission('infra:ai-prompt-management:query')")
+    public CommonResult<String> createChatConversationMy(@RequestBody WorkflowRunSaveReqVO createReqVO) {
+        
+        return success(workflowRunService.getDifyResul(createReqVO.getInputs(), createReqVO.getEventType()));
+    }
+    
+    
+    @PostMapping("/ocrResul")
+    @Operation(summary = "获取图片识别工作流结果")
+    @PreAuthorize("@ss.hasPermission('infra:ai-prompt-management:query')")
+    public CommonResult<String> ocrResul(@RequestBody WorkflowRunSaveReqVO createReqVO) {
+        FileResp fileResp = workflowRunService.uploadFile(createReqVO.getFileUrl());
+        Map<String, Object> inputs = new HashMap<>();
+        inputs.put("images", DifyFilesUtils.getVariableValue(fileResp));
+        inputs.put("type", createReqVO.getEventType());
+        return success(workflowRunService.getDifyResul(inputs, createReqVO.getEventType()));
+    }
+    
+    @PostMapping("/difyOrc")
+    @Operation(summary = "图片文档识别并返回前端")
+    @PreAuthorize("@ss.hasPermission('infra:ai-prompt-management:query')")
+    public CommonResult<String> difyOrc(@RequestBody WorkflowDifyReqVO createReqVO) {
+        Map<String, Object> inputs = new HashMap<>();
+      /*  if (StrUtil.isNotBlank(createReqVO.getImage())) {
+            inputs.put("doc", createReqVO.getDoc());
+            FileResp fileResp = workflowRunService.uploadFile(createReqVO.getImage());
+            
+            inputs.put("images", DifyFilesUtils.getVariableValue(fileResp));
+        }
+        if (StrUtil.isNotBlank(createReqVO.getDoc())) {
+            FileResp docResp = workflowRunService.uploadFile(createReqVO.getDoc());
+            
+            inputs.put("docs", DifyFilesUtils.getVariableValue(docResp));
+        }*/
+        
+        if (StrUtil.isNotBlank(createReqVO.getImage())) {
+            List<Map<String, Object>> images = new ArrayList<>();
+            Map<String, Object> variableValue = new HashMap<>();
+            variableValue.put("transfer_method", "remote_url");
+            variableValue.put("url", createReqVO.getImage());
+            variableValue.put("type", "image");
+            images.add(variableValue);
+            inputs.put("images", images);
+        }
+        if (StrUtil.isNotBlank(createReqVO.getDoc())) {
+            List<Map<String, Object>> docs = new ArrayList<>();
+            Map<String, Object> variableValue = new HashMap<>();
+            variableValue.put("transfer_method", "remote_url");
+            variableValue.put("url", createReqVO.getDoc());
+            variableValue.put("type", "document");
+            docs.add(variableValue);
+            inputs.put("images", docs);
+        }
+        inputs.put("type", createReqVO.getType());
+        return success(workflowRunService.getDifyResul(inputs, createReqVO.getType()));
+    }
+    
+    @PostMapping("/difyOrcs")
+    @Operation(summary = "批量图片文档识别并返回前端")
+    @PreAuthorize("@ss.hasPermission('infra:ai-prompt-management:query')")
+    public CommonResult<String> difyOrcs(@RequestBody WorkflowDifyFilesReqVO createReqVO) {
+        Map<String, Object> inputs = new HashMap<>();
+        if (CollUtil.isNotEmpty(createReqVO.getImages())) {
+            List<Map<String, Object>> images = new ArrayList<>();
+            for (String image : createReqVO.getImages()) {
+                Map<String, Object> variableValue = new HashMap<>();
+                variableValue.put("transfer_method", "remote_url");
+                variableValue.put("url", image);
+                variableValue.put("type", "image");
+                images.add(variableValue);
+            }
+            inputs.put("images", images);
+        }
+        if (CollUtil.isNotEmpty(createReqVO.getDocs())) {
+            List<Map<String, Object>> docs = new ArrayList<>();
+            for (String image : createReqVO.getDocs()) {
+                Map<String, Object> variableValue = new HashMap<>();
+                variableValue.put("transfer_method", "remote_url");
+                variableValue.put("url", image);
+                variableValue.put("type", "document");
+                docs.add(variableValue);
+            }
+            inputs.put("docs", docs);
+        }
+        
+        
+        inputs.put("type", createReqVO.getType());
+        return success(workflowRunService.getDifyResul(inputs, createReqVO.getType()));
+    }
+    
+    /**
+     * 上传新文件
+     *
+     * @param files 文件
+     * @return {@link CommonResult }<{@link List }<{@link FileNewUploadReqVO }>>
+     * @throws Exception 例外
+     */
+    @PostMapping("/upload")
+    @Operation(summary = "上传文件")
+    @PreAuthorize("@ss.hasPermission('infra:ai-prompt-management:query')")
+    public CommonResult<List<FileResp>> uploadNewFile(@RequestParam(value = "file", required = false) MultipartFile[] files) throws Exception {
+        List<FileResp> resList = new ArrayList<>();
+        for (MultipartFile file : files) {
+            
+            resList.add(workflowRunService.uploadFile(convert(file)));
+        }
+        return success(resList);
+    }
+    
+    
+    /**
+     * 上传新文件
+     *
+     * @param files 文件
+     * @return {@link CommonResult }<{@link List }<{@link FileNewUploadReqVO }>>
+     * @throws Exception 例外
+     */
+    @PostMapping("/uploadByUrl")
+    @Operation(summary = "上传文件")
+    @PreAuthorize("@ss.hasPermission('infra:ai-prompt-management:query')")
+    public CommonResult<FileResp> uploadNewFile(@RequestBody WorkflowRunFileReqVO files) {
+        
+        return success(workflowRunService.uploadFile(files.getUrl()));
+    }
+    
+}