Commit 333130e4 authored by alex yao's avatar alex yao

style: Text In Client

parent c0df8f2d
...@@ -8,7 +8,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.function.entity.FunctionLLMConfi ...@@ -8,7 +8,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.function.entity.FunctionLLMConfi
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Parameters; import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Parameters;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Properties; import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Properties;
import cn.com.poc.thirdparty.resource.demand.ai.function.extraction.entity.KeyInfo; import cn.com.poc.thirdparty.resource.demand.ai.function.extraction.entity.KeyInfo;
import cn.com.poc.thirdparty.resource.demand.ai.function.text_in_pdf2md.api.TextInClient; import cn.com.poc.thirdparty.resource.textin.api.TextInClient;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONException; import cn.hutool.json.JSONException;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
...@@ -19,13 +19,17 @@ import java.util.ArrayList; ...@@ -19,13 +19,17 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* 合同关键信息抽取-要素提取
*
* @author alex.yao * @author alex.yao
* @date 2025/5/12 * @date 2025/5/12
*/ */
@Component @Component
public class ContractExtractionFunction extends AbstractLargeModelFunction { public class ContractExtractionFunction extends AbstractLargeModelFunction {
private String DESC = "合同关键信息抽取"; private final String DESC = "合同关键信息抽取";
private final TextInClient textInClient = new TextInClient();
private final FunctionLLMConfig functionLLMConfig = new FunctionLLMConfig.FunctionLLMConfigBuilder() private final FunctionLLMConfig functionLLMConfig = new FunctionLLMConfig.FunctionLLMConfigBuilder()
.name("contract_extraction") .name("contract_extraction")
...@@ -85,8 +89,6 @@ public class ContractExtractionFunction extends AbstractLargeModelFunction { ...@@ -85,8 +89,6 @@ public class ContractExtractionFunction extends AbstractLargeModelFunction {
} }
keyInfos.add(keyInfo); keyInfos.add(keyInfo);
} }
TextInClient textInClient = new TextInClient();
String extraction = textInClient.extraction(fileUrl, keyInfos); String extraction = textInClient.extraction(fileUrl, keyInfos);
result.setFunctionResult(extraction); result.setFunctionResult(extraction);
result.setPromptContent(extraction); result.setPromptContent(extraction);
......
...@@ -7,7 +7,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractLargeModelFunct ...@@ -7,7 +7,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractLargeModelFunct
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.FunctionLLMConfig; import cn.com.poc.thirdparty.resource.demand.ai.function.entity.FunctionLLMConfig;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Parameters; import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Parameters;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Properties; import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Properties;
import cn.com.poc.thirdparty.resource.demand.ai.function.text_in_pdf2md.api.TextInClient; import cn.com.poc.thirdparty.resource.textin.api.TextInClient;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
...@@ -64,7 +64,8 @@ public class PdfToMDFunction extends AbstractLargeModelFunction { ...@@ -64,7 +64,8 @@ public class PdfToMDFunction extends AbstractLargeModelFunction {
options.put("parse_mode", "auto"); options.put("parse_mode", "auto");
options.put("table_flavor", "md"); options.put("table_flavor", "md");
try { try {
String response = TextInClient.recognize(fileContent, options); TextInClient textInClient = new TextInClient();
String response = textInClient.OCR(fileContent, options);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(response); JsonNode jsonNode = mapper.readTree(response);
if (jsonNode.has("result") && jsonNode.get("result").has("markdown")) { if (jsonNode.has("result") && jsonNode.get("result").has("markdown")) {
......
package cn.com.poc.thirdparty.resource.demand.ai.function.text_in_pdf2md.api; package cn.com.poc.thirdparty.resource.textin.api;
/** /**
* @author alex.yao * @author alex.yao
...@@ -32,20 +32,27 @@ import java.util.Map; ...@@ -32,20 +32,27 @@ import java.util.Map;
public class TextInClient { public class TextInClient {
final private static Logger logger = LoggerFactory.getLogger(TextInClient.class); final private Logger logger = LoggerFactory.getLogger(TextInClient.class);
private static final String appId = "dafd04a574230c00ccba61132160de0c"; private String appId = "dafd04a574230c00ccba61132160de0c";
private static final String secretCode = "3bc03c7e6f9402963e6e71d16d786a9c"; private String secretCode = "3bc03c7e6f9402963e6e71d16d786a9c";
public TextInClient() { public TextInClient() {
} }
public TextInClient(String appId, String secretCode) {
this.appId = appId;
this.secretCode = secretCode;
}
/** /**
* ocr
*
* @param fileContent * @param fileContent
* @param options * @param options
* @return * @return
* @throws IOException * @throws IOException
*/ */
public static String recognize(byte[] fileContent, HashMap<String, Object> options) throws IOException { public String OCR(byte[] fileContent, HashMap<String, Object> options) throws IOException {
StringBuilder queryParams = new StringBuilder(); StringBuilder queryParams = new StringBuilder();
for (Map.Entry<String, Object> entry : options.entrySet()) { for (Map.Entry<String, Object> entry : options.entrySet()) {
if (queryParams.length() > 0) { if (queryParams.length() > 0) {
...@@ -55,7 +62,7 @@ public class TextInClient { ...@@ -55,7 +62,7 @@ public class TextInClient {
.append("=") .append("=")
.append(URLEncoder.encode(entry.getValue().toString(), "UTF-8")); .append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
} }
HttpURLConnection connection = getRecoGinzeHttpURLConnection(queryParams); HttpURLConnection connection = getOCRHttpURLConnection(queryParams);
try (OutputStream os = connection.getOutputStream()) { try (OutputStream os = connection.getOutputStream()) {
os.write(fileContent); os.write(fileContent);
os.flush(); os.flush();
...@@ -79,7 +86,7 @@ public class TextInClient { ...@@ -79,7 +86,7 @@ public class TextInClient {
} }
private static HttpURLConnection getRecoGinzeHttpURLConnection(StringBuilder queryParams) throws IOException { private HttpURLConnection getOCRHttpURLConnection(StringBuilder queryParams) throws IOException {
String baseUrl = "https://api.textin.com/ai/service/v1/pdf_to_markdown"; String baseUrl = "https://api.textin.com/ai/service/v1/pdf_to_markdown";
String fullUrl = baseUrl + (queryParams.length() > 0 ? "?" + queryParams : ""); String fullUrl = baseUrl + (queryParams.length() > 0 ? "?" + queryParams : "");
URL url = new URL(fullUrl); URL url = new URL(fullUrl);
...@@ -102,7 +109,7 @@ public class TextInClient { ...@@ -102,7 +109,7 @@ public class TextInClient {
* @return * @return
*/ */
public static String extraction(String fileUrl, List<KeyInfo> keyInfoList) { public String extraction(String fileUrl, List<KeyInfo> keyInfoList) {
try { try {
// 读取文件并将其转换为Base64编码 // 读取文件并将其转换为Base64编码
File file = DocumentLoad.downloadURLDocument(fileUrl); File file = DocumentLoad.downloadURLDocument(fileUrl);
...@@ -166,7 +173,7 @@ public class TextInClient { ...@@ -166,7 +173,7 @@ public class TextInClient {
* @param taskId * @param taskId
* @return * @return
*/ */
private static String extractedResults(String taskId) { private String extractedResults(String taskId) {
String baseUrl = "https://doc-compare.intsig.com/doc_extraction/keyinfo/extracted_results?format=json&task_id=" + taskId; String baseUrl = "https://doc-compare.intsig.com/doc_extraction/keyinfo/extracted_results?format=json&task_id=" + taskId;
HttpUriRequest httpUriRequest = RequestBuilder.post() HttpUriRequest httpUriRequest = RequestBuilder.post()
.setUri(baseUrl) .setUri(baseUrl)
......
package cn.com.poc.thirdparty.resource.demand.ai.function.text_in_pdf2md.entity; package cn.com.poc.thirdparty.resource.textin.entity;
/** /**
* @author alex.yao * @author alex.yao
......
package cn.com.poc.thirdparty.resource.demand.ai.function.text_in_pdf2md.entity; package cn.com.poc.thirdparty.resource.textin.entity;
/** /**
* @author alex.yao * @author alex.yao
......
package cn.com.poc.thirdparty.resource.demand.ai.function; package cn.com.poc.thirdparty.resource.demand.ai.function;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.thirdparty.resource.demand.ai.function.extraction.ContractExtractionFunction; import cn.com.poc.thirdparty.resource.demand.ai.function.extraction.ContractExtractionFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.text_in_pdf2md.api.TextInClient; import cn.com.poc.thirdparty.resource.textin.api.TextInClient;
import cn.com.yict.framemax.core.spring.SingleContextInitializer; import cn.com.yict.framemax.core.spring.SingleContextInitializer;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -44,7 +43,8 @@ public class PdfToMdFunctionTest { ...@@ -44,7 +43,8 @@ public class PdfToMdFunctionTest {
options.put("parse_mode", "auto"); options.put("parse_mode", "auto");
options.put("table_flavor", "md"); options.put("table_flavor", "md");
try { try {
String response = TextInClient.recognize(fileContent, options); TextInClient textInClient = new TextInClient();
String response = textInClient.OCR(fileContent, options);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(response); JsonNode jsonNode = mapper.readTree(response);
if (jsonNode.has("result") && jsonNode.get("result").has("markdown")) { if (jsonNode.has("result") && jsonNode.get("result").has("markdown")) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment