|
5 | 5 | #include "model_status_cmd.h" |
6 | 6 | #include "nlohmann/json.hpp" |
7 | 7 | #include "server_start_cmd.h" |
| 8 | +#include "services/model_service.h" |
8 | 9 | #include "trantor/utils/Logger.h" |
9 | 10 | #include "utils/file_manager_utils.h" |
10 | 11 | #include "utils/logging_utils.h" |
11 | 12 |
|
12 | 13 | namespace commands { |
13 | 14 | bool ModelStartCmd::Exec(const std::string& host, int port, |
14 | 15 | const std::string& model_handle) { |
| 16 | + ModelService ms; |
| 17 | + auto res = ms.StartModel(host, port, model_handle); |
15 | 18 |
|
16 | | - cortex::db::Models modellist_handler; |
17 | | - config::YamlHandler yaml_handler; |
18 | | - try { |
19 | | - auto model_entry = modellist_handler.GetModelInfo(model_handle); |
20 | | - if (model_entry.has_error()) { |
21 | | - CLI_LOG("Error: " + model_entry.error()); |
22 | | - return false; |
23 | | - } |
24 | | - yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml); |
25 | | - auto mc = yaml_handler.GetModelConfig(); |
26 | | - return Exec(host, port, mc); |
27 | | - } catch (const std::exception& e) { |
28 | | - CLI_LOG("Fail to start model information with ID '" + model_handle + |
29 | | - "': " + e.what()); |
| 19 | + if (res.has_error()) { |
| 20 | + CLI_LOG("Error: " + res.error()); |
30 | 21 | return false; |
31 | 22 | } |
32 | | -} |
33 | | - |
34 | | -bool ModelStartCmd::Exec(const std::string& host, int port, |
35 | | - const config::ModelConfig& mc) { |
36 | | - // Check if server is started |
37 | | - if (!commands::IsServerAlive(host, port)) { |
38 | | - CLI_LOG("Server is not started yet, please run `" |
39 | | - << commands::GetCortexBinary() << " start` to start server!"); |
40 | | - return false; |
41 | | - } |
42 | | - |
43 | | - // Only check for llamacpp for now |
44 | | - if ((mc.engine.find("llamacpp") != std::string::npos) && |
45 | | - commands::ModelStatusCmd().IsLoaded(host, port, mc)) { |
46 | | - CLI_LOG("Model has already been started!"); |
47 | | - return true; |
48 | | - } |
49 | | - |
50 | | - httplib::Client cli(host + ":" + std::to_string(port)); |
51 | | - |
52 | | - nlohmann::json json_data; |
53 | | - if (mc.files.size() > 0) { |
54 | | - // TODO(sang) support multiple files |
55 | | - json_data["model_path"] = mc.files[0]; |
56 | | - } else { |
57 | | - LOG_WARN << "model_path is empty"; |
58 | | - return false; |
59 | | - } |
60 | | - json_data["model"] = mc.name; |
61 | | - json_data["system_prompt"] = mc.system_template; |
62 | | - json_data["user_prompt"] = mc.user_template; |
63 | | - json_data["ai_prompt"] = mc.ai_template; |
64 | | - json_data["ctx_len"] = mc.ctx_len; |
65 | | - json_data["stop"] = mc.stop; |
66 | | - json_data["engine"] = mc.engine; |
67 | | - |
68 | | - auto data_str = json_data.dump(); |
69 | | - cli.set_read_timeout(std::chrono::seconds(60)); |
70 | | - auto res = cli.Post("/inferences/server/loadmodel", httplib::Headers(), |
71 | | - data_str.data(), data_str.size(), "application/json"); |
72 | | - if (res) { |
73 | | - if (res->status == httplib::StatusCode::OK_200) { |
74 | | - CLI_LOG("Model loaded!"); |
75 | | - return true; |
76 | | - } else { |
77 | | - CTL_ERR("Model failed to load with status code: " << res->status); |
78 | | - return false; |
79 | | - } |
80 | | - } else { |
81 | | - auto err = res.error(); |
82 | | - CTL_ERR("HTTP error: " << httplib::to_string(err)); |
83 | | - return false; |
84 | | - } |
85 | | - return false; |
| 23 | + CLI_LOG("Model loaded!"); |
| 24 | + return true; |
86 | 25 | } |
87 | 26 |
|
88 | 27 | }; // namespace commands |
0 commit comments