[feature][headless-chat]Introduce ChatApp to support more flexible chat model config.#1739

This commit is contained in:
jerryjzhang
2024-10-12 15:05:47 +08:00
parent fc94a6718b
commit 7c76c69ac0
9 changed files with 25 additions and 21 deletions

View File

@@ -29,14 +29,14 @@ import static com.tencent.supersonic.chat.server.parser.ParserConfig.PARSER_MULT
public class PlainTextExecutor implements ChatQueryExecutor {
private 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."
+ "#Rules: 1.ALWAYS use the same language as the input.\n" + "#History Inputs: %s\n"
+ "#Current Input: %s\n" + "#Your response: ";
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`."
+ "\n#History Inputs: %s" + "\n#Current Input: %s" + "\n#Response: ";
public PlainTextExecutor() {
ChatAppManager.register(ChatApp.builder().key(APP_KEY).prompt(INSTRUCTION).name("闲聊对话")
.description("直接将原始输入透传大模型").enable(true).build());
ChatAppManager.register(APP_KEY, ChatApp.builder().prompt(INSTRUCTION).name("闲聊对话")
.description("直接将原始输入透传大模型").enable(false).build());
}
@Override

View File

@@ -49,7 +49,7 @@ public class MemoryReviewTask {
private AgentService agentService;
public MemoryReviewTask() {
ChatAppManager.register(ChatApp.builder().key(APP_KEY).prompt(INSTRUCTION).name("记忆启用评估")
ChatAppManager.register(APP_KEY, ChatApp.builder().prompt(INSTRUCTION).name("记忆启用评估")
.description("通过大模型对记忆做正确性评估以决定是否启用").enable(false).build());
}

View File

@@ -77,13 +77,13 @@ public class NL2SQLParser implements ChatQueryParser {
+ "#Examples: {{examples}}\n" + "#Response: ";
public NL2SQLParser() {
ChatAppManager.register(
ChatApp.builder().key(APP_KEY_MULTI_TURN).prompt(REWRITE_MULTI_TURN_INSTRUCTION)
ChatAppManager.register(APP_KEY_MULTI_TURN,
ChatApp.builder().prompt(REWRITE_MULTI_TURN_INSTRUCTION)
.name("多轮对话改写").description("通过大模型根据历史对话来改写本轮对话").enable(false).build());
ChatAppManager.register(ChatApp.builder().key(APP_KEY_ERROR_MESSAGE)
.prompt(REWRITE_ERROR_MESSAGE_INSTRUCTION).name("异常提示改写")
.description("通过大模型将异常信息改写为更友好和引导性的提示用语").enable(false).build());
ChatAppManager.register(APP_KEY_ERROR_MESSAGE,
ChatApp.builder().prompt(REWRITE_ERROR_MESSAGE_INSTRUCTION)
.name("异常提示改写").description("通过大模型将异常信息改写为更友好和引导性的提示用语").enable(false).build());
}
@Override

View File

@@ -16,8 +16,8 @@ import com.tencent.supersonic.common.util.ChatAppManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping({"/api/chat/model", "/openapi/chat/model"})
@@ -51,8 +51,8 @@ public class ChatModelController {
}
@RequestMapping("/getModelAppList")
public List<ChatApp> getModelAppList() {
return new ArrayList(ChatAppManager.getAllApps().values());
public Map<String, ChatApp> getChatAppList() {
return ChatAppManager.getAllApps();
}
@RequestMapping("/getModelParameters")

View File

@@ -15,6 +15,7 @@ import com.tencent.supersonic.chat.server.processor.execute.ExecuteResultProcess
import com.tencent.supersonic.chat.server.processor.parse.ParseResultProcessor;
import com.tencent.supersonic.chat.server.service.AgentService;
import com.tencent.supersonic.chat.server.service.ChatManageService;
import com.tencent.supersonic.chat.server.service.ChatModelService;
import com.tencent.supersonic.chat.server.service.ChatQueryService;
import com.tencent.supersonic.chat.server.util.ComponentFactory;
import com.tencent.supersonic.chat.server.util.QueryReqConverter;
@@ -86,6 +87,8 @@ public class ChatQueryServiceImpl implements ChatQueryService {
private SemanticLayerService semanticLayerService;
@Autowired
private AgentService agentService;
@Autowired
private ChatModelService chatModelService;
private List<ChatQueryParser> chatQueryParsers = ComponentFactory.getChatParsers();
private List<ChatQueryExecutor> chatQueryExecutors = ComponentFactory.getChatExecutors();
@@ -168,6 +171,8 @@ 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()));
parseContext.setAgent(agent);
return parseContext;
}