From 5454488a48bcbaa6df65b4e350052ff7705a0763 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 14 Oct 2024 15:17:26 +0700 Subject: [PATCH 01/12] fix: update swagger --- engine/controllers/swagger.cc | 360 +++++++++++++++++----------------- 1 file changed, 180 insertions(+), 180 deletions(-) diff --git a/engine/controllers/swagger.cc b/engine/controllers/swagger.cc index 27d9b7b15..94d2f1117 100644 --- a/engine/controllers/swagger.cc +++ b/engine/controllers/swagger.cc @@ -493,14 +493,14 @@ Json::Value SwaggerController::generateOpenAPISpec() { ["$ref"] = "#/components/schemas/ModelList"; // Create Fine-tuning Job - Json::Value& finetune = spec["paths"]["/v1/fine_tuning/job"]["post"]; - finetune["summary"] = "Create fine-tuning job"; - finetune["description"] = "Creates a job that fine-tunes a specified model"; - finetune["requestBody"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/FineTuningRequest"; - finetune["responses"]["200"]["description"] = "Successful response"; - finetune["responses"]["200"]["content"]["application/json"]["schema"] - ["$ref"] = "#/components/schemas/FineTuningResponse"; + // Json::Value& finetune = spec["paths"]["/v1/fine_tuning/job"]["post"]; + // finetune["summary"] = "Create fine-tuning job"; + // finetune["description"] = "Creates a job that fine-tunes a specified model"; + // finetune["requestBody"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/FineTuningRequest"; + // finetune["responses"]["200"]["description"] = "Successful response"; + // finetune["responses"]["200"]["content"]["application/json"]["schema"] + // ["$ref"] = "#/components/schemas/FineTuningResponse"; // Create Embeddings Json::Value& embed = spec["paths"]["/v1/embeddings"]["post"]; @@ -517,104 +517,104 @@ Json::Value SwaggerController::generateOpenAPISpec() { // Custom Cortex Endpoints { // Chat Completion - Json::Value& chat = - spec["paths"]["/inferences/server/chat_completion"]["post"]; - chat["summary"] = "Create chat completion (Cortex)"; - chat["description"] = - "Creates a completion for the chat message using Cortex engine"; - chat["requestBody"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/CortexChatCompletionRequest"; - chat["responses"]["200"]["description"] = "Successful response"; - chat["responses"]["200"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/CortexChatCompletionResponse"; + // Json::Value& chat = + // spec["paths"]["/inferences/server/chat_completion"]["post"]; + // chat["summary"] = "Create chat completion (Cortex)"; + // chat["description"] = + // "Creates a completion for the chat message using Cortex engine"; + // chat["requestBody"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/CortexChatCompletionRequest"; + // chat["responses"]["200"]["description"] = "Successful response"; + // chat["responses"]["200"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/CortexChatCompletionResponse"; // Embedding - Json::Value& embed = spec["paths"]["/inferences/server/embedding"]["post"]; - embed["summary"] = "Create embeddings (Cortex)"; - embed["description"] = "Creates an embedding vector using Cortex engine"; - embed["requestBody"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/CortexEmbeddingRequest"; - embed["responses"]["200"]["description"] = "Successful response"; - embed["responses"]["200"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/CortexEmbeddingResponse"; + // Json::Value& embed = spec["paths"]["/inferences/server/embedding"]["post"]; + // embed["summary"] = "Create embeddings (Cortex)"; + // embed["description"] = "Creates an embedding vector using Cortex engine"; + // embed["requestBody"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/CortexEmbeddingRequest"; + // embed["responses"]["200"]["description"] = "Successful response"; + // embed["responses"]["200"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/CortexEmbeddingResponse"; // Load Model - Json::Value& load = spec["paths"]["/inferences/server/loadmodel"]["post"]; - load["summary"] = "Load a model"; - load["description"] = "Loads a specified model into the engine"; - load["requestBody"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/LoadModelRequest"; - load["responses"]["200"]["description"] = "Model loaded successfully"; - load["responses"]["200"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/SuccessResponse"; + // Json::Value& load = spec["paths"]["/inferences/server/loadmodel"]["post"]; + // load["summary"] = "Load a model"; + // load["description"] = "Loads a specified model into the engine"; + // load["requestBody"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/LoadModelRequest"; + // load["responses"]["200"]["description"] = "Model loaded successfully"; + // load["responses"]["200"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/SuccessResponse"; // Unload Model - Json::Value& unload = - spec["paths"]["/inferences/server/unloadmodel"]["post"]; - unload["summary"] = "Unload a model"; - unload["description"] = "Unloads a specified model from the engine"; - unload["requestBody"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/UnloadModelRequest"; - unload["responses"]["200"]["description"] = "Model unloaded successfully"; - unload["responses"]["200"]["content"]["application/json"]["schema"] - ["$ref"] = "#/components/schemas/SuccessResponse"; + // Json::Value& unload = + // spec["paths"]["/inferences/server/unloadmodel"]["post"]; + // unload["summary"] = "Unload a model"; + // unload["description"] = "Unloads a specified model from the engine"; + // unload["requestBody"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/UnloadModelRequest"; + // unload["responses"]["200"]["description"] = "Model unloaded successfully"; + // unload["responses"]["200"]["content"]["application/json"]["schema"] + // ["$ref"] = "#/components/schemas/SuccessResponse"; // Model Status - Json::Value& status = - spec["paths"]["/inferences/server/modelstatus"]["post"]; - status["summary"] = "Get model status"; - status["description"] = "Retrieves the status of a specified model"; - status["requestBody"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/ModelStatusRequest"; - status["responses"]["200"]["description"] = - "Model status retrieved successfully"; - status["responses"]["200"]["content"]["application/json"]["schema"] - ["$ref"] = "#/components/schemas/ModelStatusResponse"; + // Json::Value& status = + // spec["paths"]["/inferences/server/modelstatus"]["post"]; + // status["summary"] = "Get model status"; + // status["description"] = "Retrieves the status of a specified model"; + // status["requestBody"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/ModelStatusRequest"; + // status["responses"]["200"]["description"] = + // "Model status retrieved successfully"; + // status["responses"]["200"]["content"]["application/json"]["schema"] + // ["$ref"] = "#/components/schemas/ModelStatusResponse"; // Get Models - Json::Value& getModels = spec["paths"]["/inferences/server/models"]["get"]; - getModels["summary"] = "Get all models (Cortex)"; - getModels["description"] = - "Retrieves a list of all available models in Cortex"; - getModels["responses"]["200"]["description"] = - "Models retrieved successfully"; - getModels["responses"]["200"]["content"]["application/json"]["schema"] - ["$ref"] = "#/components/schemas/CortexModelList"; + // Json::Value& getModels = spec["paths"]["/inferences/server/models"]["get"]; + // getModels["summary"] = "Get all models (Cortex)"; + // getModels["description"] = + // "Retrieves a list of all available models in Cortex"; + // getModels["responses"]["200"]["description"] = + // "Models retrieved successfully"; + // getModels["responses"]["200"]["content"]["application/json"]["schema"] + // ["$ref"] = "#/components/schemas/CortexModelList"; // Get Engines - Json::Value& getEngines = - spec["paths"]["/inferences/server/engines"]["get"]; - getEngines["summary"] = "Get all engines"; - getEngines["description"] = "Retrieves a list of all available engines"; - getEngines["responses"]["200"]["description"] = - "Engines retrieved successfully"; - getEngines["responses"]["200"]["content"]["application/json"]["schema"] - ["$ref"] = "#/components/schemas/EngineList"; + // Json::Value& getEngines = + // spec["paths"]["/inferences/server/engines"]["get"]; + // getEngines["summary"] = "Get all engines"; + // getEngines["description"] = "Retrieves a list of all available engines"; + // getEngines["responses"]["200"]["description"] = + // "Engines retrieved successfully"; + // getEngines["responses"]["200"]["content"]["application/json"]["schema"] + // ["$ref"] = "#/components/schemas/EngineList"; // Fine Tuning - Json::Value& fineTuning = - spec["paths"]["/inferences/server/finetuning"]["post"]; - fineTuning["summary"] = "Create fine-tuning job (Cortex)"; - fineTuning["description"] = - "Creates a job that fine-tunes a specified model using Cortex engine"; - fineTuning["requestBody"]["content"]["application/json"]["schema"]["$ref"] = - "#/components/schemas/CortexFineTuningRequest"; - fineTuning["responses"]["200"]["description"] = - "Fine-tuning job created successfully"; - fineTuning["responses"]["200"]["content"]["application/json"]["schema"] - ["$ref"] = "#/components/schemas/CortexFineTuningResponse"; + // Json::Value& fineTuning = + // spec["paths"]["/inferences/server/finetuning"]["post"]; + // fineTuning["summary"] = "Create fine-tuning job (Cortex)"; + // fineTuning["description"] = + // "Creates a job that fine-tunes a specified model using Cortex engine"; + // fineTuning["requestBody"]["content"]["application/json"]["schema"]["$ref"] = + // "#/components/schemas/CortexFineTuningRequest"; + // fineTuning["responses"]["200"]["description"] = + // "Fine-tuning job created successfully"; + // fineTuning["responses"]["200"]["content"]["application/json"]["schema"] + // ["$ref"] = "#/components/schemas/CortexFineTuningResponse"; // Unload Engine - Json::Value& unloadEngine = - spec["paths"]["/inferences/server/unloadengine"]["post"]; - unloadEngine["summary"] = "Unload an engine"; - unloadEngine["description"] = "Unloads a specified engine"; - unloadEngine["requestBody"]["content"]["application/json"]["schema"] - ["$ref"] = "#/components/schemas/UnloadEngineRequest"; - unloadEngine["responses"]["200"]["description"] = - "Engine unloaded successfully"; - unloadEngine["responses"]["200"]["content"]["application/json"]["schema"] - ["$ref"] = "#/components/schemas/SuccessResponse"; + // Json::Value& unloadEngine = + // spec["paths"]["/inferences/server/unloadengine"]["post"]; + // unloadEngine["summary"] = "Unload an engine"; + // unloadEngine["description"] = "Unloads a specified engine"; + // unloadEngine["requestBody"]["content"]["application/json"]["schema"] + // ["$ref"] = "#/components/schemas/UnloadEngineRequest"; + // unloadEngine["responses"]["200"]["description"] = + // "Engine unloaded successfully"; + // unloadEngine["responses"]["200"]["content"]["application/json"]["schema"] + // ["$ref"] = "#/components/schemas/SuccessResponse"; } // Define schemas @@ -645,12 +645,12 @@ Json::Value SwaggerController::generateOpenAPISpec() { schemas["Model"]["properties"]["id"]["type"] = "string"; schemas["Model"]["properties"]["object"]["type"] = "string"; - schemas["FineTuningRequest"]["type"] = "object"; - schemas["FineTuningRequest"]["properties"]["model"]["type"] = "string"; - schemas["FineTuningRequest"]["properties"]["training_file"]["type"] = - "string"; +// schemas["FineTuningRequest"]["type"] = "object"; +// schemas["FineTuningRequest"]["properties"]["model"]["type"] = "string"; +// schemas["FineTuningRequest"]["properties"]["training_file"]["type"] = +// "string"; - schemas["FineTuningResponse"]["type"] = "object"; +// schemas["FineTuningResponse"]["type"] = "object"; // Add properties based on your implementation schemas["EmbeddingRequest"]["type"] = "object"; @@ -667,117 +667,117 @@ Json::Value SwaggerController::generateOpenAPISpec() { schemas["EmbeddingResponse"]["properties"]["data"]["items"]["properties"] ["embedding"]["items"]["type"] = "number"; - schemas["CortexChatCompletionRequest"]["type"] = "object"; - schemas["CortexChatCompletionRequest"]["properties"]["engine"]["type"] = - "string"; - schemas["CortexChatCompletionRequest"]["properties"]["model"]["type"] = - "string"; - schemas["CortexChatCompletionRequest"]["properties"]["messages"]["type"] = - "array"; - schemas["CortexChatCompletionRequest"]["properties"]["messages"]["items"] - ["$ref"] = "#/components/schemas/ChatMessage"; - schemas["CortexChatCompletionRequest"]["properties"]["stream"]["type"] = - "boolean"; +// schemas["CortexChatCompletionRequest"]["type"] = "object"; +// schemas["CortexChatCompletionRequest"]["properties"]["engine"]["type"] = +// "string"; +// schemas["CortexChatCompletionRequest"]["properties"]["model"]["type"] = +// "string"; +// schemas["CortexChatCompletionRequest"]["properties"]["messages"]["type"] = +// "array"; +// schemas["CortexChatCompletionRequest"]["properties"]["messages"]["items"] +// ["$ref"] = "#/components/schemas/ChatMessage"; +// schemas["CortexChatCompletionRequest"]["properties"]["stream"]["type"] = +// "boolean"; // Add other properties based on your implementation - schemas["CortexChatCompletionResponse"]["type"] = "object"; +// schemas["CortexChatCompletionResponse"]["type"] = "object"; // Add properties based on your implementation - schemas["CortexEmbeddingRequest"]["type"] = "object"; - schemas["CortexEmbeddingRequest"]["properties"]["engine"]["type"] = "string"; - schemas["CortexEmbeddingRequest"]["properties"]["input"]["type"] = "string"; +// schemas["CortexEmbeddingRequest"]["type"] = "object"; +// schemas["CortexEmbeddingRequest"]["properties"]["engine"]["type"] = "string"; +// schemas["CortexEmbeddingRequest"]["properties"]["input"]["type"] = "string"; - schemas["CortexEmbeddingResponse"]["type"] = "object"; +// schemas["CortexEmbeddingResponse"]["type"] = "object"; // Add properties based on your implementation - schemas["LoadModelRequest"]["type"] = "object"; - schemas["LoadModelRequest"]["properties"]["engine"]["type"] = "string"; - schemas["LoadModelRequest"]["properties"]["model_path"]["type"] = "string"; - schemas["LoadModelRequest"]["properties"]["model"]["type"] = "string"; - schemas["LoadModelRequest"]["properties"]["engine"]["type"] = "string"; - schemas["LoadModelRequest"]["properties"]["stop"]["type"] = "array"; - schemas["LoadModelRequest"]["properties"]["stop"]["items"]["type"] = "string"; - schemas["LoadModelRequest"]["properties"]["stop"]["description"] = - "List of stop sequences"; - - schemas["LoadModelRequest"]["properties"]["stream"]["type"] = "boolean"; - schemas["LoadModelRequest"]["properties"]["stream"]["description"] = - "Whether to stream the output"; - - schemas["LoadModelRequest"]["properties"]["ngl"]["type"] = "integer"; - schemas["LoadModelRequest"]["properties"]["ngl"]["description"] = - "Number of GPU layers"; - - schemas["LoadModelRequest"]["properties"]["ctx_len"]["type"] = "integer"; - schemas["LoadModelRequest"]["properties"]["ctx_len"]["description"] = - "Context length"; - - schemas["LoadModelRequest"]["properties"]["engine"]["type"] = "string"; - schemas["LoadModelRequest"]["properties"]["engine"]["description"] = - "Engine used for the model"; - - schemas["LoadModelRequest"]["properties"]["system_template"]["type"] = - "string"; - schemas["LoadModelRequest"]["properties"]["system_template"]["description"] = - "Template for system messages"; - - schemas["LoadModelRequest"]["properties"]["user_template"]["type"] = "string"; - schemas["LoadModelRequest"]["properties"]["user_template"]["description"] = - "Template for user messages"; - - schemas["LoadModelRequest"]["properties"]["ai_template"]["type"] = "string"; - schemas["LoadModelRequest"]["properties"]["ai_template"]["description"] = - "Template for AI responses"; - - schemas["LoadModelRequest"]["properties"]["n_probs"]["type"] = "integer"; - schemas["LoadModelRequest"]["properties"]["n_probs"]["description"] = - "Number of probabilities to return"; +// schemas["LoadModelRequest"]["type"] = "object"; +// schemas["LoadModelRequest"]["properties"]["engine"]["type"] = "string"; +// schemas["LoadModelRequest"]["properties"]["model_path"]["type"] = "string"; +// schemas["LoadModelRequest"]["properties"]["model"]["type"] = "string"; +// schemas["LoadModelRequest"]["properties"]["engine"]["type"] = "string"; +// schemas["LoadModelRequest"]["properties"]["stop"]["type"] = "array"; +// schemas["LoadModelRequest"]["properties"]["stop"]["items"]["type"] = "string"; +// schemas["LoadModelRequest"]["properties"]["stop"]["description"] = +// "List of stop sequences"; + +// schemas["LoadModelRequest"]["properties"]["stream"]["type"] = "boolean"; +// schemas["LoadModelRequest"]["properties"]["stream"]["description"] = +// "Whether to stream the output"; + +// schemas["LoadModelRequest"]["properties"]["ngl"]["type"] = "integer"; +// schemas["LoadModelRequest"]["properties"]["ngl"]["description"] = +// "Number of GPU layers"; + +// schemas["LoadModelRequest"]["properties"]["ctx_len"]["type"] = "integer"; +// schemas["LoadModelRequest"]["properties"]["ctx_len"]["description"] = +// "Context length"; + +// schemas["LoadModelRequest"]["properties"]["engine"]["type"] = "string"; +// schemas["LoadModelRequest"]["properties"]["engine"]["description"] = +// "Engine used for the model"; + +// schemas["LoadModelRequest"]["properties"]["system_template"]["type"] = +// "string"; +// schemas["LoadModelRequest"]["properties"]["system_template"]["description"] = +// "Template for system messages"; + +// schemas["LoadModelRequest"]["properties"]["user_template"]["type"] = "string"; +// schemas["LoadModelRequest"]["properties"]["user_template"]["description"] = +// "Template for user messages"; + +// schemas["LoadModelRequest"]["properties"]["ai_template"]["type"] = "string"; +// schemas["LoadModelRequest"]["properties"]["ai_template"]["description"] = +// "Template for AI responses"; + +// schemas["LoadModelRequest"]["properties"]["n_probs"]["type"] = "integer"; +// schemas["LoadModelRequest"]["properties"]["n_probs"]["description"] = +// "Number of probabilities to return"; // Add other properties based on your implementation - schemas["UnloadModelRequest"]["type"] = "object"; - schemas["UnloadModelRequest"]["properties"]["engine"]["type"] = "string"; - schemas["UnloadModelRequest"]["properties"]["model"]["type"] = "string"; +// schemas["UnloadModelRequest"]["type"] = "object"; +// schemas["UnloadModelRequest"]["properties"]["engine"]["type"] = "string"; +// schemas["UnloadModelRequest"]["properties"]["model"]["type"] = "string"; // Add other properties based on your implementation - schemas["ModelStatusRequest"]["type"] = "object"; - schemas["ModelStatusRequest"]["properties"]["engine"]["type"] = "string"; - schemas["ModelStatusRequest"]["properties"]["model"]["type"] = "string"; +// schemas["ModelStatusRequest"]["type"] = "object"; +// schemas["ModelStatusRequest"]["properties"]["engine"]["type"] = "string"; +// schemas["ModelStatusRequest"]["properties"]["model"]["type"] = "string"; // Add other properties based on your implementation - schemas["ModelStatusResponse"]["type"] = "object"; +// schemas["ModelStatusResponse"]["type"] = "object"; // Add properties based on your implementation - schemas["CortexModelList"]["type"] = "object"; - schemas["CortexModelList"]["properties"]["data"]["type"] = "array"; - schemas["CortexModelList"]["properties"]["data"]["items"]["$ref"] = - "#/components/schemas/CortexModel"; +// schemas["CortexModelList"]["type"] = "object"; +// schemas["CortexModelList"]["properties"]["data"]["type"] = "array"; +// schemas["CortexModelList"]["properties"]["data"]["items"]["$ref"] = +// "#/components/schemas/CortexModel"; - schemas["CortexModel"]["type"] = "object"; +// schemas["CortexModel"]["type"] = "object"; // Add properties based on your implementation - schemas["EngineList"]["type"] = "object"; - schemas["EngineList"]["properties"]["object"]["type"] = "string"; - schemas["EngineList"]["properties"]["data"]["type"] = "array"; - schemas["EngineList"]["properties"]["data"]["items"]["$ref"] = - "#/components/schemas/Engine"; +// schemas["EngineList"]["type"] = "object"; +// schemas["EngineList"]["properties"]["object"]["type"] = "string"; +// schemas["EngineList"]["properties"]["data"]["type"] = "array"; +// schemas["EngineList"]["properties"]["data"]["items"]["$ref"] = +// "#/components/schemas/Engine"; - schemas["Engine"]["type"] = "object"; - schemas["Engine"]["properties"]["id"]["type"] = "string"; - schemas["Engine"]["properties"]["object"]["type"] = "string"; +// schemas["Engine"]["type"] = "object"; +// schemas["Engine"]["properties"]["id"]["type"] = "string"; +// schemas["Engine"]["properties"]["object"]["type"] = "string"; - schemas["CortexFineTuningRequest"]["type"] = "object"; - schemas["CortexFineTuningRequest"]["properties"]["engine"]["type"] = "string"; +// schemas["CortexFineTuningRequest"]["type"] = "object"; +// schemas["CortexFineTuningRequest"]["properties"]["engine"]["type"] = "string"; // Add other properties based on your implementation - schemas["CortexFineTuningResponse"]["type"] = "object"; +// schemas["CortexFineTuningResponse"]["type"] = "object"; // Add properties based on your implementation - schemas["UnloadEngineRequest"]["type"] = "object"; - schemas["UnloadEngineRequest"]["properties"]["engine"]["type"] = "string"; +// schemas["UnloadEngineRequest"]["type"] = "object"; +// schemas["UnloadEngineRequest"]["properties"]["engine"]["type"] = "string"; - schemas["SuccessResponse"]["type"] = "object"; - schemas["SuccessResponse"]["properties"]["message"]["type"] = "string"; +// schemas["SuccessResponse"]["type"] = "object"; +// schemas["SuccessResponse"]["properties"]["message"]["type"] = "string"; // TODO: Add more paths and details based on your API return spec; From 3e3dff662243f9551b62bc8fc59bf34b1320ed74 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 14 Oct 2024 15:53:54 +0700 Subject: [PATCH 02/12] fix: start stop model with API call --- engine/commands/model_start_cmd.cc | 35 ++++++++++++++++++++++++------ engine/commands/model_stop_cmd.cc | 23 +++++++++++++++----- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/engine/commands/model_start_cmd.cc b/engine/commands/model_start_cmd.cc index aebc8400c..5756a012d 100644 --- a/engine/commands/model_start_cmd.cc +++ b/engine/commands/model_start_cmd.cc @@ -1,18 +1,39 @@ #include "model_start_cmd.h" +#include "httplib.h" +#include "server_start_cmd.h" #include "utils/logging_utils.h" namespace commands { bool ModelStartCmd::Exec(const std::string& host, int port, const std::string& model_handle) { - auto res = model_service_.StartModel(host, port, model_handle); - - if (res.has_error()) { - CLI_LOG("Error: " + res.error()); + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { + return false; + } + } + // Call API to start model + httplib::Client cli(host + ":" + std::to_string(port)); + Json::Value json_data; + json_data["model"] = model_handle; + auto data_str = json_data.toStyledString(); + auto res = cli.Post("/v1/models/start", httplib::Headers(), data_str.data(), + data_str.size(), "application/json"); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + CLI_LOG("Model loaded!"); + return true; + } else { + CTL_ERR("Model failed to load with status code: " << res->status); + return false; + } + } else { + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); return false; } - - CLI_LOG("Model loaded!"); - return true; } }; // namespace commands diff --git a/engine/commands/model_stop_cmd.cc b/engine/commands/model_stop_cmd.cc index 15d3b0abd..d84f17003 100644 --- a/engine/commands/model_stop_cmd.cc +++ b/engine/commands/model_stop_cmd.cc @@ -1,17 +1,28 @@ #include "model_stop_cmd.h" #include "utils/logging_utils.h" +#include "httplib.h" namespace commands { void ModelStopCmd::Exec(const std::string& host, int port, const std::string& model_handle) { - auto res = model_service_.StopModel(host, port, model_handle); - - if (res.has_error()) { - CLI_LOG("Error: " + res.error()); - return; + // Call API to stop model + httplib::Client cli(host + ":" + std::to_string(port)); + Json::Value json_data; + json_data["model"] = model_handle; + auto data_str = json_data.toStyledString(); + auto res = cli.Post("/v1/models/stop", httplib::Headers(), data_str.data(), + data_str.size(), "application/json"); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + CLI_LOG("Model unloaded!"); + } else { + CTL_ERR("Model failed to unload with status code: " << res->status); + } + } else { + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); } - CLI_LOG("Model unloaded!"); } }; // namespace commands From 1bd0bbfa8a8767f942cfe80cdf495ba40bb650a1 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 14 Oct 2024 16:09:31 +0700 Subject: [PATCH 03/12] fix: model del --- engine/commands/model_del_cmd.cc | 30 +++++++++++++++++++---- engine/commands/model_del_cmd.h | 2 +- engine/controllers/command_line_parser.cc | 4 ++- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/engine/commands/model_del_cmd.cc b/engine/commands/model_del_cmd.cc index f955a2591..693de804d 100644 --- a/engine/commands/model_del_cmd.cc +++ b/engine/commands/model_del_cmd.cc @@ -1,13 +1,33 @@ #include "model_del_cmd.h" +#include "httplib.h" +#include "server_start_cmd.h" #include "utils/logging_utils.h" namespace commands { -void ModelDelCmd::Exec(const std::string& model_handle) { - auto result = model_service_.DeleteModel(model_handle); - if (result.has_error()) { - CLI_LOG(result.error()); + +void ModelDelCmd::Exec(const std::string& host, int port, + const std::string& model_handle) { + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { + return; + } + } + + // Call API to delete model + httplib::Client cli(host + ":" + std::to_string(port)); + auto res = cli.Delete("/v1/models/" + model_handle); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + CLI_LOG("Model " + model_handle + " deleted successfully"); + } else { + CTL_ERR("Model failed to delete with status code: " << res->status); + } } else { - CLI_LOG("Model " + model_handle + " deleted successfully"); + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); } } } // namespace commands diff --git a/engine/commands/model_del_cmd.h b/engine/commands/model_del_cmd.h index 38f172ad2..008f43e73 100644 --- a/engine/commands/model_del_cmd.h +++ b/engine/commands/model_del_cmd.h @@ -10,7 +10,7 @@ class ModelDelCmd { explicit ModelDelCmd() : model_service_{ModelService(std::make_shared())} {}; - void Exec(const std::string& model_handle); + void Exec(const std::string& host, int port, const std::string& model_handle); private: ModelService model_service_; diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index ab7e41da5..cb54416d2 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -278,7 +278,9 @@ void CommandLineParser::SetupModelCommands() { CLI_LOG(model_del_cmd->help()); return; }; - commands::ModelDelCmd().Exec(cml_data_.model_id); + commands::ModelDelCmd().Exec(cml_data_.config.apiServerHost, + std::stoi(cml_data_.config.apiServerPort), + cml_data_.model_id); }); std::string model_alias; From 536e8ad9231688b2a55df03bd2b5dafe9042a984 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 15 Oct 2024 14:32:48 +0700 Subject: [PATCH 04/12] fix: model get --- engine/commands/model_get_cmd.cc | 41 ++++++++++++----------- engine/commands/model_get_cmd.h | 2 +- engine/controllers/command_line_parser.cc | 4 ++- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/engine/commands/model_get_cmd.cc b/engine/commands/model_get_cmd.cc index 3cf46bd84..b5b230c9c 100644 --- a/engine/commands/model_get_cmd.cc +++ b/engine/commands/model_get_cmd.cc @@ -5,33 +5,36 @@ #include #include "config/yaml_config.h" #include "database/models.h" +#include "httplib.h" +#include "server_start_cmd.h" #include "utils/file_manager_utils.h" #include "utils/logging_utils.h" namespace commands { -void ModelGetCmd::Exec(const std::string& model_handle) { - namespace fs = std::filesystem; - namespace fmu = file_manager_utils; - cortex::db::Models modellist_handler; - config::YamlHandler yaml_handler; - try { - auto model_entry = modellist_handler.GetModelInfo(model_handle); - if (model_entry.has_error()) { - CLI_LOG("Error: " + model_entry.error()); +void ModelGetCmd::Exec(const std::string& host, int port, + const std::string& model_handle) { + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { return; } - yaml_handler.ModelConfigFromFile( - fmu::ToAbsoluteCortexDataPath( - fs::path(model_entry.value().path_to_model_yaml)) - .string()); - auto model_config = yaml_handler.GetModelConfig(); - - std::cout << model_config.ToString() << std::endl; + } - } catch (const std::exception& e) { - CLI_LOG("Fail to get model information with ID '" + model_handle + - "': " + e.what()); + // Call API to delete model + httplib::Client cli(host + ":" + std::to_string(port)); + auto res = cli.Get("/v1/models/" + model_handle); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + CLI_LOG(res->body); + } else { + CTL_ERR("Model failed to get with status code: " << res->status); + } + } else { + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); } } diff --git a/engine/commands/model_get_cmd.h b/engine/commands/model_get_cmd.h index 1836f7d99..e903d4b64 100644 --- a/engine/commands/model_get_cmd.h +++ b/engine/commands/model_get_cmd.h @@ -5,6 +5,6 @@ namespace commands { class ModelGetCmd { public: - void Exec(const std::string& model_handle); + void Exec(const std::string& host, int port, const std::string& model_handle); }; } // namespace commands diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index cb54416d2..f4b2a33e2 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -261,7 +261,9 @@ void CommandLineParser::SetupModelCommands() { CLI_LOG(get_models_cmd->help()); return; }; - commands::ModelGetCmd().Exec(cml_data_.model_id); + commands::ModelGetCmd().Exec(cml_data_.config.apiServerHost, + std::stoi(cml_data_.config.apiServerPort), + cml_data_.model_id); }); auto model_del_cmd = From e4a6f7cae7296baa0dda46f42a7561966800f97d Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 15 Oct 2024 14:46:56 +0700 Subject: [PATCH 05/12] fix: model alias --- engine/commands/model_alias_cmd.cc | 46 ++++++++++++++--------- engine/commands/model_alias_cmd.h | 3 +- engine/controllers/command_line_parser.cc | 4 +- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/engine/commands/model_alias_cmd.cc b/engine/commands/model_alias_cmd.cc index cd7f647a5..d7792e131 100644 --- a/engine/commands/model_alias_cmd.cc +++ b/engine/commands/model_alias_cmd.cc @@ -1,28 +1,40 @@ #include "model_alias_cmd.h" #include "database/models.h" +#include "httplib.h" +#include "server_start_cmd.h" +#include "json/json.h" namespace commands { -void ModelAliasCmd::Exec(const std::string& model_handle, +void ModelAliasCmd::Exec(const std::string& host, int port, + const std::string& model_handle, const std::string& model_alias) { - cortex::db::Models modellist_handler; - try { - auto result = modellist_handler.UpdateModelAlias(model_handle, model_alias); - if (result.has_error()) { - CLI_LOG(result.error()); - } else { - if (result.value()) { - CLI_LOG("Successfully set model alias '" + model_alias + - "' for modeID '" + model_handle + "'."); - } else { - CLI_LOG("Unable to set model alias for modelID '" + model_handle + - "': model alias '" + model_alias + "' is not unique!"); - } + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { + return; } + } - } catch (const std::exception& e) { - CLI_LOG("Error when setting model alias ('" + model_alias + - "') for modelID '" + model_handle + "':" + e.what()); + // Call API to delete model + httplib::Client cli(host + ":" + std::to_string(port)); + Json::Value json_data; + json_data["model"] = model_handle; + auto data_str = json_data.toStyledString(); + auto res = cli.Post("/v1/models/alias", httplib::Headers(), data_str.data(), + data_str.size(), "application/json"); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + CLI_LOG("Successfully set model alias '" + model_alias + + "' for modeID '" + model_handle + "'."); + } else { + CTL_ERR("Model failed to set alias with status code: " << res->status); + } + } else { + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); } } diff --git a/engine/commands/model_alias_cmd.h b/engine/commands/model_alias_cmd.h index dcec44947..b61dd724c 100644 --- a/engine/commands/model_alias_cmd.h +++ b/engine/commands/model_alias_cmd.h @@ -5,6 +5,7 @@ namespace commands { class ModelAliasCmd { public: - void Exec(const std::string& model_handle, const std::string& model_alias); + void Exec(const std::string& host, int port, const std::string& model_handle, + const std::string& model_alias); }; } // namespace commands \ No newline at end of file diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index f4b2a33e2..29da1a1ce 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -305,7 +305,9 @@ void CommandLineParser::SetupModelCommands() { return; } commands::ModelAliasCmd mdc; - mdc.Exec(cml_data_.model_id, cml_data_.model_alias); + mdc.Exec(cml_data_.config.apiServerHost, + std::stoi(cml_data_.config.apiServerPort), cml_data_.model_id, + cml_data_.model_alias); }); // Model update parameters comment ModelUpdate(models_cmd); From 30e62fd20dd2b9af15ec1db03ac348c46223673c Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 15 Oct 2024 14:52:19 +0700 Subject: [PATCH 06/12] fix: model import --- engine/commands/model_alias_cmd.cc | 1 + engine/commands/model_import_cmd.cc | 70 ++++++++++------------- engine/commands/model_import_cmd.h | 8 +-- engine/controllers/command_line_parser.cc | 5 +- 4 files changed, 36 insertions(+), 48 deletions(-) diff --git a/engine/commands/model_alias_cmd.cc b/engine/commands/model_alias_cmd.cc index d7792e131..08a0c88a0 100644 --- a/engine/commands/model_alias_cmd.cc +++ b/engine/commands/model_alias_cmd.cc @@ -22,6 +22,7 @@ void ModelAliasCmd::Exec(const std::string& host, int port, httplib::Client cli(host + ":" + std::to_string(port)); Json::Value json_data; json_data["model"] = model_handle; + json_data["modelAlias"] = model_alias; auto data_str = json_data.toStyledString(); auto res = cli.Post("/v1/models/alias", httplib::Headers(), data_str.data(), data_str.size(), "application/json"); diff --git a/engine/commands/model_import_cmd.cc b/engine/commands/model_import_cmd.cc index 15fc6f897..ef0bca556 100644 --- a/engine/commands/model_import_cmd.cc +++ b/engine/commands/model_import_cmd.cc @@ -4,54 +4,44 @@ #include "config/gguf_parser.h" #include "config/yaml_config.h" #include "database/models.h" +#include "httplib.h" +#include "json/json.h" +#include "server_start_cmd.h" #include "utils/file_manager_utils.h" #include "utils/logging_utils.h" namespace commands { -ModelImportCmd::ModelImportCmd(std::string model_handle, std::string model_path) - : model_handle_(std::move(model_handle)), - model_path_(std::move(model_path)) {} - -void ModelImportCmd::Exec() { - namespace fs = std::filesystem; - namespace fmu = file_manager_utils; - config::GGUFHandler gguf_handler; - config::YamlHandler yaml_handler; - cortex::db::Models modellist_utils_obj; - - std::string model_yaml_path = (file_manager_utils::GetModelsContainerPath() / - std::filesystem::path("imported") / - std::filesystem::path(model_handle_ + ".yml")) - .string(); - try { - // Use relative path for model_yaml_path. In case of import, we use absolute path for model - auto yaml_rel_path = - fmu::ToRelativeCortexDataPath(fs::path(model_yaml_path)); - cortex::db::ModelEntry model_entry{model_handle_, "local", "imported", - yaml_rel_path.string(), model_handle_}; - - std::filesystem::create_directories( - std::filesystem::path(model_yaml_path).parent_path()); - gguf_handler.Parse(model_path_); - auto model_config = gguf_handler.GetModelConfig(); - model_config.files.push_back(model_path_); - model_config.model = model_handle_; - yaml_handler.UpdateModelConfig(model_config); +void ModelImportCmd::Exec(const std::string& host, int port, + const std::string& model_handle, + const std::string& model_path) { + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { + return; + } + } - if (modellist_utils_obj.AddModelEntry(model_entry).value()) { - yaml_handler.WriteYamlFile(model_yaml_path); - CLI_LOG("Model is imported successfully!"); + // Call API to delete model + httplib::Client cli(host + ":" + std::to_string(port)); + Json::Value json_data; + json_data["model"] = model_handle; + json_data["modelPath"] = model_path; + auto data_str = json_data.toStyledString(); + auto res = cli.Post("/v1/models/import", httplib::Headers(), data_str.data(), + data_str.size(), "application/json"); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + CLI_LOG("Successfully import model from '" + model_path + + "' for modeID '" + model_handle + "'."); } else { - CLI_LOG("Fail to import model, model_id '" + model_handle_ + - "' already exists!"); + CTL_ERR("Model failed to import model with status code: " << res->status); } - - } catch (const std::exception& e) { - // don't need to remove yml file here, because it's written only if model entry is successfully added, - // remove file here can make it fail with edge case when user try to import new model with existed model_id - CLI_LOG("Error importing model path '" + model_path_ + "' with model_id '" + - model_handle_ + "': " + e.what()); + } else { + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); } } } // namespace commands diff --git a/engine/commands/model_import_cmd.h b/engine/commands/model_import_cmd.h index d4248281f..141351909 100644 --- a/engine/commands/model_import_cmd.h +++ b/engine/commands/model_import_cmd.h @@ -5,11 +5,7 @@ namespace commands { class ModelImportCmd { public: - ModelImportCmd(std::string model_handle, std::string model_path); - void Exec(); - - private: - std::string model_handle_; - std::string model_path_; + void Exec(const std::string& host, int port, const std::string& model_handle, + const std::string& model_path); }; } // namespace commands \ No newline at end of file diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index 29da1a1ce..b3b0f8156 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -331,8 +331,9 @@ void CommandLineParser::SetupModelCommands() { CLI_LOG(model_import_cmd->help()); return; } - commands::ModelImportCmd command(cml_data_.model_id, cml_data_.model_path); - command.Exec(); + commands::ModelImportCmd().Exec(cml_data_.config.apiServerHost, + std::stoi(cml_data_.config.apiServerPort), + cml_data_.model_id, cml_data_.model_path); }); } From 0f2e0a4fc3052c0fecf6859d7972c4a99a1ff061 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 15 Oct 2024 17:58:36 +0700 Subject: [PATCH 07/12] fix: model status --- engine/commands/model_status_cmd.cc | 26 +++++++++++++++++++++++--- engine/controllers/models.cc | 23 +++++++++++++++++++++++ engine/controllers/models.h | 6 ++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/engine/commands/model_status_cmd.cc b/engine/commands/model_status_cmd.cc index 572b64bbe..6677fe0af 100644 --- a/engine/commands/model_status_cmd.cc +++ b/engine/commands/model_status_cmd.cc @@ -1,13 +1,33 @@ #include "model_status_cmd.h" +#include "httplib.h" +#include "server_start_cmd.h" #include "utils/logging_utils.h" namespace commands { bool ModelStatusCmd::IsLoaded(const std::string& host, int port, const std::string& model_handle) { - auto res = model_service_.GetModelStatus(host, port, model_handle); + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { + return false; + } + } - if (res.has_error()) { - CTL_ERR("Error: " + res.error()); + // Call API to check model status + httplib::Client cli(host + ":" + std::to_string(port)); + auto res = cli.Get("/v1/models/status/" + model_handle); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + CTL_INF(res->body); + } else { + CTL_WRN("Failed to get model status with code: " << res->status); + return false; + } + } else { + auto err = res.error(); + CTL_WRN("HTTP error: " << httplib::to_string(err)); return false; } return true; diff --git a/engine/controllers/models.cc b/engine/controllers/models.cc index eefc0a941..c6b669f91 100644 --- a/engine/controllers/models.cc +++ b/engine/controllers/models.cc @@ -408,3 +408,26 @@ void Models::StopModel(const HttpRequestPtr& req, callback(resp); } } + +void Models::GetModelStatus( + const HttpRequestPtr& req, + std::function&& callback, + const std::string& model_id) { + auto config = file_manager_utils::GetCortexConfig(); + + auto result = model_service_->GetModelStatus( + config.apiServerHost, std::stoi(config.apiServerPort), model_id); + if (result.has_error()) { + Json::Value ret; + ret["message"] = result.error(); + auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret); + resp->setStatusCode(drogon::k400BadRequest); + callback(resp); + } else { + Json::Value ret; + ret["message"] = "Model is running"; + auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret); + resp->setStatusCode(k200OK); + callback(resp); + } +} diff --git a/engine/controllers/models.h b/engine/controllers/models.h index 2aa99f5e9..212432b76 100644 --- a/engine/controllers/models.h +++ b/engine/controllers/models.h @@ -18,6 +18,7 @@ class Models : public drogon::HttpController { METHOD_ADD(Models::SetModelAlias, "/alias", Post); METHOD_ADD(Models::StartModel, "/start", Post); METHOD_ADD(Models::StopModel, "/stop", Post); + METHOD_ADD(Models::GetModelStatus, "/status/{1}", Get); ADD_METHOD_TO(Models::PullModel, "/v1/models/pull", Post); ADD_METHOD_TO(Models::ListModel, "/v1/models", Get); @@ -28,6 +29,7 @@ class Models : public drogon::HttpController { ADD_METHOD_TO(Models::SetModelAlias, "/v1/models/alias", Post); ADD_METHOD_TO(Models::StartModel, "/v1/models/start", Post); ADD_METHOD_TO(Models::StopModel, "/v1/models/stop", Post); + ADD_METHOD_TO(Models::GetModelStatus, "/v1/models/status/{1}", Get); METHOD_LIST_END explicit Models(std::shared_ptr model_service) @@ -59,6 +61,10 @@ class Models : public drogon::HttpController { void StopModel(const HttpRequestPtr& req, std::function&& callback); + void GetModelStatus(const HttpRequestPtr& req, + std::function&& callback, + const std::string& model_id); + private: std::shared_ptr model_service_; }; From d37f0bff08d97e45a140f68ec50ef2ffeab0fe71 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 15 Oct 2024 18:52:00 +0700 Subject: [PATCH 08/12] fix: model list --- engine/commands/model_list_cmd.cc | 76 ++++++++++++----------- engine/commands/model_list_cmd.h | 3 +- engine/controllers/command_line_parser.cc | 3 +- engine/controllers/models.cc | 1 + 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/engine/commands/model_list_cmd.cc b/engine/commands/model_list_cmd.cc index 14f929c93..c92836456 100644 --- a/engine/commands/model_list_cmd.cc +++ b/engine/commands/model_list_cmd.cc @@ -1,19 +1,26 @@ #include "model_list_cmd.h" #include -#include + #include -#include "config/yaml_config.h" -#include "database/models.h" -#include "utils/file_manager_utils.h" +#include "httplib.h" +#include "json/json.h" +#include "server_start_cmd.h" #include "utils/logging_utils.h" - +// clang-format off +#include +// clang-format on namespace commands { -void ModelListCmd::Exec() { - namespace fs = std::filesystem; - namespace fmu = file_manager_utils; - cortex::db::Models modellist_handler; - config::YamlHandler yaml_handler; +void ModelListCmd::Exec(const std::string& host, int port) { + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { + return; + } + } + tabulate::Table table; table.add_row({"(Index)", "ID", "model alias", "engine", "version"}); @@ -21,35 +28,30 @@ void ModelListCmd::Exec() { int count = 0; // Iterate through directory - auto list_entry = modellist_handler.LoadModelList(); - if (list_entry.has_error()) { - CTL_ERR("Fail to get list model information: " << list_entry.error()); - return; - } - for (const auto& model_entry : list_entry.value()) { - // auto model_entry = modellist_handler.GetModelInfo(model_handle); - try { - try { - yaml_handler.ModelConfigFromFile( - fmu::ToAbsoluteCortexDataPath( - fs::path(model_entry.path_to_model_yaml)) - .string()); - } catch (const std::exception& e) { - CTL_WRN("Fail to get model '" + model_entry.model + - "' information: " + std::string(e.what())); - yaml_handler.Reset(); - continue; + httplib::Client cli(host + ":" + std::to_string(port)); + auto res = cli.Get("/v1/models"); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + // CLI_LOG(res->body); + Json::Value body; + Json::Reader reader; + reader.parse(res->body, body); + if (!body["data"].isNull()) { + for (auto const& v : body["data"]) { + count += 1; + table.add_row({std::to_string(count), v["model"].asString(), + v["model_alias"].asString(), v["engine"].asString(), + v["version"].asString()}); + } } - - count += 1; - auto model_config = yaml_handler.GetModelConfig(); - table.add_row({std::to_string(count), model_entry.model, - model_entry.model_alias, model_config.engine, - model_config.version}); - yaml_handler.Reset(); - } catch (const std::exception& e) { - CTL_ERR("Fail to get list model information: " + std::string(e.what())); + } else { + CTL_ERR("Failed to get model list with status code: " << res->status); + return; } + } else { + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); + return; } for (int i = 0; i < 5; i++) { diff --git a/engine/commands/model_list_cmd.h b/engine/commands/model_list_cmd.h index a24da8928..2f25cc1cf 100644 --- a/engine/commands/model_list_cmd.h +++ b/engine/commands/model_list_cmd.h @@ -1,9 +1,10 @@ #pragma once +#include namespace commands { class ModelListCmd { public: - void Exec(); + void Exec(const std::string& host, int port); }; } // namespace commands diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index b3b0f8156..58ee9160f 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -244,7 +244,8 @@ void CommandLineParser::SetupModelCommands() { list_models_cmd->callback([this]() { if (std::exchange(executed_, true)) return; - commands::ModelListCmd().Exec(); + commands::ModelListCmd().Exec(cml_data_.config.apiServerHost, + std::stoi(cml_data_.config.apiServerPort)); }); auto get_models_cmd = diff --git a/engine/controllers/models.cc b/engine/controllers/models.cc index c6b669f91..6ecc65289 100644 --- a/engine/controllers/models.cc +++ b/engine/controllers/models.cc @@ -82,6 +82,7 @@ void Models::ListModel( auto model_config = yaml_handler.GetModelConfig(); Json::Value obj = model_config.ToJson(); obj["id"] = model_config.model; + obj["model_alias"] = model_entry.model_alias; data.append(std::move(obj)); yaml_handler.Reset(); } catch (const std::exception& e) { From 2a00d34e02f3d0c17d86b7aaa535e6f526dbecf7 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 15 Oct 2024 19:48:48 +0700 Subject: [PATCH 09/12] fix: models update --- engine/commands/model_import_cmd.cc | 1 - engine/commands/model_upd_cmd.cc | 318 ++-------------------- engine/commands/model_upd_cmd.h | 17 +- engine/controllers/command_line_parser.cc | 6 +- 4 files changed, 36 insertions(+), 306 deletions(-) diff --git a/engine/commands/model_import_cmd.cc b/engine/commands/model_import_cmd.cc index ef0bca556..6abaad2c8 100644 --- a/engine/commands/model_import_cmd.cc +++ b/engine/commands/model_import_cmd.cc @@ -24,7 +24,6 @@ void ModelImportCmd::Exec(const std::string& host, int port, } } - // Call API to delete model httplib::Client cli(host + ":" + std::to_string(port)); Json::Value json_data; json_data["model"] = model_handle; diff --git a/engine/commands/model_upd_cmd.cc b/engine/commands/model_upd_cmd.cc index 0864c227a..18edf1b61 100644 --- a/engine/commands/model_upd_cmd.cc +++ b/engine/commands/model_upd_cmd.cc @@ -1,4 +1,7 @@ #include "model_upd_cmd.h" +#include "httplib.h" +#include "json/json.h" +#include "server_start_cmd.h" #include "utils/file_manager_utils.h" #include "utils/logging_utils.h" @@ -8,301 +11,40 @@ ModelUpdCmd::ModelUpdCmd(std::string model_handle) : model_handle_(std::move(model_handle)) {} void ModelUpdCmd::Exec( + const std::string& host, int port, const std::unordered_map& options) { - namespace fs = std::filesystem; - namespace fmu = file_manager_utils; - try { - auto model_entry = model_list_utils_.GetModelInfo(model_handle_); - if (model_entry.has_error()) { - CLI_LOG("Error: " + model_entry.error()); + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { return; } - auto yaml_fp = fmu::ToAbsoluteCortexDataPath( - fs::path(model_entry.value().path_to_model_yaml)); - yaml_handler_.ModelConfigFromFile(yaml_fp.string()); - model_config_ = yaml_handler_.GetModelConfig(); + } - for (const auto& [key, value] : options) { - if (!value.empty()) { - UpdateConfig(key, value); - } + httplib::Client cli(host + ":" + std::to_string(port)); + Json::Value json_data; + for (const auto& [key, value] : options) { + if (!value.empty()) { + json_data[key] = value; + CLI_LOG("Updated " << key << " to: " << value); } - - yaml_handler_.UpdateModelConfig(model_config_); - yaml_handler_.WriteYamlFile(yaml_fp.string()); - CLI_LOG("Successfully updated model ID '" + model_handle_ + "'!"); - } catch (const std::exception& e) { - CLI_LOG("Failed to update model with model ID '" + model_handle_ + - "': " + e.what()); } -} - -void ModelUpdCmd::UpdateConfig(const std::string& key, - const std::string& value) { - static const std::unordered_map< - std::string, - std::function> - updaters = { - {"name", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.name = v; - }}, - {"model", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.model = v; - }}, - {"version", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.version = v; - }}, - {"engine", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.engine = v; - }}, - {"prompt_template", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.prompt_template = v; - }}, - {"system_template", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.system_template = v; - }}, - {"user_template", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.user_template = v; - }}, - {"ai_template", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.ai_template = v; - }}, - {"os", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.os = v; - }}, - {"gpu_arch", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.gpu_arch = v; - }}, - {"quantization_method", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.quantization_method = v; - }}, - {"precision", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.precision = v; - }}, - {"trtllm_version", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.trtllm_version = v; - }}, - {"object", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.object = v; - }}, - {"owned_by", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.owned_by = v; - }}, - {"grammar", - [](ModelUpdCmd* self, const std::string&, const std::string& v) { - self->model_config_.grammar = v; - }}, - {"stop", &ModelUpdCmd::UpdateVectorField}, - {"files", &ModelUpdCmd::UpdateVectorField}, - {"top_p", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField( - k, v, [self](float f) { self->model_config_.top_p = f; }); - }}, - {"temperature", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.temperature = f; - }); - }}, - {"frequency_penalty", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.frequency_penalty = f; - }); - }}, - {"presence_penalty", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.presence_penalty = f; - }); - }}, - {"dynatemp_range", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.dynatemp_range = f; - }); - }}, - {"dynatemp_exponent", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.dynatemp_exponent = f; - }); - }}, - {"min_p", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField( - k, v, [self](float f) { self->model_config_.min_p = f; }); - }}, - {"tfs_z", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField( - k, v, [self](float f) { self->model_config_.tfs_z = f; }); - }}, - {"typ_p", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField( - k, v, [self](float f) { self->model_config_.typ_p = f; }); - }}, - {"repeat_penalty", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.repeat_penalty = f; - }); - }}, - {"mirostat_tau", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.mirostat_tau = f; - }); - }}, - {"mirostat_eta", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.mirostat_eta = f; - }); - }}, - {"max_tokens", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.max_tokens = static_cast(f); - }); - }}, - {"ngl", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.ngl = static_cast(f); - }); - }}, - {"ctx_len", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.ctx_len = static_cast(f); - }); - }}, - {"tp", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.tp = static_cast(f); - }); - }}, - {"seed", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.seed = static_cast(f); - }); - }}, - {"top_k", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.top_k = static_cast(f); - }); - }}, - {"repeat_last_n", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.repeat_last_n = static_cast(f); - }); - }}, - {"n_probs", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.n_probs = static_cast(f); - }); - }}, - {"min_keep", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.min_keep = static_cast(f); - }); - }}, - {"stream", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateBooleanField( - k, v, [self](bool b) { self->model_config_.stream = b; }); - }}, - {"text_model", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateBooleanField( - k, v, [self](bool b) { self->model_config_.text_model = b; }); - }}, - {"mirostat", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateBooleanField( - k, v, [self](bool b) { self->model_config_.mirostat = b; }); - }}, - {"penalize_nl", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateBooleanField( - k, v, [self](bool b) { self->model_config_.penalize_nl = b; }); - }}, - {"ignore_eos", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateBooleanField( - k, v, [self](bool b) { self->model_config_.ignore_eos = b; }); - }}, - {"created", - [](ModelUpdCmd* self, const std::string& k, const std::string& v) { - self->UpdateNumericField(k, v, [self](float f) { - self->model_config_.created = static_cast(f); - }); - }}, - }; - - if (auto it = updaters.find(key); it != updaters.end()) { - it->second(this, key, value); - LogUpdate(key, value); + auto data_str = json_data.toStyledString(); + auto res = cli.Patch("/v1/models/" + model_handle_, httplib::Headers(), + data_str.data(), data_str.size(), "application/json"); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + CLI_LOG("Successfully updated model ID '" + model_handle_ + "'!"); + return; + } else { + CTL_ERR("Model failed to update with status code: " << res->status); + return; + } } else { - CLI_LOG("Warning: Unknown configuration key '" << key << "' ignored."); + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); + return; } } - -void ModelUpdCmd::UpdateVectorField(const std::string& key, - const std::string& value) { - std::vector tokens; - std::istringstream iss(value); - std::string token; - while (std::getline(iss, token, ',')) { - tokens.push_back(token); - } - model_config_.stop = tokens; -} - -void ModelUpdCmd::UpdateNumericField(const std::string& key, - const std::string& value, - std::function setter) { - try { - float numericValue = std::stof(value); - setter(numericValue); - } catch (const std::exception& e) { - CLI_LOG("Failed to parse numeric value for " << key << ": " << e.what()); - } -} - -void ModelUpdCmd::UpdateBooleanField(const std::string& key, - const std::string& value, - std::function setter) { - bool boolValue = (value == "true" || value == "1"); - setter(boolValue); -} - -void ModelUpdCmd::LogUpdate(const std::string& key, const std::string& value) { - CLI_LOG("Updated " << key << " to: " << value); -} - } // namespace commands \ No newline at end of file diff --git a/engine/commands/model_upd_cmd.h b/engine/commands/model_upd_cmd.h index 49c104157..6a5e8f42f 100644 --- a/engine/commands/model_upd_cmd.h +++ b/engine/commands/model_upd_cmd.h @@ -4,27 +4,14 @@ #include #include #include -#include "config/model_config.h" -#include "config/yaml_config.h" -#include "database/models.h" namespace commands { class ModelUpdCmd { public: ModelUpdCmd(std::string model_handle); - void Exec(const std::unordered_map& options); + void Exec(const std::string& host, int port, + const std::unordered_map& options); private: std::string model_handle_; - config::ModelConfig model_config_; - config::YamlHandler yaml_handler_; - cortex::db::Models model_list_utils_; - - void UpdateConfig(const std::string& key, const std::string& value); - void UpdateVectorField(const std::string& key, const std::string& value); - void UpdateNumericField(const std::string& key, const std::string& value, - std::function setter); - void UpdateBooleanField(const std::string& key, const std::string& value, - std::function setter); - void LogUpdate(const std::string& key, const std::string& value); }; } // namespace commands \ No newline at end of file diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index 58ee9160f..38a7eaf66 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -245,7 +245,7 @@ void CommandLineParser::SetupModelCommands() { if (std::exchange(executed_, true)) return; commands::ModelListCmd().Exec(cml_data_.config.apiServerHost, - std::stoi(cml_data_.config.apiServerPort)); + std::stoi(cml_data_.config.apiServerPort)); }); auto get_models_cmd = @@ -598,6 +598,8 @@ void CommandLineParser::ModelUpdate(CLI::App* parent) { if (std::exchange(executed_, true)) return; commands::ModelUpdCmd command(cml_data_.model_id); - command.Exec(cml_data_.model_update_options); + command.Exec(cml_data_.config.apiServerHost, + std::stoi(cml_data_.config.apiServerPort), + cml_data_.model_update_options); }); } From 55b5ae2741ea53aaa94b35df097f16965ab794d6 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 15 Oct 2024 20:05:19 +0700 Subject: [PATCH 10/12] fix: engine list --- engine/commands/engine_list_cmd.cc | 50 +++++++++++++++++++---- engine/commands/engine_list_cmd.h | 10 +---- engine/controllers/command_line_parser.cc | 3 +- engine/controllers/engines.cc | 1 + 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/engine/commands/engine_list_cmd.cc b/engine/commands/engine_list_cmd.cc index 335e10439..c34bef1aa 100644 --- a/engine/commands/engine_list_cmd.cc +++ b/engine/commands/engine_list_cmd.cc @@ -1,21 +1,53 @@ #include "engine_list_cmd.h" +#include "httplib.h" +#include "json/json.h" +#include "server_start_cmd.h" +#include "utils/logging_utils.h" +// clang-format off #include +// clang-format on namespace commands { -bool EngineListCmd::Exec() { - auto status_list = engine_service_.GetEngineInfoList(); +bool EngineListCmd::Exec(const std::string& host, int port) { + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { + return false; + } + } tabulate::Table table; table.add_row( {"#", "Name", "Supported Formats", "Version", "Variant", "Status"}); - for (int i = 0; i < status_list.size(); i++) { - auto engine_status = status_list[i]; - std::string index = std::to_string(i + 1); - auto variant = engine_status.variant.value_or(""); - auto version = engine_status.version.value_or(""); - table.add_row({index, engine_status.product_name, engine_status.format, - version, variant, engine_status.status}); + + httplib::Client cli(host + ":" + std::to_string(port)); + auto res = cli.Get("/v1/engines"); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + int count = 0; + // CLI_LOG(res->body); + Json::Value body; + Json::Reader reader; + reader.parse(res->body, body); + if (!body["data"].isNull()) { + for (auto const& v : body["data"]) { + count += 1; + table.add_row({std::to_string(count), v["name"].asString(), + v["format"].asString(), v["version"].asString(), + v["variant"].asString(), v["status"].asString()}); + } + } + } else { + CTL_ERR("Failed to get engine list with status code: " << res->status); + return false; + } + } else { + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); + return false; } std::cout << table << std::endl; diff --git a/engine/commands/engine_list_cmd.h b/engine/commands/engine_list_cmd.h index be38bdd2d..96ad956b2 100644 --- a/engine/commands/engine_list_cmd.h +++ b/engine/commands/engine_list_cmd.h @@ -1,17 +1,11 @@ #pragma once -#include "services/engine_service.h" +#include namespace commands { class EngineListCmd { public: - explicit EngineListCmd() - : engine_service_{EngineService(std::make_shared())} {}; - - bool Exec(); - - private: - EngineService engine_service_; + bool Exec(const std::string& host, int port); }; } // namespace commands diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index 38a7eaf66..d0fc301d0 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -360,7 +360,8 @@ void CommandLineParser::SetupEngineCommands() { if (std::exchange(executed_, true)) return; commands::EngineListCmd command; - command.Exec(); + command.Exec(cml_data_.config.apiServerHost, + std::stoi(cml_data_.config.apiServerPort)); }); auto install_cmd = engines_cmd->add_subcommand("install", "Install engine"); diff --git a/engine/controllers/engines.cc b/engine/controllers/engines.cc index 8ce792742..cc17b1b12 100644 --- a/engine/controllers/engines.cc +++ b/engine/controllers/engines.cc @@ -53,6 +53,7 @@ void Engines::ListEngine( ret["variant"] = status.variant.value_or(""); ret["productName"] = status.product_name; ret["status"] = status.status; + ret["format"] = status.format; data.append(std::move(ret)); } From 963d83f1d2f73c66680b26aaeeb969a6d38239bf Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 15 Oct 2024 20:14:12 +0700 Subject: [PATCH 11/12] fix: engine get --- engine/commands/engine_get_cmd.cc | 51 ++++++++++++++++++----- engine/commands/engine_get_cmd.h | 11 ++--- engine/controllers/command_line_parser.cc | 4 +- engine/controllers/engines.cc | 1 + 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/engine/commands/engine_get_cmd.cc b/engine/commands/engine_get_cmd.cc index 67818a537..96180e49a 100644 --- a/engine/commands/engine_get_cmd.cc +++ b/engine/commands/engine_get_cmd.cc @@ -1,24 +1,53 @@ #include "engine_get_cmd.h" #include -#include -#include "services/engine_service.h" + +#include "httplib.h" +#include "json/json.h" +#include "server_start_cmd.h" #include "utils/logging_utils.h" +// clang-format off +#include +// clang-format on + namespace commands { -void EngineGetCmd::Exec(const std::string& engine_name) const { - auto engine = engine_service_.GetEngineInfo(engine_name); - if (engine.has_error()) { - CLI_LOG(engine.error()); - return; +void EngineGetCmd::Exec(const std::string& host, int port, + const std::string& engine_name) const { + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { + return; + } } - auto version = engine->version.value_or(""); - auto variant = engine->variant.value_or(""); tabulate::Table table; table.add_row({"Name", "Supported Formats", "Version", "Variant", "Status"}); - table.add_row( - {engine->product_name, engine->format, version, variant, engine->status}); + httplib::Client cli(host + ":" + std::to_string(port)); + auto res = cli.Get("/v1/engines/" + engine_name); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + // CLI_LOG(res->body); + Json::Value v; + Json::Reader reader; + reader.parse(res->body, v); + + table.add_row({v["name"].asString(), v["format"].asString(), + v["version"].asString(), v["variant"].asString(), + v["status"].asString()}); + + } else { + CTL_ERR("Failed to get engine list with status code: " << res->status); + return; + } + } else { + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); + return; + } + std::cout << table << std::endl; } }; // namespace commands diff --git a/engine/commands/engine_get_cmd.h b/engine/commands/engine_get_cmd.h index 0a58b7e37..c6415b2db 100644 --- a/engine/commands/engine_get_cmd.h +++ b/engine/commands/engine_get_cmd.h @@ -1,16 +1,11 @@ #pragma once - -#include "services/engine_service.h" +#include namespace commands { class EngineGetCmd { public: - explicit EngineGetCmd() - : engine_service_{EngineService(std::make_shared())} {}; - - void Exec(const std::string& engineName) const; + void Exec(const std::string& host, int port, + const std::string& engineName) const; - private: - EngineService engine_service_; }; } // namespace commands diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index d0fc301d0..c7cb22f82 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -530,7 +530,9 @@ void CommandLineParser::EngineGet(CLI::App* parent) { engine_get_cmd->callback([this, engine_name] { if (std::exchange(executed_, true)) return; - commands::EngineGetCmd().Exec(engine_name); + commands::EngineGetCmd().Exec(cml_data_.config.apiServerHost, + std::stoi(cml_data_.config.apiServerPort), + engine_name); }); } } diff --git a/engine/controllers/engines.cc b/engine/controllers/engines.cc index cc17b1b12..d9e6af3ef 100644 --- a/engine/controllers/engines.cc +++ b/engine/controllers/engines.cc @@ -77,6 +77,7 @@ void Engines::GetEngine(const HttpRequestPtr& req, ret["variant"] = status->variant.value_or(""); ret["productName"] = status->product_name; ret["status"] = status->status; + ret["format"] = status->format; auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret); resp->setStatusCode(k200OK); From ca7b23551aa8dcb250747f3f68f09fc1cff065b7 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 15 Oct 2024 20:20:37 +0700 Subject: [PATCH 12/12] fix: engine uninstall --- engine/commands/engine_uninstall_cmd.cc | 29 ++++++++++++++++++----- engine/commands/engine_uninstall_cmd.h | 9 +------ engine/commands/model_del_cmd.h | 6 ----- engine/controllers/command_line_parser.cc | 4 +++- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/engine/commands/engine_uninstall_cmd.cc b/engine/commands/engine_uninstall_cmd.cc index 1f2095edb..6953e2dea 100644 --- a/engine/commands/engine_uninstall_cmd.cc +++ b/engine/commands/engine_uninstall_cmd.cc @@ -1,16 +1,33 @@ #include "engine_uninstall_cmd.h" -#include "services/engine_service.h" +#include "httplib.h" +#include "server_start_cmd.h" #include "utils/logging_utils.h" namespace commands { -void EngineUninstallCmd::Exec(const std::string& engine) { - auto result = engine_service_.UninstallEngine(engine); +void EngineUninstallCmd::Exec(const std::string& host, int port, + const std::string& engine) { + // Start server if server is not started yet + if (!commands::IsServerAlive(host, port)) { + CLI_LOG("Starting server ..."); + commands::ServerStartCmd ssc; + if (!ssc.Exec(host, port)) { + return; + } + } - if (result.has_error()) { - CLI_LOG(result.error()); + // Call API to delete engine + httplib::Client cli(host + ":" + std::to_string(port)); + auto res = cli.Delete("/v1/engines/" + engine); + if (res) { + if (res->status == httplib::StatusCode::OK_200) { + CLI_LOG("Engine " + engine + " uninstalled successfully"); + } else { + CTL_ERR("Engine failed to uninstall with status code: " << res->status); + } } else { - CLI_LOG("Engine " + engine + " uninstalled successfully!"); + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); } } }; // namespace commands diff --git a/engine/commands/engine_uninstall_cmd.h b/engine/commands/engine_uninstall_cmd.h index 46b1bd923..d2ffbf9fa 100644 --- a/engine/commands/engine_uninstall_cmd.h +++ b/engine/commands/engine_uninstall_cmd.h @@ -1,18 +1,11 @@ #pragma once -#include #include -#include "services/engine_service.h" namespace commands { class EngineUninstallCmd { public: - explicit EngineUninstallCmd() - : engine_service_{EngineService(std::make_shared())} {}; + void Exec(const std::string& host, int port, const std::string& engine); - void Exec(const std::string& engine); - - private: - EngineService engine_service_; }; } // namespace commands diff --git a/engine/commands/model_del_cmd.h b/engine/commands/model_del_cmd.h index 008f43e73..ad89b3eb4 100644 --- a/engine/commands/model_del_cmd.h +++ b/engine/commands/model_del_cmd.h @@ -1,18 +1,12 @@ #pragma once #include -#include "services/model_service.h" namespace commands { class ModelDelCmd { public: - explicit ModelDelCmd() - : model_service_{ModelService(std::make_shared())} {}; - void Exec(const std::string& host, int port, const std::string& model_handle); - private: - ModelService model_service_; }; } // namespace commands diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index c7cb22f82..a3a67fc84 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -498,7 +498,9 @@ void CommandLineParser::EngineUninstall(CLI::App* parent, if (std::exchange(executed_, true)) return; try { - commands::EngineUninstallCmd().Exec(engine_name); + commands::EngineUninstallCmd().Exec( + cml_data_.config.apiServerHost, + std::stoi(cml_data_.config.apiServerPort), engine_name); } catch (const std::exception& e) { CTL_ERR(e.what()); }