Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 7c31b71

Browse files
authored
feat: cortex chat/run/models start with new model data structure (#1301)
* feat: cortex chat/run/models start with new model data structure * temp * fix: use model_id from model service and std::get_line * f:m
1 parent e89a72f commit 7c31b71

File tree

16 files changed

+183
-118
lines changed

16 files changed

+183
-118
lines changed

engine/commands/chat_cmd.cc

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "server_start_cmd.h"
77
#include "trantor/utils/Logger.h"
88
#include "utils/logging_utils.h"
9+
#include "utils/modellist_utils.h"
910

1011
namespace commands {
1112
namespace {
@@ -36,23 +37,36 @@ struct ChunkParser {
3637
}
3738
};
3839

39-
ChatCmd::ChatCmd(std::string host, int port, const config::ModelConfig& mc)
40-
: host_(std::move(host)), port_(port), mc_(mc) {}
40+
void ChatCmd::Exec(const std::string& host, int port,
41+
const std::string& model_handle, std::string msg) {
42+
modellist_utils::ModelListUtils modellist_handler;
43+
config::YamlHandler yaml_handler;
44+
try {
45+
auto model_entry = modellist_handler.GetModelInfo(model_handle);
46+
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
47+
auto mc = yaml_handler.GetModelConfig();
48+
Exec(host, port, mc, std::move(msg));
49+
} catch (const std::exception& e) {
50+
CLI_LOG("Fail to start model information with ID '" + model_handle +
51+
"': " + e.what());
52+
}
53+
}
4154

42-
void ChatCmd::Exec(std::string msg) {
55+
void ChatCmd::Exec(const std::string& host, int port,
56+
const config::ModelConfig& mc, std::string msg) {
57+
auto address = host + ":" + std::to_string(port);
4358
// Check if server is started
4459
{
45-
if (!commands::IsServerAlive(host_, port_)) {
60+
if (!commands::IsServerAlive(host, port)) {
4661
CLI_LOG("Server is not started yet, please run `"
4762
<< commands::GetCortexBinary() << " start` to start server!");
4863
return;
4964
}
5065
}
5166

52-
auto address = host_ + ":" + std::to_string(port_);
5367
// Only check if llamacpp engine
54-
if ((mc_.engine.find("llamacpp") != std::string::npos) &&
55-
!commands::ModelStatusCmd().IsLoaded(host_, port_, mc_)) {
68+
if ((mc.engine.find("llamacpp") != std::string::npos) &&
69+
!commands::ModelStatusCmd().IsLoaded(host, port, mc)) {
5670
CLI_LOG("Model is not loaded yet!");
5771
return;
5872
}
@@ -78,12 +92,12 @@ void ChatCmd::Exec(std::string msg) {
7892
new_data["role"] = kUser;
7993
new_data["content"] = user_input;
8094
histories_.push_back(std::move(new_data));
81-
json_data["engine"] = mc_.engine;
95+
json_data["engine"] = mc.engine;
8296
json_data["messages"] = histories_;
83-
json_data["model"] = mc_.name;
97+
json_data["model"] = mc.name;
8498
//TODO: support non-stream
8599
json_data["stream"] = true;
86-
json_data["stop"] = mc_.stop;
100+
json_data["stop"] = mc.stop;
87101
auto data_str = json_data.dump();
88102
// std::cout << data_str << std::endl;
89103
cli.set_read_timeout(std::chrono::seconds(60));

engine/commands/chat_cmd.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
namespace commands {
88
class ChatCmd {
99
public:
10-
ChatCmd(std::string host, int port, const config::ModelConfig& mc);
11-
void Exec(std::string msg);
10+
void Exec(const std::string& host, int port, const std::string& model_handle,
11+
std::string msg);
12+
void Exec(const std::string& host, int port, const config::ModelConfig& mc,
13+
std::string msg);
1214

1315
private:
14-
std::string host_;
15-
int port_;
16-
const config::ModelConfig& mc_;
1716
std::vector<nlohmann::json> histories_;
1817
};
1918
} // namespace commands

engine/commands/model_start_cmd.cc

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,59 @@
77
#include "trantor/utils/Logger.h"
88
#include "utils/file_manager_utils.h"
99
#include "utils/logging_utils.h"
10+
#include "utils/modellist_utils.h"
1011

1112
namespace commands {
12-
ModelStartCmd::ModelStartCmd(std::string host, int port,
13-
const config::ModelConfig& mc)
14-
: host_(std::move(host)), port_(port), mc_(mc) {}
13+
bool ModelStartCmd::Exec(const std::string& host, int port,
14+
const std::string& model_handle) {
1515

16-
bool ModelStartCmd::Exec() {
16+
modellist_utils::ModelListUtils modellist_handler;
17+
config::YamlHandler yaml_handler;
18+
try {
19+
auto model_entry = modellist_handler.GetModelInfo(model_handle);
20+
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
21+
auto mc = yaml_handler.GetModelConfig();
22+
return Exec(host, port, mc);
23+
} catch (const std::exception& e) {
24+
CLI_LOG("Fail to start model information with ID '" + model_handle +
25+
"': " + e.what());
26+
return false;
27+
}
28+
}
29+
30+
bool ModelStartCmd::Exec(const std::string& host, int port,
31+
const config::ModelConfig& mc) {
1732
// Check if server is started
18-
if (!commands::IsServerAlive(host_, port_)) {
33+
if (!commands::IsServerAlive(host, port)) {
1934
CLI_LOG("Server is not started yet, please run `"
2035
<< commands::GetCortexBinary() << " start` to start server!");
2136
return false;
2237
}
38+
2339
// Only check for llamacpp for now
24-
if ((mc_.engine.find("llamacpp") != std::string::npos) &&
25-
commands::ModelStatusCmd().IsLoaded(host_, port_, mc_)) {
40+
if ((mc.engine.find("llamacpp") != std::string::npos) &&
41+
commands::ModelStatusCmd().IsLoaded(host, port, mc)) {
2642
CLI_LOG("Model has already been started!");
2743
return true;
2844
}
2945

30-
httplib::Client cli(host_ + ":" + std::to_string(port_));
46+
httplib::Client cli(host + ":" + std::to_string(port));
3147

3248
nlohmann::json json_data;
33-
if (mc_.files.size() > 0) {
49+
if (mc.files.size() > 0) {
3450
// TODO(sang) support multiple files
35-
json_data["model_path"] = mc_.files[0];
51+
json_data["model_path"] = mc.files[0];
3652
} else {
3753
LOG_WARN << "model_path is empty";
3854
return false;
3955
}
40-
json_data["model"] = mc_.name;
41-
json_data["system_prompt"] = mc_.system_template;
42-
json_data["user_prompt"] = mc_.user_template;
43-
json_data["ai_prompt"] = mc_.ai_template;
44-
json_data["ctx_len"] = mc_.ctx_len;
45-
json_data["stop"] = mc_.stop;
46-
json_data["engine"] = mc_.engine;
56+
json_data["model"] = mc.name;
57+
json_data["system_prompt"] = mc.system_template;
58+
json_data["user_prompt"] = mc.user_template;
59+
json_data["ai_prompt"] = mc.ai_template;
60+
json_data["ctx_len"] = mc.ctx_len;
61+
json_data["stop"] = mc.stop;
62+
json_data["engine"] = mc.engine;
4763

4864
auto data_str = json_data.dump();
4965
cli.set_read_timeout(std::chrono::seconds(60));
@@ -52,13 +68,17 @@ bool ModelStartCmd::Exec() {
5268
if (res) {
5369
if (res->status == httplib::StatusCode::OK_200) {
5470
CLI_LOG("Model loaded!");
71+
return true;
72+
} else {
73+
CTL_ERR("Model failed to load with status code: " << res->status);
74+
return false;
5575
}
5676
} else {
5777
auto err = res.error();
5878
CTL_ERR("HTTP error: " << httplib::to_string(err));
5979
return false;
6080
}
61-
return true;
81+
return false;
6282
}
6383

6484
}; // namespace commands

engine/commands/model_start_cmd.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ namespace commands {
66

77
class ModelStartCmd {
88
public:
9-
explicit ModelStartCmd(std::string host, int port,
10-
const config::ModelConfig& mc);
11-
bool Exec();
9+
bool Exec(const std::string& host, int port, const std::string& model_handle);
1210

13-
private:
14-
std::string host_;
15-
int port_;
16-
const config::ModelConfig& mc_;
11+
bool Exec(const std::string& host, int port, const config::ModelConfig& mc);
1712
};
1813
} // namespace commands

engine/commands/model_status_cmd.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,25 @@
33
#include "httplib.h"
44
#include "nlohmann/json.hpp"
55
#include "utils/logging_utils.h"
6+
#include "utils/modellist_utils.h"
67

78
namespace commands {
9+
bool ModelStatusCmd::IsLoaded(const std::string& host, int port,
10+
const std::string& model_handle) {
11+
modellist_utils::ModelListUtils modellist_handler;
12+
config::YamlHandler yaml_handler;
13+
try {
14+
auto model_entry = modellist_handler.GetModelInfo(model_handle);
15+
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
16+
auto mc = yaml_handler.GetModelConfig();
17+
return IsLoaded(host, port, mc);
18+
} catch (const std::exception& e) {
19+
CLI_LOG("Fail to get model status with ID '" + model_handle +
20+
"': " + e.what());
21+
return false;
22+
}
23+
}
24+
825
bool ModelStatusCmd::IsLoaded(const std::string& host, int port,
926
const config::ModelConfig& mc) {
1027
httplib::Client cli(host + ":" + std::to_string(port));

engine/commands/model_status_cmd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace commands {
66

77
class ModelStatusCmd {
88
public:
9+
bool IsLoaded(const std::string& host, int port,
10+
const std::string& model_handle);
911
bool IsLoaded(const std::string& host, int port,
1012
const config::ModelConfig& mc);
1113
};

engine/commands/run_cmd.cc

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,76 @@
55
#include "model_start_cmd.h"
66
#include "model_status_cmd.h"
77
#include "server_start_cmd.h"
8+
#include "utils/cortex_utils.h"
89
#include "utils/file_manager_utils.h"
9-
10+
#include "utils/modellist_utils.h"
1011
namespace commands {
1112

1213
void RunCmd::Exec() {
14+
std::optional<std::string> model_id = model_handle_;
15+
16+
modellist_utils::ModelListUtils modellist_handler;
17+
config::YamlHandler yaml_handler;
1318
auto address = host_ + ":" + std::to_string(port_);
14-
CmdInfo ci(model_id_);
15-
std::string model_file =
16-
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
17-
// TODO should we clean all resource if something fails?
18-
// Check if model existed. If not, download it
19-
{
20-
auto model_conf = model_service_.GetDownloadedModel(model_file + ".yaml");
21-
if (!model_conf.has_value()) {
22-
model_service_.DownloadModel(model_id_);
23-
}
24-
}
2519

26-
// Check if engine existed. If not, download it
20+
// Download model if it does not exist
2721
{
28-
auto required_engine = engine_service_.GetEngineInfo(ci.engine_name);
29-
if (!required_engine.has_value()) {
30-
throw std::runtime_error("Engine not found: " + ci.engine_name);
31-
}
32-
if (required_engine.value().status == EngineService::kIncompatible) {
33-
throw std::runtime_error("Engine " + ci.engine_name + " is incompatible");
34-
}
35-
if (required_engine.value().status == EngineService::kNotInstalled) {
36-
engine_service_.InstallEngine(ci.engine_name);
22+
if (!modellist_handler.HasModel(model_handle_)) {
23+
model_id = model_service_.DownloadModel(model_handle_);
24+
if (!model_id.has_value()) {
25+
CTL_ERR("Error: Could not get model_id from handle: " << model_handle_);
26+
return;
27+
} else {
28+
CTL_INF("model_id: " << model_id.value());
29+
}
3730
}
3831
}
3932

40-
// Start server if it is not running
41-
{
42-
if (!commands::IsServerAlive(host_, port_)) {
43-
CLI_LOG("Starting server ...");
44-
commands::ServerStartCmd ssc;
45-
if (!ssc.Exec(host_, port_)) {
46-
return;
33+
try {
34+
auto model_entry = modellist_handler.GetModelInfo(*model_id);
35+
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
36+
auto mc = yaml_handler.GetModelConfig();
37+
38+
// Check if engine existed. If not, download it
39+
{
40+
auto required_engine = engine_service_.GetEngineInfo(mc.engine);
41+
if (!required_engine.has_value()) {
42+
throw std::runtime_error("Engine not found: " + mc.engine);
43+
}
44+
if (required_engine.value().status == EngineService::kIncompatible) {
45+
throw std::runtime_error("Engine " + mc.engine + " is incompatible");
46+
}
47+
if (required_engine.value().status == EngineService::kNotInstalled) {
48+
engine_service_.InstallEngine(mc.engine);
4749
}
4850
}
49-
}
5051

51-
config::YamlHandler yaml_handler;
52-
yaml_handler.ModelConfigFromFile(
53-
file_manager_utils::GetModelsContainerPath().string() + "/" + model_file +
54-
".yaml");
55-
auto mc = yaml_handler.GetModelConfig();
52+
// Start server if it is not running
53+
{
54+
if (!commands::IsServerAlive(host_, port_)) {
55+
CLI_LOG("Starting server ...");
56+
commands::ServerStartCmd ssc;
57+
if (!ssc.Exec(host_, port_)) {
58+
return;
59+
}
60+
}
61+
}
5662

57-
// Always start model if not llamacpp
58-
// If it is llamacpp, then check model status first
59-
{
60-
if ((mc.engine.find("llamacpp") == std::string::npos) ||
61-
!commands::ModelStatusCmd().IsLoaded(host_, port_, mc)) {
62-
ModelStartCmd msc(host_, port_, mc);
63-
if (!msc.Exec()) {
64-
return;
63+
// Always start model if not llamacpp
64+
// If it is llamacpp, then check model status first
65+
{
66+
if ((mc.engine.find("llamacpp") == std::string::npos) ||
67+
!commands::ModelStatusCmd().IsLoaded(host_, port_, mc)) {
68+
if (!ModelStartCmd().Exec(host_, port_, mc)) {
69+
return;
70+
}
6571
}
6672
}
67-
}
6873

69-
// Chat
70-
{
71-
ChatCmd cc(host_, port_, mc);
72-
cc.Exec("");
74+
// Chat
75+
ChatCmd().Exec(host_, port_, mc, "");
76+
} catch (const std::exception& e) {
77+
CLI_LOG("Fail to run model with ID '" + model_handle_ + "': " + e.what());
7378
}
7479
}
7580
}; // namespace commands

engine/commands/run_cmd.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
namespace commands {
77
class RunCmd {
88
public:
9-
explicit RunCmd(std::string host, int port, std::string model_id)
9+
explicit RunCmd(std::string host, int port, std::string model_handle)
1010
: host_{std::move(host)},
1111
port_{port},
12-
model_id_{std::move(model_id)},
12+
model_handle_{std::move(model_handle)},
1313
model_service_{ModelService()} {};
1414

1515
void Exec();
1616

1717
private:
1818
std::string host_;
1919
int port_;
20-
std::string model_id_;
20+
std::string model_handle_;
2121

2222
ModelService model_service_;
2323
EngineService engine_service_;

0 commit comments

Comments
 (0)