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

Commit a8b2503

Browse files
authored
feat: add filter compatible for engine variant api (#1819)
1 parent 5414e02 commit a8b2503

File tree

5 files changed

+139
-11
lines changed

5 files changed

+139
-11
lines changed

docs/static/openapi/cortex.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,84 @@
21992199
"tags": ["Engines"]
22002200
}
22012201
},
2202+
"/v1/engines/{name}/releases/{version}": {
2203+
"get": {
2204+
"summary": "List variants for a specific engine version",
2205+
"description": "Lists all available variants (builds) for a specific version of an engine. Variants can include different CPU architectures (AVX, AVX2, AVX512), GPU support (CUDA, Vulkan), and operating systems (Windows, Linux, macOS).",
2206+
"parameters": [
2207+
{
2208+
"name": "name",
2209+
"in": "path",
2210+
"required": true,
2211+
"schema": {
2212+
"type": "string",
2213+
"enum": ["llama-cpp", "onnxruntime", "tensorrt-llm"],
2214+
"default": "llama-cpp"
2215+
},
2216+
"description": "The type of engine"
2217+
},
2218+
{
2219+
"name": "version",
2220+
"in": "path",
2221+
"required": true,
2222+
"schema": {
2223+
"type": "string"
2224+
},
2225+
"description": "The version of the engine"
2226+
},
2227+
{
2228+
"name": "show",
2229+
"in": "query",
2230+
"required": false,
2231+
"schema": {
2232+
"type": "string",
2233+
"enum": ["all", "compatible"],
2234+
"default": "all"
2235+
},
2236+
"description": "Filter the variants list. Use 'compatible' to show only variants compatible with the current system, or 'all' to show all available variants."
2237+
}
2238+
],
2239+
"responses": {
2240+
"200": {
2241+
"description": "Successfully retrieved variants list",
2242+
"content": {
2243+
"application/json": {
2244+
"schema": {
2245+
"type": "array",
2246+
"items": {
2247+
"type": "object",
2248+
"properties": {
2249+
"name": {
2250+
"type": "string",
2251+
"description": "The name of the variant, including OS, architecture, and capabilities",
2252+
"example": "linux-amd64-avx-cuda-11-7"
2253+
},
2254+
"created_at": {
2255+
"type": "string",
2256+
"format": "date-time",
2257+
"description": "Creation timestamp of the variant",
2258+
"example": "2024-11-13T04:51:16Z"
2259+
},
2260+
"size": {
2261+
"type": "integer",
2262+
"description": "Size of the variant in bytes",
2263+
"example": 151224604
2264+
},
2265+
"download_count": {
2266+
"type": "integer",
2267+
"description": "Number of times this variant has been downloaded",
2268+
"example": 0
2269+
}
2270+
}
2271+
}
2272+
}
2273+
}
2274+
}
2275+
}
2276+
},
2277+
"tags": ["Engines"]
2278+
}
2279+
},
22022280
"/v1/engines/{name}/releases/latest": {
22032281
"get": {
22042282
"summary": "Get latest release",

engine/controllers/engines.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ void Engines::GetEngineReleases(
129129
void Engines::GetEngineVariants(
130130
const HttpRequestPtr& req,
131131
std::function<void(const HttpResponsePtr&)>&& callback,
132-
const std::string& engine, const std::string& version) const {
132+
const std::string& engine, const std::string& version,
133+
std::optional<std::string> show) const {
133134
if (engine.empty()) {
134135
Json::Value res;
135136
res["message"] = "Engine name is required";
@@ -140,7 +141,18 @@ void Engines::GetEngineVariants(
140141
return;
141142
}
142143

143-
auto result = engine_service_->GetEngineVariants(engine, version);
144+
auto show_value = show.value_or("all");
145+
if (show_value != "all" && show_value != "compatible") {
146+
Json::Value res;
147+
res["message"] = "Invalid show value. Can either be `all` or `compatible`";
148+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
149+
resp->setStatusCode(k400BadRequest);
150+
callback(resp);
151+
return;
152+
}
153+
154+
auto result = engine_service_->GetEngineVariants(engine, version,
155+
show_value == "compatible");
144156

145157
auto normalize_version = string_utils::RemoveSubstring(version, "v");
146158
Json::Value releases(Json::arrayValue);

engine/controllers/engines.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@ class Engines : public drogon::HttpController<Engines, false> {
5353
METHOD_ADD(Engines::GetEngineReleases, "/{1}/releases", Get);
5454
ADD_METHOD_TO(Engines::GetEngineReleases, "/v1/engines/{1}/releases", Get);
5555

56-
METHOD_ADD(Engines::GetEngineVariants, "/{1}/releases/{2}", Get);
57-
ADD_METHOD_TO(Engines::GetEngineVariants, "/v1/engines/{1}/releases/{2}",
58-
Get);
56+
ADD_METHOD_TO(Engines::GetEngineVariants,
57+
"/v1/engines/{engine}/releases/{version}?show={show}", Get);
5958

60-
METHOD_ADD(Engines::GetLatestEngineVersion, "/{1}/releases/latest", Get);
6159
ADD_METHOD_TO(Engines::GetLatestEngineVersion,
62-
"/v1/engines/{1}/releases/latest", Get);
60+
"/v1/engines/{engine}/releases/latest", Get);
6361

6462
METHOD_LIST_END
6563

@@ -83,8 +81,8 @@ class Engines : public drogon::HttpController<Engines, false> {
8381

8482
void GetEngineVariants(const HttpRequestPtr& req,
8583
std::function<void(const HttpResponsePtr&)>&& callback,
86-
const std::string& engine,
87-
const std::string& version) const;
84+
const std::string& engine, const std::string& version,
85+
std::optional<std::string> show) const;
8886

8987
void GetInstalledEngineVariants(
9088
const HttpRequestPtr& req,

engine/services/engine_service.cc

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ EngineService::GetEngineReleases(const std::string& engine) const {
482482

483483
cpp::result<std::vector<EngineService::EngineVariant>, std::string>
484484
EngineService::GetEngineVariants(const std::string& engine,
485-
const std::string& version) const {
485+
const std::string& version,
486+
bool filter_compatible_only) const {
486487
auto ne = NormalizeEngine(engine);
487488
auto engine_release =
488489
github_release_utils::GetReleaseByVersion("janhq", ne, version);
@@ -506,6 +507,44 @@ EngineService::GetEngineVariants(const std::string& engine,
506507
return cpp::fail("No compatible variants found for " + engine);
507508
}
508509

510+
if (filter_compatible_only) {
511+
auto system_info = system_info_utils::GetSystemInfo();
512+
compatible_variants.erase(
513+
std::remove_if(compatible_variants.begin(), compatible_variants.end(),
514+
[&system_info](const EngineVariant& variant) {
515+
std::string name = variant.name;
516+
std::transform(name.begin(), name.end(), name.begin(),
517+
::tolower);
518+
519+
bool os_match = false;
520+
if (system_info->os == "mac" &&
521+
name.find("mac") != std::string::npos)
522+
os_match = true;
523+
if (system_info->os == "windows" &&
524+
name.find("windows") != std::string::npos)
525+
os_match = true;
526+
if (system_info->os == "linux" &&
527+
name.find("linux") != std::string::npos)
528+
os_match = true;
529+
530+
bool arch_match = false;
531+
if (system_info->arch == "arm64" &&
532+
name.find("arm64") != std::string::npos)
533+
arch_match = true;
534+
if (system_info->arch == "amd64" &&
535+
name.find("amd64") != std::string::npos)
536+
arch_match = true;
537+
538+
return !(os_match && arch_match);
539+
}),
540+
compatible_variants.end());
541+
542+
if (compatible_variants.empty()) {
543+
return cpp::fail("No compatible variants found for system " +
544+
system_info->os + "/" + system_info->arch);
545+
}
546+
}
547+
509548
return compatible_variants;
510549
}
511550

engine/services/engine_service.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ class EngineService : public EngineServiceI {
101101
const std::string& engine) const;
102102

103103
cpp::result<std::vector<EngineVariant>, std::string> GetEngineVariants(
104-
const std::string& engine, const std::string& version) const;
104+
const std::string& engine, const std::string& version,
105+
bool filter_compatible_only = false) const;
105106

106107
cpp::result<DefaultEngineVariant, std::string> SetDefaultEngineVariant(
107108
const std::string& engine, const std::string& version,

0 commit comments

Comments
 (0)