|
1 | 1 | #include "engines.h" |
| 2 | +#include <filesystem> |
| 3 | +#include <sstream> |
| 4 | +#include <stdexcept> |
| 5 | +#include <utility> |
| 6 | +#include "services/engine_service.h" |
2 | 7 | #include "utils/archive_utils.h" |
3 | 8 | #include "utils/cortex_utils.h" |
4 | 9 | #include "utils/system_info_utils.h" |
5 | 10 |
|
6 | | -void Engines::InitEngine(const HttpRequestPtr& req, |
7 | | - std::function<void(const HttpResponsePtr&)>&& callback, |
8 | | - const std::string& engine) const { |
| 11 | +void Engines::InstallEngine( |
| 12 | + const HttpRequestPtr& req, |
| 13 | + std::function<void(const HttpResponsePtr&)>&& callback, |
| 14 | + const std::string& engine) const { |
9 | 15 | LOG_DEBUG << "InitEngine, Engine: " << engine; |
10 | 16 | if (engine.empty()) { |
11 | 17 | Json::Value res; |
@@ -114,62 +120,84 @@ void Engines::InitEngine(const HttpRequestPtr& req, |
114 | 120 | void Engines::ListEngine( |
115 | 121 | const HttpRequestPtr& req, |
116 | 122 | std::function<void(const HttpResponsePtr&)>&& callback) const { |
| 123 | + auto engine_service = EngineService(); |
| 124 | + auto status_list = engine_service.GetEngineInfoList(); |
| 125 | + |
117 | 126 | Json::Value ret; |
118 | 127 | ret["object"] = "list"; |
119 | 128 | Json::Value data(Json::arrayValue); |
120 | | - Json::Value obj_onnx, obj_llamacpp, obj_tensorrt; |
121 | | - obj_onnx["name"] = "cortex.onnx"; |
122 | | - obj_onnx["description"] = |
123 | | - "This extension enables chat completion API calls using the Onnx engine"; |
124 | | - obj_onnx["version"] = "0.0.1"; |
125 | | - obj_onnx["productName"] = "Onnx Inference Engine"; |
126 | | - |
127 | | - obj_llamacpp["name"] = "cortex.llamacpp"; |
128 | | - obj_llamacpp["description"] = |
129 | | - "This extension enables chat completion API calls using the LlamaCPP " |
130 | | - "engine"; |
131 | | - obj_llamacpp["version"] = "0.0.1"; |
132 | | - obj_llamacpp["productName"] = "LlamaCPP Inference Engine"; |
133 | | - |
134 | | - obj_tensorrt["name"] = "cortex.tensorrt-llm"; |
135 | | - obj_tensorrt["description"] = |
136 | | - "This extension enables chat completion API calls using the TensorrtLLM " |
137 | | - "engine"; |
138 | | - obj_tensorrt["version"] = "0.0.1"; |
139 | | - obj_tensorrt["productName"] = "TensorrtLLM Inference Engine"; |
140 | | - |
141 | | -#ifdef _WIN32 |
142 | | - if (std::filesystem::exists(std::filesystem::current_path().string() + |
143 | | - cortex_utils::kOnnxLibPath)) { |
144 | | - obj_onnx["status"] = "ready"; |
145 | | - } else { |
146 | | - obj_onnx["status"] = "not_initialized"; |
147 | | - } |
148 | | -#else |
149 | | - obj_onnx["status"] = "not_supported"; |
150 | | -#endif |
151 | | - // lllamacpp |
152 | | - if (std::filesystem::exists(std::filesystem::current_path().string() + |
153 | | - cortex_utils::kLlamaLibPath)) { |
154 | | - |
155 | | - obj_llamacpp["status"] = "ready"; |
156 | | - } else { |
157 | | - obj_llamacpp["status"] = "not_initialized"; |
158 | | - } |
159 | | - // tensorrt llm |
160 | | - if (std::filesystem::exists(std::filesystem::current_path().string() + |
161 | | - cortex_utils::kTensorrtLlmPath)) { |
162 | | - obj_tensorrt["status"] = "ready"; |
163 | | - } else { |
164 | | - obj_tensorrt["status"] = "not_initialized"; |
| 129 | + for (auto& status : status_list) { |
| 130 | + Json::Value ret; |
| 131 | + ret["name"] = status.name; |
| 132 | + ret["description"] = status.description; |
| 133 | + ret["version"] = status.version; |
| 134 | + ret["productName"] = status.product_name; |
| 135 | + ret["status"] = status.status; |
| 136 | + |
| 137 | + data.append(std::move(ret)); |
165 | 138 | } |
166 | 139 |
|
167 | | - data.append(std::move(obj_onnx)); |
168 | | - data.append(std::move(obj_llamacpp)); |
169 | | - data.append(std::move(obj_tensorrt)); |
170 | 140 | ret["data"] = data; |
171 | 141 | ret["result"] = "OK"; |
172 | 142 | auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret); |
173 | 143 | resp->setStatusCode(k200OK); |
174 | 144 | callback(resp); |
175 | 145 | } |
| 146 | + |
| 147 | +void Engines::GetEngine(const HttpRequestPtr& req, |
| 148 | + std::function<void(const HttpResponsePtr&)>&& callback, |
| 149 | + const std::string& engine) const { |
| 150 | + auto engine_service = EngineService(); |
| 151 | + try { |
| 152 | + auto status = engine_service.GetEngineInfo(engine); |
| 153 | + Json::Value ret; |
| 154 | + ret["name"] = status.name; |
| 155 | + ret["description"] = status.description; |
| 156 | + ret["version"] = status.version; |
| 157 | + ret["productName"] = status.product_name; |
| 158 | + ret["status"] = status.status; |
| 159 | + |
| 160 | + auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret); |
| 161 | + resp->setStatusCode(k200OK); |
| 162 | + callback(resp); |
| 163 | + } catch (const std::runtime_error e) { |
| 164 | + Json::Value ret; |
| 165 | + ret["message"] = e.what(); |
| 166 | + auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret); |
| 167 | + resp->setStatusCode(k400BadRequest); |
| 168 | + callback(resp); |
| 169 | + } catch (const std::exception& e) { |
| 170 | + Json::Value ret; |
| 171 | + ret["message"] = e.what(); |
| 172 | + auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret); |
| 173 | + resp->setStatusCode(k500InternalServerError); |
| 174 | + callback(resp); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +void Engines::UninstallEngine( |
| 179 | + const HttpRequestPtr& req, |
| 180 | + std::function<void(const HttpResponsePtr&)>&& callback, |
| 181 | + const std::string& engine) const { |
| 182 | + LOG_INFO << "[Http] Uninstall engine " << engine; |
| 183 | + // TODO: think of a way to prevent code duplication. This should be shared with cmd as well |
| 184 | + |
| 185 | + // TODO: Unload the model which is currently running on engine_ |
| 186 | + |
| 187 | + // TODO: Unload engine if is loaded |
| 188 | + |
| 189 | + // auto ecp = file_manager_utils::GetEnginesContainerPath(); |
| 190 | + // auto engine_path = ecp / engine; |
| 191 | + // if (!std::filesystem::exists(engine_path)) { |
| 192 | + // ("Engine " << engine_ << " is not installed!"); |
| 193 | + // return; |
| 194 | + // } |
| 195 | + // |
| 196 | + // // remove |
| 197 | + // try { |
| 198 | + // std::filesystem::remove_all(engine_path); |
| 199 | + // CTL_INF("Engine " << engine_ << " uninstalled successfully!"); |
| 200 | + // } catch (const std::exception& e) { |
| 201 | + // CTL_ERR("Failed to uninstall engine " << engine_ + ": " << e.what()); |
| 202 | + // } |
| 203 | +} |
0 commit comments