Commit c4b4ee9b authored by alex yao's avatar alex yao

feat:优化agent调用流程

parent 964a0b97
...@@ -5,6 +5,7 @@ package cn.com.poc.thirdparty.resource.demand.ai.function.extraction.entity; ...@@ -5,6 +5,7 @@ package cn.com.poc.thirdparty.resource.demand.ai.function.extraction.entity;
* @date 2025/5/12 * @date 2025/5/12
*/ */
public class RequestData { public class RequestData {
public Integer token_mode;
public String creator; public String creator;
public Config config; public Config config;
public String filedata; public String filedata;
...@@ -12,6 +13,7 @@ public class RequestData { ...@@ -12,6 +13,7 @@ public class RequestData {
public KeyInfo[] key_info_list; public KeyInfo[] key_info_list;
public RequestData(String creator, Config config, String filedata, String filename, KeyInfo[] key_info_list) { public RequestData(String creator, Config config, String filedata, String filename, KeyInfo[] key_info_list) {
token_mode = 1;
this.creator = creator; this.creator = creator;
this.config = config; this.config = config;
this.filedata = filedata; this.filedata = filedata;
......
...@@ -63,9 +63,8 @@ public class PdfToMDFunction extends AbstractLargeModelFunction { ...@@ -63,9 +63,8 @@ public class PdfToMDFunction extends AbstractLargeModelFunction {
options.put("paratext_mode", "annotation"); options.put("paratext_mode", "annotation");
options.put("parse_mode", "auto"); options.put("parse_mode", "auto");
options.put("table_flavor", "md"); options.put("table_flavor", "md");
TextInClient client = new TextInClient();
try { try {
String response = client.recognize(fileContent, options); String response = TextInClient.recognize(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")) {
......
...@@ -6,12 +6,16 @@ package cn.com.poc.thirdparty.resource.demand.ai.function.text_in_pdf2md.api; ...@@ -6,12 +6,16 @@ package cn.com.poc.thirdparty.resource.demand.ai.function.text_in_pdf2md.api;
*/ */
import cn.com.poc.common.utils.DocumentLoad; import cn.com.poc.common.utils.DocumentLoad;
import cn.com.poc.common.utils.http.LocalHttpClient;
import cn.com.poc.thirdparty.resource.demand.ai.function.extraction.entity.Config; import cn.com.poc.thirdparty.resource.demand.ai.function.extraction.entity.Config;
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.extraction.entity.RequestData; import cn.com.poc.thirdparty.resource.demand.ai.function.extraction.entity.RequestData;
import cn.com.yict.framemax.core.exception.BusinessException; import cn.com.yict.framemax.core.exception.BusinessException;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.jetbrains.annotations.NotNull; import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -21,8 +25,6 @@ import java.net.URL; ...@@ -21,8 +25,6 @@ import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64; import java.util.Base64;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -30,14 +32,20 @@ import java.util.Map; ...@@ -30,14 +32,20 @@ import java.util.Map;
public class TextInClient { public class TextInClient {
private Logger logger = LoggerFactory.getLogger(TextInClient.class); final private static Logger logger = LoggerFactory.getLogger(TextInClient.class);
private final String appId = "a595e68578ff9a853c3d60a879157547"; final private static String appId = "a595e68578ff9a853c3d60a879157547";
private final String secretCode = "d723d69522c92ff59a3e48b37ac3df79"; final private static String secretCode = "d723d69522c92ff59a3e48b37ac3df79";
public TextInClient() { public TextInClient() {
} }
public String recognize(byte[] fileContent, HashMap<String, Object> options) throws IOException { /**
* @param fileContent
* @param options
* @return
* @throws IOException
*/
public static String recognize(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) {
...@@ -70,7 +78,31 @@ public class TextInClient { ...@@ -70,7 +78,31 @@ public class TextInClient {
} }
} }
public String extraction(String fileUrl, List<KeyInfo> keyInfoList) {
private static HttpURLConnection getRecoGinzeHttpURLConnection(StringBuilder queryParams) throws IOException {
String baseUrl = "https://api.textin.com/ai/service/v1/pdf_to_markdown";
String fullUrl = baseUrl + (queryParams.length() > 0 ? "?" + queryParams : "");
URL url = new URL(fullUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("x-ti-app-id", appId);
connection.setRequestProperty("x-ti-secret-code", secretCode);
connection.setRequestProperty("Content-Type", "text/plain;charset=utf-8");
connection.setDoOutput(true);
return connection;
}
/**
* 【合同抽取】-创建抽取
* https://www.textin.com/document/doc_extraction_create
*
* @param fileUrl
* @param keyInfoList
* @return
*/
public static String extraction(String fileUrl, List<KeyInfo> keyInfoList) {
try { try {
// 读取文件并将其转换为Base64编码 // 读取文件并将其转换为Base64编码
File file = DocumentLoad.downloadURLDocument(fileUrl); File file = DocumentLoad.downloadURLDocument(fileUrl);
...@@ -96,7 +128,7 @@ public class TextInClient { ...@@ -96,7 +128,7 @@ public class TextInClient {
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setRequestProperty("x-ti-app-id", appId); connection.setRequestProperty("x-ti-app-id", appId);
connection.setRequestProperty("x-ti-secret-code", secretCode); connection.setRequestProperty("x-ti-secret-code", secretCode);
connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
connection.setDoOutput(true); // 开启输出流 connection.setDoOutput(true); // 开启输出流
// 发送请求数据 // 发送请求数据
...@@ -117,26 +149,40 @@ public class TextInClient { ...@@ -117,26 +149,40 @@ public class TextInClient {
response.append(inputLine); response.append(inputLine);
} }
// 输出响应内容 // 输出响应内容
return response.toString(); logger.info("Response: {}", response);
JSONObject jsonResponse = JSONObject.parseObject(response.toString());
String taskId = jsonResponse.getJSONObject("result").getString("task_id");
return extractedResults(taskId);
} }
} catch (IOException e) { } catch (IOException e) {
throw new BusinessException(e); throw new BusinessException(e);
} }
} }
private HttpURLConnection getRecoGinzeHttpURLConnection(StringBuilder queryParams) throws IOException { /**
String baseUrl = "https://api.textin.com/ai/service/v1/pdf_to_markdown"; * 【合同抽取】 -获取抽取结果
String fullUrl = baseUrl + (queryParams.length() > 0 ? "?" + queryParams : ""); * https://www.textin.com/document/doc_extraction_result
URL url = new URL(fullUrl); *
* @param taskId
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); * @return
connection.setRequestMethod("POST"); */
connection.setRequestProperty("x-ti-app-id", appId); private static String extractedResults(String taskId) {
connection.setRequestProperty("x-ti-secret-code", secretCode); String baseUrl = "https://doc-compare.intsig.com/doc_extraction/keyinfo/extracted_results?format=json&task_id=" + taskId;
connection.setRequestProperty("Content-Type", "text/plain;charset=utf-8"); HttpUriRequest httpUriRequest = RequestBuilder.post()
connection.setDoOutput(true); .setUri(baseUrl)
return connection; .addHeader("x-ti-app-id", appId)
.addHeader("x-ti-secret-code", secretCode)
.addHeader("Content-Type", "application/json;charset=utf-8")
.build();
String result = LocalHttpClient.executeJsonResult(httpUriRequest, String.class);
JSONObject resultJson = JSONObject.parseObject(result);
Integer code = resultJson.getInteger("code");
if (code.equals(200)) {
return resultJson.getJSONObject("result").toJSONString();
} else {
logger.error("获取token失败,错误码:{}", code);
return StringUtils.EMPTY;
}
} }
} }
\ No newline at end of file
...@@ -43,19 +43,18 @@ public class PdfToMdFunctionTest { ...@@ -43,19 +43,18 @@ public class PdfToMdFunctionTest {
options.put("paratext_mode", "annotation"); options.put("paratext_mode", "annotation");
options.put("parse_mode", "auto"); options.put("parse_mode", "auto");
options.put("table_flavor", "md"); options.put("table_flavor", "md");
TextInClient client = new TextInClient();
try { try {
String response = client.recognize(fileContent, options); String response = TextInClient.recognize(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")) {
String markdown = jsonNode.get("result").get("markdown").asText(); String markdown = jsonNode.get("result").get("markdown").asText();
System.out.println(markdown); System.out.println(markdown);
}else{ } else {
System.out.println(response); System.out.println(response);
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("1111111"); e.printStackTrace();
} }
} }
......
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