mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 11:07:06 +00:00
[improvement][chat]Adopt accept pattern to parsers and executors.
This commit is contained in:
@@ -5,5 +5,7 @@ import com.tencent.supersonic.chat.server.pojo.ExecuteContext;
|
||||
|
||||
public interface ChatQueryExecutor {
|
||||
|
||||
boolean accept(ExecuteContext executeContext);
|
||||
|
||||
QueryResult execute(ExecuteContext executeContext);
|
||||
}
|
||||
|
||||
@@ -37,11 +37,12 @@ public class PlainTextExecutor implements ChatQueryExecutor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryResult execute(ExecuteContext executeContext) {
|
||||
if (!"PLAIN_TEXT".equals(executeContext.getParseInfo().getQueryMode())) {
|
||||
return null;
|
||||
public boolean accept(ExecuteContext executeContext) {
|
||||
return "PLAIN_TEXT".equals(executeContext.getParseInfo().getQueryMode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryResult execute(ExecuteContext executeContext) {
|
||||
AgentService agentService = ContextUtils.getBean(AgentService.class);
|
||||
Agent chatAgent = agentService.getAgent(executeContext.getAgent().getId());
|
||||
ChatApp chatApp = chatAgent.getChatAppConfig().get(APP_KEY);
|
||||
|
||||
@@ -8,6 +8,11 @@ import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
|
||||
|
||||
public class PluginExecutor implements ChatQueryExecutor {
|
||||
|
||||
@Override
|
||||
public boolean accept(ExecuteContext executeContext) {
|
||||
return PluginQueryManager.isPluginQuery(executeContext.getParseInfo().getQueryMode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryResult execute(ExecuteContext executeContext) {
|
||||
SemanticParseInfo parseInfo = executeContext.getParseInfo();
|
||||
|
||||
@@ -25,6 +25,11 @@ import java.util.Objects;
|
||||
|
||||
public class SqlExecutor implements ChatQueryExecutor {
|
||||
|
||||
@Override
|
||||
public boolean accept(ExecuteContext executeContext) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public QueryResult execute(ExecuteContext executeContext) {
|
||||
|
||||
@@ -4,5 +4,7 @@ import com.tencent.supersonic.chat.server.pojo.ParseContext;
|
||||
|
||||
public interface ChatQueryParser {
|
||||
|
||||
boolean accept(ParseContext parseContext);
|
||||
|
||||
void parse(ParseContext parseContext);
|
||||
}
|
||||
|
||||
@@ -14,12 +14,12 @@ public class NL2PluginParser implements ChatQueryParser {
|
||||
private final List<PluginRecognizer> pluginRecognizers =
|
||||
ComponentFactory.getPluginRecognizers();
|
||||
|
||||
@Override
|
||||
public void parse(ParseContext parseContext) {
|
||||
if (!parseContext.getAgent().containsPluginTool()) {
|
||||
return;
|
||||
public boolean accept(ParseContext parseContext) {
|
||||
return parseContext.getAgent().containsPluginTool();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(ParseContext parseContext) {
|
||||
pluginRecognizers.forEach(pluginRecognizer -> {
|
||||
pluginRecognizer.recognize(parseContext);
|
||||
log.info("{} recallResult:{}", pluginRecognizer.getClass().getSimpleName(),
|
||||
|
||||
@@ -73,12 +73,12 @@ public class NL2SQLParser implements ChatQueryParser {
|
||||
.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(ParseContext parseContext) {
|
||||
if (!parseContext.enableNL2SQL()) {
|
||||
return;
|
||||
public boolean accept(ParseContext parseContext) {
|
||||
return parseContext.enableNL2SQL();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(ParseContext parseContext) {
|
||||
// first go with rule-based parsers unless the user has already selected one parse.
|
||||
if (Objects.isNull(parseContext.getRequest().getSelectedParse())) {
|
||||
QueryNLReq queryNLReq = QueryReqConverter.buildQueryNLReq(parseContext);
|
||||
|
||||
@@ -6,12 +6,12 @@ import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
|
||||
|
||||
public class PlainTextParser implements ChatQueryParser {
|
||||
|
||||
@Override
|
||||
public void parse(ParseContext parseContext) {
|
||||
if (parseContext.getAgent().containsAnyTool()) {
|
||||
return;
|
||||
public boolean accept(ParseContext parseContext) {
|
||||
return !parseContext.getAgent().containsAnyTool();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(ParseContext parseContext) {
|
||||
SemanticParseInfo parseInfo = new SemanticParseInfo();
|
||||
parseInfo.setQueryMode("PLAIN_TEXT");
|
||||
parseInfo.setId(1);
|
||||
|
||||
@@ -95,7 +95,11 @@ public class ChatQueryServiceImpl implements ChatQueryService {
|
||||
}
|
||||
|
||||
ParseContext parseContext = buildParseContext(chatParseReq, new ChatParseResp(queryId));
|
||||
chatQueryParsers.forEach(p -> p.parse(parseContext));
|
||||
for (ChatQueryParser parser : chatQueryParsers) {
|
||||
if (parser.accept(parseContext)) {
|
||||
parser.parse(parseContext);
|
||||
}
|
||||
}
|
||||
|
||||
for (ParseResultProcessor processor : parseResultProcessors) {
|
||||
if (processor.accept(parseContext)) {
|
||||
@@ -116,11 +120,13 @@ public class ChatQueryServiceImpl implements ChatQueryService {
|
||||
QueryResult queryResult = new QueryResult();
|
||||
ExecuteContext executeContext = buildExecuteContext(chatExecuteReq);
|
||||
for (ChatQueryExecutor chatQueryExecutor : chatQueryExecutors) {
|
||||
if (chatQueryExecutor.accept(executeContext)) {
|
||||
queryResult = chatQueryExecutor.execute(executeContext);
|
||||
if (queryResult != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
executeContext.setResponse(queryResult);
|
||||
if (queryResult != null) {
|
||||
|
||||
@@ -96,8 +96,7 @@ public class DatabaseServiceImpl extends ServiceImpl<DatabaseDOMapper, DatabaseD
|
||||
databaseResp.setHasEditPermission(true);
|
||||
databaseResp.setHasUsePermission(true);
|
||||
}
|
||||
if (databaseResp.getViewers().contains(user.getName())
|
||||
|| databaseResp.isPublic()) {
|
||||
if (databaseResp.getViewers().contains(user.getName()) || databaseResp.isPublic()) {
|
||||
databaseResp.setHasUsePermission(true);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user