Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit deb2600

Browse files
authored
feat: get running models (#592)
1 parent 9a115ba commit deb2600

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

cortex-cpp/common/base.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class BaseModel {
1616
virtual void ModelStatus(
1717
const HttpRequestPtr& req,
1818
std::function<void(const HttpResponsePtr&)>&& callback) = 0;
19+
20+
virtual void GetModels(const HttpRequestPtr& req,
21+
std::function<void(const HttpResponsePtr&)>&& callback) = 0;
1922
};
2023

2124
class BaseChatCompletion {

cortex-cpp/controllers/server.cc

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,40 @@ void server::ModelStatus(
129129
LOG_TRACE << "Done get model status";
130130
}
131131

132+
void server::GetModels(const HttpRequestPtr& req,
133+
std::function<void(const HttpResponsePtr&)>&& callback) {
134+
if (!IsEngineLoaded()) {
135+
Json::Value res;
136+
res["message"] = "Engine is not loaded yet";
137+
auto resp = cortex_utils::nitroHttpJsonResponse(res);
138+
resp->setStatusCode(k409Conflict);
139+
callback(resp);
140+
LOG_WARN << "Engine is not loaded yet";
141+
return;
142+
}
143+
144+
LOG_TRACE << "Start to get models";
145+
if (engine_->IsSupported("GetModels")) {
146+
engine_->GetModels(
147+
req->getJsonObject(),
148+
[cb = std::move(callback)](Json::Value status, Json::Value res) {
149+
auto resp = cortex_utils::nitroHttpJsonResponse(res);
150+
resp->setStatusCode(static_cast<drogon::HttpStatusCode>(
151+
status["status_code"].asInt()));
152+
cb(resp);
153+
});
154+
} else {
155+
Json::Value res;
156+
res["message"] = "Method is not supported yet";
157+
auto resp = cortex_utils::nitroHttpJsonResponse(res);
158+
resp->setStatusCode(k500InternalServerError);
159+
callback(resp);
160+
LOG_WARN << "Method is not supported yet";
161+
}
162+
163+
LOG_TRACE << "Done get models";
164+
}
165+
132166
void server::LoadModel(const HttpRequestPtr& req,
133167
std::function<void(const HttpResponsePtr&)>&& callback) {
134168
auto engine_type =
@@ -144,10 +178,9 @@ void server::LoadModel(const HttpRequestPtr& req,
144178
};
145179

146180
try {
147-
std::string abs_path = cortex_utils::GetCurrentPath() +
148-
get_engine_path(cur_engine_name_);
149-
dylib_ =
150-
std::make_unique<cortex_cpp::dylib>(abs_path, "engine");
181+
std::string abs_path =
182+
cortex_utils::GetCurrentPath() + get_engine_path(cur_engine_name_);
183+
dylib_ = std::make_unique<cortex_cpp::dylib>(abs_path, "engine");
151184
} catch (const cortex_cpp::dylib::load_error& e) {
152185
LOG_ERROR << "Could not load engine: " << e.what();
153186
dylib_.reset();

cortex-cpp/controllers/server.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ class server : public drogon::HttpController<server>,
4646
METHOD_ADD(server::LoadModel, "loadmodel", Post);
4747
METHOD_ADD(server::UnloadModel, "unloadmodel", Post);
4848
METHOD_ADD(server::ModelStatus, "modelstatus", Post);
49+
METHOD_ADD(server::GetModels, "models", Get);
50+
4951

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

@@ -72,6 +75,9 @@ class server : public drogon::HttpController<server>,
7275
void ModelStatus(
7376
const HttpRequestPtr& req,
7477
std::function<void(const HttpResponsePtr&)>&& callback) override;
78+
void GetModels(
79+
const HttpRequestPtr& req,
80+
std::function<void(const HttpResponsePtr&)>&& callback) override;
7581

7682
private:
7783
void ProcessStreamRes(std::function<void(const HttpResponsePtr&)> cb,

cortex-cpp/cortex-common/EngineI.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <functional>
4+
#include <iostream>
45
#include <memory>
56

67
#include "json/value.h"
@@ -24,4 +25,12 @@ class EngineI {
2425
virtual void GetModelStatus(
2526
std::shared_ptr<Json::Value> jsonBody,
2627
std::function<void(Json::Value&&, Json::Value&&)>&& callback) = 0;
28+
29+
// For backward compatible checking
30+
virtual bool IsSupported(const std::string& f) = 0;
31+
32+
// Get list of running models
33+
virtual void GetModels(
34+
std::shared_ptr<Json::Value> jsonBody,
35+
std::function<void(Json::Value&&, Json::Value&&)>&& callback) = 0;
2736
};

0 commit comments

Comments
 (0)