(fix):fill values into param options in webservice query, some refactor (#557)

This commit is contained in:
Scott
2023-12-20 19:56:06 +08:00
committed by GitHub
parent ab077df36d
commit f7914ff6f4
3 changed files with 84 additions and 72 deletions

View File

@@ -1,8 +1,19 @@
package com.tencent.supersonic.chat.query.plugin; package com.tencent.supersonic.chat.query.plugin;
import com.google.common.collect.Lists;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.chat.api.pojo.SchemaElementMatch;
import com.tencent.supersonic.chat.api.pojo.SchemaElementType;
import com.tencent.supersonic.chat.api.pojo.request.QueryFilter;
import com.tencent.supersonic.chat.api.pojo.request.QueryFilters;
import com.tencent.supersonic.chat.api.pojo.request.QueryReq;
import com.tencent.supersonic.chat.plugin.PluginParseResult;
import com.tencent.supersonic.chat.query.BaseSemanticQuery; import com.tencent.supersonic.chat.query.BaseSemanticQuery;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
@Slf4j @Slf4j
public abstract class PluginSemanticQuery extends BaseSemanticQuery { public abstract class PluginSemanticQuery extends BaseSemanticQuery {
@@ -16,4 +27,73 @@ public abstract class PluginSemanticQuery extends BaseSemanticQuery {
public void initS2Sql(User user) { public void initS2Sql(User user) {
} }
private Map<Long, Object> getFilterMap(PluginParseResult pluginParseResult) {
Map<Long, Object> map = new HashMap<>();
QueryReq queryReq = pluginParseResult.getRequest();
if (queryReq == null || queryReq.getQueryFilters() == null) {
return map;
}
QueryFilters queryFilters = queryReq.getQueryFilters();
List<QueryFilter> queryFilterList = queryFilters.getFilters();
if (CollectionUtils.isEmpty(queryFilterList)) {
return map;
}
for (QueryFilter queryFilter : queryFilterList) {
map.put(queryFilter.getElementID(), queryFilter.getValue());
}
return map;
}
protected Map<String, Object> getElementMap(PluginParseResult pluginParseResult) {
Map<String, Object> elementValueMap = new HashMap<>();
Map<Long, Object> filterValueMap = getFilterMap(pluginParseResult);
List<SchemaElementMatch> schemaElementMatchList = parseInfo.getElementMatches();
if (!CollectionUtils.isEmpty(schemaElementMatchList)) {
schemaElementMatchList.stream()
.filter(schemaElementMatch ->
SchemaElementType.VALUE.equals(schemaElementMatch.getElement().getType())
|| SchemaElementType.ID.equals(schemaElementMatch.getElement().getType()))
.filter(schemaElementMatch -> schemaElementMatch.getSimilarity() == 1.0)
.forEach(schemaElementMatch -> {
Object queryFilterValue = filterValueMap.get(schemaElementMatch.getElement().getId());
if (queryFilterValue != null) {
if (String.valueOf(queryFilterValue).equals(String.valueOf(schemaElementMatch.getWord()))) {
elementValueMap.put(
String.valueOf(schemaElementMatch.getElement().getId()),
schemaElementMatch.getWord());
}
} else {
elementValueMap.computeIfAbsent(
String.valueOf(schemaElementMatch.getElement().getId()),
k -> schemaElementMatch.getWord());
}
});
}
return elementValueMap;
}
protected WebBase fillWebBaseResult(WebBase webPage, PluginParseResult pluginParseResult) {
WebBase webBaseResult = new WebBase();
webBaseResult.setUrl(webPage.getUrl());
Map<String, Object> elementValueMap = getElementMap(pluginParseResult);
List<ParamOption> paramOptions = Lists.newArrayList();
if (!CollectionUtils.isEmpty(webPage.getParamOptions()) && !CollectionUtils.isEmpty(elementValueMap)) {
for (ParamOption paramOption : webPage.getParamOptions()) {
if (paramOption.getModelId() != null
&& !parseInfo.getModel().getModelIds().contains(paramOption.getModelId())) {
continue;
}
paramOptions.add(paramOption);
if (!ParamOption.ParamType.SEMANTIC.equals(paramOption.getParamType())) {
continue;
}
String elementId = String.valueOf(paramOption.getElementId());
Object elementValue = elementValueMap.get(elementId);
paramOption.setValue(elementValue);
}
}
webBaseResult.setParamOptions(paramOptions);
return webBaseResult;
}
} }

View File

@@ -60,78 +60,8 @@ public class WebPageQuery extends PluginSemanticQuery {
webPageResponse.setPluginId(plugin.getId()); webPageResponse.setPluginId(plugin.getId());
webPageResponse.setPluginType(plugin.getType()); webPageResponse.setPluginType(plugin.getType());
WebBase webPage = JsonUtil.toObject(plugin.getConfig(), WebBase.class); WebBase webPage = JsonUtil.toObject(plugin.getConfig(), WebBase.class);
WebBase webBase = buildWebPageResult(webPage, pluginParseResult); WebBase webBase = fillWebBaseResult(webPage, pluginParseResult);
webPageResponse.setWebPage(webBase); webPageResponse.setWebPage(webBase);
return webPageResponse; return webPageResponse;
} }
private WebBase buildWebPageResult(WebBase webPage, PluginParseResult pluginParseResult) {
WebBase webBaseResult = new WebBase();
webBaseResult.setUrl(webPage.getUrl());
Map<String, Object> elementValueMap = getElementMap(pluginParseResult);
List<ParamOption> paramOptions = Lists.newArrayList();
if (!CollectionUtils.isEmpty(webPage.getParamOptions()) && !CollectionUtils.isEmpty(elementValueMap)) {
for (ParamOption paramOption : webPage.getParamOptions()) {
if (paramOption.getModelId() != null
&& !parseInfo.getModel().getModelIds().contains(paramOption.getModelId())) {
continue;
}
paramOptions.add(paramOption);
if (!ParamOption.ParamType.SEMANTIC.equals(paramOption.getParamType())) {
continue;
}
String elementId = String.valueOf(paramOption.getElementId());
Object elementValue = elementValueMap.get(elementId);
paramOption.setValue(elementValue);
}
}
webBaseResult.setParamOptions(paramOptions);
return webBaseResult;
}
protected Map<String, Object> getElementMap(PluginParseResult pluginParseResult) {
Map<String, Object> elementValueMap = new HashMap<>();
Map<Long, Object> filterValueMap = getFilterMap(pluginParseResult);
List<SchemaElementMatch> schemaElementMatchList = parseInfo.getElementMatches();
if (!CollectionUtils.isEmpty(schemaElementMatchList)) {
schemaElementMatchList.stream()
.filter(schemaElementMatch ->
SchemaElementType.VALUE.equals(schemaElementMatch.getElement().getType())
|| SchemaElementType.ID.equals(schemaElementMatch.getElement().getType()))
.filter(schemaElementMatch -> schemaElementMatch.getSimilarity() == 1.0)
.forEach(schemaElementMatch -> {
Object queryFilterValue = filterValueMap.get(schemaElementMatch.getElement().getId());
if (queryFilterValue != null) {
if (String.valueOf(queryFilterValue).equals(String.valueOf(schemaElementMatch.getWord()))) {
elementValueMap.put(
String.valueOf(schemaElementMatch.getElement().getId()),
schemaElementMatch.getWord());
}
} else {
elementValueMap.computeIfAbsent(
String.valueOf(schemaElementMatch.getElement().getId()),
k -> schemaElementMatch.getWord());
}
});
}
return elementValueMap;
}
private Map<Long, Object> getFilterMap(PluginParseResult pluginParseResult) {
Map<Long, Object> map = new HashMap<>();
QueryReq queryReq = pluginParseResult.getRequest();
if (queryReq == null || queryReq.getQueryFilters() == null) {
return map;
}
QueryFilters queryFilters = queryReq.getQueryFilters();
List<QueryFilter> queryFilterList = queryFilters.getFilters();
if (CollectionUtils.isEmpty(queryFilterList)) {
return map;
}
for (QueryFilter queryFilter : queryFilterList) {
map.put(queryFilter.getElementID(), queryFilter.getValue());
}
return map;
}
} }

