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

Commit 9dc157d

Browse files
committed
Merge branch 'dev' of github.com:janhq/nitro into feat/python-runtime-engine
2 parents f9e7971 + deb2600 commit 9dc157d

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

cortex-cpp/common/base.h

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

cortex-cpp/controllers/server.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,43 @@ void server::ModelStatus(
140140
LOG_TRACE << "Done get model status";
141141
}
142142

143+
void server::GetModels(const HttpRequestPtr& req,
144+
std::function<void(const HttpResponsePtr&)>&& callback) {
145+
auto engine_type =
146+
(*(req->getJsonObject())).get("engine", kLlamaEngine).asString();
147+
if (!IsEngineLoaded(engine_type)) {
148+
Json::Value res;
149+
res["message"] = "Engine is not loaded yet";
150+
auto resp = cortex_utils::nitroHttpJsonResponse(res);
151+
resp->setStatusCode(k409Conflict);
152+
callback(resp);
153+
LOG_WARN << "Engine is not loaded yet";
154+
return;
155+
}
156+
157+
LOG_TRACE << "Start to get models";
158+
auto& en = std::get<EngineI*>(engines_[engine_type].engine);
159+
if (en->IsSupported("GetModels")) {
160+
en->GetModels(
161+
req->getJsonObject(),
162+
[cb = std::move(callback)](Json::Value status, Json::Value res) {
163+
auto resp = cortex_utils::nitroHttpJsonResponse(res);
164+
resp->setStatusCode(static_cast<drogon::HttpStatusCode>(
165+
status["status_code"].asInt()));
166+
cb(resp);
167+
});
168+
} else {
169+
Json::Value res;
170+
res["message"] = "Method is not supported yet";
171+
auto resp = cortex_utils::nitroHttpJsonResponse(res);
172+
resp->setStatusCode(k500InternalServerError);
173+
callback(resp);
174+
LOG_WARN << "Method is not supported yet";
175+
}
176+
177+
LOG_TRACE << "Done get models";
178+
}
179+
143180
void server::GetEngines(
144181
const HttpRequestPtr& req,
145182
std::function<void(const HttpResponsePtr&)>&& callback) {
@@ -233,6 +270,7 @@ void server::LoadModel(const HttpRequestPtr& req,
233270
cortex_utils::GetCurrentPath() + get_engine_path(engine_type);
234271
engines_[engine_type].dl =
235272
std::make_unique<cortex_cpp::dylib>(abs_path, "engine");
273+
236274
} catch (const cortex_cpp::dylib::load_error& e) {
237275
LOG_ERROR << "Could not load engine: " << e.what();
238276
engines_.erase(engine_type);

cortex-cpp/controllers/server.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ class server : public drogon::HttpController<server>,
4848
METHOD_ADD(server::LoadModel, "loadmodel", Post);
4949
METHOD_ADD(server::UnloadModel, "unloadmodel", Post);
5050
METHOD_ADD(server::ModelStatus, "modelstatus", Post);
51+
METHOD_ADD(server::GetModels, "models", Get);
5152
METHOD_ADD(server::GetEngines, "engines", Get);
5253

5354
// cortex.python API
5455
METHOD_ADD(server::FineTuning, "finetuning", Post);
5556

5657
// Openai compatible path
5758
ADD_METHOD_TO(server::ChatCompletion, "/v1/chat/completions", Post);
59+
ADD_METHOD_TO(server::GetModels, "/v1/models", Get);
5860
ADD_METHOD_TO(server::FineTuning, "/v1/fine_tuning/job", Post);
61+
5962
// ADD_METHOD_TO(server::handlePrelight, "/v1/chat/completions", Options);
6063
// NOTE: prelight will be added back when browser support is properly planned
6164

@@ -79,10 +82,12 @@ class server : public drogon::HttpController<server>,
7982
void ModelStatus(
8083
const HttpRequestPtr& req,
8184
std::function<void(const HttpResponsePtr&)>&& callback) override;
85+
void GetModels(
86+
const HttpRequestPtr& req,
87+
std::function<void(const HttpResponsePtr&)>&& callback) override;
8288
void GetEngines(
8389
const HttpRequestPtr& req,
8490
std::function<void(const HttpResponsePtr&)>&& callback) override;
85-
8691
void FineTuning(
8792
const HttpRequestPtr& req,
8893
std::function<void(const HttpResponsePtr&)>&& callback) override;

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"
@@ -25,4 +26,12 @@ class EngineI {
2526
virtual void GetModelStatus(
2627
std::shared_ptr<Json::Value> json_body,
2728
std::function<void(Json::Value&&, Json::Value&&)>&& callback) = 0;
29+
30+
// For backward compatible checking
31+
virtual bool IsSupported(const std::string& f) = 0;
32+
33+
// Get list of running models
34+
virtual void GetModels(
35+
std::shared_ptr<Json::Value> jsonBody,
36+
std::function<void(Json::Value&&, Json::Value&&)>&& callback) = 0;
2837
};

0 commit comments

Comments
 (0)