Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vansangpfiev committed Dec 27, 2024
1 parent ca35c1f commit 95b2b01
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 76 deletions.
5 changes: 0 additions & 5 deletions engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,6 @@ void Engines::InstallRemoteEngine(
resp->setStatusCode(k400BadRequest);
callback(resp);
} else {
// auto gr = engine_service_->GenerateRemoteModel(engine);
// if (gr.has_error()) {
// CTL_INF("Error: " << gr.error());
// }

Json::Value res;
if (get_models_url.empty()) {
res["warning"] =
Expand Down
9 changes: 6 additions & 3 deletions engine/extensions/remote-engine/remote_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ bool is_anthropic(const std::string& model) {
}

bool is_openai(const std::string& model) {
return model.find("gpt") != std::string::npos;
return model.find("gpt-") != std::string::npos ||
model.find("o1-") != std::string::npos;
}

constexpr const std::array<std::string_view, 5> kAnthropicModels = {
Expand Down Expand Up @@ -181,7 +182,7 @@ static size_t WriteCallback(char* ptr, size_t size, size_t nmemb,
}

RemoteEngine::RemoteEngine(const std::string& engine_name)
: engine_name_(engine_name) {
: engine_name_(engine_name), q_(1 /*n_parallel*/, engine_name) {
curl_global_init(CURL_GLOBAL_ALL);
}

Expand Down Expand Up @@ -552,7 +553,9 @@ void RemoteEngine::HandleChatCompletion(
}

if (is_stream) {
MakeStreamingChatCompletionRequest(*model_config, result, callback);
q_.runTaskInQueue([this, model_config, result, cb = std::move(callback)] {
MakeStreamingChatCompletionRequest(*model_config, result, cb);
});
} else {

auto response = MakeChatCompletionRequest(*model_config, result);
Expand Down
2 changes: 2 additions & 0 deletions engine/extensions/remote-engine/remote_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <unordered_map>
#include "cortex-common/remote_enginei.h"
#include "extensions/remote-engine/template_renderer.h"
#include "trantor/utils/ConcurrentTaskQueue.h"
#include "utils/engine_constants.h"
#include "utils/file_logger.h"
// Helper for CURL response
Expand Down Expand Up @@ -52,6 +53,7 @@ class RemoteEngine : public RemoteEngineI {
std::string chat_res_template_;
std::string api_key_header_;
std::string engine_name_;
trantor::ConcurrentTaskQueue q_;

// Helper functions
CurlResponse MakeChatCompletionRequest(const ModelConfig& config,
Expand Down
65 changes: 0 additions & 65 deletions engine/services/engine_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1140,71 +1140,6 @@ bool EngineService::IsRemoteEngine(const std::string& engine_name) {
return true;
}

cpp::result<bool, std::string> EngineService::GenerateRemoteModel(
const std::string& engine_name) {
namespace fmu = file_manager_utils;
namespace fs = std::filesystem;
auto exist_engine = GetEngineByNameAndVariant(engine_name);
if (exist_engine.has_error()) {
return cpp::fail("Remote engine '" + engine_name + "' is not installed");
}

if (!IsEngineLoaded(engine_name)) {
engines_[engine_name].engine = new remote_engine::RemoteEngine(engine_name);
CTL_INF("Loaded engine: " << engine_name);
}

auto remote_engine_json = exist_engine.value().ToJson();
auto& e = std::get<RemoteEngineI*>(engines_[engine_name].engine);
auto url = remote_engine_json["metadata"]["get_models_url"].asString();
auto api_key = remote_engine_json["api_key"].asString();
auto api_key_template =
remote_engine_json["metadata"]["api_key_template"].asString();
auto res = e->GetRemoteModels(url, api_key, api_key_template);
if (!res["error"].isNull()) {
return cpp::fail(res["error"].asString());
} else {
for (auto& d : res["data"]) {
auto model_handle = d["id"].asString();
config::RemoteModelConfig model_config;
Json::Value body =
json_helper::ParseJsonString(config::kDefaultRemoteModelConfig);
body["model"] = model_handle;
body["engine"] = engine_name;
// CTL_INF(body.toStyledString());
model_config.LoadFromJson(body);
cortex::db::Models modellist_utils_obj;

std::string model_yaml_path =
(file_manager_utils::GetModelsContainerPath() /
std::filesystem::path("remote") /
std::filesystem::path(model_handle + ".yml"))
.string();
try {
auto yaml_rel_path =
fmu::ToRelativeCortexDataPath(fs::path(model_yaml_path));
cortex::db::ModelEntry model_entry{
model_handle, "", "", yaml_rel_path.string(),
model_handle, "remote", "imported", cortex::db::ModelStatus::Remote,
engine_name};
std::filesystem::create_directories(
std::filesystem::path(model_yaml_path).parent_path());
if (modellist_utils_obj.AddModelEntry(model_entry).value()) {
model_config.SaveToYamlFile(model_yaml_path);
} else {
CTL_INF("Fail to import model, model_id '" + model_handle +
"' already exists!");
}
} catch (const std::exception& e) {
return cpp::fail("Error while adding Remote model with model_id '" +
model_handle + "': " + e.what());
}
}
}

return true;
}

cpp::result<std::vector<std::string>, std::string>
EngineService::GetSupportedEngineNames() {
return file_manager_utils::GetCortexConfig().supportedEngines;
Expand Down
3 changes: 0 additions & 3 deletions engine/services/engine_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ class EngineService : public EngineServiceI {

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

cpp::result<bool, std::string> GenerateRemoteModel(
const std::string& engine_name);

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

Expand Down
3 changes: 3 additions & 0 deletions engine/services/model_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1253,5 +1253,8 @@ ModelService::GetModelMetadata(const std::string& model_id) const {

std::shared_ptr<ModelMetadata> ModelService::GetCachedModelMetadata(
const std::string& model_id) const {
if (loaded_model_metadata_map_.find(model_id) ==
loaded_model_metadata_map_.end())
return nullptr;
return loaded_model_metadata_map_.at(model_id);
}

0 comments on commit 95b2b01

Please sign in to comment.