View File

@@ -1,6 +1,7 @@
package com.tencent.supersonic.chat.query.plugin.webservice; package com.tencent.supersonic.chat.query.plugin.webservice;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.chat.api.pojo.response.QueryResult; import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
import com.tencent.supersonic.chat.api.pojo.response.QueryState; import com.tencent.supersonic.chat.api.pojo.response.QueryState;
@@ -26,6 +27,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
@@ -75,7 +77,7 @@ public class WebServiceQuery extends PluginSemanticQuery {
protected WebServiceResp buildResponse(PluginParseResult pluginParseResult) { protected WebServiceResp buildResponse(PluginParseResult pluginParseResult) {
WebServiceResp webServiceResponse = new WebServiceResp(); WebServiceResp webServiceResponse = new WebServiceResp();
Plugin plugin = pluginParseResult.getPlugin(); Plugin plugin = pluginParseResult.getPlugin();
WebBase webBase = JsonUtil.toObject(plugin.getConfig(), WebBase.class); WebBase webBase = fillWebBaseResult(JsonUtil.toObject(plugin.getConfig(), WebBase.class), pluginParseResult);
webServiceResponse.setWebBase(webBase); webServiceResponse.setWebBase(webBase);
List<ParamOption> paramOptions = webBase.getParamOptions(); List<ParamOption> paramOptions = webBase.getParamOptions();
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();