Selaa lähdekoodia

图片解析ai

zrd 4 kuukautta sitten
vanhempi
sitoutus
9625c0ca5f

+ 77 - 3
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/app/aipromptmanagement/AppAiController.java

@@ -1,7 +1,11 @@
 package cn.iocoder.yudao.module.infra.controller.app.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;
@@ -52,13 +56,83 @@ public class AppAiController {
     @PostMapping("/ocrResul")
     @Operation(summary = "获取图片识别工作流结果")
     public CommonResult<String> ocrResul(@RequestBody WorkflowRunSaveReqVO createReqVO) {
-        FileResp fileResp = workflowRunService.uploadFile(createReqVO.getFileUrl(), createReqVO.getEventType());
+        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 = "图片文档识别并返回前端")
+    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 = "批量图片文档识别并返回前端")
+    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()));
+    }
+    
     /**
      * 上传新文件
      *
@@ -72,7 +146,7 @@ public class AppAiController {
         List<FileResp> resList = new ArrayList<>();
         for (MultipartFile file : files) {
             
-            resList.add(workflowRunService.uploadFile(convert(file), "uploadNew"));
+            resList.add(workflowRunService.uploadFile(convert(file)));
         }
         return success(resList);
     }
@@ -89,7 +163,7 @@ public class AppAiController {
     @Operation(summary = "上传文件")
     public CommonResult<FileResp> uploadNewFile(@RequestBody WorkflowRunFileReqVO files) {
         
-        return success(workflowRunService.uploadFile(files.getUrl(), files.getEventType()));
+        return success(workflowRunService.uploadFile(files.getUrl()));
     }
     
 }

+ 27 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/app/aipromptmanagement/vo/WorkflowDifyFilesReqVO.java

@@ -0,0 +1,27 @@
+package cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.util.List;
+
+@Schema(description = "管理后台 - 工作流运行记录新增/修改 Request VO")
+@Data
+public class WorkflowDifyFilesReqVO {
+    
+    
+    /**
+     * 事件类型
+     */
+    private String type;
+    /**
+     * 文件url
+     */
+    private List<String> images;
+    
+    /**
+     * doc
+     */
+    private List<String> docs;
+    
+}

+ 25 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/app/aipromptmanagement/vo/WorkflowDifyReqVO.java

@@ -0,0 +1,25 @@
+package cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+@Schema(description = "管理后台 - 工作流运行记录新增/修改 Request VO")
+@Data
+public class WorkflowDifyReqVO {
+    
+    
+    /**
+     * 事件类型
+     */
+    private String type;
+    /**
+     * 文件url
+     */
+    private String image;
+    
+    /**
+     * doc
+     */
+    private String doc;
+    
+}

+ 2 - 2
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/aipromptmanagement/WorkflowRunService.java

