Commit bfc5ed6c authored by alex yao's avatar alex yao

feat:Agent应用插件功能

parent e24d793b
......@@ -48,7 +48,7 @@ public class DocumentLoad {
// 关闭资源
inputStream.close();
isr.close();
String htmlStr = sb.toString();
String htmlStr = sb.toString().replaceAll("<head>.*?</head>", "");
return converter.convert(htmlStr);
} catch (IOException e) {
return "";
......
package cn.com.poc.thirdparty.resource.demand.ai.function.image_ocr;
import cn.com.poc.agent_application.entity.Variable;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractLargeModelFunction;
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.Properties;
import cn.hutool.core.collection.ListUtil;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author alex.yao
* @date 2025/1/14
*/
@Component
public class ImageOCRFunction extends AbstractLargeModelFunction {
private final String DESC = "该方法是通过OCR获取图片内容,并且根据用户提问对图片内容进行提取总结与分析。";
private final FunctionLLMConfig functionLLMConfig = new FunctionLLMConfig.FunctionLLMConfigBuilder()
.name("image_ocr")
.description(DESC)
.parameters(new Parameters("object")
.addProperties("query", new Properties("string", "用户提问问题"))
.addProperties("image_url", new Properties("string", "图片地址")))
.build();
@Override
public String doFunction(String content, String identifier) {
//todo 对接豆包多模态大模型
return null;
}
@Override
public String getDesc() {
return getDesc();
}
@Override
public List<String> getLLMConfig() {
return ListUtil.toList(JsonUtils.serialize(functionLLMConfig));
}
@Override
public List<String> getLLMConfig(List<Variable> variableStructure) {
return this.getLLMConfig();
}
}
......@@ -8,9 +8,11 @@ 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.Parameters;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Properties;
import cn.com.yict.framemax.core.config.Config;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.baidubce.services.bec.model.vm.NetworkConfig;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
......@@ -20,6 +22,8 @@ import com.google.api.services.customsearch.v1.CustomSearchAPI;
import com.google.api.services.customsearch.v1.CustomSearchAPIRequestInitializer;
import com.google.api.services.customsearch.v1.model.Result;
import com.google.api.services.customsearch.v1.model.Search;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
......@@ -32,10 +36,15 @@ import java.util.List;
@Component
public class WebSearchFunction extends AbstractLargeModelFunction {
private Logger logger = LoggerFactory.getLogger(WebSearchFunction.class);
private final HttpTransport transport = new ApacheHttpTransport();
private final JsonFactory jsonFactory = new GsonFactory();
private final String KEY = Config.get("google.custom.search.key");
private final String CX = Config.get("google.custom.search.cx");
private final String DESC = "该方法是通过Bing所有对用户给出的关键词进行网站、网页搜索检索获取网页内容";
private final FunctionLLMConfig functionLLMConfig = new FunctionLLMConfig.FunctionLLMConfigBuilder()
......@@ -52,7 +61,6 @@ public class WebSearchFunction extends AbstractLargeModelFunction {
}
JSONObject jsonObject = JSONObject.parseObject(content);
String query = jsonObject.getString("query");
List<WebSearchFunctionResult> results = new ArrayList<>();
try {
CustomSearchAPI customSearchAPI = new CustomSearchAPI.Builder(transport, jsonFactory, GoogleNetHttpTransport.newTrustedTransport().createRequestFactory().getInitializer())
......@@ -60,10 +68,9 @@ public class WebSearchFunction extends AbstractLargeModelFunction {
.setCustomSearchAPIRequestInitializer(new CustomSearchAPIRequestInitializer("AIzaSyCV8PTQ10rG5wo4E004dR3mcGD1RM_PrBw"))
.build();
Search execute = customSearchAPI
.cse().list().setCx("049026ecb26e840ed")
.setKey("AIzaSyCV8PTQ10rG5wo4E004dR3mcGD1RM_PrBw")
.cse().list().setCx(CX)
.setKey(KEY)
.setQ(query)
.setStart(1L)
.setNum(3)
.execute();
List<Result> items = execute.getItems();
......@@ -72,6 +79,9 @@ public class WebSearchFunction extends AbstractLargeModelFunction {
String htmlContent = DocumentLoad.htmlToMarkdown(link);
if (StringUtils.isNotBlank(htmlContent)) {
htmlContent = htmlContent.replaceAll(StringUtils.SPACE, StringUtils.EMPTY);
if (htmlContent.length() > 10000) {
htmlContent = htmlContent.substring(0, 10000);
}
}
String title = item.getTitle();
String snippet = item.getSnippet();
......@@ -84,6 +94,7 @@ public class WebSearchFunction extends AbstractLargeModelFunction {
}
return JsonUtils.serialize(results);
} catch (Exception e) {
logger.error("web search error:" + e.getMessage());
return JsonUtils.serialize(results);
}
}
......
......@@ -26,4 +26,6 @@ large-model.apikey=sk-bNzgFwFtw3k4jR7CE3575a19C3304a53Bb803cCeDeC154F2
# \u652F\u4ED8\u56DE\u8C03\u5730\u5740
pay.domain.callback.url=https://poc-sit.gsstcloud.com/api/rest/payCallBackRest/payCallBack.json
# \u8C37\u6B4C\u767B\u5F55
google.client.id=867985016759-n9qj00k174n9bibrtrqngvt89vbmnjrp.apps.googleusercontent.com
\ No newline at end of file
google.client.id=867985016759-n9qj00k174n9bibrtrqngvt89vbmnjrp.apps.googleusercontent.com
google.custom.search.key=AIzaSyCV8PTQ10rG5wo4E004dR3mcGD1RM_PrBw
google.custom.search.cx=049026ecb26e840ed
\ No newline at end of file
......@@ -26,4 +26,6 @@ large-model.apikey=sk-gQM7bpiRxaOl2emt0cBbF47481C5403885263eFeB95c7a8e
# \u652F\u4ED8\u56DE\u8C03\u5730\u5740
pay.domain.callback.url=https://model-link.gsstcloud.com/api/rest/payCallBackRest/payCallBack.json
# \u8C37\u6B4C\u767B\u5F55
google.client.id=867985016759-n9qj00k174n9bibrtrqngvt89vbmnjrp.apps.googleusercontent.com
\ No newline at end of file
google.client.id=867985016759-n9qj00k174n9bibrtrqngvt89vbmnjrp.apps.googleusercontent.com
google.custom.search.key=AIzaSyCV8PTQ10rG5wo4E004dR3mcGD1RM_PrBw
google.custom.search.cx=049026ecb26e840ed
\ No newline at end of file
......@@ -26,4 +26,6 @@ large-model.apikey=sk-bNzgFwFtw3k4jR7CE3575a19C3304a53Bb803cCeDeC154F2
# \u652F\u4ED8\u56DE\u8C03\u5730\u5740
pay.domain.callback.url=https://poc-sit.gsstcloud.com/api/rest/payCallBackRest/payCallBack.json
# \u8C37\u6B4C\u767B\u5F55
google.client.id=867985016759-n9qj00k174n9bibrtrqngvt89vbmnjrp.apps.googleusercontent.com
\ No newline at end of file
google.client.id=867985016759-n9qj00k174n9bibrtrqngvt89vbmnjrp.apps.googleusercontent.com
google.custom.search.key=AIzaSyCV8PTQ10rG5wo4E004dR3mcGD1RM_PrBw
google.custom.search.cx=049026ecb26e840ed
\ No newline at end of file
......@@ -26,4 +26,6 @@ large-model.apikey=sk-gQM7bpiRxaOl2emt0cBbF47481C5403885263eFeB95c7a8e
# \u652F\u4ED8\u56DE\u8C03\u5730\u5740
pay.domain.callback.url=https://model-link.gsstcloud.com/api/rest/payCallBackRest/payCallBack.json
# \u8C37\u6B4C\u767B\u5F55
google.client.id=867985016759-n9qj00k174n9bibrtrqngvt89vbmnjrp.apps.googleusercontent.com
\ No newline at end of file
google.client.id=867985016759-n9qj00k174n9bibrtrqngvt89vbmnjrp.apps.googleusercontent.com
google.custom.search.key=AIzaSyCV8PTQ10rG5wo4E004dR3mcGD1RM_PrBw
google.custom.search.cx=049026ecb26e840ed
\ No newline at end of file
......@@ -22,6 +22,6 @@ public class GoogleSearchFunctionTest {
@Test
public void testSearch() throws IOException, GeneralSecurityException {
WebSearchFunction webSearchFunction = new WebSearchFunction();
System.out.println(webSearchFunction.doFunction("{\"query\":\"什么是区块链\"}", "1"));
System.out.println(webSearchFunction.doFunction("{\"query\":\"今日广州天气\"}", "1"));
}
}
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