Browse Source

ai解析pdf的 个人征信

zrd 3 tháng trước cách đây
mục cha
commit
f75494e068

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

@@ -4,6 +4,7 @@ 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.framework.dict.core.DictFrameworkUtils;
 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;
@@ -133,6 +134,17 @@ public class AppAiController {
         return success(workflowRunService.getDifyResul(inputs, createReqVO.getType()));
     }
     
+    @PostMapping("/difyPdf")
+    @Operation(summary = "处理图片型pdf并返回前端")
+    public CommonResult<String> difyPdf(@RequestBody WorkflowDifyReqVO createReqVO) {
+        Map<String, Object> inputs = new HashMap<>();
+        String apiKey = DictFrameworkUtils.parseDictDataValue("ai_key", "pdf识别");
+        inputs.put("type", createReqVO.getType());
+        
+        inputs.put("pdfUrl", createReqVO.getDoc());
+        return success(workflowRunService.getDifyResul(inputs, createReqVO.getType(), apiKey));
+    }
+    
     /**
      * 上传新文件
      *

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

@@ -36,4 +36,6 @@ public interface WorkflowRunService {
      * @return {@link String }
      */
     String getDifyResul(Map<String, Object> inputs, String eventType);
+    
+    String getDifyResul(Map<String, Object> inputs, String eventType, String aiKey);
 }

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

@@ -152,6 +152,63 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
         return difyResponse;
     }
     
+    /**
+     * 获取dify响应
+     *
+     * @param inputs 输入
+     * @param user   用户
+     * @return {@link DifyResponse }
+     */
+    public DifyResponse getDifyResponse(Map<String, Object> inputs, String user, String apiKey) {
+        DifyResponse difyResponse = new DifyResponse();
+        // 接口地址
+        String url = baseUrl + "/v1/workflows/run";
+        // 替换为你的实际 API Key
+        
+        
+        // 创建一个 ObjectMapper 实例,用于处理 JSON 转换
+        
+        
+        // 构建请求体
+        JSONObject requestBody = new JSONObject();
+        requestBody.put("inputs", inputs);
+        requestBody.put("response_mode", "streaming");
+        requestBody.put("user", user);
+        
+        // 发送 POST 请求
+        HttpResponse response = HttpRequest.post(url)
+                .header("Authorization", "Bearer " + apiKey)
+                .header("Content-Type", "application/json")
+                .body(requestBody.toString())
+                .execute();
+        
+        
+        // 处理响应
+        if (response.isOk()) {
+            // Hutool工具示例
+            String decodedBody = Convert.unicodeToStr(response.body());
+            
+            // 使用 \n\n 分割字符串
+            String[] parts = decodedBody.split("\n\n");
+            
+            // 取最后一条内容
+            String lastPart = parts[parts.length - 1];
+            
+            System.out.println("请求成功,响应内容:");
+            System.out.println(lastPart);
+            // 去除"data: "前缀
+            String jsonStr = lastPart.substring(6);
+            
+            // 转换为DifyResponse实体
+            difyResponse = JSON.parseObject(jsonStr, DifyResponse.class);
+            System.out.println(difyResponse.getData().getOutputs().getText());
+        } else {
+            System.out.println("请求失败,状态码:" + response.getStatus());
+            System.out.println("错误信息:" + response.body());
+        }
+        return difyResponse;
+    }
+    
     @Override
     public FileResp uploadFile(String fileurl) {
         String extension = getFileExtension(fileurl);
@@ -172,4 +229,12 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
         DifyResponse response = getDifyResponse(inputs, SecurityFrameworkUtils.getLoginUserId().toString());
         return response.getData().getOutputs().getText();
     }
+    
+    @Override
+    public String getDifyResul(Map<String, Object> inputs, String eventType, String aiKey) {
+        // 创建 inputs 的 Map
+//        inputs.put("images", list);
+        DifyResponse response = getDifyResponse(inputs, SecurityFrameworkUtils.getLoginUserId().toString(), aiKey);
+        return response.getData().getOutputs().getText();
+    }
 }