Commit c611a9fa authored by alex yao's avatar alex yao

fix:配置网页读取超时

parent fddacb73
...@@ -768,7 +768,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ ...@@ -768,7 +768,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
if (!"0".equals(result.getCode())) { if (!"0".equals(result.getCode())) {
logger.error("LLM Error,code:{}", result.getCode()); logger.error("LLM Error,code:{}", result.getCode());
I18nMessageException ex = new I18nMessageException("exception/call.failure"); I18nMessageException ex = new I18nMessageException("exception/call.failure");
writer.write(EVENT_STREAM_PREFIX + JsonUtils.serialize(ex) + "\n\n"); writer.write(EVENT_STREAM_PREFIX + JsonUtils.serialize(ex.getMessage()) + "\n\n");
writer.flush(); writer.flush();
throw ex; throw ex;
} }
......
...@@ -12,7 +12,6 @@ import cn.com.yict.framemax.core.config.Config; ...@@ -12,7 +12,6 @@ import cn.com.yict.framemax.core.config.Config;
import cn.com.yict.framemax.core.exception.BusinessException; import cn.com.yict.framemax.core.exception.BusinessException;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject; 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.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport; import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport; import com.google.api.client.http.apache.v2.ApacheHttpTransport;
...@@ -26,8 +25,8 @@ import org.slf4j.Logger; ...@@ -26,8 +25,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.*;
/** /**
* @author alex.yao * @author alex.yao
...@@ -38,6 +37,7 @@ public class WebSearchFunction extends AbstractLargeModelFunction { ...@@ -38,6 +37,7 @@ public class WebSearchFunction extends AbstractLargeModelFunction {
private Logger logger = LoggerFactory.getLogger(WebSearchFunction.class); private Logger logger = LoggerFactory.getLogger(WebSearchFunction.class);
private CustomSearchAPI customSearchAPI;
private final HttpTransport transport = new ApacheHttpTransport(); private final HttpTransport transport = new ApacheHttpTransport();
private final JsonFactory jsonFactory = new GsonFactory(); private final JsonFactory jsonFactory = new GsonFactory();
...@@ -54,6 +54,20 @@ public class WebSearchFunction extends AbstractLargeModelFunction { ...@@ -54,6 +54,20 @@ public class WebSearchFunction extends AbstractLargeModelFunction {
.addProperties("query", new Properties("string", "搜索关键词"))) .addProperties("query", new Properties("string", "搜索关键词")))
.build(); .build();
private CustomSearchAPI getCustomSearchAPI() {
try {
if (customSearchAPI == null) {
customSearchAPI = new CustomSearchAPI.Builder(transport, jsonFactory, GoogleNetHttpTransport.newTrustedTransport().createRequestFactory().getInitializer())
.setRootUrl("https://google-api.gsstcloud.com")
.setCustomSearchAPIRequestInitializer(new CustomSearchAPIRequestInitializer("AIzaSyCV8PTQ10rG5wo4E004dR3mcGD1RM_PrBw"))
.build();
}
return customSearchAPI;
} catch (Exception e) {
throw new BusinessException("初始化google搜索服务失败");
}
}
@Override @Override
public String doFunction(String content, String identifier) { public String doFunction(String content, String identifier) {
if (StringUtils.isBlank(content)) { if (StringUtils.isBlank(content)) {
...@@ -62,33 +76,16 @@ public class WebSearchFunction extends AbstractLargeModelFunction { ...@@ -62,33 +76,16 @@ public class WebSearchFunction extends AbstractLargeModelFunction {
JSONObject jsonObject = JSONObject.parseObject(content); JSONObject jsonObject = JSONObject.parseObject(content);
String query = jsonObject.getString("query"); String query = jsonObject.getString("query");
query = query.replaceAll(StringUtils.SPACE, StringUtils.EMPTY); query = query.replaceAll(StringUtils.SPACE, StringUtils.EMPTY);
List<WebSearchFunctionResult> results = new ArrayList<>(); List<WebSearchFunctionResult> results = new CopyOnWriteArrayList<>();
try { try {
CustomSearchAPI customSearchAPI = new CustomSearchAPI.Builder(transport, jsonFactory, GoogleNetHttpTransport.newTrustedTransport().createRequestFactory().getInitializer()) Search execute = getCustomSearchAPI()
.setRootUrl("https://google-api.gsstcloud.com")
.setCustomSearchAPIRequestInitializer(new CustomSearchAPIRequestInitializer("AIzaSyCV8PTQ10rG5wo4E004dR3mcGD1RM_PrBw"))
.build();
Search execute = customSearchAPI
.cse().list().setCx(CX) .cse().list().setCx(CX)
.setKey(KEY) .setKey(KEY)
.setQ(query) .setQ(query)
.setStart(1L) .setStart(2L)
.setNum(3) .setNum(3)
.execute(); .execute();
List<Result> items = execute.getItems(); loadHTML(execute, results);
for (Result item : items) {
String link = item.getLink();
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);
}
}
WebSearchFunctionResult webSearchResult = new WebSearchFunctionResult();
webSearchResult.setContent(htmlContent);
results.add(webSearchResult);
}
logger.info("web search result:{}", results.toString()); logger.info("web search result:{}", results.toString());
return JsonUtils.serialize(results); return JsonUtils.serialize(results);
} catch (Exception e) { } catch (Exception e) {
...@@ -97,6 +94,35 @@ public class WebSearchFunction extends AbstractLargeModelFunction { ...@@ -97,6 +94,35 @@ public class WebSearchFunction extends AbstractLargeModelFunction {
} }
} }
private void loadHTML(Search execute, List<WebSearchFunctionResult> results) {
List<Result> items = execute.getItems();
try {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(3));
CountDownLatch countDownLatch = new CountDownLatch(items.size());
for (Result item : items) {
threadPoolExecutor.submit(() -> {
String link = item.getLink();
String htmlContent = DocumentLoad.htmlToMarkdown(link);
if (StringUtils.isNotBlank(htmlContent)) {
htmlContent = htmlContent.replaceAll(StringUtils.SPACE, StringUtils.EMPTY);
if (htmlContent.length() > 1500) {
int index = htmlContent.length() / 2;
htmlContent = htmlContent.substring(index - 725, index + 725);
}
}
WebSearchFunctionResult webSearchResult = new WebSearchFunctionResult();
webSearchResult.setContent(htmlContent);
results.add(webSearchResult);
countDownLatch.countDown();
});
}
countDownLatch.await(30, TimeUnit.SECONDS);
threadPoolExecutor.shutdown();
} catch (Exception e) {
logger.error("web search error:" + e.getMessage());
}
}
@Override @Override
public String getDesc() { public String getDesc() {
return DESC; return DESC;
......
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