Commit c4b4ee9b authored by alex yao's avatar alex yao

feat:优化agent调用流程

parent 964a0b97
...@@ -79,6 +79,8 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -79,6 +79,8 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
final private Logger logger = LoggerFactory.getLogger(AgentApplicationService.class); final private Logger logger = LoggerFactory.getLogger(AgentApplicationService.class);
final private ThreadPoolExecutor executor = new ThreadPoolExecutor(16, 64, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100));
@Resource @Resource
private BizKnowledgeDocumentService bizKnowledgeDocumentService; private BizKnowledgeDocumentService bizKnowledgeDocumentService;
...@@ -139,8 +141,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -139,8 +141,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
// 保存或更新 // 保存或更新
return StringUtils.isEmpty(entity.getAgentId()) ? return StringUtils.isEmpty(entity.getAgentId()) ? bizAgentApplicationInfoService.save(entity) : bizAgentApplicationInfoService.update(entity);
bizAgentApplicationInfoService.save(entity) : bizAgentApplicationInfoService.update(entity);
} }
@Override @Override
...@@ -201,41 +202,45 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -201,41 +202,45 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
} }
@Override @Override
public AgentResultEntity callAgentApplication(String agentId, String dialogueId, String largeModel, String agentSystem, Integer[] kdIds, Integer[] databaseIds, public AgentResultEntity callAgentApplication(String agentId, String dialogueId, String largeModel, String agentSystem, Integer[] kdIds, Integer[] databaseIds, Integer communicationTurn, Float topP, Float temperature, List<Message> messages, List<Tool> tools, FunctionCallResult functionCallResult, boolean stream, Double score, Integer topK, KnowledgeSearchTypeEnum knowledgeSearchType, KnowledgeSuperclassProblemConfig superclassProblemConfig, HttpServletResponse httpServletResponse) throws Exception {
Integer communicationTurn, Float topP, Float temperature, List<Message> messages, List<Tool> tools, FunctionCallResult functionCallResult, boolean stream,
Double score, Integer topK, KnowledgeSearchTypeEnum knowledgeSearchType, KnowledgeSuperclassProblemConfig superclassProblemConfig, HttpServletResponse httpServletResponse) throws Exception { // 使用 CompletableFuture 来异步执行任务并处理可能的异常
String model = modelConvert(largeModel); CompletableFuture<FunctionResult> functionResultFuture = CompletableFuture.
supplyAsync(() -> functionCall(dialogueId, functionCallResult, agentId), executor)
Tool[] toolArray = tools.toArray(new Tool[0]); .exceptionally(ex -> {
logger.error("functionCall error", ex);
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2)); return new FunctionResult();
try { });
CompletableFuture<List<DBChainResult>> dbChainResultsFuture = CompletableFuture
// 使用 CompletableFuture 来异步执行任务并处理可能的异常 .supplyAsync(() -> database(messages, databaseIds), executor)
CompletableFuture<List<DBChainResult>> dbChainResultsFuture = CompletableFuture.supplyAsync(() -> database(messages, databaseIds), executor); .exceptionally(ex -> {
CompletableFuture<List<KnowledgeContentResult>> knowledgeResultFuture = CompletableFuture.supplyAsync(() -> knowledge(kdIds, messages, topK, score, knowledgeSearchType), executor); logger.error("database chain error", ex);
return (List<DBChainResult>) CollectionUtils.EMPTY_COLLECTION;
// 等待所有任务完成并获取结果 });
CompletableFuture.allOf(dbChainResultsFuture, knowledgeResultFuture).join(); CompletableFuture<List<KnowledgeContentResult>> knowledgeResultFuture = CompletableFuture
List<DBChainResult> dbChainResults = dbChainResultsFuture.join(); .supplyAsync(() -> knowledge(kdIds, messages, topK, score, knowledgeSearchType), executor)
List<KnowledgeContentResult> knowledgeResult = knowledgeResultFuture.join(); .exceptionally(ex -> {
logger.error("knowledge chain error", ex);
return (List<KnowledgeContentResult>) CollectionUtils.EMPTY_COLLECTION;
});
// 等待所有任务完成并获取结果
CompletableFuture.allOf(functionResultFuture, dbChainResultsFuture, knowledgeResultFuture).join();
FunctionResult functionResult = functionResultFuture.join();
List<DBChainResult> dbChainResults = dbChainResultsFuture.join();
List<KnowledgeContentResult> knowledgeResult = knowledgeResultFuture.join();
if (superclassProblem(superclassProblemConfig, kdIds, knowledgeResult)) {
return sendProblemMess(stream, superclassProblemConfig, httpServletResponse);
if (superclassProblem(superclassProblemConfig, kdIds, knowledgeResult)) { } else {
return sendProblemMess(stream, superclassProblemConfig, httpServletResponse);
} else {
FunctionResult functionResult = functionCall(dialogueId, functionCallResult, agentId);
String promptTemplate = buildDialogsPrompt(functionResult, agentSystem, toolArray, dialogueId, agentId, knowledgeResult, dbChainResults); String promptTemplate = buildDialogsPrompt(functionResult, agentSystem, tools.toArray(new Tool[0]), dialogueId, agentId, knowledgeResult, dbChainResults);
Message[] messageArray = buildMessages(messages, communicationTurn, promptTemplate); Message[] messageArray = buildMessages(messages, communicationTurn, promptTemplate);
return llmExecutorAndOutput(topP, stream, model, messageArray, functionResult, knowledgeResult, dbChainResults, httpServletResponse); return llmExecutorAndOutput(topP, stream, modelConvert(largeModel), messageArray, functionResult, knowledgeResult, dbChainResults, httpServletResponse);
}
} finally {
executor.shutdown();
} }
} }
...@@ -424,9 +429,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -424,9 +429,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
baiduAISailsText2ImageRequest.setAcctId(null); baiduAISailsText2ImageRequest.setAcctId(null);
BaiduAISailsText2ImageResult baiduAISailsText2ImageResult = aiCreateImageService.executeSailsText2Image(baiduAISailsText2ImageRequest); BaiduAISailsText2ImageResult baiduAISailsText2ImageResult = aiCreateImageService.executeSailsText2Image(baiduAISailsText2ImageRequest);
if (baiduAISailsText2ImageResult == null if (baiduAISailsText2ImageResult == null || CollectionUtils.isEmpty(baiduAISailsText2ImageResult.getData()) || StringUtils.isEmpty(baiduAISailsText2ImageResult.getData().get(0).getUrl())) {
|| CollectionUtils.isEmpty(baiduAISailsText2ImageResult.getData())
|| StringUtils.isEmpty(baiduAISailsText2ImageResult.getData().get(0).getUrl())) {
logger.error("create agent icon error, baiduAISailsText2ImageRequest:{} ", baiduAISailsText2ImageRequest); logger.error("create agent icon error, baiduAISailsText2ImageRequest:{} ", baiduAISailsText2ImageRequest);
throw new I18nMessageException("exception/failed.to.create.[avatar].please.try.again.later"); throw new I18nMessageException("exception/failed.to.create.[avatar].please.try.again.later");
} }
...@@ -644,9 +647,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -644,9 +647,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
* @return 输出结果 * @return 输出结果
* @throws Exception * @throws Exception
*/ */
private AgentResultEntity llmExecutorAndOutput(Float topP, boolean stream, String model, Message[] messageArray, private AgentResultEntity llmExecutorAndOutput(Float topP, boolean stream, String model, Message[] messageArray, FunctionResult functionResult, List<KnowledgeContentResult> knowledgeResult, List<DBChainResult> dbChainResults, HttpServletResponse httpServletResponse) throws Exception {
FunctionResult functionResult, List<KnowledgeContentResult> knowledgeResult, List<DBChainResult> dbChainResults,
HttpServletResponse httpServletResponse) throws Exception {
if (stream) { if (stream) {
SSEUtil sseUtil = new SSEUtil(httpServletResponse); SSEUtil sseUtil = new SSEUtil(httpServletResponse);
if (ObjectUtil.isNotNull(functionResult) && StringUtils.isNotBlank(functionResult.getFunctionName())) { if (ObjectUtil.isNotNull(functionResult) && StringUtils.isNotBlank(functionResult.getFunctionName())) {
...@@ -703,8 +704,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -703,8 +704,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
} }
//若 name desc 其中一项不为空则入参 不传入prompt //若 name desc 其中一项不为空则入参 不传入prompt
if (StringUtils.isNotBlank(agentTitle) || (StringUtils.isNotBlank(agentDesc))) { if (StringUtils.isNotBlank(agentTitle) || (StringUtils.isNotBlank(agentDesc))) {
return configSystem.replace("${agent_title}", StringUtils.isNotBlank(agentTitle) ? agentTitle : StringUtils.EMPTY) return configSystem.replace("${agent_title}", StringUtils.isNotBlank(agentTitle) ? agentTitle : StringUtils.EMPTY).replace("${agent_desc}", StringUtils.isNotBlank(agentDesc) ? agentDesc : StringUtils.EMPTY);
.replace("${agent_desc}", StringUtils.isNotBlank(agentDesc) ? agentDesc : StringUtils.EMPTY);
} }
// 若 name desc 都空 prompt 有内容,则把prompt 传入desc // 若 name desc 都空 prompt 有内容,则把prompt 传入desc
...@@ -726,9 +726,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -726,9 +726,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
* @param dbChainResults 数据库链结果 * @param dbChainResults 数据库链结果
* @return * @return
*/ */
private String buildDialogsPrompt(FunctionResult functionResult, String agentSystem, private String buildDialogsPrompt(FunctionResult functionResult, String agentSystem, Tool[] tools, String dialogueId, String agentId, List<KnowledgeContentResult> knowledgeContentResults, List<DBChainResult> dbChainResults) {
Tool[] tools, String dialogueId, String agentId,
List<KnowledgeContentResult> knowledgeContentResults, List<DBChainResult> dbChainResults) {
String promptTemplate = bizAgentApplicationGcConfigService.getByConfigCode(AgentApplicationGCConfigConstants.AGENT_BASE_SYSTEM).getConfigSystem(); String promptTemplate = bizAgentApplicationGcConfigService.getByConfigCode(AgentApplicationGCConfigConstants.AGENT_BASE_SYSTEM).getConfigSystem();
// 系统语言 // 系统语言
String language = ""; String language = "";
...@@ -757,10 +755,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -757,10 +755,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
StringBuilder dbChainPromptBuilder = new StringBuilder(""); StringBuilder dbChainPromptBuilder = new StringBuilder("");
if (CollectionUtils.isNotEmpty(dbChainResults)) { if (CollectionUtils.isNotEmpty(dbChainResults)) {
for (int i = 1; i <= dbChainResults.size(); i++) { for (int i = 1; i <= dbChainResults.size(); i++) {
dbChainPromptBuilder.append("### DBChain Result ").append(i).append(":") dbChainPromptBuilder.append("### DBChain Result ").append(i).append(":").append(StringUtils.LF).append(dbChainResults.get(i - 1).getResult()).append(StringUtils.LF);
.append(StringUtils.LF)
.append(dbChainResults.get(i - 1).getResult())
.append(StringUtils.LF);
} }
} }
promptTemplate = promptTemplate.replace("${databaseResult}", dbChainPromptBuilder.toString()); promptTemplate = promptTemplate.replace("${databaseResult}", dbChainPromptBuilder.toString());
...@@ -777,10 +772,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -777,10 +772,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
StringBuilder knowledgePromptBuilder = new StringBuilder(""); StringBuilder knowledgePromptBuilder = new StringBuilder("");
if (CollectionUtils.isNotEmpty(knowledgeContentResults)) { if (CollectionUtils.isNotEmpty(knowledgeContentResults)) {
for (int i = 1; i <= knowledgeContentResults.size(); i++) { for (int i = 1; i <= knowledgeContentResults.size(); i++) {
knowledgePromptBuilder.append("### Chunk ").append(i).append(":") knowledgePromptBuilder.append("### Chunk ").append(i).append(":").append(StringUtils.LF).append(knowledgeContentResults.get(i - 1).getContent()).append(StringUtils.LF);
.append(StringUtils.LF)
.append(knowledgeContentResults.get(i - 1).getContent())
.append(StringUtils.LF);
} }
} }
promptTemplate = promptTemplate.replace("${knowledgeResults}", knowledgePromptBuilder.toString()); promptTemplate = promptTemplate.replace("${knowledgeResults}", knowledgePromptBuilder.toString());
...@@ -808,11 +800,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -808,11 +800,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
if (CollectionUtils.isNotEmpty(longMemoryEntities)) { if (CollectionUtils.isNotEmpty(longMemoryEntities)) {
StringBuilder stringBuilder = new StringBuilder(""); StringBuilder stringBuilder = new StringBuilder("");
for (AgentLongMemoryEntity agentLongMemoryEntity : longMemoryEntities) { for (AgentLongMemoryEntity agentLongMemoryEntity : longMemoryEntities) {
stringBuilder stringBuilder.append("Time").append(":").append(agentLongMemoryEntity.getTimestamp()).append(StringUtils.SPACE).append("Content").append(":").append(agentLongMemoryEntity.getContent()).append(StringUtils.LF);
.append("Time").append(":").append(agentLongMemoryEntity.getTimestamp())
.append(StringUtils.SPACE)
.append("Content").append(":").append(agentLongMemoryEntity.getContent())
.append(StringUtils.LF);
} }
longMemoryContent = stringBuilder.toString(); longMemoryContent = stringBuilder.toString();
} }
...@@ -844,10 +832,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -844,10 +832,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
*/ */
private String buildFunctionPrompt(FunctionResult functionResult, String promptTemplate) { private String buildFunctionPrompt(FunctionResult functionResult, String promptTemplate) {
if (functionResult != null) { if (functionResult != null) {
promptTemplate = promptTemplate.replace("${functionName}", StringUtils.isNotBlank(functionResult.getFunctionName()) ? functionResult.getFunctionName() : StringUtils.EMPTY) promptTemplate = promptTemplate.replace("${functionName}", StringUtils.isNotBlank(functionResult.getFunctionName()) ? functionResult.getFunctionName() : StringUtils.EMPTY).replace("${functionArg}", StringUtils.isNotBlank(functionResult.getFunctionArg()) ? functionResult.getFunctionArg() : StringUtils.EMPTY).replace("${functionDesc}", StringUtils.isNotBlank(functionResult.getFunctionDesc()) ? functionResult.getFunctionDesc() : StringUtils.EMPTY).replace("${functionResult}", StringUtils.isNotBlank(functionResult.getPromptContent()) ? functionResult.getPromptContent() : StringUtils.EMPTY);
.replace("${functionArg}", StringUtils.isNotBlank(functionResult.getFunctionArg()) ? functionResult.getFunctionArg() : StringUtils.EMPTY)
.replace("${functionDesc}", StringUtils.isNotBlank(functionResult.getFunctionDesc()) ? functionResult.getFunctionDesc() : StringUtils.EMPTY)
.replace("${functionResult}", StringUtils.isNotBlank(functionResult.getPromptContent()) ? functionResult.getPromptContent() : StringUtils.EMPTY);
} }
return promptTemplate; return promptTemplate;
} }
...@@ -897,9 +882,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -897,9 +882,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
* @param httpServletResponse * @param httpServletResponse
* @throws IOException * @throws IOException
*/ */
private AgentResultEntity textOutput(HttpServletResponse httpServletResponse, LargeModelDemandResult private AgentResultEntity textOutput(HttpServletResponse httpServletResponse, LargeModelDemandResult largeModelDemandResult) throws IOException {
largeModelDemandResult) throws
IOException {
PrintWriter writer = httpServletResponse.getWriter(); PrintWriter writer = httpServletResponse.getWriter();
writer.write(JsonUtils.serialize(largeModelDemandResult)); writer.write(JsonUtils.serialize(largeModelDemandResult));
writer.flush(); writer.flush();
...@@ -1076,7 +1059,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -1076,7 +1059,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
/** /**
* 获取模型 * 获取模型
* *
* @param largeModel * @param largeModel 模型名称
* @return * @return
*/ */
private String modelConvert(String largeModel) { private String modelConvert(String largeModel) {
...@@ -1090,51 +1073,10 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -1090,51 +1073,10 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
/** /**
* 判断是否需要FunctionCall * 判断是否需要FunctionCall
* *
* @param dialogueId * @param dialogueId 对话id
* @param messages * @param functionCallResult 函数调用结果
* @param tools * @param agentId 应用id
* @param agentId
* @param fileUrls
* @param imageUrls
*/ */
private FunctionResult functionCall(String dialogueId, List<Message> messages, Tool[] tools, String
agentId, List<String> fileUrls, List<String> imageUrls) {
FunctionResult result = new FunctionResult();
if (ArrayUtils.isEmpty(tools)) {
return result;
}
Object content = messages.get(messages.size() - 1).getContent();
String query;
if (content instanceof List) {
query = ((List<HashMap>) content).get(0).get("text").toString();
} else {
query = content.toString();
}
query = "用户输入:" + query + "\n";
if (CollectionUtils.isNotEmpty(fileUrls)) {
query = query + "用户上传文件地址:" + JsonUtils.serialize(fileUrls) + "\n";
}
if (CollectionUtils.isNotEmpty(imageUrls)) {
query = query + "用户上传图片地址:" + JsonUtils.serialize(imageUrls);
}
FunctionCallResult functionCallResult = llmService.functionCall(query, tools);
if (functionCallResult != null && functionCallResult.isNeed()) {
// 执行函数返回结果
LargeModelFunctionEnum functionEnum = LargeModelFunctionEnum.valueOf(functionCallResult.getFunctionCall().getName());
AbstractFunctionResult abstractFunctionResult = functionEnum.getFunction().doFunction(functionCallResult.getFunctionCall().getArguments(), AgentApplicationTools.identifier(dialogueId, agentId));
//构造返回结果
result.setFunctionName(functionCallResult.getFunctionCall().getName());
result.setFunctionArg(functionCallResult.getFunctionCall().getArguments());
result.setFunctionDesc(functionEnum.getFunction().getDesc());
result.setFunctionResult(abstractFunctionResult.getFunctionResult());
result.setPromptContent(abstractFunctionResult.getPromptContent());
}
return result;
}
private FunctionResult functionCall(String dialogueId, FunctionCallResult functionCallResult, String agentId) { private FunctionResult functionCall(String dialogueId, FunctionCallResult functionCallResult, String agentId) {
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
if (functionCallResult != null && functionCallResult.isNeed()) { if (functionCallResult != null && functionCallResult.isNeed()) {
...@@ -1328,9 +1270,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService { ...@@ -1328,9 +1270,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
private boolean superclassProblem(KnowledgeSuperclassProblemConfig superclassProblemConfig, Integer[] kdIds, List<KnowledgeContentResult> knowledgeContentResult) { private boolean superclassProblem(KnowledgeSuperclassProblemConfig superclassProblemConfig, Integer[] kdIds, List<KnowledgeContentResult> knowledgeContentResult) {
if (ArrayUtils.isNotEmpty(kdIds) && CollectionUtils.isEmpty(knowledgeContentResult)) { if (ArrayUtils.isNotEmpty(kdIds) && CollectionUtils.isEmpty(knowledgeContentResult)) {
String knowledgeResponseType = superclassProblemConfig.getKnowledgeResponseType(); String knowledgeResponseType = superclassProblemConfig.getKnowledgeResponseType();
if (AgentApplicationKnowledgeConstants.SUPER_CLASS_PROBLEM_TYPE.CUSTOM.equals(knowledgeResponseType) if (AgentApplicationKnowledgeConstants.SUPER_CLASS_PROBLEM_TYPE.CUSTOM.equals(knowledgeResponseType) && ArrayUtils.isNotEmpty(superclassProblemConfig.getKnowledgeCustomResponse()) && StringUtils.isNotBlank(superclassProblemConfig.getKnowledgeCustomResponse()[0])) {
&& ArrayUtils.isNotEmpty(superclassProblemConfig.getKnowledgeCustomResponse())
&& StringUtils.isNotBlank(superclassProblemConfig.getKnowledgeCustomResponse()[0])) {
return true; return true;
} }
} }
......
...@@ -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