[improvement](chat) Adjust related packages, add other SqlGeneration implementations, and rename EmbedLLMProxy to JavaLLMProxy. (#489)

This commit is contained in:
lexluo09
2023-12-11 10:24:22 +08:00
committed by GitHub
parent cb4b91878f
commit 899047dbd1
15 changed files with 77 additions and 26 deletions

View File

@@ -1,12 +1,12 @@
package com.tencent.supersonic.chat.parser;
import com.tencent.supersonic.chat.api.pojo.QueryContext;
import com.tencent.supersonic.chat.parser.plugin.function.FunctionCallPromptGenerator;
import com.tencent.supersonic.chat.parser.plugin.function.FunctionPromptGenerator;
import com.tencent.supersonic.chat.parser.plugin.function.FunctionReq;
import com.tencent.supersonic.chat.parser.plugin.function.FunctionResp;
import com.tencent.supersonic.chat.parser.sql.llm.SqlGeneration;
import com.tencent.supersonic.chat.parser.sql.llm.SqlGenerationFactory;
import com.tencent.supersonic.chat.parser.sql.llm.prompt.OutputFormat;
import com.tencent.supersonic.chat.parser.sql.llm.OutputFormat;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMResp;
import com.tencent.supersonic.common.util.ContextUtils;
@@ -14,14 +14,17 @@ import dev.langchain4j.model.chat.ChatLanguageModel;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
/**
* LLMProxy based on langchain4j Java version.
*/
@Slf4j
public class EmbedLLMProxy implements LLMProxy {
public class JavaLLMProxy implements LLMProxy {
@Override
public boolean isSkip(QueryContext queryContext) {
ChatLanguageModel chatLanguageModel = ContextUtils.getBean(ChatLanguageModel.class);
if (Objects.isNull(chatLanguageModel)) {
log.warn("chatLanguageModel is null, skip EmbedLLMProxy");
log.warn("chatLanguageModel is null, skip :{}", JavaLLMProxy.class.getName());
return true;
}
return false;
@@ -43,7 +46,7 @@ public class EmbedLLMProxy implements LLMProxy {
@Override
public FunctionResp requestFunction(FunctionReq functionReq) {
FunctionCallPromptGenerator promptGenerator = ContextUtils.getBean(FunctionCallPromptGenerator.class);
FunctionPromptGenerator promptGenerator = ContextUtils.getBean(FunctionPromptGenerator.class);
String functionCallPrompt = promptGenerator.generateFunctionCallPrompt(functionReq.getQueryText(),
functionReq.getPluginConfigs());

View File

@@ -32,7 +32,7 @@ public class PythonLLMProxy implements LLMProxy {
public boolean isSkip(QueryContext queryContext) {
LLMParserConfig llmParserConfig = ContextUtils.getBean(LLMParserConfig.class);
if (StringUtils.isEmpty(llmParserConfig.getUrl())) {
log.warn("llmParserUrl is empty, skip PythonLLMProxy, config:{}", llmParserConfig);
log.warn("llmParserUrl is empty, skip :{}", PythonLLMProxy.class.getName());
return true;
}
return false;

View File

@@ -1,6 +1,6 @@
package com.tencent.supersonic.chat.parser.plugin.function;
import com.tencent.supersonic.chat.parser.sql.llm.prompt.InputFormat;
import com.tencent.supersonic.chat.parser.sql.llm.InputFormat;
import com.tencent.supersonic.chat.plugin.PluginParseConfig;
import java.util.List;
import java.util.stream.Collectors;
@@ -9,7 +9,7 @@ import org.springframework.stereotype.Component;
@Component
@Slf4j
public class FunctionCallPromptGenerator {
public class FunctionPromptGenerator {
public String generateFunctionCallPrompt(String queryText, List<PluginParseConfig> toolConfigList) {
List<String> toolExplainList = toolConfigList.stream()

View File

@@ -1,4 +1,4 @@
package com.tencent.supersonic.chat.parser.sql.llm.prompt;
package com.tencent.supersonic.chat.parser.sql.llm;
import java.util.ArrayList;
import java.util.HashMap;

View File

@@ -0,0 +1,24 @@
package com.tencent.supersonic.chat.parser.sql.llm;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq.SqlGenerationMode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class OneStepsSqlGeneration implements SqlGeneration, InitializingBean {
@Override
public String generation(LLMReq llmReq, String modelClusterKey) {
//TODO
return "";
}
@Override
public void afterPropertiesSet() {
SqlGenerationFactory.addSqlGenerationForFactory(SqlGenerationMode.ONE_STEP, this);
}
}

View File

@@ -1,4 +1,4 @@
package com.tencent.supersonic.chat.parser.sql.llm.prompt;
package com.tencent.supersonic.chat.parser.sql.llm;
import com.tencent.supersonic.chat.parser.plugin.function.FunctionResp;
import com.tencent.supersonic.common.util.JsonUtil;

View File

@@ -1,4 +1,4 @@
package com.tencent.supersonic.chat.parser.sql.llm.prompt;
package com.tencent.supersonic.chat.parser.sql.llm;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

View File

@@ -1,4 +1,4 @@
package com.tencent.supersonic.chat.parser.sql.llm.prompt;
package com.tencent.supersonic.chat.parser.sql.llm;
import com.fasterxml.jackson.core.type.TypeReference;

View File

@@ -3,6 +3,9 @@ package com.tencent.supersonic.chat.parser.sql.llm;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq;
/**
* Sql Generation
*/
public interface SqlGeneration {
String generation(LLMReq llmReq, String modelClusterKey);

View File

@@ -1,4 +1,4 @@
package com.tencent.supersonic.chat.parser.sql.llm.prompt;
package com.tencent.supersonic.chat.parser.sql.llm;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq.ElementValue;
import java.util.Arrays;

View File

@@ -0,0 +1,24 @@
package com.tencent.supersonic.chat.parser.sql.llm;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq.SqlGenerationMode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class TwoStepCSSqlGeneration implements SqlGeneration, InitializingBean {
@Override
public String generation(LLMReq llmReq, String modelClusterKey) {
//TODO
return "";
}
@Override
public void afterPropertiesSet() {
SqlGenerationFactory.addSqlGenerationForFactory(SqlGenerationMode.TWO_STEP_CS, this);
}
}

View File

@@ -2,9 +2,6 @@ package com.tencent.supersonic.chat.parser.sql.llm;
import com.tencent.supersonic.chat.config.OptimizationConfig;
import com.tencent.supersonic.chat.parser.sql.llm.prompt.OutputFormat;
import com.tencent.supersonic.chat.parser.sql.llm.prompt.SqlExampleLoader;
import com.tencent.supersonic.chat.parser.sql.llm.prompt.SqlPromptGenerator;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq.ElementValue;
import com.tencent.supersonic.chat.query.llm.s2sql.LLMReq.SqlGenerationMode;
@@ -25,7 +22,7 @@ import org.springframework.stereotype.Service;
@Service
@Slf4j
public class TwoStepsSqlGeneration implements SqlGeneration, InitializingBean {
public class TwoStepSqlGeneration implements SqlGeneration, InitializingBean {
@Autowired
private ChatLanguageModel chatLanguageModel;
@@ -68,6 +65,6 @@ public class TwoStepsSqlGeneration implements SqlGeneration, InitializingBean {
@Override
public void afterPropertiesSet() {
SqlGenerationFactory.addSqlGenerationForFactory(SqlGenerationMode.TWO_STEPS, this);
SqlGenerationFactory.addSqlGenerationForFactory(SqlGenerationMode.TWO_STEP, this);
}
}

View File

@@ -18,7 +18,7 @@ public class LLMReq {
private String priorExts;
private SqlGenerationMode sqlGenerationMode = SqlGenerationMode.TWO_STEPS;
private SqlGenerationMode sqlGenerationMode = SqlGenerationMode.TWO_STEP;
@Data
public static class ElementValue {
@@ -50,9 +50,9 @@ public class LLMReq {
ONE_STEP("ONE_STEP"),
TWO_STEPS("TWO_STEPS"),
TWO_STEP("TWO_STEP"),
TWO_STEPS_WITH_CS("TWO_STEPS_WITH_CS");
TWO_STEP_CS("TWO_STEP_CS");
private String name;

View File

@@ -1,9 +1,9 @@
package com.tencent.supersonic;
import com.tencent.supersonic.chat.config.OptimizationConfig;
import com.tencent.supersonic.chat.parser.sql.llm.prompt.SqlExample;
import com.tencent.supersonic.chat.parser.sql.llm.prompt.SqlExampleLoader;
import com.tencent.supersonic.chat.parser.EmbedLLMProxy;
import com.tencent.supersonic.chat.parser.sql.llm.SqlExample;
import com.tencent.supersonic.chat.parser.sql.llm.SqlExampleLoader;
import com.tencent.supersonic.chat.parser.JavaLLMProxy;
import com.tencent.supersonic.chat.parser.LLMProxy;
import com.tencent.supersonic.chat.utils.ComponentFactory;
import java.util.List;
@@ -31,7 +31,7 @@ public class EmbeddingInitListener implements CommandLineRunner {
public void initSqlExamples() {
try {
if (llmProxy instanceof EmbedLLMProxy) {
if (llmProxy instanceof JavaLLMProxy) {
List<SqlExample> sqlExamples = sqlExampleLoader.getSqlExamples();
String collectionName = optimizationConfig.getText2sqlCollectionName();
sqlExampleLoader.addEmbeddingStore(sqlExamples, collectionName);

View File

@@ -31,7 +31,7 @@ com.tencent.supersonic.chat.processor.ParseResultProcessor=\
com.tencent.supersonic.chat.processor.RespBuildProcessor
com.tencent.supersonic.chat.parser.LLMProxy=\
com.tencent.supersonic.chat.parser.EmbedLLMProxy
com.tencent.supersonic.chat.parser.JavaLLMProxy
com.tencent.supersonic.chat.api.component.SemanticInterpreter=\
com.tencent.supersonic.knowledge.semantic.LocalSemanticInterpreter