From d4697e8630e2838029a8a5bfa7ae103ad7b0f8ec Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Wed, 25 Sep 2024 07:10:54 +0700 Subject: [PATCH] fix: cortex chat is a non-interactive request --- engine/commands/chat_cmd.cc | 13 +++++++++---- engine/controllers/command_line_parser.cc | 10 +++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/engine/commands/chat_cmd.cc b/engine/commands/chat_cmd.cc index e4d0eda3d..922dc32ed 100644 --- a/engine/commands/chat_cmd.cc +++ b/engine/commands/chat_cmd.cc @@ -71,14 +71,19 @@ void ChatCmd::Exec(const std::string& host, int port, return; } + // Interactive mode or not + bool interactive = msg.empty(); + // Some instruction for user here - std::cout << "Inorder to exit, type `exit()`" << std::endl; + if (interactive) { + std::cout << "Inorder to exit, type `exit()`" << std::endl; + } // Model is loaded, start to chat { - while (true) { + do { std::string user_input = std::move(msg); - std::cout << "> "; if (user_input.empty()) { + std::cout << "> "; std::getline(std::cin, user_input); } if (user_input == kExitChat) { @@ -128,7 +133,7 @@ void ChatCmd::Exec(const std::string& host, int port, histories_.push_back(std::move(ai_res)); } // std::cout << "ok Done" << std::endl; - } + } while (interactive); } } diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index 74155a316..f9b6a04e6 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -122,20 +122,20 @@ void CommandLineParser::SetupCommonCommands() { auto chat_cmd = app_.add_subcommand("chat", "Send a chat completion request"); chat_cmd->group(kCommonCommandsGroup); chat_cmd->usage("Usage:\n" + commands::GetCortexBinary() + - " chat [model_id] [options]"); + " chat [model_id] -m [msg]"); chat_cmd->add_option("model_id", cml_data_.model_id, ""); chat_cmd->add_option("-m,--message", cml_data_.msg, "Message to chat with model"); chat_cmd->callback([this, chat_cmd] { - if (cml_data_.model_id.empty()) { - CLI_LOG("[model_id] is required\n"); + if (cml_data_.model_id.empty() || cml_data_.msg.empty()) { + CLI_LOG("[model_id] and [msg] are required\n"); CLI_LOG(chat_cmd->help()); return; } commands::ChatCmd().Exec(cml_data_.config.apiServerHost, - std::stoi(cml_data_.config.apiServerPort), cml_data_.model_id, - cml_data_.msg); + std::stoi(cml_data_.config.apiServerPort), + cml_data_.model_id, cml_data_.msg); }); }