11#include " database/models.h"
22#include < drogon/HttpTypes.h>
3+ #include < optional>
34#include " config/gguf_parser.h"
45#include " config/yaml_config.h"
56#include " models.h"
@@ -26,15 +27,22 @@ void Models::PullModel(const HttpRequestPtr& req,
2627 return ;
2728 }
2829
30+ std::optional<std::string> desired_model_id = std::nullopt ;
31+ auto id = (*(req->getJsonObject ())).get (" id" , " " ).asString ();
32+ if (!id.empty ()) {
33+ desired_model_id = id;
34+ }
35+
2936 auto handle_model_input =
3037 [&, model_handle]() -> cpp::result<DownloadTask, std::string> {
3138 CTL_INF (" Handle model input, model handle: " + model_handle);
3239 if (string_utils::StartsWith (model_handle, " https" )) {
33- return model_service_->HandleDownloadUrlAsync (model_handle);
40+ return model_service_->HandleDownloadUrlAsync (model_handle,
41+ desired_model_id);
3442 } else if (model_handle.find (" :" ) != std::string::npos) {
3543 auto model_and_branch = string_utils::SplitBy (model_handle, " :" );
3644 return model_service_->DownloadModelFromCortexsoAsync (
37- model_and_branch[0 ], model_and_branch[1 ]);
45+ model_and_branch[0 ], model_and_branch[1 ], desired_model_id );
3846 }
3947
4048 return cpp::fail (" Invalid model handle or not supported!" );
@@ -107,7 +115,6 @@ void Models::ListModel(
107115 auto list_entry = modellist_handler.LoadModelList ();
108116 if (list_entry) {
109117 for (const auto & model_entry : list_entry.value ()) {
110- // auto model_entry = modellist_handler.GetModelInfo(model_handle);
111118 try {
112119 yaml_handler.ModelConfigFromFile (
113120 fmu::ToAbsoluteCortexDataPath (
@@ -116,7 +123,6 @@ void Models::ListModel(
116123 auto model_config = yaml_handler.GetModelConfig ();
117124 Json::Value obj = model_config.ToJson ();
118125 obj[" id" ] = model_entry.model ;
119- obj[" model_alias" ] = model_entry.model_alias ;
120126 obj[" model" ] = model_entry.model ;
121127 data.append (std::move (obj));
122128 yaml_handler.Reset ();
@@ -333,71 +339,6 @@ void Models::ImportModel(
333339 }
334340}
335341
336- void Models::SetModelAlias (
337- const HttpRequestPtr& req,
338- std::function<void (const HttpResponsePtr&)>&& callback) const {
339- if (!http_util::HasFieldInReq (req, callback, " model" ) ||
340- !http_util::HasFieldInReq (req, callback, " modelAlias" )) {
341- return ;
342- }
343- auto model_handle = (*(req->getJsonObject ())).get (" model" , " " ).asString ();
344- auto model_alias = (*(req->getJsonObject ())).get (" modelAlias" , " " ).asString ();
345- LOG_DEBUG << " GetModel, Model handle: " << model_handle
346- << " , Model alias: " << model_alias;
347-
348- cortex::db::Models modellist_handler;
349- try {
350- auto result = modellist_handler.UpdateModelAlias (model_handle, model_alias);
351- if (result.has_error ()) {
352- std::string message = result.error ();
353- LOG_ERROR << message;
354- Json::Value ret;
355- ret[" result" ] = " Set alias failed!" ;
356- ret[" modelHandle" ] = model_handle;
357- ret[" message" ] = message;
358- auto resp = cortex_utils::CreateCortexHttpJsonResponse (ret);
359- resp->setStatusCode (k400BadRequest);
360- callback (resp);
361- } else {
362- if (result.value ()) {
363- std::string message = " Successfully set model alias '" + model_alias +
364- " ' for modeID '" + model_handle + " '." ;
365- LOG_INFO << message;
366- Json::Value ret;
367- ret[" result" ] = " OK" ;
368- ret[" modelHandle" ] = model_handle;
369- ret[" message" ] = message;
370- auto resp = cortex_utils::CreateCortexHttpJsonResponse (ret);
371- resp->setStatusCode (k200OK);
372- callback (resp);
373- } else {
374- std::string message = " Unable to set model alias for modelID '" +
375- model_handle + " ': model alias '" + model_alias +
376- " ' is not unique!" ;
377- LOG_ERROR << message;
378- Json::Value ret;
379- ret[" result" ] = " Set alias failed!" ;
380- ret[" modelHandle" ] = model_handle;
381- ret[" message" ] = message;
382- auto resp = cortex_utils::CreateCortexHttpJsonResponse (ret);
383- resp->setStatusCode (k400BadRequest);
384- callback (resp);
385- }
386- }
387- } catch (const std::exception& e) {
388- std::string message = " Error when setting model alias ('" + model_alias +
389- " ') for modelID '" + model_handle + " ':" + e.what ();
390- LOG_ERROR << message;
391- Json::Value ret;
392- ret[" result" ] = " Set alias failed!" ;
393- ret[" modelHandle" ] = model_handle;
394- ret[" message" ] = message;
395- auto resp = cortex_utils::CreateCortexHttpJsonResponse (ret);
396- resp->setStatusCode (k400BadRequest);
397- callback (resp);
398- }
399- }
400-
401342void Models::StartModel (
402343 const HttpRequestPtr& req,
403344 std::function<void (const HttpResponsePtr&)>&& callback) {
0 commit comments