deason 1 mese fa
parent
commit
5760afe50d

+ 17 - 0
pom.xml

@@ -57,6 +57,23 @@
             <groupId>com.qmth.boot</groupId>
             <artifactId>tools-freemarker</artifactId>
         </dependency>
+
+        <!--<dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>tea-openapi</artifactId>
+            <version>0.2.5</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>docmind_api20220711</artifactId>
+            <version>2.0.3</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>credentials-java</artifactId>
+            <version>0.2.11</version>
+        </dependency>-->
+
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>

+ 130 - 0
src/main/java/com/qmth/ops/biz/ai/client/aliyun/doc/AliyunParseDocClient.java

@@ -0,0 +1,130 @@
+// package com.qmth.ops.biz.ai.client.aliyun.doc;
+//
+// import com.aliyun.docmind_api20220711.Client;
+// import com.aliyun.docmind_api20220711.models.*;
+// import com.aliyun.teaopenapi.models.Config;
+// import com.aliyun.teautil.models.RuntimeOptions;
+// import com.fasterxml.jackson.databind.json.JsonMapper;
+// import com.qmth.boot.core.ai.model.doc.ParseDocTask;
+// import com.qmth.boot.core.ai.model.doc.ParseDocTaskResult;
+// import com.qmth.boot.core.ai.model.doc.ParseDocTaskStatus;
+// import com.qmth.boot.core.exception.StatusException;
+// import com.qmth.boot.tools.codec.CodecUtils;
+// import com.qmth.ops.biz.ai.client.DocApiClient;
+// import com.qmth.ops.biz.ai.client.DocApiConfig;
+// import com.qmth.ops.biz.ai.exception.DocRateLimitExceeded;
+// import org.slf4j.Logger;
+// import org.slf4j.LoggerFactory;
+//
+// import java.io.ByteArrayInputStream;
+//
+// public class AliyunParseDocClient extends DocApiClient {
+//
+//     private static final Logger log = LoggerFactory.getLogger(AliyunParseDocClient.class);
+//
+//     public AliyunParseDocClient(DocApiConfig config) {
+//         super(config);
+//     }
+//
+//     @Override
+//     public ParseDocTask parseDocTask(byte[] fileData, String fileName) throws Exception {
+//         if (getQueryRateLimiter() != null && !getQueryRateLimiter().acquire()) {
+//             throw new DocRateLimitExceeded(getConfig().getQps());
+//         }
+//
+//         Config config = new Config().setEndpoint(getConfig().getUrl())
+//                 .setAccessKeyId(getConfig().getKey())
+//                 .setAccessKeySecret(getConfig().getSecret());
+//
+//         RuntimeOptions runtimeOptions = new RuntimeOptions();
+//         runtimeOptions.connectTimeout = 10000;
+//         runtimeOptions.readTimeout = 50000;
+//
+//         Client client = new Client(config);
+//         SubmitDocParserJobAdvanceRequest request = new SubmitDocParserJobAdvanceRequest();
+//         request.fileUrlObject = new ByteArrayInputStream(fileData);
+//         request.fileName = CodecUtils.urlEncode(fileName);
+//
+//         try {
+//             SubmitDocParserJobResponse response = client.submitDocParserJobAdvance(request, runtimeOptions);
+//             SubmitDocParserJobResponseBody body = response.getBody();
+//
+//             if (response.getStatusCode() == 200 && body.getData() != null) {
+//                 return new ParseDocTask(encodeTaskId(body.getData().getId()));
+//             }
+//
+//             String respBodyStr = new JsonMapper().writeValueAsString(body);
+//             log.warn("responseCode:{} {}", response.getStatusCode(), respBodyStr);
+//         } catch (Exception e) {
+//             log.error("Aliyun parseDocTask fail. {}", e.getMessage(), e);
+//         }
+//         throw new StatusException("请求文档解析任务失败");
+//     }
+//
+//     @Override
+//     public ParseDocTaskResult parseDocTaskQuery(String taskId) throws Exception {
+//         if (getQueryRateLimiter() != null && !getQueryRateLimiter().acquire()) {
+//             throw new DocRateLimitExceeded(getConfig().getQps());
+//         }
+//         taskId = decodeTaskId(taskId)[2];
+//
+//         Config config = new Config().setEndpoint(getConfig().getUrl())
+//                 .setAccessKeyId(getConfig().getKey())
+//                 .setAccessKeySecret(getConfig().getSecret());
+//
+//         Client client = new Client(config);
+//         QueryDocParserStatusRequest request = new QueryDocParserStatusRequest();
+//         request.id = taskId;
+//
+//         try {
+//             QueryDocParserStatusResponse response = client.queryDocParserStatus(request);
+//             QueryDocParserStatusResponseBody body = response.getBody();
+//
+//             if (response.getStatusCode() == 200 && body.getData() != null) {
+//                 ParseDocTaskResult result = new ParseDocTaskResult();
+//                 if ("success".equals(body.getData().getStatus())) {
+//                     result.setStatus(ParseDocTaskStatus.SUCCESS);
+//
+//                     GetDocParserResultRequest resultRequest = new GetDocParserResultRequest();
+//                     resultRequest.id = taskId;
+//                     resultRequest.layoutNum = 0;
+//                     resultRequest.layoutStepSize = 10;
+//                     GetDocParserResultResponse resultResponse = client.getDocParserResult(resultRequest);
+//                     if (resultResponse.getStatusCode() == 200 && resultResponse.getBody().getData() != null) {
+//                         result.setContent(resultResponse.getBody().getData().get("markdownContent").toString());
+//                     }
+//                 } else if ("Fail".equals(body.getData().getStatus())) {
+//                     result.setStatus(ParseDocTaskStatus.FAILED);
+//                 } else {
+//                     result.setStatus(ParseDocTaskStatus.PROCESSING);
+//                 }
+//                 return result;
+//             }
+//
+//             String respBodyStr = new JsonMapper().writeValueAsString(body);
+//             log.warn("responseCode:{} {}", response.getStatusCode(), respBodyStr);
+//         } catch (Exception e) {
+//             log.error("Aliyun parseDocTaskQuery fail. {}", e.getMessage(), e);
+//         }
+//         throw new StatusException("查询文档解析结果失败");
+//     }
+//
+//     public static void main(String[] args) throws Exception {
+//         DocApiConfig config = new DocApiConfig();
+//         config.setUrl("docmind-api.cn-hangzhou.aliyuncs.com");
+//         config.setKey("xxx");
+//         config.setSecret("xxx");
+//         config.setQps(10);
+//         config.setSupplier("1@@aliyun.doc.AliyunParseDocClient");
+//         DocApiClient client = new AliyunParseDocClient(config);
+//
+//         // File file = new File("D:\\home\\大纲.pdf");
+//         // byte[] fileData = ByteArray.fromFile(file).value();
+//         // System.out.println(client.parseDocTask(fileData, file.getName()).getTaskId());
+//
+//         ParseDocTaskResult result = client.parseDocTaskQuery("M0BAYmFpZHUuZG9jLkJhaWR1UGFyc2VEb2NDbGllbnRAQHRhc2stdnVrV1RHZnNSbFNJdDFBaDNsRUxqNzRRczJzdXNhOXk=");
+//         System.out.println(result.getStatus());
+//         System.out.println(result.getContent());
+//     }
+//
+// }

