Skip to content

Commit

Permalink
fix: improve error message when load model failed (#1533)
Browse files Browse the repository at this point in the history
* fix: error for models start

* fix: add unit tests

---------

Co-authored-by: vansangpfiev <[email protected]>
  • Loading branch information
vansangpfiev and sangjanai authored Oct 22, 2024
1 parent 0fd3c62 commit c434ac2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
6 changes: 4 additions & 2 deletions engine/cli/commands/model_start_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "run_cmd.h"
#include "server_start_cmd.h"
#include "utils/cli_selection_utils.h"
#include "utils/json_helper.h"
#include "utils/logging_utils.h"

namespace commands {
Expand All @@ -14,7 +15,7 @@ bool ModelStartCmd::Exec(const std::string& host, int port,
std::optional<std::string> model_id =
SelectLocalModel(model_service_, model_handle);

if(!model_id.has_value()) {
if (!model_id.has_value()) {
return false;
}

Expand All @@ -41,7 +42,8 @@ bool ModelStartCmd::Exec(const std::string& host, int port,
<< *model_id << "` for interactive chat shell");
return true;
} else {
CTL_ERR("Model failed to load with status code: " << res->status);
auto root = json_helper::ParseJsonString(res->body);
CLI_LOG(root["message"].asString());
return false;
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions engine/services/inference_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ InferResult InferenceService::LoadModel(
if (IsEngineLoaded(kLlamaRepo) && ne == kTrtLlmRepo) {
// Remove llamacpp dll directory
if (!RemoveDllDirectory(engines_[kLlamaRepo].cookie)) {
LOG_INFO << "Could not remove dll directory: " << kLlamaRepo;
LOG_WARN << "Could not remove dll directory: " << kLlamaRepo;
} else {
LOG_WARN << "Removed dll directory: " << kLlamaRepo;
LOG_INFO << "Removed dll directory: " << kLlamaRepo;
}

add_dll(ne, abs_path);
Expand All @@ -158,7 +158,7 @@ InferResult InferenceService::LoadModel(
LOG_ERROR << "Could not load engine: " << e.what();
engines_.erase(ne);

r["message"] = "Could not load engine " + ne;
r["message"] = "Could not load engine " + ne + ": " + e.what();
stt["status_code"] = k500InternalServerError;
return std::make_pair(stt, r);
}
Expand Down
5 changes: 3 additions & 2 deletions engine/services/model_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "utils/logging_utils.h"
#include "utils/result.hpp"
#include "utils/string_utils.h"
#include "utils/json_helper.h"

namespace {
void ParseGguf(const DownloadItem& ggufDownloadItem,
Expand Down Expand Up @@ -622,9 +623,9 @@ cpp::result<bool, std::string> ModelService::StartModel(
CTL_INF("Model '" + model_handle + "' is already loaded");
return true;
} else {
auto root = json_helper::ParseJsonString(res->body);
CTL_ERR("Model failed to load with status code: " << res->status);
return cpp::fail("Model failed to load with status code: " +
std::to_string(res->status));
return cpp::fail("Model failed to start: " + root["message"].asString());
}
} else {
auto err = res.error();
Expand Down
35 changes: 35 additions & 0 deletions engine/test/components/test_json_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <gtest/gtest.h>
#include <json/json.h>
#include <string>
#include "utils/json_helper.h"

// Test Suite
TEST(ParseJsonStringTest, ValidJsonObject) {
std::string json_str = R"({"name": "John", "age": 30})";
Json::Value result = json_helper::ParseJsonString(json_str);

EXPECT_EQ(result["name"].asString(), "John");
EXPECT_EQ(result["age"].asInt(), 30);
}

TEST(ParseJsonStringTest, ValidJsonArray) {
std::string json_str = R"([1, 2, 3, 4, 5])";
Json::Value result = json_helper::ParseJsonString(json_str);

EXPECT_EQ(result.size(), 5);
EXPECT_EQ(result[0].asInt(), 1);
}

TEST(ParseJsonStringTest, InvalidJson) {
std::string json_str = R"({"name": "John", "age": )";
Json::Value result = json_helper::ParseJsonString(json_str);

EXPECT_TRUE(result["age"].isNull());
}

TEST(ParseJsonStringTest, EmptyString) {
std::string json_str = "";
Json::Value result = json_helper::ParseJsonString(json_str);

EXPECT_TRUE(result.isNull());
}
11 changes: 11 additions & 0 deletions engine/utils/json_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <json/json.h>
#include <string>
namespace json_helper {
inline Json::Value ParseJsonString(const std::string& json_str) {
Json::Value root;
Json::Reader reader;
reader.parse(json_str, root);
return root;
}
}

0 comments on commit c434ac2

Please sign in to comment.