[fix][chat]Fix NPE problem.

This commit is contained in:
jerryjzhang
2024-10-16 13:08:22 +08:00
parent 3df4011f81
commit d064393253
7 changed files with 40 additions and 11 deletions

View File

@@ -25,7 +25,7 @@ import java.util.stream.Collectors;
public class PlainTextExecutor implements ChatQueryExecutor {
private static final String APP_KEY = "SMALL_TALK";
public static final String APP_KEY = "SMALL_TALK";
private static final String INSTRUCTION = "" + "#Role: You are a nice person to talk to."
+ "\n#Task: Respond quickly and nicely to the user."
+ "\n#Rules: 1.ALWAYS use the same language as the `#Current Input`."
@@ -45,7 +45,7 @@ public class PlainTextExecutor implements ChatQueryExecutor {
AgentService agentService = ContextUtils.getBean(AgentService.class);
Agent chatAgent = agentService.getAgent(executeContext.getAgent().getId());
ChatApp chatApp = chatAgent.getChatAppConfig().get(APP_KEY);
if (!chatApp.isEnable()) {
if (Objects.isNull(chatApp) || !chatApp.isEnable()) {
return null;
}

View File

@@ -75,7 +75,7 @@ public class MemoryReviewTask {
}
ChatApp chatApp = chatAgent.getChatAppConfig().get(APP_KEY);
if (!chatApp.isEnable()) {
if (Objects.isNull(chatApp) || !chatApp.isEnable()) {
return;
}

View File

@@ -173,7 +173,7 @@ public class NL2SQLParser implements ChatQueryParser {
private void processMultiTurn(ParseContext parseContext) {
ChatApp chatApp = parseContext.getAgent().getChatAppConfig().get(APP_KEY_MULTI_TURN);
if (!chatApp.isEnable()) {
if (Objects.isNull(chatApp) || !chatApp.isEnable()) {
return;
}
@@ -222,7 +222,7 @@ public class NL2SQLParser implements ChatQueryParser {
List<Text2SQLExemplar> similarExemplars) {
ChatApp chatApp = parseContext.getAgent().getChatAppConfig().get(APP_KEY_ERROR_MESSAGE);
if (!chatApp.isEnable()) {
if (Objects.isNull(chatApp) || !chatApp.isEnable()) {
return errMsg;
}

View File

@@ -9,6 +9,7 @@ import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
import com.tencent.supersonic.chat.server.agent.Agent;
import com.tencent.supersonic.chat.server.executor.ChatQueryExecutor;
import com.tencent.supersonic.chat.server.parser.ChatQueryParser;
import com.tencent.supersonic.chat.server.pojo.ChatModel;
import com.tencent.supersonic.chat.server.pojo.ExecuteContext;
import com.tencent.supersonic.chat.server.pojo.ParseContext;
import com.tencent.supersonic.chat.server.processor.execute.ExecuteResultProcessor;
@@ -171,8 +172,12 @@ public class ChatQueryServiceImpl implements ChatQueryService {
ParseContext parseContext = new ParseContext();
BeanMapper.mapper(chatParseReq, parseContext);
Agent agent = agentService.getAgent(chatParseReq.getAgentId());
agent.getChatAppConfig().values().forEach(c -> c
.setChatModelConfig(chatModelService.getChatModel(c.getChatModelId()).getConfig()));
agent.getChatAppConfig().values().forEach(c -> {
ChatModel chatModel = chatModelService.getChatModel(c.getChatModelId());
if (Objects.nonNull(chatModel)) {
c.setChatModelConfig(chatModelService.getChatModel(c.getChatModelId()).getConfig());
}
});
parseContext.setAgent(agent);
return parseContext;
}

View File

@@ -1,6 +1,7 @@
package com.tencent.supersonic.chat.server.util;
import com.tencent.supersonic.chat.server.agent.Agent;
import com.tencent.supersonic.chat.server.pojo.ChatModel;
import com.tencent.supersonic.chat.server.service.ChatModelService;
import com.tencent.supersonic.common.pojo.ChatApp;
import com.tencent.supersonic.common.pojo.ChatModelConfig;
@@ -12,6 +13,8 @@ import dev.langchain4j.provider.ModelProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.Objects;
@Slf4j
public class ModelConfigHelper {
public static boolean testConnection(ChatModelConfig modelConfig) {
@@ -30,8 +33,11 @@ public class ModelConfigHelper {
public static ChatModelConfig getChatModelConfig(ChatApp chatApp) {
ChatModelService chatModelService = ContextUtils.getBean(ChatModelService.class);
ChatModelConfig chatModelConfig =
chatModelService.getChatModel(chatApp.getChatModelId()).getConfig();
return chatModelConfig;
ChatModel chatModel = chatModelService.getChatModel(chatApp.getChatModelId());
if (Objects.isNull(chatModel)) {
return null;
}
return chatModel.getConfig();
}
}