+ 7 - 9
src/main/java/com/qmth/ops/biz/ai/client/baidu/doc/BaiduParseDocClient.java

@@ -43,7 +43,6 @@ public class BaiduParseDocClient extends DocApiClient {
         try (Response response = super.getClient().newCall(request).execute()) {
             ResponseBody respBody = response.body();
             String respBodyStr = respBody != null ? respBody.string() : "";
-            log.info(respBodyStr);
 
             if (response.isSuccessful()) {
                 BaiduParseDocTaskResp resp = new ObjectMapper().readValue(respBodyStr, BaiduParseDocTaskResp.class);
@@ -52,11 +51,11 @@ public class BaiduParseDocClient extends DocApiClient {
                 }
             }
 
-            throw new StatusException(respBodyStr);
+            log.warn("responseCode:{} {}", response.code(), respBodyStr);
         } catch (Exception e) {
-            log.error("Baidu parse doc error. {}", e.getMessage());
-            throw new StatusException(e.getMessage(), e);
+            log.error("Baidu parseDocTask fail. {}", e.getMessage(), e);
         }
+        throw new StatusException("请求文档解析任务失败");
     }
 
     @Override
@@ -74,7 +73,6 @@ public class BaiduParseDocClient extends DocApiClient {
         try (Response response = super.getClient().newCall(request).execute()) {
             ResponseBody respBody = response.body();
             String respBodyStr = respBody != null ? respBody.string() : "";
-            log.info(respBodyStr);
 
             if (response.isSuccessful()) {
                 BaiduParseDocTaskQueryResp resp = new ObjectMapper().readValue(respBodyStr, BaiduParseDocTaskQueryResp.class);
@@ -93,11 +91,11 @@ public class BaiduParseDocClient extends DocApiClient {
                 }
             }
 
-            throw new StatusException(respBodyStr);
+            log.warn("responseCode:{} {}", response.code(), respBodyStr);
         } catch (Exception e) {
-            log.error("Baidu parse doc query error. {}", e.getMessage());
-            throw new StatusException(e.getMessage(), e);
+            log.error("Baidu parseDocTaskQuery fail. {}", e.getMessage(), e);
         }
+        throw new StatusException("查询文档解析结果失败");
     }
 
     private Request buildRequest(String url, FormBody.Builder formBuilder) {
@@ -122,7 +120,7 @@ public class BaiduParseDocClient extends DocApiClient {
         config.setKey("xxx");
         config.setSecret("xxx");
         config.setQps(10);
-        config.setSupplier("3@@baidu.doc.BaiduParseDocClient");
+        config.setSupplier("1@@baidu.doc.BaiduParseDocClient");
         DocApiClient client = new BaiduParseDocClient(config);
 
         // File file = new File("D:\\home\\大纲.pdf");