Skip to content

Commit

Permalink
fix: add remote engine to /v1/engines GET
Browse files Browse the repository at this point in the history
  • Loading branch information
vansangpfiev committed Dec 30, 2024
1 parent 5da3540 commit 8922896
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 40 deletions.
7 changes: 4 additions & 3 deletions engine/common/engine_servicei.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ struct EngineVariantResponse {
std::string name;
std::string version;
std::string engine;
std::string type;

Json::Value ToJson() const {
Json::Value root;
root["name"] = name;
root["version"] = version;
root["engine"] = engine;
root["type"] = "local";
root["type"] = type.empty() ? "local" : type;
return root;
}
};
Expand All @@ -58,7 +59,7 @@ class EngineServiceI {
virtual cpp::result<cortex::db::EngineEntry, std::string>
GetEngineByNameAndVariant(
const std::string& engine_name,
const std::optional<std::string> variant = std::nullopt) = 0;
const std::optional<std::string> variant = std::nullopt) const = 0;

virtual bool IsRemoteEngine(const std::string& engine_name) = 0;
virtual bool IsRemoteEngine(const std::string& engine_name) const = 0;
};
18 changes: 18 additions & 0 deletions engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,24 @@ void Engines::GetInstalledEngineVariants(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& engine) const {

if (engine_service_->IsRemoteEngine(engine)) {
auto remote_engines = engine_service_->GetEngines();
Json::Value releases(Json::arrayValue);
if (remote_engines.has_value()) {
for (auto e : remote_engines.value()) {
if (e.type == kRemote && e.engine_name == engine) {
releases.append(e.ToJson());
break;
}
}
}
auto resp = cortex_utils::CreateCortexHttpJsonResponse(releases);
resp->setStatusCode(k200OK);
callback(resp);
return;
}

auto result = engine_service_->GetInstalledEngineVariants(engine);
if (result.has_error()) {
Json::Value res;
Expand Down
55 changes: 22 additions & 33 deletions engine/extensions/remote-engine/remote_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ size_t StreamWriteCallback(char* ptr, size_t size, size_t nmemb,
continue;
}

Json::Reader reader;

Json::Value status;
status["is_done"] = false;
status["has_error"] = false;
Expand Down Expand Up @@ -114,6 +112,12 @@ CurlResponse RemoteEngine::MakeStreamingChatCompletionRequest(
headers = curl_slist_append(headers, "Connection: keep-alive");

std::string stream_template = chat_res_template_;
if (!config.transform_resp["chat_completions"] &&
!config.transform_resp["chat_completions"]["template"]) {
// Model level overrides engine level
stream_template =
config.transform_resp["chat_completions"]["template"].as<std::string>();
}

StreamContext context{
std::make_shared<std::function<void(Json::Value&&, Json::Value&&)>>(
Expand Down Expand Up @@ -522,12 +526,16 @@ void RemoteEngine::HandleChatCompletion(

// Get template string with error check
std::string template_str;
try {
if (!chat_req_template_.empty()) {
CTL_DBG("Use engine transform request template: " << chat_req_template_);
template_str = chat_req_template_;
}
if (!model_config->transform_req["chat_completions"] &&
!model_config->transform_req["chat_completions"]["template"]) {
// Model level overrides engine level
template_str = model_config->transform_req["chat_completions"]["template"]
.as<std::string>();
} catch (const YAML::BadConversion& e) {
throw std::runtime_error("Failed to convert template node to string: " +
std::string(e.what()));
CTL_DBG("Use model transform request template: " << template_str);
}

// Render with error handling
Expand Down Expand Up @@ -586,33 +594,14 @@ void RemoteEngine::HandleChatCompletion(
CTL_DBG(
"Use engine transform response template: " << chat_res_template_);
template_str = chat_res_template_;
} else {
// Check if required YAML nodes exist
if (!model_config->transform_resp["chat_completions"]) {
throw std::runtime_error(
"Missing 'chat_completions' node in transform_resp");
}
if (!model_config->transform_resp["chat_completions"]["template"]) {
throw std::runtime_error(
"Missing 'template' node in chat_completions");
}

// Validate JSON body
if (!response_json || response_json.isNull()) {
throw std::runtime_error("Invalid or null JSON body");
}

// Get template string with error check

try {
template_str =
model_config->transform_resp["chat_completions"]["template"]
.as<std::string>();
} catch (const YAML::BadConversion& e) {
throw std::runtime_error(
"Failed to convert template node to string: " +
std::string(e.what()));
}
}
if (!model_config->transform_resp["chat_completions"] &&
!model_config->transform_resp["chat_completions"]["template"]) {
// Model level overrides engine level
template_str =
model_config->transform_resp["chat_completions"]["template"]
.as<std::string>();
CTL_DBG("Use model transform request template: " << template_str);
}

try {
Expand Down
5 changes: 3 additions & 2 deletions engine/services/engine_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,8 @@ cpp::result<cortex::db::EngineEntry, std::string> EngineService::GetEngineById(

cpp::result<cortex::db::EngineEntry, std::string>
EngineService::GetEngineByNameAndVariant(
const std::string& engine_name, const std::optional<std::string> variant) {
const std::string& engine_name,
const std::optional<std::string> variant) const {

cortex::db::Engines engines;
auto get_res = engines.GetEngineByNameAndVariant(engine_name, variant);
Expand Down Expand Up @@ -1130,7 +1131,7 @@ cpp::result<Json::Value, std::string> EngineService::GetRemoteModels(
}
}

bool EngineService::IsRemoteEngine(const std::string& engine_name) {
bool EngineService::IsRemoteEngine(const std::string& engine_name) const {
auto ne = Repo2Engine(engine_name);
auto local_engines = file_manager_utils::GetCortexConfig().supportedEngines;
for (auto const& le : local_engines) {
Expand Down
4 changes: 2 additions & 2 deletions engine/services/engine_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class EngineService : public EngineServiceI {

cpp::result<cortex::db::EngineEntry, std::string> GetEngineByNameAndVariant(
const std::string& engine_name,
const std::optional<std::string> variant = std::nullopt) override;
const std::optional<std::string> variant = std::nullopt) const override;

cpp::result<cortex::db::EngineEntry, std::string> UpsertEngine(
const std::string& engine_name, const std::string& type,
Expand All @@ -150,7 +150,7 @@ class EngineService : public EngineServiceI {

void RegisterEngineLibPath();

bool IsRemoteEngine(const std::string& engine_name) override;
bool IsRemoteEngine(const std::string& engine_name) const override;

private:
bool IsEngineLoaded(const std::string& engine);
Expand Down

0 comments on commit 8922896

Please sign in to comment.