Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get running models #592

Merged
merged 6 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cortex-cpp/common/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class BaseModel {
virtual void ModelStatus(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) = 0;

virtual void GetModels(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) = 0;
};

class BaseChatCompletion {
Expand Down
41 changes: 37 additions & 4 deletions cortex-cpp/controllers/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,40 @@ void server::ModelStatus(
LOG_TRACE << "Done get model status";
}

void server::GetModels(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) {
if (!IsEngineLoaded()) {
Json::Value res;
res["message"] = "Engine is not loaded yet";
auto resp = cortex_utils::nitroHttpJsonResponse(res);
resp->setStatusCode(k409Conflict);
callback(resp);
LOG_WARN << "Engine is not loaded yet";
return;
}

LOG_TRACE << "Start to get models";
if (engine_->IsSupported("GetModels")) {
engine_->GetModels(
req->getJsonObject(),
[cb = std::move(callback)](Json::Value status, Json::Value res) {
auto resp = cortex_utils::nitroHttpJsonResponse(res);
resp->setStatusCode(static_cast<drogon::HttpStatusCode>(
status["status_code"].asInt()));
cb(resp);
});
} else {
Json::Value res;
res["message"] = "Method is not supported yet";
auto resp = cortex_utils::nitroHttpJsonResponse(res);
resp->setStatusCode(k500InternalServerError);
callback(resp);
LOG_WARN << "Method is not supported yet";
}

LOG_TRACE << "Done get models";
}

void server::LoadModel(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) {
auto engine_type =
Expand All @@ -144,10 +178,9 @@ void server::LoadModel(const HttpRequestPtr& req,
};

try {
std::string abs_path = cortex_utils::GetCurrentPath() +
get_engine_path(cur_engine_name_);
dylib_ =
std::make_unique<cortex_cpp::dylib>(abs_path, "engine");
std::string abs_path =
cortex_utils::GetCurrentPath() + get_engine_path(cur_engine_name_);
dylib_ = std::make_unique<cortex_cpp::dylib>(abs_path, "engine");
} catch (const cortex_cpp::dylib::load_error& e) {
LOG_ERROR << "Could not load engine: " << e.what();
dylib_.reset();
Expand Down
6 changes: 6 additions & 0 deletions cortex-cpp/controllers/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ class server : public drogon::HttpController<server>,
METHOD_ADD(server::LoadModel, "loadmodel", Post);
METHOD_ADD(server::UnloadModel, "unloadmodel", Post);
METHOD_ADD(server::ModelStatus, "modelstatus", Post);
METHOD_ADD(server::GetModels, "models", Get);


// Openai compatible path
ADD_METHOD_TO(server::ChatCompletion, "/v1/chat/completions", Post);
ADD_METHOD_TO(server::GetModels, "/v1/models", Get);
// ADD_METHOD_TO(server::handlePrelight, "/v1/chat/completions", Options);
// NOTE: prelight will be added back when browser support is properly planned

Expand All @@ -72,6 +75,9 @@ class server : public drogon::HttpController<server>,
void ModelStatus(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) override;
void GetModels(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) override;

private:
void ProcessStreamRes(std::function<void(const HttpResponsePtr&)> cb,
Expand Down
9 changes: 9 additions & 0 deletions cortex-cpp/cortex-common/EngineI.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <iostream>
#include <memory>

#include "json/value.h"
Expand All @@ -24,4 +25,12 @@ class EngineI {
virtual void GetModelStatus(
std::shared_ptr<Json::Value> jsonBody,
std::function<void(Json::Value&&, Json::Value&&)>&& callback) = 0;

// For backward compatible checking
virtual bool IsSupported(const std::string& f) = 0;

// Get list of running models
virtual void GetModels(
std::shared_ptr<Json::Value> jsonBody,
std::function<void(Json::Value&&, Json::Value&&)>&& callback) = 0;
};
Loading