Skip to content

Commit

Permalink
[feature][headless-chat]Introduce ChatApp to support more flexible ch…
Browse files Browse the repository at this point in the history
…at model config.#1739
  • Loading branch information
jerryjzhang committed Oct 12, 2024
1 parent fc94a67 commit 7c76c69
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
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"})
Expand Down Expand Up @@ -51,8 +51,8 @@ public List<ChatModel> getModelList() {
}

@RequestMapping("/getModelAppList")
public List<ChatApp> getModelAppList() {
return new ArrayList(ChatAppManager.getAllApps().values());
public Map<String, ChatApp> getChatAppList() {
return ChatAppManager.getAllApps();
}

@RequestMapping("/getModelParameters")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -168,6 +171,8 @@ private ParseContext buildParseContext(ChatParseReq chatParseReq) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@AllArgsConstructor
@NoArgsConstructor
public class ChatApp {
private String key;
private String name;
private String description;
private String prompt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
public class ChatAppManager {
private static final Map<String, ChatApp> chatApps = Maps.newConcurrentMap();

public static void register(ChatApp chatApp) {
chatApps.put(chatApp.getKey(), chatApp);
public static void register(String key, ChatApp app) {
chatApps.put(key, app);
}

public static Map<String, ChatApp> getAllApps() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class LLMSqlCorrector extends BaseSemanticCorrector {
+ "\n#Question:{{question}} #InputSQL:{{sql}} #Response:";

public LLMSqlCorrector() {
ChatAppManager.register(ChatApp.builder().key(APP_KEY).prompt(INSTRUCTION).name("语义SQL修正")
.description("").enable(false).build());
ChatAppManager.register(APP_KEY, ChatApp.builder().prompt(INSTRUCTION).name("语义SQL修正")
.description("通过大模型对解析S2SQL做二次修正").enable(false).build());
}

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class OnePassSCSqlGenStrategy extends SqlGenStrategy {
+ "\n#Question: Question:{{question}},Schema:{{schema}},SideInfo:{{information}}";

public OnePassSCSqlGenStrategy() {
ChatAppManager.register(ChatApp.builder().key(APP_KEY).prompt(INSTRUCTION).name("语义SQL解析")
ChatAppManager.register(APP_KEY, ChatApp.builder().prompt(INSTRUCTION).name("语义SQL解析")
.description("通过大模型做语义解析生成S2SQL").enable(true).build());
}

Expand Down

0 comments on commit 7c76c69

Please sign in to comment.