mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 19:51:00 +00:00
(improvement)(Chat) Format the results of the parse and query phases into text (#1045)
Co-authored-by: jolunoluo
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
package com.tencent.supersonic.chat.api.pojo.enums;
|
||||
|
||||
public enum DefaultShowType {
|
||||
|
||||
TEXT,
|
||||
TABLE,
|
||||
WIDGET
|
||||
|
||||
}
|
||||
@@ -33,7 +33,6 @@ public class Agent extends RecordInfo {
|
||||
private String agentConfig;
|
||||
private LLMConfig llmConfig;
|
||||
private MultiTurnConfig multiTurnConfig;
|
||||
private VisualConfig visualConfig;
|
||||
|
||||
public List<String> getTools(AgentToolType type) {
|
||||
Map map = JSONObject.parseObject(agentConfig, Map.class);
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.tencent.supersonic.chat.server.agent;
|
||||
|
||||
import com.tencent.supersonic.chat.api.pojo.enums.DefaultShowType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class VisualConfig {
|
||||
|
||||
private DefaultShowType defaultShowType;
|
||||
|
||||
}
|
||||
@@ -60,6 +60,4 @@ public class AgentDO {
|
||||
|
||||
private String multiTurnConfig;
|
||||
|
||||
private String visualConfig;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.tencent.supersonic.chat.server.processor.execute;
|
||||
|
||||
import com.tencent.supersonic.chat.server.pojo.ChatExecuteContext;
|
||||
import com.tencent.supersonic.chat.server.util.ResultFormatter;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.QueryResult;
|
||||
|
||||
public class QueryResultFormatProcessor implements ExecuteResultProcessor {
|
||||
|
||||
@Override
|
||||
public void process(ChatExecuteContext chatExecuteContext, QueryResult queryResult) {
|
||||
String textResult = ResultFormatter.transform2TextNew(queryResult.getQueryColumns(),
|
||||
queryResult.getQueryResults());
|
||||
queryResult.setTextResult(textResult);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.tencent.supersonic.chat.server.processor.parse;
|
||||
|
||||
import com.tencent.supersonic.chat.server.pojo.ChatParseContext;
|
||||
import com.tencent.supersonic.headless.api.pojo.SchemaElement;
|
||||
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
|
||||
import com.tencent.supersonic.headless.api.pojo.request.QueryFilter;
|
||||
import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ParseResultFormatProcessor implements ParseResultProcessor {
|
||||
|
||||
@Override
|
||||
public void process(ChatParseContext chatParseContext, ParseResp parseResp) {
|
||||
formatParseResult(parseResp);
|
||||
}
|
||||
|
||||
private void formatParseResult(ParseResp parseResp) {
|
||||
List<SemanticParseInfo> selectedParses = parseResp.getSelectedParses();
|
||||
for (SemanticParseInfo parseInfo : selectedParses) {
|
||||
formatParseInfo(parseInfo);
|
||||
}
|
||||
}
|
||||
|
||||
private void formatParseInfo(SemanticParseInfo parseInfo) {
|
||||
StringBuilder textBuilder = new StringBuilder();
|
||||
textBuilder.append("**数据集:** ").append(parseInfo.getDataSet().getName()).append(" ");
|
||||
Optional<SchemaElement> metric = parseInfo.getMetrics().stream().findFirst();
|
||||
metric.ifPresent(schemaElement ->
|
||||
textBuilder.append("**指标:** ").append(schemaElement.getName()).append(" "));
|
||||
List<String> dimensionNames = parseInfo.getDimensions().stream()
|
||||
.map(SchemaElement::getName).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
if (!CollectionUtils.isEmpty(dimensionNames)) {
|
||||
textBuilder.append("**维度:** ").append(String.join(",", dimensionNames));
|
||||
}
|
||||
textBuilder.append("\n**筛选条件:**\n");
|
||||
if (parseInfo.getDateInfo() != null) {
|
||||
textBuilder.append("数据时间:").append(parseInfo.getDateInfo().getStartDate()).append("~")
|
||||
.append(parseInfo.getDateInfo().getEndDate()).append(" ");
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(parseInfo.getDimensionFilters())
|
||||
|| CollectionUtils.isEmpty(parseInfo.getMetricFilters())) {
|
||||
Set<QueryFilter> queryFilters = parseInfo.getDimensionFilters();
|
||||
queryFilters.addAll(parseInfo.getMetricFilters());
|
||||
for (QueryFilter queryFilter : queryFilters) {
|
||||
textBuilder.append(queryFilter.getName())
|
||||
.append(" ")
|
||||
.append(queryFilter.getOperator().getValue())
|
||||
.append(" ")
|
||||
.append(queryFilter.getValue())
|
||||
.append(" ");
|
||||
}
|
||||
}
|
||||
parseInfo.setTextInfo(textBuilder.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,13 +3,12 @@ package com.tencent.supersonic.chat.server.service.impl;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||
import com.tencent.supersonic.chat.server.agent.Agent;
|
||||
import com.tencent.supersonic.headless.api.pojo.LLMConfig;
|
||||
import com.tencent.supersonic.chat.server.agent.MultiTurnConfig;
|
||||
import com.tencent.supersonic.chat.server.agent.VisualConfig;
|
||||
import com.tencent.supersonic.chat.server.persistence.dataobject.AgentDO;
|
||||
import com.tencent.supersonic.chat.server.persistence.mapper.AgentDOMapper;
|
||||
import com.tencent.supersonic.chat.server.service.AgentService;
|
||||
import com.tencent.supersonic.common.util.JsonUtil;
|
||||
import com.tencent.supersonic.headless.api.pojo.LLMConfig;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.List;
|
||||
@@ -66,7 +65,6 @@ public class AgentServiceImpl extends ServiceImpl<AgentDOMapper, AgentDO>
|
||||
agent.setExamples(JsonUtil.toList(agentDO.getExamples(), String.class));
|
||||
agent.setLlmConfig(JsonUtil.toObject(agentDO.getLlmConfig(), LLMConfig.class));
|
||||
agent.setMultiTurnConfig(JsonUtil.toObject(agentDO.getMultiTurnConfig(), MultiTurnConfig.class));
|
||||
agent.setVisualConfig(JsonUtil.toObject(agentDO.getVisualConfig(), VisualConfig.class));
|
||||
return agent;
|
||||
}
|
||||
|
||||
@@ -77,7 +75,6 @@ public class AgentServiceImpl extends ServiceImpl<AgentDOMapper, AgentDO>
|
||||
agentDO.setExamples(JsonUtil.toString(agent.getExamples()));
|
||||
agentDO.setLlmConfig(JsonUtil.toString(agent.getLlmConfig()));
|
||||
agentDO.setMultiTurnConfig(JsonUtil.toString(agent.getMultiTurnConfig()));
|
||||
agentDO.setVisualConfig(JsonUtil.toString(agent.getVisualConfig()));
|
||||
if (agentDO.getStatus() == null) {
|
||||
agentDO.setStatus(1);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.tencent.supersonic.chat.server.util;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.QueryColumn;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ResultFormatter {
|
||||
|
||||
public static String transform2TextNew(List<QueryColumn> queryColumns, List<Map<String, Object>> queryResults) {
|
||||
StringBuilder table = new StringBuilder();
|
||||
for (QueryColumn column : queryColumns) {
|
||||
String columnName = column.getName();
|
||||
table.append("| ").append(columnName).append(" ");
|
||||
}
|
||||
table.append("|\n");
|
||||
for (int i = 0; i < queryColumns.size(); i++) {
|
||||
table.append("|:---:");
|
||||
}
|
||||
table.append("|\n");
|
||||
for (Map<String, Object> row : queryResults) {
|
||||
for (QueryColumn column : queryColumns) {
|
||||
String columnKey = column.getNameEn();
|
||||
Object value = row.get(columnKey);
|
||||
table.append("| ").append(value != null ? value.toString() : "").append(" ");
|
||||
}
|
||||
table.append("|\n");
|
||||
}
|
||||
return table.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user