@@ -18,7 +18,7 @@ public interface WorkflowRunService {
      * @param url url
      * @return {@link FileResp }
      */
-    FileResp uploadFile(String url, String eventType);
+    FileResp uploadFile(String url);
     
     /**
      * 上传文件
@@ -26,7 +26,7 @@ public interface WorkflowRunService {
      * @param url url
      * @return {@link FileResp }
      */
-    FileResp uploadFile(File file, String eventType);
+    FileResp uploadFile(File file);
     
     
     /**

+ 6 - 6
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/aipromptmanagement/WorkflowRunServiceImpl.java

@@ -5,6 +5,7 @@ import cn.hutool.core.util.IdUtil;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
 import cn.hutool.json.JSONObject;
+import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
 import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
 import cn.iocoder.yudao.module.infra.service.aipromptmanagement.utils.DifyFilesUtils;
 import cn.iocoder.yudao.module.infra.service.aipromptmanagement.utils.DifyResponse;
@@ -73,7 +74,7 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
         // 接口地址
         String url = baseUrl + "/v1/files/upload";
         // 替换为你的实际 API Key
-        String apiKey = ocrKey;
+        String apiKey = DictFrameworkUtils.parseDictDataValue("ai_key", "图文识别");
         
         // 发送 POST 请求进行文件上传
         HttpResponse response = HttpRequest.post(url)
@@ -106,8 +107,7 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
         // 接口地址
         String url = baseUrl + "/v1/workflows/run";
         // 替换为你的实际 API Key
-        String apiKey = ocrKey;
-        
+        String apiKey = DictFrameworkUtils.parseDictDataValue("ai_key", "图文识别");
         
         // 创建一个 ObjectMapper 实例,用于处理 JSON 转换
         
@@ -153,7 +153,7 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
     }
     
     @Override
-    public FileResp uploadFile(String fileurl, String eventType) {
+    public FileResp uploadFile(String fileurl) {
         String extension = getFileExtension(fileurl);
         
         return uploadFileDify(DifyFilesUtils.downloadFile(fileurl, filePath +
@@ -161,7 +161,7 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
     }
     
     @Override
-    public FileResp uploadFile(File file, String eventType) {
+    public FileResp uploadFile(File file) {
         return uploadFileDify(file, SecurityFrameworkUtils.getLoginUserId().toString());
     }
     
@@ -170,6 +170,6 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
         // 创建 inputs 的 Map
 //        inputs.put("images", list);
         DifyResponse response = getDifyResponse(inputs, SecurityFrameworkUtils.getLoginUserId().toString());
-        return response.getData().getOutputs().getOutput();
+        return response.getData().getOutputs().getText();
     }
 }

+ 19 - 55
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/aipromptmanagement/utils/DifyResponse.java

@@ -6,17 +6,12 @@ public class DifyResponse {
     
     /**
      * event : workflow_finished
-     * workflow_run_id : bad54afd-3650-4b24-86e6-c625fcc600cb
-     * task_id : 15712ca3-3350-4cd7-a0ad-c1d53350c9a5
-     * data : {"id":"bad54afd-3650-4b24-86e6-c625fcc600cb","workflow_id":"26b654ed-c5eb-498a-8f28-c303dcf3349b",
-     * "sequence_number":24,"status":"succeeded","outputs":{"text":"INSERT INTO `ruoyi-vue-pro`.`game_character_info`
-     * (player_name, level, combat_power, profession) VALUES('小杰', '8转216级', 40261, '道士'),('小沐沐', '8转200级', 30111,
-     * '法师'),('天命', '8转199级', 30384, '道士'),('司令', '8转197级', 24999, '法师'),('祖承泽', '7转185级', 21333, '法师'),('tx小道',
-     * '7转168级', 6792, '道士'),('S5563.神经侠侣', '8转188级', 21368, '法师'),('天~虎', '8转196级', 23122, '法师'),('王者', '7转174级',
-     * 15664, '战士'),('平凡爱人', '7转179级', 12249, '法师'),('没穿内裤', '8转225级', 49935, '法师'),('S5596.蜡笔小新', '8转187级', 22172,
-     * '法师'),('S5576.太阳的心', '7转183级', 19011, '法师');"},"error":null,"elapsed_time":16.629782980307937,
-     * "total_tokens":1773,"total_steps":4,"created_by":{"id":"8e1c763e-5dd2-4ccb-a633-eee77b10f9e9","user":"abc-123"
-     * },"created_at":1741768563,"finished_at":1741768580,"exceptions_count":0,"files":[]}
+     * workflow_run_id : e633cad4-37a4-4d58-8d1e-f13c616bd3da
+     * task_id : 6d4f4242-21ab-4d9e-a5ee-02e464db2bbf
+     * data : {"id":"e633cad4-37a4-4d58-8d1e-f13c616bd3da","workflow_id":"d2a906b7-f195-4902-98d3-636c6b240ef7",
+     * "sequence_number":20,"status":"succeeded","outputs":{"text":"21322312321"},"error":null,"elapsed_time":6
+     * .21334216100513,"total_tokens":1515,"total_steps":7,"created_by":{"id":"74e67587-f99b-483d-8d98-96f7cc6f1f80",
+     * "user":"286"},"created_at":1744874006,"finished_at":1744874012,"exceptions_count":0,"files":[]}
      */
     
     private String event;
@@ -24,18 +19,6 @@ public class DifyResponse {
     private String task_id;
     private DataBean data;
     
-    // {"event": "workflow_finished", "workflow_run_id": "bad54afd-3650-4b24-86e6-c625fcc600cb", "task_id":
-    // "15712ca3-3350-4cd7-a0ad-c1d53350c9a5", "data": {"id": "bad54afd-3650-4b24-86e6-c625fcc600cb", "workflow_id":
-    // "26b654ed-c5eb-498a-8f28-c303dcf3349b", "sequence_number": 24, "status": "succeeded", "outputs": {"text":
-    // "INSERT INTO `ruoyi-vue-pro`.`game_character_info` (player_name, level, combat_power, profession) VALUES('小杰',
-    // '8转216级', 40261, '道士'),('小沐沐', '8转200级', 30111, '法师'),('天命', '8转199级', 30384, '道士'),('司令', '8转197级', 24999,
-    // '法师'),('祖承泽', '7转185级', 21333, '法师'),('tx小道', '7转168级', 6792, '道士'),('S5563.神经侠侣', '8转188级', 21368, '法师'),
-    // ('天~虎', '8转196级', 23122, '法师'),('王者', '7转174级', 15664, '战士'),('平凡爱人', '7转179级', 12249, '法师'),('没穿内裤',
-    // '8转225级', 49935, '法师'),('S5596.蜡笔小新', '8转187级', 22172, '法师'),('S5576.太阳的心', '7转183级', 19011, '法师');"},
-    // "error": null, "elapsed_time": 16.629782980307937, "total_tokens": 1773, "total_steps": 4, "created_by":
-    // {"id": "8e1c763e-5dd2-4ccb-a633-eee77b10f9e9", "user": "abc-123"}, "created_at": 1741768563, "finished_at":
-    // 1741768580, "exceptions_count": 0, "files": []}}
-    
     public String getEvent() {return event;}
     
     public void setEvent(String event) {this.event = event;}
@@ -54,23 +37,18 @@ public class DifyResponse {
     
     public static class DataBean {
         /**
-         * id : bad54afd-3650-4b24-86e6-c625fcc600cb
-         * workflow_id : 26b654ed-c5eb-498a-8f28-c303dcf3349b
-         * sequence_number : 24
+         * id : e633cad4-37a4-4d58-8d1e-f13c616bd3da
+         * workflow_id : d2a906b7-f195-4902-98d3-636c6b240ef7
+         * sequence_number : 20
          * status : succeeded
-         * outputs : {"text":"INSERT INTO `ruoyi-vue-pro`.`game_character_info` (player_name, level, combat_power,
-         * profession) VALUES('小杰', '8转216级', 40261, '道士'),('小沐沐', '8转200级', 30111, '法师'),('天命', '8转199级', 30384,
-         * '道士'),('司令', '8转197级', 24999, '法师'),('祖承泽', '7转185级', 21333, '法师'),('tx小道', '7转168级', 6792, '道士'),('S5563
-         * .神经侠侣', '8转188级', 21368, '法师'),('天~虎', '8转196级', 23122, '法师'),('王者', '7转174级', 15664, '战士'),('平凡爱人',
-         * '7转179级', 12249, '法师'),('没穿内裤', '8转225级', 49935, '法师'),('S5596.蜡笔小新', '8转187级', 22172, '法师'),('S5576
-         * .太阳的心', '7转183级', 19011, '法师');"}
+         * outputs : {"text":"21322312321"}
          * error : null
-         * elapsed_time : 16.629782980307937
-         * total_tokens : 1773
-         * total_steps : 4
-         * created_by : {"id":"8e1c763e-5dd2-4ccb-a633-eee77b10f9e9","user":"abc-123"}
-         * created_at : 1741768563
-         * finished_at : 1741768580
+         * elapsed_time : 6.21334216100513
+         * total_tokens : 1515
+         * total_steps : 7
+         * created_by : {"id":"74e67587-f99b-483d-8d98-96f7cc6f1f80","user":"286"}
+         * created_at : 1744874006
+         * finished_at : 1744874012
          * exceptions_count : 0
          * files : []
          */
@@ -148,24 +126,10 @@ public class DifyResponse {
         
         public static class OutputsBean {
             /**
-             * text : INSERT INTO `ruoyi-vue-pro`.`game_character_info` (player_name, level, combat_power,
-             * profession) VALUES('小杰', '8转216级', 40261, '道士'),('小沐沐', '8转200级', 30111, '法师'),('天命', '8转199级', 30384,
-             * '道士'),('司令', '8转197级', 24999, '法师'),('祖承泽', '7转185级', 21333, '法师'),('tx小道', '7转168级', 6792, '道士'),
-             * ('S5563.神经侠侣', '8转188级', 21368, '法师'),('天~虎', '8转196级', 23122, '法师'),('王者', '7转174级', 15664, '战士'),
-             * ('平凡爱人', '7转179级', 12249, '法师'),('没穿内裤', '8转225级', 49935, '法师'),('S5596.蜡笔小新', '8转187级', 22172, '法师'),
-             * ('S5576.太阳的心', '7转183级', 19011, '法师');
+             * text : 21322312321
              */
             
             private String text;
-            private String output;
-            
-            public String getOutput() {
-                return output;
-            }
-            
-            public void setOutput(String output) {
-                this.output = output;
-            }
             
             public String getText() {return text;}
             
@@ -174,8 +138,8 @@ public class DifyResponse {
         
         public static class CreatedByBean {
             /**
-             * id : 8e1c763e-5dd2-4ccb-a633-eee77b10f9e9
-             * user : abc-123
+             * id : 74e67587-f99b-483d-8d98-96f7cc6f1f80
+             * user : 286
              */
             
             private String id;

+ 49 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/aipromptmanagement/utils/DifyRess.java

@@ -0,0 +1,49 @@
+package cn.iocoder.yudao.module.infra.service.aipromptmanagement.utils;
+
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@NoArgsConstructor
+@Data
+public class DifyRess {
+    
+    
+    private String event;
+    private String workflow_run_id;
+    private String task_id;
+    private DataBean data;
+    
+    @NoArgsConstructor
+    @Data
+    public static class DataBean {
+        private String id;
+        private String workflow_id;
+        private int sequence_number;
+        private String status;
+        private OutputsBean outputs;
+        private Object error;
+        private double elapsed_time;
+        private int total_tokens;
+        private int total_steps;
+        private CreatedByBean created_by;
+        private int created_at;
+        private int finished_at;
+        private int exceptions_count;
+        private List<?> files;
+        
+        @NoArgsConstructor
+        @Data
+        public static class OutputsBean {
+            private String text;
+        }
+        
+        @NoArgsConstructor
+        @Data
+        public static class CreatedByBean {
+            private String id;
+            private String user;
+        }
+    }
+}